This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 8f7af29d74 libs/libc/string: unify implementation across the functions
8f7af29d74 is described below
commit 8f7af29d746f3e921b58ed66b00d10e61b5f217b
Author: Petro Karashchenko <[email protected]>
AuthorDate: Sat Apr 23 15:25:11 2022 +0200
libs/libc/string: unify implementation across the functions
Signed-off-by: Petro Karashchenko <[email protected]>
---
libs/libc/string/lib_strcasecmp.c | 5 +++--
libs/libc/string/lib_strcasestr.c | 11 ++++-------
libs/libc/string/lib_strcmp.c | 2 +-
libs/libc/string/lib_strncasecmp.c | 7 ++++---
libs/libc/string/lib_strncat.c | 4 ++--
libs/libc/string/lib_strncmp.c | 6 +++---
6 files changed, 17 insertions(+), 18 deletions(-)
diff --git a/libs/libc/string/lib_strcasecmp.c
b/libs/libc/string/lib_strcasecmp.c
index e02fddd717..b1be6b07ab 100644
--- a/libs/libc/string/lib_strcasecmp.c
+++ b/libs/libc/string/lib_strcasecmp.c
@@ -35,10 +35,11 @@
#undef strcasecmp /* See mm/README.txt */
int strcasecmp(FAR const char *cs, FAR const char *ct)
{
- int result;
+ register int result;
for (; ; )
{
- if ((result = (int)toupper(*cs) - (int)toupper(*ct)) != 0 || !*cs)
+ if ((result = toupper(*cs) - toupper(*ct)) != 0 ||
+ *cs == '\0')
{
break;
}
diff --git a/libs/libc/string/lib_strcasestr.c
b/libs/libc/string/lib_strcasestr.c
index e63e644a73..8da18860a1 100644
--- a/libs/libc/string/lib_strcasestr.c
+++ b/libs/libc/string/lib_strcasestr.c
@@ -36,15 +36,12 @@ static FAR char *strcasechr(FAR const char *s, int uc)
{
register char ch;
- if (s)
+ for (; *s; s++)
{
- for (; *s; s++)
+ ch = *s;
+ if (toupper(ch) == uc)
{
- ch = *s;
- if (toupper(ch) == uc)
- {
- return (FAR char *)s;
- }
+ return (FAR char *)s;
}
}
diff --git a/libs/libc/string/lib_strcmp.c b/libs/libc/string/lib_strcmp.c
index edb6d23c83..3067684698 100644
--- a/libs/libc/string/lib_strcmp.c
+++ b/libs/libc/string/lib_strcmp.c
@@ -38,7 +38,7 @@ int strcmp(FAR const char *cs, FAR const char *ct)
for (; ; )
{
if ((result = (unsigned char)*cs - (unsigned char)*ct++) != 0 ||
- !*cs++)
+ *cs++ == '\0')
{
break;
}
diff --git a/libs/libc/string/lib_strncasecmp.c
b/libs/libc/string/lib_strncasecmp.c
index 2b3ac6bfe3..6aeb57273c 100644
--- a/libs/libc/string/lib_strncasecmp.c
+++ b/libs/libc/string/lib_strncasecmp.c
@@ -34,12 +34,13 @@
#ifndef CONFIG_ARCH_STRNCASECMP
#undef strncasecmp /* See mm/README.txt */
-int strncasecmp(const char *cs, const char *ct, size_t nb)
+int strncasecmp(FAR const char *cs, FAR const char *ct, size_t nb)
{
- int result = 0;
+ register int result = 0;
for (; nb > 0; nb--)
{
- if ((result = (int)toupper(*cs) - (int)toupper(*ct)) != 0 || !*cs)
+ if ((result = toupper(*cs) - toupper(*ct)) != 0 ||
+ *cs == '\0')
{
break;
}
diff --git a/libs/libc/string/lib_strncat.c b/libs/libc/string/lib_strncat.c
index cc68ea156a..23ae796054 100644
--- a/libs/libc/string/lib_strncat.c
+++ b/libs/libc/string/lib_strncat.c
@@ -32,9 +32,9 @@
#ifndef CONFIG_ARCH_STRNCAT
#undef strncat /* See mm/README.txt */
-char *strncat(char *dest, const char *src, size_t n)
+FAR char *strncat(FAR char *dest, FAR const char *src, size_t n)
{
- char *ret = dest;
+ FAR char *ret = dest;
dest += strlen(dest);
for (; n > 0 && *src != '\0' ; n--)
diff --git a/libs/libc/string/lib_strncmp.c b/libs/libc/string/lib_strncmp.c
index 48e2a2486a..10869cb411 100644
--- a/libs/libc/string/lib_strncmp.c
+++ b/libs/libc/string/lib_strncmp.c
@@ -32,13 +32,13 @@
#ifndef CONFIG_ARCH_STRNCMP
#undef strncmp /* See mm/README.txt */
-int strncmp(const char *cs, const char *ct, size_t nb)
+int strncmp(FAR const char *cs, FAR const char *ct, size_t nb)
{
- int result = 0;
+ register int result = 0;
for (; nb > 0; nb--)
{
if ((result = (unsigned char)*cs - (unsigned char)*ct++) != 0 ||
- !*cs++)
+ *cs++ == '\0')
{
break;
}