From ed26b04a270d3065b08616395c92610dbbf8c5b2 Mon Sep 17 00:00:00 2001 From: ketmar Date: Thu, 22 Aug 2013 13:34:24 +0300 Subject: [PATCH] more enhancements to re9_sub() --- src/libre9/re9.c | 25 ++++++++++++++++++++++--- src/libre9/re9.h | 4 +++- src/test.c | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/libre9/re9.c b/src/libre9/re9.c index 25c972b..351be37 100644 --- a/src/libre9/re9.c +++ b/src/libre9/re9.c @@ -1466,8 +1466,23 @@ int re9_sub (char *dp, size_t dlen, const char *sp, const re9_sub_t *mp, int ms) if (dp == NULL) dlen = 0; dlast = (dlen > 0 ? dp+dlen-1 : NULL); while (*sp) { - if (*sp == '\\' && sp[1] >= '0' && sp[1] <= '9') { + if (*sp == '\\' && (sp[1] == '{' || (sp[1] >= '0' && sp[1] <= '9'))) { int i = sp[1]-'0'; + if (i > 9) { + /* parse {...} */ + i = 0; + sp += 2; + while (*sp && *sp != '}') { + if (*sp < '0' || *sp > '9') break; + i = i*10+sp[0]-'0'; + ++sp; + if (i > ms) break; + } + while (*sp && *sp != '}') ++sp; + if (*sp == '}') ++sp; + } else { + sp += 2; + } if (i < ms && mp[i].sp != NULL) { const char *ssp = mp[i].sp; int len = (int)(mp[i].ep-ssp); /*FIXME: 2GB strings?*/ @@ -1479,9 +1494,13 @@ int re9_sub (char *dp, size_t dlen, const char *sp, const re9_sub_t *mp, int ms) dp += len; } } - sp += 2; } else { - if (dlen > 0) { *dp++ = *sp++; --dlen; } else ++sp; + if (*sp == '\\' && sp[1] == 'z') { + if (dlen > 0) { *dp++ = 0; --dlen; } + sp += 2; + } else { + if (dlen > 0) { *dp++ = *sp++; --dlen; } else ++sp; + } ++res; } } diff --git a/src/libre9/re9.h b/src/libre9/re9.h index 75e52f9..16dfbe8 100644 --- a/src/libre9/re9.h +++ b/src/libre9/re9.h @@ -156,7 +156,9 @@ extern void re9_prepared_free (re9_prog_prepared_t *pp); * delimited by match[n].sp and match[n].ep. Each instance of is replaced by the string * delimited by match[0].sp and match[0].ep. The substitution will always be null terminated * and trimmed to fit into dlen bytes. - * function will return number of bytes needed to successfully insert everything (including trailing 0). + * Function will return number of bytes needed to successfully insert everything (including trailing 0). + * Use '\{num}' to insert 10th and bigger match. + * Use '\z' to insert character with code 0. */ /* sp: source string * dp: destination string diff --git a/src/test.c b/src/test.c index 8360117..d72b28c 100644 --- a/src/test.c +++ b/src/test.c @@ -22,7 +22,7 @@ static struct x t[] = { { "^[^!]*\\.[^!]*!.*$", "inet!\\0", 0 }, { "^\xE2\x98\xBA$", "smiley", 0 }, { "^(coma|research|pipe|pyxis|inet|hunny|gauss)!(.*)$", "/mail/lib/qmail '\\s' 'net!\\1' '\\2'", 0 }, - { "^.*$", "/mail/lib/qmail '\\s' 'net!research' '\\0'", 0 }, + { "^.*$", "/mail/lib/qmail '\\s' 'net!research' '\\{0}'", 0 }, { 0, 0, 0 }, }; -- 2.11.4.GIT