[Bf-blender-cvs] [2814cdbd863] master: BLI_string: extract quote utility into BLI_str_escape_find_quote

Campbell Barton noreply at git.blender.org
Thu Dec 10 07:03:55 CET 2020


Commit: 2814cdbd86389516eeea570ae12f7c2c7338d81b
Author: Campbell Barton
Date:   Thu Dec 10 15:06:16 2020 +1100
Branches: master
https://developer.blender.org/rB2814cdbd86389516eeea570ae12f7c2c7338d81b

BLI_string: extract quote utility into BLI_str_escape_find_quote

Duplicate logic for this exists in BLI_str_quoted_substrN,
which doesn't properly support escaping.

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

M	source/blender/blenlib/BLI_string.h
M	source/blender/blenlib/intern/string.c
M	source/blender/makesrna/intern/rna_access.c

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

diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index 2d1f8ac9c5e..096e7818013 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -89,6 +89,7 @@ size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const si
     ATTR_NONNULL();
 size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const size_t src_maxncpy)
     ATTR_NONNULL();
+const char *BLI_str_escape_find_quote(const char *str) ATTR_NONNULL();
 
 size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL();
 size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num) ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index c4b51338360..d02241230de 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -398,6 +398,21 @@ size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const
   return len;
 }
 
+/**
+ * Find the first un-escaped quote in the string (to find the end of the string).
+ */
+const char *BLI_str_escape_find_quote(const char *str)
+{
+  bool escape = false;
+  while (*str && (*str != '"' || escape)) {
+    /* A pair of back-slashes represents a single back-slash,
+     * only use a single back-slash for escaping. */
+    escape = (escape == false) && (*str == '\\');
+    str++;
+  }
+  return (*str == '"') ? str : NULL;
+}
+
 /**
  * Makes a copy of the text within the "" that appear after some text 'blahblah'
  * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c
index 70b4d4204df..deedf72600a 100644
--- a/source/blender/makesrna/intern/rna_access.c
+++ b/source/blender/makesrna/intern/rna_access.c
@@ -4978,21 +4978,16 @@ static char *rna_path_token(const char **path, char *fixedbuf, int fixedlen, int
       }
     }
     else {
-      bool escape = false;
-      /* Skip the first quote. */
-      len++;
-      p++;
-      while (*p && (*p != '"' || escape)) {
-        /* A pair of back-slashes represents a single back-slash,
-         * only use a single back-slash for escaping. */
-        escape = (escape == false) && (*p == '\\');
-        len++;
-        p++;
+      const char *p_end = BLI_str_escape_find_quote(p + 1);
+      if (p_end == NULL) {
+        /* No Matching quote. */
+        return NULL;
       }
-
       /* Skip the last quoted char to get the `]`. */
-      len++;
-      p++;
+      p_end += 1;
+
+      len += (p_end - p);
+      p = p_end;
     }
 
     if (*p != ']') {



More information about the Bf-blender-cvs mailing list