/* Implementation of :s and :gs filename modifiers. */ static int apply_s_gs_mod(const char *path, const char *mod, char *buf, size_t buf_len) { char pattern[256], sub[256]; int global; const char *start = mod; char c = (mod[1] == 'g') ? mod++[3] : mod[2]; const char *t, *p = find_nth_chr(mod, c, 3); if(p == NULL) { snprintf(buf, buf_len, "%s", path); return 0; } t = find_nth_chr(mod, c, 2); snprintf(pattern, t - (mod + 3) + 1, "%s", mod + 3); snprintf(sub, p - (t + 1) + 1, "%s", t + 1); global = (mod[0] == 'g'); snprintf(buf, buf_len, "%s", substitute_in_name(path, pattern, sub, global)); return (p + 1) - start - 2; }
static void test_substitute_end_global(void) { assert_string_equal("10", substitute_in_name("100", "0$", "", 1)); }
static void test_substitute_segfault_bug(void) { /* see #SF3515922 */ assert_string_equal("barfoobar", substitute_in_name("foobar", "^", "bar", 1)); }
static void test_substitute_begin_global(void) { assert_string_equal("01", substitute_in_name("001", "^0", "", 1)); }