[Bf-blender-cvs] [82e1b65d91f] master: BLI_string: support escaping additional control character

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


Commit: 82e1b65d91f9ac8f09a9a4698776f21113db46c9
Author: Campbell Barton
Date:   Thu Dec 10 13:45:57 2020 +1100
Branches: master
https://developer.blender.org/rB82e1b65d91f9ac8f09a9a4698776f21113db46c9

BLI_string: support escaping additional control character

Add support for escaping \a, \b & \f for completeness,
currently it's not required.

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

M	source/blender/blenlib/intern/string.c
M	source/blender/blenlib/tests/BLI_string_test.cc

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

diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index c8b2f3f6e93..98eed838197 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -340,7 +340,10 @@ size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const si
     if (ELEM(c, '\\', '"') ||                       /* Use as-is. */
         ((c == '\t') && ((void)(c = 't'), true)) || /* Tab. */
         ((c == '\n') && ((void)(c = 'n'), true)) || /* Newline. */
-        ((c == '\r') && ((void)(c = 'r'), true)))   /* Carriage return. */
+        ((c == '\r') && ((void)(c = 'r'), true)) || /* Carriage return. */
+        ((c == '\a') && ((void)(c = 'a'), true)) || /* Bell. */
+        ((c == '\b') && ((void)(c = 'b'), true)) || /* Backspace. */
+        ((c == '\f') && ((void)(c = 'f'), true)))   /* Form-feed. */
     {
       if (UNLIKELY(len + 1 >= dst_maxncpy)) {
         /* Not enough space to escape. */
@@ -378,7 +381,11 @@ size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const
       if (((c_next == '"') && ((void)(c = '"'), true)) ||   /* Quote. */
           ((c_next == '\\') && ((void)(c = '\\'), true)) || /* Backslash. */
           ((c_next == 't') && ((void)(c = '\t'), true)) ||  /* Tab. */
-          ((c_next == 'n') && ((void)(c = '\n'), true)))    /* Newline. */
+          ((c_next == 'n') && ((void)(c = '\n'), true)) ||  /* Newline. */
+          ((c_next == 'r') && ((void)(c = '\r'), true)) ||  /* Carriage return. */
+          ((c_next == 'a') && ((void)(c = '\a'), true)) ||  /* Bell. */
+          ((c_next == 'b') && ((void)(c = '\b'), true)) ||  /* Backspace. */
+          ((c_next == 'f') && ((void)(c = '\f'), true)))    /* Form-feed. */
       {
         i++;
         src++;
diff --git a/source/blender/blenlib/tests/BLI_string_test.cc b/source/blender/blenlib/tests/BLI_string_test.cc
index 4b6cad12813..4446639322c 100644
--- a/source/blender/blenlib/tests/BLI_string_test.cc
+++ b/source/blender/blenlib/tests/BLI_string_test.cc
@@ -863,11 +863,14 @@ TEST_F(StringEscape, Control)
       {"\n", "\\n"},
       {"\r", "\\r"},
       {"\t", "\\t"},
+      {"\a", "\\a"},
+      {"\b", "\\b"},
+      {"\f", "\\f"},
       {"A\n", "A\\n"},
       {"\nA", "\\nA"},
-      {"\n\r\t", "\\n\\r\\t"},
-      {"\n_\r_\t", "\\n_\\r_\\t"},
-      {"\n\\\r\\\t", "\\n\\\\\\r\\\\\\t"},
+      {"\n\r\t\a\b\f", "\\n\\r\\t\\a\\b\\f"},
+      {"\n_\r_\t_\a_\b_\f", "\\n_\\r_\\t_\\a_\\b_\\f"},
+      {"\n\\\r\\\t\\\a\\\b\\\f", "\\n\\\\\\r\\\\\\t\\\\\\a\\\\\\b\\\\\\f"},
   };
 
   testEscapeWords(escaped);



More information about the Bf-blender-cvs mailing list