From fdbaafb43b00f78c1fa350b8808a390918248cd1 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 6 Feb 2007 13:11:23 +0000 Subject: [PATCH] Fix bug with strsep(&string, ""). Optimize case of 1 delimiter. --- ChangeLog | 5 +++++ lib/strsep.c | 25 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index dbd0ac032..6b575717b 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 1a86852b8..9f2fdd2b5 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'; -- 2.11.0