diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-06-27 11:22:29 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-06-27 11:24:54 +0300 |
commit | 4d043c99dc49022e10a940b26ed9bc469581926f (patch) | |
tree | 3e9ed33fa07b11378ce3af0824c2557e850a13ce /source/blender/blenlib/intern/string.c | |
parent | e78b03f9e9bb8aaba7c02c9f5d26a85ddfe2079c (diff) |
Extend `BLI_str_partition_ex`: add possibility to define a right limit to the string.
Now you can define `end` pointer as right limit of the string (allows to easily search
in substring, especially useful when searching from right).
Diffstat (limited to 'source/blender/blenlib/intern/string.c')
-rw-r--r-- | source/blender/blenlib/intern/string.c | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index e41e52a7c6e..0dcb84c9c6e 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -856,7 +856,7 @@ bool BLI_str_endswith(const char *__restrict str, const char *end) */ size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf) { - return BLI_str_partition_ex(str, delim, sep, suf, false); + return BLI_str_partition_ex(str, NULL, delim, sep, suf, false); } /** @@ -870,31 +870,53 @@ size_t BLI_str_partition(const char *str, const char delim[], char **sep, char * */ size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf) { - return BLI_str_partition_ex(str, delim, sep, suf, true); + return BLI_str_partition_ex(str, NULL, 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 end If non-NULL, the right delimiter of the string. * \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 char delim[], char **sep, char **suf, const bool from_right) +size_t BLI_str_partition_ex( + const char *str, const char *end, const char delim[], char **sep, char **suf, const bool from_right) { const char *d; char *(*func)(const char *str, int c) = from_right ? strrchr : strchr; + BLI_assert(end == NULL || end > str); + *sep = *suf = NULL; for (d = delim; *d != '\0'; ++d) { - char *tmp = func(str, *d); + const char *tmp; + + if (end) { + if (from_right) { + for (tmp = end - 1; (tmp >= str) && (*tmp != *d); tmp--); + if (tmp < str) { + tmp = NULL; + } + } + else { + tmp = func(str, *d); + if (tmp >= end) { + tmp = NULL; + } + } + } + else { + tmp = func(str, *d); + } if (tmp && (from_right ? (*sep < tmp) : (!*sep || *sep > tmp))) { - *sep = tmp; + *sep = (char *)tmp; } } @@ -903,7 +925,7 @@ size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, cha return (size_t)(*sep - str); } - return strlen(str); + return end ? (size_t)(end - str) : strlen(str); } /** |