summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwtc%netscape.com <devnull@localhost>2001-10-31 23:49:52 +0000
committerwtc%netscape.com <devnull@localhost>2001-10-31 23:49:52 +0000
commit6ec6e81fe9c6d72770c3c57f3993b47c512e295f (patch)
treede10bff08d7e20755d8a4dbd5387488b18c3dbe0
parent9973dd577464cbfcd318015e8e97f5db2a3394a8 (diff)
downloadnspr-hg-6ec6e81fe9c6d72770c3c57f3993b47c512e295f.tar.gz
Added lib/tests/Makefile to the list of makefiles to be generated by
configure. Modified files: configure configure.in Bugzilla bug 106372: added new function PL_strtok_r. The function was implemented by Roland Mainz <Roland.Mainz@informatik.med.uni-giessen.de>. Modified files: lib/libc/include/plstr.h lib/libc/src/Makefile.in lib/tests/string.c Added file: lib/libc/src/strtok.c
-rwxr-xr-xconfigure1
-rw-r--r--configure.in1
-rw-r--r--lib/libc/include/plstr.h25
-rw-r--r--lib/libc/src/Makefile.in1
-rw-r--r--lib/libc/src/strtok.c86
-rw-r--r--lib/tests/string.c54
6 files changed, 167 insertions, 1 deletions
diff --git a/configure b/configure
index fa0ab20b..15d1cfa0 100755
--- a/configure
+++ b/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/configure.in b/configure.in
index e3d16de5..56f20ab6 100644
--- a/configure.in
+++ b/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/lib/libc/include/plstr.h b/lib/libc/include/plstr.h
index b619a934..551567e4 100644
--- a/lib/libc/include/plstr.h
+++ b/lib/libc/include/plstr.h
@@ -18,6 +18,7 @@
* Rights Reserved.
*
* Contributor(s):
+ * Roland Mainz <roland mainz@informatik.med.uni-giessen.de>
*
* 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/lib/libc/src/Makefile.in b/lib/libc/src/Makefile.in
index d79b9c9a..2977399e 100644
--- a/lib/libc/src/Makefile.in
+++ b/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/lib/libc/src/strtok.c b/lib/libc/src/strtok.c
new file mode 100644
index 00000000..243a8981
--- /dev/null
+++ b/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 <roland mainz@informatik.med.uni-giessen.de>
+ *
+ * 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/lib/tests/string.c b/lib/tests/string.c
index 895189c3..0e2c3a55 100644
--- a/lib/tests/string.c
+++ b/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");