From: Bruno Haible Date: Tue, 6 Feb 2007 13:11:23 +0000 (+0000) Subject: Fix bug with strsep(&string, ""). Optimize case of 1 delimiter. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdbaafb43b00f78c1fa350b8808a390918248cd1;p=pspp Fix bug with strsep(&string, ""). Optimize case of 1 delimiter. --- diff --git a/ChangeLog b/ChangeLog index dbd0ac0328..6b575717b4 100644 --- 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 + + * lib/strsep.c (strsep): Fix actions in case of no delimiters. + Optimize search in case of 1 delimiter. + 2007-02-05 Paolo Bonzini * lib/acl.h: Include sys/types.h before sys/acl.h. diff --git a/lib/strsep.c b/lib/strsep.c index 1a86852b8d..9f2fdd2b57 100644 --- a/lib/strsep.c +++ b/lib/strsep.c @@ -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';