str: allow escaping with prefix and suffix

Allow `git_str_puts_escaped` to take an escaping prefix and an escaping
suffix; this allows for more options, including the ability to better
support escaping executed paths.
This commit is contained in:
Edward Thomson
2025-10-13 22:30:52 +01:00
parent adac288376
commit 4c9134e501
3 changed files with 42 additions and 12 deletions

View File

@@ -691,17 +691,33 @@ void test_gitstr__puts_escaped(void)
git_str a = GIT_STR_INIT;
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "", ""));
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "", "", ""));
cl_assert_equal_s("this is a test", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "t", "\\"));
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "", NULL, NULL));
cl_assert_equal_s("this is a test", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "t", "\\", ""));
cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "i ", "__"));
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "t", "\\", NULL));
cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "i ", "__", NULL));
cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this is a test", "i ", "__", "!!"));
cl_assert_equal_s("th__i!!s__ !!__i!!s__ !!a__ !!test", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escaped(&a, "this' is' an' escape! ", "'!", "'\\", "'"));
cl_assert_equal_s("this'\\'' is'\\'' an'\\'' escape'\\!' ", a.ptr);
git_str_clear(&a);
cl_git_pass(git_str_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr);