From 2693d852f61ee5e1465d31d98a166cde4956ccd1 Mon Sep 17 00:00:00 2001 From: "Barton E. Schaefer" Date: Sun, 20 Jul 2014 11:42:09 -0700 Subject: 32891: CASE_MATCH for =~ when using pcre; fix pcre_match with empty string --- ChangeLog | 6 ++++++ Doc/Zsh/mod_pcre.yo | 10 +++++++++- Src/Modules/pcre.c | 13 ++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7b4e05738..e13398149 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2014-07-20 Barton E. Schaefer + + * 32891: Doc/Zsh/mod_pcre.yo Src/Modules/pcre.c: the CASE_MATCH + option should apply to =~ when using pcre; fix pcre_match bug + with handling of empty string argument. + 2014-07-17 Barton E. Schaefer * 32882 (cf. Augie Fackler 32879): Src/hist.c: restore correct diff --git a/Doc/Zsh/mod_pcre.yo b/Doc/Zsh/mod_pcre.yo index 6ab5a1937..faada28de 100644 --- a/Doc/Zsh/mod_pcre.yo +++ b/Doc/Zsh/mod_pcre.yo @@ -80,6 +80,14 @@ Matches a string against a perl-compatible regular expression. For example, -[[ "$text" -pcre-match ^d+$ ]] && print text variable contains only "d's". +example([[ "$text" -pcre-match ^d+$ ]] && +print text variable contains only "d's".) + +pindex(REMATCH_PCRE) +pindex(NO_CASE_MATCH) +If the tt(REMATCH_PCRE) option is set, the tt(=~) operator is equivalent to +tt(-pcre-match), and the tt(NO_CASE_MATCH) option may be used. Note that +tt(NO_CASE_MATCH) never applies to the tt(pcre_match) builtin, instead use +the tt(-i) switch of tt(pcre_compile). ) enditem() diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c index cb9f8ef57..0e43ab7a9 100644 --- a/Src/Modules/pcre.c +++ b/Src/Modules/pcre.c @@ -274,7 +274,7 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func)) int return_value = 1; /* The subject length and offset start are both int values in pcre_exec */ int subject_len; - int offset_start = 0; + int offset_start = -1; int want_offset_pair = 0; if (pcre_pattern == NULL) { @@ -289,14 +289,11 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func)) matched_portion = OPT_ARG(ops,c); } if(OPT_HASARG(ops,c='n')) { /* The offset position to start the search, in bytes. */ - offset_start = getposint(OPT_ARG(ops,c), nam); + if ((offset_start = getposint(OPT_ARG(ops,c), nam) < 0)) + return 1; } /* For the entire match, 'Return' the offset byte positions instead of the matched string */ if(OPT_ISSET(ops,'b')) want_offset_pair = 1; - - if(!*args) { - zwarnnam(nam, "not enough arguments"); - } if ((ret = pcre_fullinfo(pcre_pattern, pcre_hints, PCRE_INFO_CAPTURECOUNT, &capcount))) { @@ -311,7 +308,7 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func)) unmetafy(plaintext, NULL); subject_len = (int)strlen(plaintext); - if (offset_start < 0 || offset_start >= subject_len) + if (offset_start > 0 && offset_start >= subject_len) ret = PCRE_ERROR_NOMATCH; else ret = pcre_exec(pcre_pattern, pcre_hints, plaintext, subject_len, offset_start, 0, ovec, ovecsize); @@ -345,6 +342,8 @@ cond_pcre_match(char **a, int id) if (zpcre_utf8_enabled()) pcre_opts |= PCRE_UTF8; + if (isset(REMATCHPCRE) && !isset(CASEMATCH)) + pcre_opts |= PCRE_CASELESS; lhstr = cond_str(a,0,0); rhre = cond_str(a,1,0); -- cgit 1.4.1