Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2014-07-04 16:14:06 +0400
committerBastien Montagne <montagne29@wanadoo.fr>2014-07-04 16:14:06 +0400
commite3c8cf0a9ea00ef6c7a63b5fc8351f5f9be1ac8f (patch)
treeada2dce46e6e94aebb24cab35aed7c5e2a30fc98 /source/blender/blenlib/intern/string.c
parent85c4feab027238bb9dbf7fa727cb608052ec79d5 (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.c')
-rw-r--r--source/blender/blenlib/intern/string.c60
1 files changed, 60 insertions, 0 deletions
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);
+}