From 82e1b65d91f9ac8f09a9a4698776f21113db46c9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 10 Dec 2020 13:45:57 +1100 Subject: BLI_string: support escaping additional control character Add support for escaping \a, \b & \f for completeness, currently it's not required. --- source/blender/blenlib/intern/string.c | 11 +++++++++-- source/blender/blenlib/tests/BLI_string_test.cc | 9 ++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'source') 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); -- cgit v1.2.3