diff options
author | wchang0222%aol.com <devnull@localhost> | 2004-05-02 00:39:58 +0000 |
---|---|---|
committer | wchang0222%aol.com <devnull@localhost> | 2004-05-02 00:39:58 +0000 |
commit | a1ae1cc027b860c01d7f0e5fcdf3d4fd661f79d0 (patch) | |
tree | 7a6c28760b64d206f813b62442ddda6ecedf6893 | |
parent | cdea4595477ba8e802338265eb661a3a9cdb0a28 (diff) | |
download | nspr-hg-a1ae1cc027b860c01d7f0e5fcdf3d4fd661f79d0.tar.gz |
Bugzilla bug 209499: more improvements to PL_str functions. The patch for
strdup.c is contributed by Bernard Alleysson <balleysson@bigfoot.com>.
The patch for strcat.c and strstr.c is contributed by Dan Witte
<dwitte@stanford.edu>. r=wtc.
Modified Files: strcat.c strcpy.c strdup.c strstr.c
Tag: NSPRPUB_PRE_4_2_CLIENT_BRANCH
-rw-r--r-- | lib/libc/src/strcat.c | 9 | ||||
-rw-r--r-- | lib/libc/src/strcpy.c | 1 | ||||
-rw-r--r-- | lib/libc/src/strdup.c | 26 | ||||
-rw-r--r-- | lib/libc/src/strstr.c | 26 |
4 files changed, 32 insertions, 30 deletions
diff --git a/lib/libc/src/strcat.c b/lib/libc/src/strcat.c index 8bf70be1..f71b0974 100644 --- a/lib/libc/src/strcat.c +++ b/lib/libc/src/strcat.c @@ -52,9 +52,8 @@ PL_strncat(char *dest, const char *src, PRUint32 max) { char *rv; - if( (char *)0 == dest ) return (char *)0; - if( (const char *)0 == src ) return dest; - if( 0 == max ) return dest; + if( ((char *)0 == dest) || ((const char *)0 == src) || (0 == max) ) + return dest; for( rv = dest; *dest; dest++ ) ; @@ -69,8 +68,8 @@ PL_strcatn(char *dest, PRUint32 max, const char *src) char *rv; PRUint32 dl; - 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, dl = 0; *dest; dest++, dl++ ) ; diff --git a/lib/libc/src/strcpy.c b/lib/libc/src/strcpy.c index 23c0fb73..b576b677 100644 --- a/lib/libc/src/strcpy.c +++ b/lib/libc/src/strcpy.c @@ -58,6 +58,7 @@ PL_strncpy(char *dest, const char *src, PRUint32 max) ; #ifdef JLRU + /* XXX I (wtc) think the -- and ++ operators should be postfix. */ while( --max ) *++dest = '\0'; #endif /* JLRU */ diff --git a/lib/libc/src/strdup.c b/lib/libc/src/strdup.c index 807874e9..27a553c6 100644 --- a/lib/libc/src/strdup.c +++ b/lib/libc/src/strdup.c @@ -37,22 +37,23 @@ #include "plstr.h" #include "prmem.h" +#include <string.h> PR_IMPLEMENT(char *) PL_strdup(const char *s) { char *rv; - PRUint32 l; + size_t n; - l = PL_strlen(s); + if( (const char *)0 == s ) + s = ""; - rv = (char *)malloc(l+1); + n = strlen(s) + 1; + + rv = (char *)malloc(n); if( (char *)0 == rv ) return rv; - if( (const char *)0 == s ) - *rv = '\0'; - else - (void)PL_strcpy(rv, s); + (void)memcpy(rv, s, n); return rv; } @@ -67,17 +68,18 @@ PR_IMPLEMENT(char *) PL_strndup(const char *s, PRUint32 max) { char *rv; - PRUint32 l; + size_t l; + + if( (const char *)0 == s ) + s = ""; l = PL_strnlen(s, max); rv = (char *)malloc(l+1); if( (char *)0 == rv ) return rv; - if( (const char *)0 == s ) - *rv = '\0'; - else - (void)PL_strncpyz(rv, s, l+1); + (void)memcpy(rv, s, l); + rv[l] = '\0'; return rv; } diff --git a/lib/libc/src/strstr.c b/lib/libc/src/strstr.c index ad0ebd55..6bc03d37 100644 --- a/lib/libc/src/strstr.c +++ b/lib/libc/src/strstr.c @@ -51,20 +51,20 @@ PR_IMPLEMENT(char *) PL_strrstr(const char *big, const char *little) { const char *p; - PRUint32 ll; - PRUint32 bl; + size_t ll; + size_t bl; 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); - bl = PL_strlen(big); + ll = strlen(little); + bl = strlen(big); if( bl < ll ) return (char *)0; p = &big[ bl - ll ]; for( ; p >= big; p-- ) if( *little == *p ) - if( 0 == PL_strncmp(p, little, ll) ) + if( 0 == strncmp(p, little, ll) ) return (char *)p; return (char *)0; @@ -73,19 +73,19 @@ PL_strrstr(const char *big, const char *little) PR_IMPLEMENT(char *) PL_strnstr(const char *big, const char *little, PRUint32 max) { - PRUint32 ll; + size_t 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); - if( ll > max ) return (char *)0; - max -= ll; + ll = strlen(little); + if( ll > (size_t)max ) return (char *)0; + max -= (PRUint32)ll; max++; for( ; max && *big; big++, max-- ) if( *little == *big ) - if( 0 == PL_strncmp(big, little, ll) ) + if( 0 == strncmp(big, little, ll) ) return (char *)big; return (char *)0; @@ -95,12 +95,12 @@ PR_IMPLEMENT(char *) PL_strnrstr(const char *big, const char *little, PRUint32 max) { const char *p; - PRUint32 ll; + size_t 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); + ll = strlen(little); for( p = big; max && *p; p++, max-- ) ; @@ -110,7 +110,7 @@ PL_strnrstr(const char *big, const char *little, PRUint32 max) for( ; p >= big; p-- ) if( *little == *p ) - if( 0 == PL_strncmp(p, little, ll) ) + if( 0 == strncmp(p, little, ll) ) return (char *)p; return (char *)0; |