diff options
author | Thomas Adam <thomas@xteddy.org> | 2019-06-20 19:02:27 +0100 |
---|---|---|
committer | Thomas Adam <thomas@xteddy.org> | 2019-06-20 19:02:27 +0100 |
commit | 5aae58295e5f6ef6b93d745ae99b8bdb35f0058e (patch) | |
tree | 653f410c9e0832785fe1532f71d39b0ed6a984ed | |
parent | 4a44ae06bf6c64350a336e0224a70a9505b5670f (diff) | |
parent | f4e835754c7e13ed6e810104290608fe27c83cca (diff) | |
download | rtmux-5aae58295e5f6ef6b93d745ae99b8bdb35f0058e.tar.gz rtmux-5aae58295e5f6ef6b93d745ae99b8bdb35f0058e.tar.bz2 rtmux-5aae58295e5f6ef6b93d745ae99b8bdb35f0058e.zip |
Merge branch 'obsd-master'
-rw-r--r-- | regsub.c | 35 |
1 files changed, 26 insertions, 9 deletions
@@ -63,7 +63,8 @@ regsub(const char *pattern, const char *with, const char *text, int flags) { regex_t r; regmatch_t m[10]; - size_t start, end, len = 0; + ssize_t start, end, last, len = 0; + int empty = 0; char *buf = NULL; if (*text == '\0') @@ -72,9 +73,10 @@ regsub(const char *pattern, const char *with, const char *text, int flags) return (NULL); start = 0; + last = 0; end = strlen(text); - while (start != end) { + while (start <= end) { m[0].rm_so = start; m[0].rm_eo = end; @@ -82,14 +84,29 @@ regsub(const char *pattern, const char *with, const char *text, int flags) regsub_copy(&buf, &len, text, start, end); break; } - if (m[0].rm_so == m[0].rm_eo) { - regsub_copy(&buf, &len, text, start, end); - break; - } - regsub_copy(&buf, &len, text, start, m[0].rm_so); - regsub_expand(&buf, &len, with, text, m, nitems(m)); - start = m[0].rm_eo; + /* + * Append any text not part of this match (from the end of the + * last match). + */ + regsub_copy(&buf, &len, text, last, m[0].rm_so); + + /* + * If the last match was empty and this one isn't (it is either + * later or has matched text), expand this match. If it is + * empty, move on one character and try again from there. + */ + if (empty || m[0].rm_so != last || m[0].rm_so != m[0].rm_eo) { + regsub_expand(&buf, &len, with, text, m, nitems(m)); + + last = m[0].rm_eo; + start = m[0].rm_eo; + empty = 0; + } else { + last = m[0].rm_eo; + start = m[0].rm_eo + 1; + empty = 1; + } } buf[len] = '\0'; |