diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2014-07-04 16:14:06 +0400 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2014-07-04 16:14:06 +0400 |
commit | e3c8cf0a9ea00ef6c7a63b5fc8351f5f9be1ac8f (patch) | |
tree | ada2dce46e6e94aebb24cab35aed7c5e2a30fc98 /source | |
parent | 85c4feab027238bb9dbf7fa727cb608052ec79d5 (diff) |
Add (r)partition funcs to BLI_string, to get left-most/right-most first occurence of delimiters.
Inspired by Python (r)partition str functions. Also added some Gtest cases for those new funcs.
Reviewed by Campbell Barton, many thanks!
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenfont/intern/blf_util.c | 1 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 4 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_string_utf8.h | 4 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string.c | 60 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string_utf8.c | 46 | ||||
-rw-r--r-- | source/blender/blenlib/intern/timecode.c | 1 | ||||
-rw-r--r-- | source/blender/imbuf/intern/metadata.c | 1 |
7 files changed, 117 insertions, 0 deletions
diff --git a/source/blender/blenfont/intern/blf_util.c b/source/blender/blenfont/intern/blf_util.c index cb9b652b8ef..06309a944e9 100644 --- a/source/blender/blenfont/intern/blf_util.c +++ b/source/blender/blenfont/intern/blf_util.c @@ -37,6 +37,7 @@ #include "blf_internal.h" +#include "BLI_utildefines.h" #include "BLI_string_utf8.h" unsigned int blf_next_p2(unsigned int x) diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 235147d1bac..27bec227ece 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -79,6 +79,10 @@ int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL(); int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) ATTR_NONNULL(); int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_NONNULL(); +size_t BLI_str_partition(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL(); +size_t BLI_str_rpartition(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL(); +size_t BLI_str_partition_ex(const char *str, const unsigned int delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL(); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h index 4aef2318683..89754be25ba 100644 --- a/source/blender/blenlib/BLI_string_utf8.h +++ b/source/blender/blenlib/BLI_string_utf8.h @@ -66,6 +66,10 @@ int BLI_wcswidth(const wchar_t *pwcs, size_t n) ATTR_NONNULL(); int BLI_str_utf8_char_width(const char *p) ATTR_NONNULL(); /* warning, can return -1 on bad chars */ int BLI_str_utf8_char_width_safe(const char *p) ATTR_NONNULL(); +size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL(); +size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) ATTR_NONNULL(); +size_t BLI_str_partition_ex_utf8(const char *str, const unsigned int delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL(); + #define BLI_UTF8_MAX 6 /* mem */ #define BLI_UTF8_WIDTH_MAX 2 /* columns */ #define BLI_UTF8_ERR ((unsigned int)-1) diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 4ab568ece42..87233d26439 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -681,3 +681,63 @@ int BLI_str_index_in_array(const char *str, const char **str_array) return -1; } +/** + * Find the first char matching one of the chars in \a delim, from left. + * + * \param str The string to search within. + * \param delim The set of delimiters to search for, as unicode values. + * \param sep Return value, set to the first delimiter found (or NULL if none found). + * \param suf Return value, set to next char after the first delimiter found (or NULL if none found). + * \return The length of the prefix (i.e. *sep - str). + */ +size_t BLI_str_partition(const char *str, const unsigned int delim[], char **sep, char **suf) +{ + return BLI_str_partition_ex(str, delim, sep, suf, false); +} + +/** + * Find the first char matching one of the chars in \a delim, from right. + * + * \param str The string to search within. + * \param delim The set of delimiters to search for, as unicode values. + * \param sep Return value, set to the first delimiter found (or NULL if none found). + * \param suf Return value, set to next char after the first delimiter found (or NULL if none found). + * \return The length of the prefix (i.e. *sep - str). + */ +size_t BLI_str_rpartition(const char *str, const unsigned int delim[], char **sep, char **suf) +{ + return BLI_str_partition_ex(str, delim, sep, suf, true); +} + +/** + * Find the first char matching one of the chars in \a delim, either from left or right. + * + * \param str The string to search within. + * \param delim The set of delimiters to search for, as unicode values. + * \param sep Return value, set to the first delimiter found (or NULL if none found). + * \param suf Return value, set to next char after the first delimiter found (or NULL if none found). + * \param from_right If %true, search from the right of \a str, else, search from its left. + * \return The length of the prefix (i.e. *sep - str). + */ +size_t BLI_str_partition_ex(const char *str, const unsigned int delim[], char **sep, char **suf, const bool from_right) +{ + const unsigned int *d; + char *(*func)(const char *str, int c) = from_right ? strrchr : strchr; + + *sep = *suf = NULL; + + for (d = delim; *d != '\0'; ++d) { + char *tmp = func(str, (int)*d); + + if (tmp && (from_right ? (*sep < tmp) : (!*sep || *sep > tmp))) { + *sep = tmp; + } + } + + if (*sep) { + *suf = *sep + 1; + return (size_t)(*sep - str); + } + + return strlen(str); +} diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index 74e979a8579..9697fcf09e9 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -703,3 +703,49 @@ char *BLI_str_prev_char_utf8(const char *p) } } /* end glib copy */ + +size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) +{ + return BLI_str_partition_ex_utf8(str, delim, sep, suf, false); +} + +size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], char **sep, char **suf) +{ + return BLI_str_partition_ex_utf8(str, delim, sep, suf, true); +} + +size_t BLI_str_partition_ex_utf8(const char *str, const unsigned int delim[], char **sep, char **suf, + const bool from_right) +{ + const unsigned int *d; + const size_t str_len = strlen(str); + size_t index; + + *suf = (char *)(str + str_len); + + for (*sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(str, str + str_len) : str), index = 0; + *sep != NULL && **sep != '\0'; + *sep = (char *)(from_right ? (char *)BLI_str_find_prev_char_utf8(str, *sep) : str + index)) + { + const unsigned int c = BLI_str_utf8_as_unicode_and_size(*sep, &index); + + if (c == BLI_UTF8_ERR) { + *suf = *sep = NULL; + break; + } + + for (d = delim; *d != '\0'; ++d) { + if (*d == c) { + /* *suf is already correct in case from_right is true. */ + if (!from_right) + *suf = (char *)(str + index); + return (size_t)(*sep - str); + } + } + + *suf = *sep; /* Useful in 'from_right' case! */ + } + + *suf = *sep = NULL; + return str_len; +} diff --git a/source/blender/blenlib/intern/timecode.c b/source/blender/blenlib/intern/timecode.c index 244c2733ec2..0c8834008b6 100644 --- a/source/blender/blenlib/intern/timecode.c +++ b/source/blender/blenlib/intern/timecode.c @@ -33,6 +33,7 @@ #include <stdio.h> +#include "BLI_utildefines.h" #include "BLI_string.h" #include "BLI_math.h" diff --git a/source/blender/imbuf/intern/metadata.c b/source/blender/imbuf/intern/metadata.c index 2b4367528dd..797d34d118b 100644 --- a/source/blender/imbuf/intern/metadata.c +++ b/source/blender/imbuf/intern/metadata.c @@ -33,6 +33,7 @@ #include <stdlib.h> #include <string.h> +#include "BLI_utildefines.h" #include "BLI_string.h" #include "MEM_guardedalloc.h" |