diff options
author | ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069> | 2015-12-04 18:39:08 +0000 |
---|---|---|
committer | ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069> | 2015-12-04 18:39:08 +0000 |
commit | 4119f9c9f4a23a25441185a75bc7e2531f62196e (patch) | |
tree | 11a0e681680a83db55fb99f1e2023927e2413d85 /src/pcre2_substitute.c | |
parent | f62077471976971c426d0ae906c7c1136686d47c (diff) | |
download | pcre2-4119f9c9f4a23a25441185a75bc7e2531f62196e.tar.gz |
Implement PCRE2_SUBSTITUTE_UNSET_EMPTY.
git-svn-id: svn://vcs.exim.org/pcre2/code/trunk@461 6239d852-aaf2-0410-a92c-79f79f948069
Diffstat (limited to 'src/pcre2_substitute.c')
-rw-r--r-- | src/pcre2_substitute.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/pcre2_substitute.c b/src/pcre2_substitute.c index 94a329e..76dd37b 100644 --- a/src/pcre2_substitute.c +++ b/src/pcre2_substitute.c @@ -197,6 +197,7 @@ BOOL match_data_created = FALSE; BOOL global = FALSE; BOOL extended = FALSE; BOOL literal = FALSE; +BOOL uempty = FALSE; /* Unset/unknown groups => empty string */ #ifdef SUPPORT_UNICODE BOOL utf = (code->overall_options & PCRE2_UTF) != 0; #endif @@ -262,6 +263,12 @@ if ((options & PCRE2_SUBSTITUTE_EXTENDED) != 0) extended = TRUE; } +if ((options & PCRE2_SUBSTITUTE_UNSET_EMPTY) != 0) + { + options &= ~PCRE2_SUBSTITUTE_UNSET_EMPTY; + uempty = TRUE; + } + /* Copy up to the start offset */ if (start_offset > buff_length) goto NOROOM; @@ -471,7 +478,6 @@ do if (inparens) { - if (extended && !star && ptr < repend - 2 && next == CHAR_COLON) { special = *(++ptr); @@ -562,8 +568,20 @@ do if (group < 0) group = GET2(first, 0); } + /* We now have a group that is identified by number. Find the length of + the captured string. If a group in a non-special substitution is unset + when PCRE2_SUBSTITUTE_UNSET_EMPTY is set, substitute nothing. */ + rc = pcre2_substring_length_bynumber(match_data, group, &sublength); - if (rc < 0 && (special == 0 || rc != PCRE2_ERROR_UNSET)) goto PTREXIT; + if (rc < 0) + { + if (rc != PCRE2_ERROR_UNSET) goto PTREXIT; /* Non-unset errors */ + if (special == 0) /* Plain substitution */ + { + if (uempty) continue; /* Treat as empty */ + goto PTREXIT; /* Else error */ + } + } /* If special is '+' we have a 'set' and possibly an 'unset' text, both of which are reprocessed when used. If special is '-' we have a |