Introduce new function, escape_string_inplace(), to escape specified
characters in place.
Signed-off-by: Qu Wenruo <quwenruo@xxxxxxxxxxxxxx>
---
utils.c | 24 ++++++++++++++++++++++++
utils.h | 14 ++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/utils.c b/utils.c
index 3f54245..3c50d84 100644
--- a/utils.c
+++ b/utils.c
@@ -4251,3 +4251,27 @@ unsigned int rand_range(unsigned int upper)
*/
return (unsigned int)(jrand48(rand_seed) % upper);
}
+
+static void escape_one_char(char *restrict string, char escape)
+{
+ int i = 0;
+ int j = 0;
+
+ while (string[j] != '\0') {
+ if (string[j] != escape) {
+ string[i] = string[j];
+ i++;
+ }
+ j++;
+ }
+ /* pend the finishing '\0' */
+ string[i] = '\0';
+}
+
+void string_escape_inplace(char *restrict string, char *restrict escape_chars)
+{
+ int i;
+
+ for (i = 0; i < strlen(escape_chars); i++)
+ escape_one_char(string, escape_chars[i]);
+}
diff --git a/utils.h b/utils.h
index 1a2dbcd..dae8db4 100644
--- a/utils.h
+++ b/utils.h
@@ -457,4 +457,18 @@ unsigned int rand_range(unsigned int upper);
/* Also allow setting the seed manually */
void init_rand_seed(u64 seed);
+void string_escape_inplace(char *restrict string, char *restrict escape_chars);
+
+static inline char* string_escape(char *restrict src,
+ char *restrict escape_chars)
+{
+ char *ret;
+
+ ret = malloc(strlen(src) + 1);
+ if (!ret)
+ return NULL;
+ string_escape_inplace(ret, escape_chars);
+ return ret;
+}
+
#endif
--
2.10.1
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html