diff options
author | wchang0222%aol.com <devnull@localhost> | 2004-04-24 17:26:55 +0000 |
---|---|---|
committer | wchang0222%aol.com <devnull@localhost> | 2004-04-24 17:26:55 +0000 |
commit | 435746c70e4072c8fd84c8305286e0b157f31165 (patch) | |
tree | 0ad014f543cccb2a68fe995ac207a8a9caaf3418 | |
parent | 7062220fd65f425ce92f5e893805bc79b4684a51 (diff) | |
download | nspr-hg-435746c70e4072c8fd84c8305286e0b157f31165.tar.gz |
Bugzilla bug 209499: converted some PL_str* implementations to use ANSI C
equivalents. The patch is contributed by Dan Witte <dwitte@stanford.edu>.
r=wtc.
Modified Files: strcat.c strchr.c strcmp.c strcpy.c strdup.c strlen.c
strpbrk.c strstr.c
Tag: NSPRPUB_PRE_4_2_CLIENT_BRANCH
-rw-r--r-- | lib/libc/src/strcat.c | 15 | ||||
-rw-r--r-- | lib/libc/src/strchr.c | 20 | ||||
-rw-r--r-- | lib/libc/src/strcmp.c | 20 | ||||
-rw-r--r-- | lib/libc/src/strcpy.c | 26 | ||||
-rw-r--r-- | lib/libc/src/strdup.c | 2 | ||||
-rw-r--r-- | lib/libc/src/strlen.c | 20 | ||||
-rw-r--r-- | lib/libc/src/strpbrk.c | 10 | ||||
-rw-r--r-- | lib/libc/src/strstr.c | 12 |
8 files changed, 31 insertions, 94 deletions
diff --git a/lib/libc/src/strcat.c b/lib/libc/src/strcat.c index 0b8a1d1f..81c5d2b9 100644 --- a/lib/libc/src/strcat.c +++ b/lib/libc/src/strcat.c @@ -33,22 +33,15 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strcat(char *dest, const char *src) { - char *rv; - - if( (char *)0 == dest ) return (char *)0; - if( (const char *)0 == src ) return dest; + if( ((char *)0 == dest) || ((const char *)0 == src) ) + return dest; - for( rv = dest; *dest; dest++ ) - ; - - for( ; ((*dest = *src) != 0); dest++, src++ ) - ; - - return rv; + return strcat(dest, src); } PR_IMPLEMENT(char *) diff --git a/lib/libc/src/strchr.c b/lib/libc/src/strchr.c index 9ccae9a9..b140d5f6 100644 --- a/lib/libc/src/strchr.c +++ b/lib/libc/src/strchr.c @@ -33,36 +33,22 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strchr(const char *s, char c) { if( (const char *)0 == s ) return (char *)0; - for( ; *s; s++ ) - if( *s == c ) - return (char *)s; - - if( (char)0 == c ) return (char *)s; - - return (char *)0; + return strchr(s, c); } PR_IMPLEMENT(char *) PL_strrchr(const char *s, char c) { - const char *p; - if( (const char *)0 == s ) return (char *)0; - for( p = s; *p; p++ ) - ; - - for( ; p >= s; p-- ) - if( *p == c ) - return (char *)p; - - return (char *)0; + return strrchr(s, c); } PR_IMPLEMENT(char *) diff --git a/lib/libc/src/strcmp.c b/lib/libc/src/strcmp.c index 845f7aa0..0fb5331d 100644 --- a/lib/libc/src/strcmp.c +++ b/lib/libc/src/strcmp.c @@ -33,6 +33,7 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(PRIntn) PL_strcmp(const char *a, const char *b) @@ -40,13 +41,7 @@ PL_strcmp(const char *a, const char *b) if( ((const char *)0 == a) || (const char *)0 == b ) return (PRIntn)(a-b); - while( (*a == *b) && ('\0' != *a) ) - { - a++; - b++; - } - - return (PRIntn)(*((const unsigned char *)a) - *((const unsigned char *)b)); + return (PRIntn)strcmp(a, b); } PR_IMPLEMENT(PRIntn) @@ -55,14 +50,5 @@ PL_strncmp(const char *a, const char *b, PRUint32 max) if( ((const char *)0 == a) || (const char *)0 == b ) return (PRIntn)(a-b); - while( max && (*a == *b) && ('\0' != *a) ) - { - a++; - b++; - max--; - } - - if( 0 == max ) return (PRIntn)0; - - return (PRIntn)(*((const unsigned char *)a) - *((const unsigned char *)b)); + return (PRIntn)strncmp(a, b, (size_t)max); } diff --git a/lib/libc/src/strcpy.c b/lib/libc/src/strcpy.c index 16ff63bf..e7d695be 100644 --- a/lib/libc/src/strcpy.c +++ b/lib/libc/src/strcpy.c @@ -33,38 +33,22 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strcpy(char *dest, const char *src) { - char *rv; - - if( (char *)0 == dest ) return (char *)0; - if( (const char *)0 == src ) return (char *)0; + if( ((char *)0 == dest) || ((const char *)0 == src) ) return (char *)0; - for( rv = dest; ((*dest = *src) != 0); dest++, src++ ) - ; - - return rv; + return strcpy(dest, src); } PR_IMPLEMENT(char *) PL_strncpy(char *dest, const char *src, PRUint32 max) { - char *rv; - - if( (char *)0 == dest ) return (char *)0; - if( (const char *)0 == src ) return (char *)0; + if( ((char *)0 == dest) || ((const char *)0 == src) ) return (char *)0; - for( rv = dest; max && ((*dest = *src) != 0); dest++, src++, max-- ) - ; - -#ifdef JLRU - while( --max ) - *++dest = '\0'; -#endif /* JLRU */ - - return rv; + return strncpy(dest, src, (size_t)max); } PR_IMPLEMENT(char *) diff --git a/lib/libc/src/strdup.c b/lib/libc/src/strdup.c index 02b68e94..9f0551ef 100644 --- a/lib/libc/src/strdup.c +++ b/lib/libc/src/strdup.c @@ -57,7 +57,7 @@ PL_strdup(const char *s) PR_IMPLEMENT(void) PL_strfree(char *s) { - free(s); + free(s); } PR_IMPLEMENT(char *) diff --git a/lib/libc/src/strlen.c b/lib/libc/src/strlen.c index 30ed4403..76d6195f 100644 --- a/lib/libc/src/strlen.c +++ b/lib/libc/src/strlen.c @@ -35,22 +35,24 @@ #include "plstr.h" #include "prtypes.h" #include "prlog.h" +#include <string.h> PR_IMPLEMENT(PRUint32) PL_strlen(const char *str) { - register const char *s; + size_t l; if( (const char *)0 == str ) return 0; - for( s = str; *s; s++ ) - ; -/* error checking in case we have a 64-bit platform -- make sure we dont - * have ultra long strings that overflow a int32 - */ - if (sizeof(PRUint32) < sizeof(PRUptrdiff)) - PR_ASSERT((s-str) < 2147483647); - return (PRUint32)(s - str); + l = strlen(str); + + /* error checking in case we have a 64-bit platform -- make sure + * we don't have ultra long strings that overflow an int32 + */ + if( sizeof(PRUint32) < sizeof(size_t) ) + PR_ASSERT(l < 2147483647); + + return (PRUint32)l; } PR_IMPLEMENT(PRUint32) diff --git a/lib/libc/src/strpbrk.c b/lib/libc/src/strpbrk.c index 513ed2f6..7d0f7fac 100644 --- a/lib/libc/src/strpbrk.c +++ b/lib/libc/src/strpbrk.c @@ -33,20 +33,14 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strpbrk(const char *s, const char *list) { - const char *p; - if( ((const char *)0 == s) || ((const char *)0 == list) ) return (char *)0; - for( ; *s; s++ ) - for( p = list; *p; p++ ) - if( *s == *p ) - return (char *)s; - - return (char *)0; + return strpbrk(s, list); } PR_IMPLEMENT(char *) diff --git a/lib/libc/src/strstr.c b/lib/libc/src/strstr.c index fe658c1b..58aa5963 100644 --- a/lib/libc/src/strstr.c +++ b/lib/libc/src/strstr.c @@ -33,23 +33,15 @@ */ #include "plstr.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strstr(const char *big, const char *little) { - PRUint32 ll; - if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; - ll = PL_strlen(little); - - for( ; *big; big++ ) - if( *little == *big ) - if( 0 == PL_strncmp(big, little, ll) ) - return (char *)big; - - return (char *)0; + return strstr(big, little); } PR_IMPLEMENT(char *) |