Fix bug with strsep(&string, ""). Optimize case of 1 delimiter.
authorBruno Haible <bruno@clisp.org>
Tue, 6 Feb 2007 13:11:23 +0000 (13:11 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 6 Feb 2007 13:11:23 +0000 (13:11 +0000)
ChangeLog
lib/strsep.c

index dbd0ac0328989f41d8da3d0cccd3a8370c414a3d..6b575717b4c8b5fab1efb04a001b6873f79a50a1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        * modules/string (string.h): Also substitute GNULIB_MBSSEP.
        * MODULES.html.sh (Internationalization functions): Add mbssep.
 
+2007-02-05  Bruno Haible  <bruno@clisp.org>
+
+       * lib/strsep.c (strsep): Fix actions in case of no delimiters.
+       Optimize search in case of 1 delimiter.
+
 2007-02-05  Paolo Bonzini  <bonzini@gnu.org>
 
        * lib/acl.h: Include sys/types.h before sys/acl.h.
index 1a86852b8d907341ce8c246db850d8887c8cf7b9..9f2fdd2b57a040c8510babaa50a168dfea3118d8 100644 (file)
@@ -29,19 +29,26 @@ strsep (char **stringp, const char *delim)
   char *start = *stringp;
   char *ptr;
 
-  if (!start)
+  if (start == NULL)
     return NULL;
 
-  if (!*delim)
-    ptr = start + strlen (start);
+  /* Optimize the case of no delimiters.  */
+  if (delim[0] == '\0')
+    {
+      *stringp = NULL;
+      return start;
+    }
+
+  /* Optimize the case of one delimiter.  */
+  if (delim[1] == '\0')
+    ptr = strchr (start, delim[0]);
   else
+    /* The general case.  */
+    ptr = strpbrk (start, delim);
+  if (ptr == NULL)
     {
-      ptr = strpbrk (start, delim);
-      if (!ptr)
-       {
-         *stringp = NULL;
-         return start;
-       }
+      *stringp = NULL;
+      return start;
     }
 
   *ptr = '\0';