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) | |
download | efl-2070ca420501b691d8420dbc5f5e705bda8cf498.tar.gz |
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) return NULL; } + +char * +strsep (char **stringp, const char *delim) +{ + char *begin, *end; + + begin = *stringp; + if (begin == NULL) + return NULL; + + /* A frequent case is when the delimiter string contains only one + character. Here we don't need to call the expensive `strpbrk' + function and instead work using `strchr'. */ + if (delim[0] == '\0' || delim[1] == '\0') + { + char ch = delim[0]; + + if (ch == '\0') + end = NULL; + else + { + if (*begin == ch) + end = begin; + else if (*begin == '\0') + end = NULL; + else + end = strchr (begin + 1, ch); + } + } + else + /* Find the end of the token. */ + end = strpbrk (begin, delim); + + if (end) + { + /* Terminate the token and set *STRINGP past NUL character. */ + *end++ = '\0'; + *stringp = end; + } + else + /* No more delimiters; this is the last token. */ + *stringp = NULL; + + return begin; +} 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); */ EAPI char *strcasestr(const char *haystack, const char *needle); - /** - * @} - */ + * @brief Implements the strsep function which is used to separate strings. + * + * @param stringp The pointer to the string to search in. + * @param delim The delimiter that contains characters used to find the next token. + * @return a pointer to the next token or NULL; + * + * The strsep() function locates, in the string referenced by *stringp, the + * first occurrence of any character in the string delim (or the terminating + * `\0' character) and replaces it with a `\0'. The location of the next + * character after the delimiter character (or NULL, if the end of the + * string was reached) is stored in *stringp. The original value of + * stringp is returned. + * + * An ``empty'' field (i.e., a character in the string delim occurs as the + * first character of *stringp) can be detected by comparing the location + * referenced by the returned pointer to `\0'. + * If *stringp is initially NULL, strsep() returns NULL. + * + * This function is from LibGW32C. + * @since 1.8 + * + */ +EAPI char *strsep(char **stringp, const char *delim); #endif /* __EVIL_STRING_H__ */ |