From 2070ca420501b691d8420dbc5f5e705bda8cf498 Mon Sep 17 00:00:00 2001 From: Christophe Sadoine Date: Tue, 25 Jun 2013 12:26:20 +0900 Subject: [PATCH] evil: add strsep function. Signed-off-by: Cedric Bail --- ChangeLog | 4 ++++ NEWS | 1 + src/lib/evil/evil_string.c | 45 ++++++++++++++++++++++++++++++++++++++ src/lib/evil/evil_string.h | 26 +++++++++++++++++++--- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9d02b31f83..38c2abe92f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,10 @@ * Edje: Move cursor to correct position when selection handlers are pressed. +2013-06-20 Christophe Sadoine + + * Evil: Added strsep function. + 2013-06-19 Cedric Bail * Evas: optimized path for when map use the same color for all corner. diff --git a/NEWS b/NEWS index be1b545a1a..228e5277e5 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,7 @@ Additions: * Evil: - Add mkdtemp. - Add evil_rename() a wrapper for rename(). + - Add strsep(). * eina: - Add DOCTYPE children parsing in eina_simple_xml - Add eina_barrier thread API 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__ */