diff options
| author | Tyge Løvset <[email protected]> | 2022-07-28 00:24:13 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-28 12:39:34 +0200 |
| commit | 281a4148d9143260c614c92e81d3484103479761 (patch) | |
| tree | 6661f8f41c416c0120d677fbaffb07f9b3eabbc9 /src/cregex.c | |
| parent | 18392a1f49bb742fce3e28bb8196b2a64ea07219 (diff) | |
| download | STC-modified-281a4148d9143260c614c92e81d3484103479761.tar.gz STC-modified-281a4148d9143260c614c92e81d3484103479761.zip | |
VERSION 3.8 RC. Added cregex with "final" API + docs. README.md updated with links to cregex, coption.
crandom.h: fixed "stc64_with_seq doesn't use seq argument #31" thanks to funny-falcon. Removed deprecated funcs.
Added tags for v3.6 and v3.7.
Diffstat (limited to 'src/cregex.c')
| -rw-r--r-- | src/cregex.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/cregex.c b/src/cregex.c index 1d9609b3..c50e870f 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1228,34 +1228,35 @@ int cregex_find_p(const char* input, const char* pattern, cstr cregex_replace_re(const char* input, const cregex* re, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count) { + bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int rflags) { cstr out = cstr_null; cstr subst = cstr_null; size_t from = 0; csview match[cre_MAXCAPTURES]; unsigned nmatch = cregex_captures(re); if (!count) count = ~0; + bool copy = !(rflags & cre_r_strip); while (count-- && cregex_find(input + from, re, match, 0) == 1) { build_subst_string(replace, nmatch, match, mfun, &subst); const size_t pos = match[0].str - input; - cstr_append_n(&out, input + from, pos - from); + if (copy) cstr_append_n(&out, input + from, pos - from); cstr_append_s(&out, subst); from = pos + match[0].size; } - cstr_append(&out, input + from); + if (copy) cstr_append(&out, input + from); cstr_drop(&subst); return out; } cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int cflags) { + bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int crflags) { cregex re = cregex_init(); - int res = cregex_compile(&re, pattern, cflags); + int res = cregex_compile(&re, pattern, crflags); if (res < 0) return cstr_new("[[error: invalid regex pattern]]"); - cstr out = cregex_replace_re(input, &re, replace, mfun, count); + cstr out = cregex_replace_re(input, &re, replace, mfun, count, crflags); cregex_drop(&re); return out; } |
