changeset 8114:2a406f39aa96

Fix bug with strsep(&string, ""). Optimize case of 1 delimiter.
author Bruno Haible <bruno@clisp.org>
date Tue, 06 Feb 2007 13:11:23 +0000
parents 0bd33ddd6bf3
children b68ea869ca2d
files ChangeLog lib/strsep.c
diffstat 2 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -17,6 +17,11 @@
 	* 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.
--- a/lib/strsep.c
+++ b/lib/strsep.c
@@ -29,19 +29,26 @@
   char *start = *stringp;
   char *ptr;
 
-  if (!start)
+  if (start == NULL)
     return NULL;
 
-  if (!*delim)
-    ptr = start + strlen (start);
-  else
+  /* Optimize the case of no delimiters.  */
+  if (delim[0] == '\0')
     {
-      ptr = strpbrk (start, delim);
-      if (!ptr)
-	{
-	  *stringp = NULL;
-	  return start;
-	}
+      *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)
+    {
+      *stringp = NULL;
+      return start;
     }
 
   *ptr = '\0';