From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: zsh-workers@zsh.org
Subject: [PATCH] Re: regexp-replace and ^, word boundary or look-behind operators
Date: Tue, 17 Dec 2019 11:11:13 +0000 [thread overview]
Message-ID: <20191217111113.z242f4g6sx7xdwru@chaz.gmail.com> (raw)
In-Reply-To: <20191217073846.4usg2hnsk66bhqvl@chaz.gmail.com>
2019-12-17 07:38:46 +0000, Stephane Chazelas:
> 2019-12-16 21:27:06 +0000, Stephane Chazelas:
> [...]
> > PCRE should be OK, so it could be just a matter of
> > exposing it via the pcre_match builtin
> [...]
>
> D'oh, it's there already with the -b, -n options.
>
> I'll try and suggest a regexp-replace improvement using that.
[...]
There's another issue in that the zero-width matches cause
infinite loops. Here's my first attempt at fixing those issues
(also fixing a few issues in the zpgrep example functions while
I'm at it):
diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index d32ba018d..61e6a434f 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -4301,6 +4301,9 @@ and arithmetic expressions which will be replaced: in particular, a
reference to tt($MATCH) will be replaced by the text matched by the pattern.
The return status is 0 if at least one match was performed, else 1.
+
+Note that if not using PCRE, using the tt(^) or word boundary operators
+(where available) may not work properly.
)
findex(run-help)
item(tt(run-help) var(cmd))(
diff --git a/Functions/Example/zpgrep b/Functions/Example/zpgrep
index 8b1edaa1c..556e58cd6 100644
--- a/Functions/Example/zpgrep
+++ b/Functions/Example/zpgrep
@@ -2,24 +2,31 @@
#
zpgrep() {
-local file pattern
+local file pattern ret
pattern=$1
shift
+ret=1
if ((! ARGC)) then
set -- -
fi
-pcre_compile $pattern
+zmodload zsh/pcre || return
+pcre_compile -- "$pattern"
pcre_study
for file
do
if [[ "$file" == - ]] then
- while read -u0 buf; do pcre_match $buf && print $buf; done
+ while IFS= read -ru0 buf; do
+ pcre_match -- "$buf" && ret=0 && print -r -- "$buf"
+ done
else
- while read -u0 buf; do pcre_match $buf && print $buf; done < "$file"
+ while IFS= read -ru0 buf; do
+ pcre_match -- "$buf" && ret=0 && print -r -- "$buf"
+ done < "$file"
fi
done
+return "$ret"
}
diff --git a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace
index dec105524..41ea9d79e 100644
--- a/Functions/Misc/regexp-replace
+++ b/Functions/Misc/regexp-replace
@@ -8,36 +8,79 @@
# $ and backtick substitutions; in particular, $MATCH will be replaced
# by the portion of the string matched by the regular expression.
-integer pcre
+# we use positional parameters instead of variables to avoid
+# clashing with the user's variable. Make sure we start with 3 and only
+# 3 elements:
+argv=("$1" "$2" "$3")
-[[ -o re_match_pcre ]] && pcre=1
+# $4 records whether pcre is enabled as that information would otherwise
+# be lost after emulate -L zsh
+4=0
+[[ -o re_match_pcre ]] && 4=1
emulate -L zsh
-(( pcre )) && setopt re_match_pcre
-
-# $4 is the string to be matched
-4=${(P)1}
-# $5 is the final string
-5=
-# 6 indicates if we made a change
-6=
+
+
local MATCH MBEGIN MEND
local -a match mbegin mend
-while [[ -n $4 ]]; do
- if [[ $4 =~ $2 ]]; then
- # append initial part and subsituted match
- 5+=${4[1,MBEGIN-1]}${(e)3}
- # truncate remaining string
- 4=${4[MEND+1,-1]}
- # indicate we did something
- 6=1
- else
- break
- fi
-done
-5+=$4
-
-eval ${1}=${(q)5}
-# status 0 if we did something, else 1.
-[[ -n $6 ]]
+if (( $4 )); then
+ # if using pcre, we're using pcre_match and a running offset
+ # That's needed for ^, \A, \b, and look-behind operators to work
+ # properly.
+
+ zmodload zsh/pcre || return 2
+ pcre_compile -- "$2" && pcre_study || return 2
+
+ # $4 is the current *byte* offset, $5, $6 reserved for later
+ 4=0 5= 6=1
+
+ local ZPCRE_OP IFS=' '
+ while pcre_match -b -n $4 -- "${(P)1}"; do
+ # append offsets and computed replacement to the array
+ argv+=($=ZPCRE_OP ${(e)3})
+
+ # for 0-width matches, increase offset by 1 to avoid
+ # infinite loop
+ 4=$((argv[-2] + (argv[-3] == argv[-2])))
+ done
+
+ (($# > 6)) || return # no match
+
+ set +o multibyte
+
+ # $5 contains the result, $6 the current offset
+ for 2 3 4 in "$@[7,-1]"; do
+ 5+=${(P)1[$6,$2]}$4
+ 6=$(($3 + 1))
+ done
+ 5+=${(P)1[$6,-1]}
+else
+ # in ERE, we can't use an offset so ^, (and \<, \b, \B, [[:<:]] where
+ # available) won't work properly.
+
+ # $4 is the string to be matched
+ 4=${(P)1}
+
+ while [[ -n $4 ]]; do
+ if [[ $4 =~ $2 ]]; then
+ # append initial part and substituted match
+ 5+=${4[1,MBEGIN-1]}${(e)3}
+ # truncate remaining string
+ if ((MEND < MBEGIN)); then
+ # zero-width match, skip one character for the next match
+ ((MEND++))
+ 5+=${4[1]}
+ fi
+ 4=${4[MEND+1,-1]}
+ # indicate we did something
+ 6=1
+ else
+ break
+ fi
+ done
+ [[ -n $6 ]] || return # no match
+ 5+=$4
+fi
+
+eval $1=\$5
next prev parent reply other threads:[~2019-12-17 11:16 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-16 21:10 Stephane Chazelas
2019-12-16 21:27 ` Stephane Chazelas
2019-12-17 7:38 ` Stephane Chazelas
2019-12-17 11:11 ` Stephane Chazelas [this message]
2019-12-18 0:22 ` [PATCH] " Daniel Shahaf
2019-12-18 8:31 ` Stephane Chazelas
2020-01-01 14:03 ` [PATCH v2] " Stephane Chazelas
2021-04-30 6:11 ` Stephane Chazelas
2021-04-30 23:13 ` Bart Schaefer
2021-05-05 11:45 ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2021-05-31 0:58 ` Lawrence Velázquez
2021-05-31 18:18 ` Bart Schaefer
2021-05-31 21:37 ` [PATCH] (?) typeset array[position=index]=value Bart Schaefer
2021-06-01 5:32 ` Stephane Chazelas
2021-06-01 16:05 ` Bart Schaefer
2021-06-02 2:51 ` [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff] Bart Schaefer
2021-06-02 10:06 ` Stephane Chazelas
2021-06-02 14:52 ` Bart Schaefer
2021-06-02 16:02 ` Stephane Chazelas
2021-06-02 9:11 ` [PATCH] (?) typeset array[position=index]=value Stephane Chazelas
2021-06-02 13:34 ` Daniel Shahaf
2021-06-02 14:20 ` Stephane Chazelas
2021-06-02 15:59 ` Bart Schaefer
2021-06-03 2:04 ` [PATCH (not final)] (take three?) unset "array[$anything]" Bart Schaefer
2021-06-03 2:42 ` Bart Schaefer
2021-06-03 6:12 ` Bart Schaefer
2021-06-03 8:54 ` Peter Stephenson
2021-06-03 13:13 ` Stephane Chazelas
2021-06-03 14:41 ` Peter Stephenson
2021-06-04 19:25 ` Bart Schaefer
2021-06-05 18:18 ` Peter Stephenson
2021-06-09 23:31 ` Bart Schaefer
2021-06-13 16:51 ` Peter Stephenson
2021-06-13 18:04 ` Bart Schaefer
2021-06-13 19:48 ` Peter Stephenson
2021-06-13 21:44 ` Bart Schaefer
2021-06-14 7:19 ` Stephane Chazelas
2021-06-03 18:12 ` Bart Schaefer
2021-06-04 8:02 ` Stephane Chazelas
2021-06-04 18:36 ` Bart Schaefer
2021-06-04 20:21 ` Stephane Chazelas
2021-06-05 0:20 ` Bart Schaefer
2021-06-05 17:05 ` Stephane Chazelas
2021-06-10 0:14 ` Square brackets in command position Bart Schaefer
2021-06-03 6:05 ` [PATCH (not final)] (take three?) unset "array[$anything]" Stephane Chazelas
2021-06-03 6:43 ` Bart Schaefer
2021-06-03 7:31 ` Stephane Chazelas
2021-06-10 0:21 ` [PATCH] (?) typeset array[position=index]=value Bart Schaefer
2021-06-05 4:29 ` Mikael Magnusson
2021-06-05 5:49 ` Bart Schaefer
2021-06-05 11:06 ` Mikael Magnusson
2021-06-05 16:22 ` Bart Schaefer
2021-06-18 10:53 ` Mikael Magnusson
2024-03-08 15:30 ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2024-03-09 8:41 ` [PATCH v5] " Stephane Chazelas
2024-03-09 9:21 ` MBEGIN when =~ finds bytes inside characters (Was: [PATCH v5] regexp-replace and ^, word boundary or look-behind operators (and more).) Stephane Chazelas
2024-03-09 13:03 ` [PATCH v3] regexp-replace and ^, word boundary or look-behind operators (and more) Stephane Chazelas
2024-03-10 19:52 ` [PATCH v6] " Stephane Chazelas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191217111113.z242f4g6sx7xdwru@chaz.gmail.com \
--to=stephane.chazelas@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).