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/blender/blenlib/intern/string_utf8.c | |
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/blender/blenlib/intern/string_utf8.c')
-rw-r--r-- | source/blender/blenlib/intern/string_utf8.c | 46 |
1 files changed, 46 insertions, 0 deletions
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; +} |