Add git_buf_unescape and git__unescape to unescape all characters in a string (in-place)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
diff --git a/src/attr_file.c b/src/attr_file.c
index 0dad097..837c42d 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -426,17 +426,7 @@ int git_attr_fnmatch__parse(
return -1;
} else {
/* strip '\' that might have be used for internal whitespace */
- char *to = spec->pattern;
- for (scan = spec->pattern; *scan; to++, scan++) {
- if (*scan == '\\')
- scan++; /* skip '\' but include next char */
- if (to != scan)
- *to = *scan;
- }
- if (to != scan) {
- *to = '\0';
- spec->length = (to - spec->pattern);
- }
+ spec->length = git__unescape(spec->pattern);
}
return 0;
diff --git a/src/buffer.c b/src/buffer.c
index 5d54ee1..b57998e 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -496,3 +496,7 @@ bool git_buf_is_binary(const git_buf *buf)
return ((printable >> 7) < nonprintable);
}
+void git_buf_unescape(git_buf *buf)
+{
+ buf->size = git__unescape(buf->ptr);
+}
diff --git a/src/buffer.h b/src/buffer.h
index 75f3b0e..17922e4 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -151,4 +151,7 @@ int git_buf_common_prefix(git_buf *buf, const git_strarray *strings);
/* Check if buffer looks like it contains binary data */
bool git_buf_is_binary(const git_buf *buf);
+/* Unescape all characters in a buffer */
+void git_buf_unescape(git_buf *buf);
+
#endif
diff --git a/src/util.c b/src/util.c
index 3093cd7..90bb3d0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -435,3 +435,21 @@ int git__parse_bool(int *out, const char *value)
return -1;
}
+
+size_t git__unescape(char *str)
+{
+ char *scan, *pos = str;
+
+ for (scan = str; *scan; pos++, scan++) {
+ if (*scan == '\\' && *(scan + 1) != '\0')
+ scan++; /* skip '\' but include next char */
+ if (pos != scan)
+ *pos = *scan;
+ }
+
+ if (pos != scan) {
+ *pos = '\0';
+ }
+
+ return (pos - str);
+}
diff --git a/src/util.h b/src/util.h
index a84dcab..905fc92 100644
--- a/src/util.h
+++ b/src/util.h
@@ -238,4 +238,13 @@ extern int git__parse_bool(int *out, const char *value);
*/
int git__date_parse(git_time_t *out, const char *date);
+/*
+ * Unescapes a string in-place.
+ *
+ * Edge cases behavior:
+ * - "jackie\" -> "jacky\"
+ * - "chan\\" -> "chan\"
+ */
+extern size_t git__unescape(char *str);
+
#endif /* INCLUDE_util_h__ */
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c
index 21aaaed..b6274b0 100644
--- a/tests-clar/core/buffer.c
+++ b/tests-clar/core/buffer.c
@@ -658,3 +658,23 @@ void test_core_buffer__puts_escaped(void)
git_buf_free(&a);
}
+
+static void assert_unescape(char *expected, char *to_unescape) {
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_sets(&buf, to_unescape));
+ git_buf_unescape(&buf);
+ cl_assert_equal_s(expected, buf.ptr);
+ cl_assert_equal_i(strlen(expected), buf.size);
+
+ git_buf_free(&buf);
+}
+
+void test_core_buffer__unescape(void)
+{
+ assert_unescape("Escaped\\", "Es\\ca\\ped\\");
+ assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\");
+ assert_unescape("\\", "\\");
+ assert_unescape("\\", "\\\\");
+ assert_unescape("", "");
+}