[Bf-blender-cvs] [2dbda801602] temp-eevee-next-cryptomatte: Cleanup: simplify partition functions

Campbell Barton noreply at git.blender.org
Tue Aug 23 12:59:56 CEST 2022


Commit: 2dbda801602c1159503ff0bf3789646b5c92fc7a
Author: Campbell Barton
Date:   Tue Aug 23 12:44:40 2022 +1000
Branches: temp-eevee-next-cryptomatte
https://developer.blender.org/rB2dbda801602c1159503ff0bf3789646b5c92fc7a

Cleanup: simplify partition functions

- Assign return arguments last instead of manipulating them.
- Remove redundant NULL assignment of return arguments.

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

M	source/blender/blenlib/BLI_string_utf8.h
M	source/blender/blenlib/intern/string_utf8.c

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

diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h
index 58c9ad23e4c..61a21fd8bbf 100644
--- a/source/blender/blenlib/BLI_string_utf8.h
+++ b/source/blender/blenlib/BLI_string_utf8.h
@@ -174,17 +174,17 @@ int BLI_str_utf8_char_width_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NON
 
 size_t BLI_str_partition_utf8(const char *str,
                               const unsigned int delim[],
-                              const char **sep,
-                              const char **suf) ATTR_NONNULL(1, 2, 3, 4);
+                              const char **r_sep,
+                              const char **r_suf) ATTR_NONNULL(1, 2, 3, 4);
 size_t BLI_str_rpartition_utf8(const char *str,
                                const unsigned int delim[],
-                               const char **sep,
-                               const char **suf) ATTR_NONNULL(1, 2, 3, 4);
+                               const char **r_sep,
+                               const char **r_suf) ATTR_NONNULL(1, 2, 3, 4);
 size_t BLI_str_partition_ex_utf8(const char *str,
                                  const char *end,
                                  const unsigned int delim[],
-                                 const char **sep,
-                                 const char **suf,
+                                 const char **r_sep,
+                                 const char **r_suf,
                                  bool from_right) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 3, 4, 5);
 
 int BLI_str_utf8_offset_to_index(const char *str, int offset) ATTR_WARN_UNUSED_RESULT
diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c
index 93045bd3680..992a07b2062 100644
--- a/source/blender/blenlib/intern/string_utf8.c
+++ b/source/blender/blenlib/intern/string_utf8.c
@@ -692,25 +692,25 @@ const char *BLI_str_find_next_char_utf8(const char *p, const char *str_end)
 
 size_t BLI_str_partition_utf8(const char *str,
                               const uint delim[],
-                              const char **sep,
-                              const char **suf)
+                              const char **r_sep,
+                              const char **r_suf)
 {
-  return BLI_str_partition_ex_utf8(str, NULL, delim, sep, suf, false);
+  return BLI_str_partition_ex_utf8(str, NULL, delim, r_sep, r_suf, false);
 }
 
 size_t BLI_str_rpartition_utf8(const char *str,
                                const uint delim[],
-                               const char **sep,
-                               const char **suf)
+                               const char **r_sep,
+                               const char **r_suf)
 {
-  return BLI_str_partition_ex_utf8(str, NULL, delim, sep, suf, true);
+  return BLI_str_partition_ex_utf8(str, NULL, delim, r_sep, r_suf, true);
 }
 
 size_t BLI_str_partition_ex_utf8(const char *str,
                                  const char *end,
                                  const uint delim[],
-                                 const char **sep,
-                                 const char **suf,
+                                 const char **r_sep,
+                                 const char **r_suf,
                                  const bool from_right)
 {
   const size_t str_len = end ? (size_t)(end - str) : strlen(str);
@@ -721,36 +721,32 @@ size_t BLI_str_partition_ex_utf8(const char *str,
   /* Note that here, we assume end points to a valid utf8 char! */
   BLI_assert((end >= str) && (BLI_str_utf8_as_unicode(end) != BLI_UTF8_ERR));
 
-  *suf = (char *)(str + str_len);
-
-  size_t index;
-  for (*sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(end, str) : str), index = 0;
-       from_right ? (*sep > str) : ((*sep < end) && (**sep != '\0'));
-       *sep = (char *)(from_right ? (str != *sep ? BLI_str_find_prev_char_utf8(*sep, str) : NULL) :
-                                    str + index)) {
+  char *suf = (char *)(str + str_len);
+  size_t index = 0;
+  for (char *sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(end, str) : str);
+       from_right ? (sep > str) : ((sep < end) && (*sep != '\0'));
+       sep = (char *)(from_right ? (str != sep ? BLI_str_find_prev_char_utf8(sep, str) : NULL) :
+                                   str + index)) {
     size_t index_ofs = 0;
-    const uint c = BLI_str_utf8_as_unicode_step_or_error(*sep, (size_t)(end - *sep), &index_ofs);
-    index += index_ofs;
-
-    if (c == BLI_UTF8_ERR) {
-      *suf = *sep = NULL;
+    const uint c = BLI_str_utf8_as_unicode_step_or_error(sep, (size_t)(end - sep), &index_ofs);
+    if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
     }
+    index += index_ofs;
 
     for (const uint *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` is already correct in case from_right is true. */
+        *r_sep = sep;
+        *r_suf = from_right ? suf : (char *)(str + index);
+        return (size_t)(sep - str);
       }
     }
 
-    *suf = *sep; /* Useful in 'from_right' case! */
+    suf = sep; /* Useful in 'from_right' case! */
   }
 
-  *suf = *sep = NULL;
+  *r_suf = *r_sep = NULL;
   return str_len;
 }



More information about the Bf-blender-cvs mailing list