diff options
author | Christophe Sadoine <chris@indefini.org> | 2013-06-25 12:26:20 +0900 |
---|---|---|
committer | Cedric Bail <cedric.bail@samsung.com> | 2013-06-25 12:29:37 +0900 |
commit | 2070ca420501b691d8420dbc5f5e705bda8cf498 (patch) | |
tree | d828f9ec8d024278438452588c89d4531b511296 /src/lib/evil | |
parent | d9e1b7d67cb278b5ad78128f15ba36f77b04756b (diff) |
evil: add strsep function.
Signed-off-by: Cedric Bail <cedric.bail@samsung.com>
Diffstat (limited to 'src/lib/evil')
-rw-r--r-- | src/lib/evil/evil_string.c | 45 | ||||
-rw-r--r-- | src/lib/evil/evil_string.h | 26 |
2 files changed, 68 insertions, 3 deletions
diff --git a/src/lib/evil/evil_string.c b/src/lib/evil/evil_string.c index 491a880e1a..c6d017aa4f 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 | |||
131 | char * | ||
132 | strsep (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 | } | ||
diff --git a/src/lib/evil/evil_string.h b/src/lib/evil/evil_string.h index 45c9de3638..14f49b1c94 100644 --- a/src/lib/evil/evil_string.h +++ b/src/lib/evil/evil_string.h | |||
@@ -144,10 +144,30 @@ EAPI int strcasecmp(const char *s1, const char *s2); | |||
144 | */ | 144 | */ |
145 | EAPI char *strcasestr(const char *haystack, const char *needle); | 145 | EAPI char *strcasestr(const char *haystack, const char *needle); |
146 | 146 | ||
147 | |||
148 | /** | 147 | /** |
149 | * @} | 148 | * @brief Implements the strsep function which is used to separate strings. |
150 | */ | 149 | * |
150 | * @param stringp The pointer to the string to search in. | ||
151 | * @param delim The delimiter that contains characters used to find the next token. | ||
152 | * @return a pointer to the next token or NULL; | ||
153 | * | ||
154 | * The strsep() function locates, in the string referenced by *stringp, the | ||
155 | * first occurrence of any character in the string delim (or the terminating | ||
156 | * `\0' character) and replaces it with a `\0'. The location of the next | ||
157 | * character after the delimiter character (or NULL, if the end of the | ||
158 | * string was reached) is stored in *stringp. The original value of | ||
159 | * stringp is returned. | ||
160 | * | ||
161 | * An ``empty'' field (i.e., a character in the string delim occurs as the | ||
162 | * first character of *stringp) can be detected by comparing the location | ||
163 | * referenced by the returned pointer to `\0'. | ||
151 | 164 | ||
165 | * If *stringp is initially NULL, strsep() returns NULL. | ||
166 | * | ||
167 | * This function is from LibGW32C. | ||
168 | * @since 1.8 | ||
169 | * | ||
170 | */ | ||
171 | EAPI char *strsep(char **stringp, const char *delim); | ||
152 | 172 | ||
153 | #endif /* __EVIL_STRING_H__ */ | 173 | #endif /* __EVIL_STRING_H__ */ |