summaryrefslogtreecommitdiff
path: root/src/lib/evil/evil_string.c
diff options
context:
space:
mode:
authorChristophe Sadoine <chris@indefini.org>2013-06-25 12:26:20 +0900
committerCedric Bail <cedric.bail@samsung.com>2013-06-25 12:29:37 +0900
commit2070ca420501b691d8420dbc5f5e705bda8cf498 (patch)
treed828f9ec8d024278438452588c89d4531b511296 /src/lib/evil/evil_string.c
parentd9e1b7d67cb278b5ad78128f15ba36f77b04756b (diff)
evil: add strsep function.
Signed-off-by: Cedric Bail <cedric.bail@samsung.com>
Diffstat (limited to 'src/lib/evil/evil_string.c')
-rw-r--r--src/lib/evil/evil_string.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/lib/evil/evil_string.c b/src/lib/evil/evil_string.c
index 491a880..c6d017a 100644
--- a/src/lib/evil/evil_string.c
+++ b/src/lib/evil/evil_string.c
@@ -127,3 +127,48 @@ char *strcasestr(const char *haystack, const char *needle)
127 127
128 return NULL; 128 return NULL;
129} 129}
130
131char *
132strsep (char **stringp, const char *delim)
133{
134 char *begin, *end;
135
136 begin = *stringp;
137 if (begin == NULL)
138 return NULL;
139
140 /* A frequent case is when the delimiter string contains only one
141 character. Here we don't need to call the expensive `strpbrk'
142 function and instead work using `strchr'. */
143 if (delim[0] == '\0' || delim[1] == '\0')
144 {
145 char ch = delim[0];
146
147 if (ch == '\0')
148 end = NULL;
149 else
150 {
151 if (*begin == ch)
152 end = begin;
153 else if (*begin == '\0')
154 end = NULL;
155 else
156 end = strchr (begin + 1, ch);
157 }
158 }
159 else
160 /* Find the end of the token. */
161 end = strpbrk (begin, delim);
162
163 if (end)
164 {
165 /* Terminate the token and set *STRINGP past NUL character. */
166 *end++ = '\0';
167 *stringp = end;
168 }
169 else
170 /* No more delimiters; this is the last token. */
171 *stringp = NULL;
172
173 return begin;
174}