From 5a69e7cfe5760abc518f278b1507b63a830ef48c Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Mon, 13 Apr 2009 00:05:43 +0000 Subject: [PATCH] useful function to join strings using separator. SVN revision: 40010 --- legacy/ecore/src/lib/ecore/Ecore_Str.h | 52 +++++++++++++++++++++++ legacy/ecore/src/lib/ecore/ecore_str.c | 59 ++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/legacy/ecore/src/lib/ecore/Ecore_Str.h b/legacy/ecore/src/lib/ecore/Ecore_Str.h index 745ad10b32..f0cef02555 100644 --- a/legacy/ecore/src/lib/ecore/Ecore_Str.h +++ b/legacy/ecore/src/lib/ecore/Ecore_Str.h @@ -29,6 +29,7 @@ /* we need this for size_t */ #include +#include /** * @file Ecore_Str.h @@ -59,6 +60,57 @@ EAPI int ecore_str_has_extension(const char *str, const char *ext); EAPI char **ecore_str_split(const char *string, const char *delimiter, int max_tokens); +EAPI size_t ecore_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len); + + +/** + * Join two strings and store the result in @a dst buffer. + * + * Similar to ecore_str_join_len(), but will compute the length of @a + * and @a b using strlen(). + * + * @param dst where to store the result. + * @param size byte size of dst, will write at most (size - 1) + * characters and then the '\0' (null terminator). + * @param sep separator character to use. + * @param a first string to use, before @a sep. + * @param b second string to use, after @a sep. + * + * @return the number of characters printed (not including the + * trailing '\0' used to end output to strings). Just like + * snprintf(), it will not write more than @a size bytes, thus a + * return value of @a size or more means that the output was + * truncated. + * + * @see ecore_str_join_len() and ecore_str_join_static() + */ +static inline size_t ecore_str_join(char *dst, size_t size, char sep, const char *a, const char *b) +{ + return ecore_str_join_len(dst, size, sep, a, strlen(a), b, strlen(b)); +} + +/** + * Join two static strings and store the result in static @a dst buffer. + * + * Similar to ecore_str_join_len(), but will assume string sizes are + * know using sizeof(X). + * + * @param dst where to store the result. + * @param sep separator character to use. + * @param a first string to use, before @a sep. + * @param b second string to use, after @a sep. + * + * @return the number of characters printed (not including the + * trailing '\0' used to end output to strings). Just like + * snprintf(), it will not write more than @a size bytes, thus a + * return value of @a size or more means that the output was + * truncated. + * + * @see ecore_str_join() and ecore_str_join_static() + */ +#define ecore_str_join_static(dst, sep, a, b) ecore_str_join_len(dst, sizeof(dst), sep, a, (sizeof(a) > 0) ? sizeof(a) - 1 : 0, b, (sizeof(b) > 0) ? sizeof(b) - 1 : 0) + + #ifdef __cplusplus } #endif diff --git a/legacy/ecore/src/lib/ecore/ecore_str.c b/legacy/ecore/src/lib/ecore/ecore_str.c index 1682229152..dc8fc966b4 100644 --- a/legacy/ecore/src/lib/ecore/ecore_str.c +++ b/legacy/ecore/src/lib/ecore/ecore_str.c @@ -241,3 +241,62 @@ ecore_str_split(const char *str, const char *delim, int max_tokens) return str_array; } + +/** + * Join two strings of known length and store the result in @a dst buffer. + * + * @param dst where to store the result. + * @param size byte size of dst, will write at most (size - 1) + * characters and then the '\0' (null terminator). + * @param sep separator character to use. + * @param a first string to use, before @a sep. + * @param a_len length of @a a, not including '\0' (strlen()-like) + * @param b second string to use, after @a sep. + * @param b_len length of @a b, not including '\0' (strlen()-like) + * + * @return the number of characters printed (not including the + * trailing '\0' used to end output to strings). Just like + * snprintf(), it will not write more than @a size bytes, thus a + * return value of @a size or more means that the output was + * truncated. + * + * @see ecore_str_join() and ecore_str_join_static() + */ +size_t +ecore_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len) +{ + size_t ret = a_len + b_len + 1; + size_t off; + + if (size < 1) return ret; + + if (size <= a_len) + { + memcpy(dst, a, size - 1); + dst[size - 1] = '\0'; + return ret; + } + + memcpy(dst, a, a_len); + off = a_len; + + if (size <= off + 1) + { + dst[size - 1] = '\0'; + return ret; + } + + dst[off] = sep; + off++; + + if (size <= off + b_len + 1) + { + memcpy(dst + off, b, size - off - 1); + dst[size - 1] = '\0'; + return ret; + } + + memcpy(dst + off, b, b_len); + dst[off + b_len] = '\0'; + return ret; +}