[Bf-blender-cvs] [4d043c9] master: Extend `BLI_str_partition_ex`: add possibility to define a right limit to the string.

Bastien Montagne noreply at git.blender.org
Sat Jun 27 10:26:20 CEST 2015


Commit: 4d043c99dc49022e10a940b26ed9bc469581926f
Author: Bastien Montagne
Date:   Sat Jun 27 10:22:29 2015 +0200
Branches: master
https://developer.blender.org/rB4d043c99dc49022e10a940b26ed9bc469581926f

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).

===================================================================

M	source/blender/blenlib/BLI_string.h
M	source/blender/blenlib/BLI_string_utf8.h
M	source/blender/blenlib/intern/string.c
M	source/blender/blenlib/intern/string_utf8.c
M	source/blender/editors/space_node/node_select.c

===================================================================

diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index e6c1cc8..a94777d 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -92,7 +92,9 @@ bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, s
 
 size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
 size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
-size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL();
+size_t BLI_str_partition_ex(
+        const char *str, const char *end, const char delim[], char **sep, char **suf, const bool from_right)
+        ATTR_NONNULL(1, 3, 4, 5);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h
index 3e59986..18eff67 100644
--- a/source/blender/blenlib/BLI_string_utf8.h
+++ b/source/blender/blenlib/BLI_string_utf8.h
@@ -69,7 +69,9 @@ 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();
+size_t       BLI_str_partition_ex_utf8(
+        const char *str, const char *end, const unsigned int delim[], char **sep, char **suf, const bool from_right)
+        ATTR_NONNULL(1, 3, 4, 5);
 
 #define      BLI_UTF8_MAX 6        /* mem */
 #define      BLI_UTF8_WIDTH_MAX 2  /* columns */
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index e41e52a..0dcb84c 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);
 }
 
 /**
diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c
index 22f44a3..85306cc 100644
--- a/source/blender/blenlib/intern/string_utf8.c
+++ b/source/blender/blenlib/intern/string_utf8.c
@@ -736,26 +736,29 @@ char *BLI_str_prev_char_utf8(const char *p)
 
 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);
+	return BLI_str_partition_ex_utf8(str, NULL, 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);
+	return BLI_str_partition_ex_utf8(str, NULL, 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)
+size_t BLI_str_partition_ex_utf8(
+        const char *str, const char *end, const unsigned int delim[], char **sep, char **suf, const bool from_right)
 {
 	const unsigned int *d;
-	const size_t str_len = strlen(str);
+	const size_t str_len = end ? (size_t)(end - str) : strlen(str);
 	size_t index;
 
+	/* Note that here, we assume end points to a valid utf8 char! */
+	BLI_assert(end == NULL || (end >= str && (BLI_str_utf8_as_unicode(end) != BLI_UTF8_ERR)));
+
 	*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))
+	     *sep >= str && (!end || *sep < end) && **sep != '\0';
+	     *sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(str, *sep) : str + index))
 	{
 		const unsigned int c = BLI_str_utf8_as_unicode_and_size(*sep, &index);
 
diff --git a/source/blender/editors/space_node/node_select.c b/source/blender/editors/space_node/node_select.c
index 25f9d56..cd03f30 100644
--- a/source/blender/editors/space_node/node_select.c
+++ b/source/blender/editors/space_node/node_select.c
@@ -251,7 +251,7 @@ static bool node_select_grouped_name(SpaceNode *snode, bNode *node_act, const bo
 	size_t pref_len_act, pref_len_curr;
 	char *sep, *suf_act, *suf_curr;
 
-	pref_len_act = BLI_str_partition_ex_utf8(node_act->name, delims, &sep, &suf_act, from_right);
+	pref_len_act = BLI_str_partition_ex_utf8(node_act->name, NULL, delims, &sep, &suf_act, from_right);
 
 	/* Note: in case we are searching for suffix, and found none, use whole name as suffix. */
 	if (from_right && !(sep && suf_act)) {
@@ -263,7 +263,7 @@ static bool node_select_grouped_name(SpaceNode *snode, bNode *node_act, const bo
 		if (node->flag & SELECT) {
 			continue;
 		}
-		pref_len_curr = BLI_str_partition_ex_utf8(node->name, delims, &sep, &suf_curr, from_right);
+		pref_len_curr = BLI_str_partition_ex_utf8(node->name, NULL, delims, &sep, &suf_curr, from_right);
 
 		/* Same as with active node name! */
 		if (from_right && !(sep && suf_curr)) {




More information about the Bf-blender-cvs mailing list