diff --git a/nsprpub/configure b/nsprpub/configure index fa0ab20b114..15d1cfa0406 100755 --- a/nsprpub/configure +++ b/nsprpub/configure @@ -5378,6 +5378,7 @@ lib/ds/Makefile lib/libc/Makefile lib/libc/include/Makefile lib/libc/src/Makefile +lib/tests/Makefile pr/Makefile pr/include/Makefile pr/include/md/Makefile diff --git a/nsprpub/configure.in b/nsprpub/configure.in index e3d16de5f81..56f20ab69e1 100644 --- a/nsprpub/configure.in +++ b/nsprpub/configure.in @@ -2256,6 +2256,7 @@ lib/ds/Makefile lib/libc/Makefile lib/libc/include/Makefile lib/libc/src/Makefile +lib/tests/Makefile pr/Makefile pr/include/Makefile pr/include/md/Makefile diff --git a/nsprpub/lib/libc/include/plstr.h b/nsprpub/lib/libc/include/plstr.h index b619a934aeb..551567e4940 100644 --- a/nsprpub/lib/libc/include/plstr.h +++ b/nsprpub/lib/libc/include/plstr.h @@ -18,6 +18,7 @@ * Rights Reserved. * * Contributor(s): + * Roland Mainz * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the @@ -434,7 +435,29 @@ PR_EXTERN(char *) PL_strncaserstr(const char *big, const char *little, PRUint32 max); /* - * Things not (yet?) included: strspn/strcspn, strtok/strtok_r, strsep. + * PL_strtok_r + * + * Splits the string s1 into tokens, separated by one or more characters + * from the separator string s2. The argument lasts points to a + * user-supplied char * pointer in which PL_strtok_r stores information + * for it to continue scanning the same string. + * + * In the first call to PL_strtok_r, s1 points to a string and the value + * of *lasts is ignored. PL_strtok_r returns a pointer to the first + * token, writes '\0' into the character following the first token, and + * updates *lasts. + * + * In subsequent calls, s1 is null and lasts must stay unchanged from the + * previous call. The separator string s2 may be different from call to + * call. PL_strtok_r returns a pointer to the next token in s1. When no + * token remains in s1, PL_strtok_r returns null. + */ + +PR_EXTERN(char *) +PL_strtok_r(char *s1, const char *s2, char **lasts); + +/* + * Things not (yet?) included: strspn/strcspn, strsep. * memchr, memcmp, memcpy, memccpy, index, rindex, bcmp, bcopy, bzero. * Any and all i18n/l10n stuff. */ diff --git a/nsprpub/lib/libc/src/Makefile.in b/nsprpub/lib/libc/src/Makefile.in index d79b9c9a60f..2977399e81f 100644 --- a/nsprpub/lib/libc/src/Makefile.in +++ b/nsprpub/lib/libc/src/Makefile.in @@ -57,6 +57,7 @@ CSRCS =\ strpbrk.c \ strstr.c \ strcstr.c \ + strtok.c \ base64.c \ plerror.c \ plgetopt.c \ diff --git a/nsprpub/lib/libc/src/strtok.c b/nsprpub/lib/libc/src/strtok.c new file mode 100644 index 00000000000..243a8981b43 --- /dev/null +++ b/nsprpub/lib/libc/src/strtok.c @@ -0,0 +1,86 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Netscape Portable Runtime (NSPR). + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 2001 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Roland Mainz + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU General Public License Version 2 or later (the + * "GPL"), in which case the provisions of the GPL are applicable + * instead of those above. If you wish to allow use of your + * version of this file only under the terms of the GPL and not to + * allow others to use your version of this file under the MPL, + * indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by + * the GPL. If you do not delete the provisions above, a recipient + * may use your version of this file under either the MPL or the + * GPL. + */ + +#include "plstr.h" + +PR_IMPLEMENT(char *) +PL_strtok_r(char *s1, const char *s2, char **lasts) +{ + const char *sepp; + int c, sc; + char *tok; + + if( s1 == NULL ) + { + if( *lasts == NULL ) + return NULL; + + s1 = *lasts; + } + + for( ; (c = *s1) != 0; s1++ ) + { + for( sepp = s2 ; (sc = *sepp) != 0 ; sepp++ ) + { + if( c == sc ) + break; + } + if( sc == 0 ) + break; + } + + if( c == 0 ) + { + *lasts = NULL; + return NULL; + } + + tok = s1++; + + for( ; (c = *s1) != 0; s1++ ) + { + for( sepp = s2; (sc = *sepp) != 0; sepp++ ) + { + if( c == sc ) + { + *s1++ = '\0'; + *lasts = s1; + return tok; + } + } + } + *lasts = NULL; + return tok; +} diff --git a/nsprpub/lib/tests/string.c b/nsprpub/lib/tests/string.c index 895189c3ace..0e2c3a55491 100644 --- a/nsprpub/lib/tests/string.c +++ b/nsprpub/lib/tests/string.c @@ -3003,6 +3003,59 @@ PRBool test_030(void) return PR_TRUE; } +/* PL_strtok_r */ +PRBool test_031(void) +{ + static const char *tokens[] = { + "wtc", "relyea", "nelsonb", "jpierre", "nicolson", + "ian.mcgreer", "kirk.erickson", "sonja.mirtitsch", "mhein" + }; + + static const char *seps[] = { + ", ", ",", " ", "\t", ",,,", " ,", " ", " \t\t", "," + }; + + static const char s2[] = ", \t"; + + char string[ 1024 ]; + char *s1; + char *token; + char *lasts; + unsigned int i; + + printf("Test 031 (PL_strtok_r) ..."); fflush(stdout); + + /* Build the string. */ + string[0] = '\0'; + for( i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++ ) + { + PL_strcat(string, tokens[i]); + PL_strcat(string, seps[i]); + } + + /* Scan the string for tokens. */ + i = 0; + s1 = string; + while( (token = PL_strtok_r(s1, s2, &lasts)) != NULL) + { + if( strcmp(token, tokens[i]) != 0 ) + { + printf("FAIL wrong token scanned\n"); + return PR_FALSE; + } + i++; + s1 = NULL; + } + if( i != sizeof(tokens)/sizeof(tokens[0]) ) + { + printf("FAIL wrong number of tokens scanned\n"); + return PR_FALSE; + } + + printf("PASS\n"); + return PR_TRUE; +} + int main ( @@ -3044,6 +3097,7 @@ main && test_028() && test_029() && test_030() + && test_031() ) { printf("Suite passed.\n");