From bb61da36aaeeaa70413cdf5bc66d7a71194f93e5 Mon Sep 17 00:00:00 2001 From: Stephane Chazelas Date: Mon, 6 Sep 2021 14:43:01 -0700 Subject: 45180: clarify doc for POSIX EREs, fix an issue with PCRE when the replacement was empty or generated more than one element --- Functions/Misc/regexp-replace | 102 +++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 27 deletions(-) (limited to 'Functions/Misc/regexp-replace') diff --git a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace index dec105524..0d5948075 100644 --- a/Functions/Misc/regexp-replace +++ b/Functions/Misc/regexp-replace @@ -8,36 +8,84 @@ # $ 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 use + 4=0 6= + + local ZPCRE_OP + while pcre_match -b -n $4 -- "${(P)1}"; do + # append offsets and computed replacement to the array + # we need to perform the evaluation in a scalar assignment so that if + # it generates an array, the elements are converted to string (by + # joining with the first chararacter of $IFS as usual) + 5=${(e)3} + argv+=(${(s: :)ZPCRE_OP} "$5") + + # 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 + 5= 6=1 + 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 -- cgit v1.2.3 From 356dcb20cef387a5eea5f8fcbfe123b24e3bb928 Mon Sep 17 00:00:00 2001 From: Dimitris Apostolou Date: Fri, 12 Nov 2021 23:33:37 +0200 Subject: github #82: Fix typos --- ChangeLog | 11 +++++++++++ Completion/BSD/Command/_kdump | 2 +- Completion/BSD/Command/_ktrace | 2 +- Completion/Debian/Command/_aptitude | 2 +- Completion/Linux/Command/_modutils | 2 +- Completion/Linux/Command/_sysstat | 2 +- Completion/Mandriva/Command/_urpmi | 2 +- Completion/Redhat/Command/_dnf | 2 +- Completion/Unix/Command/_ansible | 2 +- Completion/Unix/Command/_gcc | 4 ++-- Etc/FAQ.yo | 4 ++-- Etc/NEWS-4.3 | 2 +- Functions/Chpwd/cdr | 2 +- Functions/Misc/regexp-replace | 2 +- Functions/Newuser/zsh-newuser-install | 2 +- NEWS | 2 +- Src/Zle/compmatch.c | 8 ++++---- Src/exec.c | 2 +- Src/math.c | 4 ++-- Test/A01grammar.ztst | 2 +- Test/B12limit.ztst | 2 +- 21 files changed, 37 insertions(+), 26 deletions(-) (limited to 'Functions/Misc/regexp-replace') diff --git a/ChangeLog b/ChangeLog index de6bbb08b..1dfe2e39a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2021-11-12 Oliver Kiddle + * github #82: Dimitris Apostolou: Completion/BSD/Command/_kdump, + Completion/Redhat/Command/_dnf, Completion/BSD/Command/_ktrace, + Completion/Linux/Command/_modutils, Test/A01grammar.ztst, + Completion/Linux/Command/_sysstat, Functions/Chpwd/cdr, + Completion/Unix/Command/_ansible, Completion/Unix/Command/_gcc, + Completion/Mandriva/Command/_urpmi, Etc/NEWS-4.3, + Completion/Debian/Command/_aptitude, Etc/FAQ.yo, + Functions/Newuser/zsh-newuser-install, NEWS, + Functions/Misc/regexp-replace, Src/Zle/compmatch.c, + Src/exec.c, Src/math.c, Test/B12limit.ztst: fix typos + * Marlon: 49572: Completion/Base/Completer/_expand, Test/Y01completion.ztst: Let _expand preserve array form w/out zstyle glob diff --git a/Completion/BSD/Command/_kdump b/Completion/BSD/Command/_kdump index 946296a75..e5c7c4cce 100644 --- a/Completion/BSD/Command/_kdump +++ b/Completion/BSD/Command/_kdump @@ -30,7 +30,7 @@ local args=( '-f+[use the specified file (- for stdin)]:dump file:_files' '-l[loop reading the trace file]' '-m+[maximum I/O bytes to display]:max data bytes:' - '-n[supress ad hoc translations]' + '-n[suppress ad hoc translations]' '-p+[show output only for the specified pid]: :_kdump_pid' '(-E -T)-R[display relative timestamps]' '(-E -R )-T[display absolute timestamps]' diff --git a/Completion/BSD/Command/_ktrace b/Completion/BSD/Command/_ktrace index 13c11f15d..9613ba2bf 100644 --- a/Completion/BSD/Command/_ktrace +++ b/Completion/BSD/Command/_ktrace @@ -4,7 +4,7 @@ local args=( '-a[append to the trace file]' '(*)-C[disable tracing on all user owned processes or all processes if executed by root]' '-c[clear the trace points]' - '-d[trace current decendants]' + '-d[trace current descendants]' '-f+[log trace to specified file]:trace file:_files' '(-p *)-g+[enable/disable tracing on specified process group]:pgid:_pgids' '-i[inherit trace flags on future children]' diff --git a/Completion/Debian/Command/_aptitude b/Completion/Debian/Command/_aptitude index 91d233f11..5b10adb80 100644 --- a/Completion/Debian/Command/_aptitude +++ b/Completion/Debian/Command/_aptitude @@ -15,7 +15,7 @@ _arguments -C \ '(-F --display-format)'{-F,--display-format}'[specify output format for search command]:format:->format-strings' \ '--group-by=[control how the versions command groups its output]:grouping:(archive auto none package source-package source-version)' \ '--log-file=[specify output log file]:file:_files' \ - '*--log-level=[specify mimimum message level to log]:level:compadd -o nosort off fatal error warn info debug trace' \ + '*--log-level=[specify minimum message level to log]:level:compadd -o nosort off fatal error warn info debug trace' \ '--log-resolver[set some standard log levels related to the resolver]' \ '(--allow-new-installs)--no-new-installs[prevent safe-upgrade from installing any new packages]' \ '(--allow-new-upgrades)--no-new-upgrades[prevent safe-upgrade from upgrading packages regardless]' \ diff --git a/Completion/Linux/Command/_modutils b/Completion/Linux/Command/_modutils index 1205f2506..3e46130a2 100644 --- a/Completion/Linux/Command/_modutils +++ b/Completion/Linux/Command/_modutils @@ -105,7 +105,7 @@ _modutils() { loaded-modules|loadable-modules) if [[ -r /proc/modules ]]; then loaded_modules=(${${(f)"$(tp != CPAT_ANY || wp->tp != CPAT_ANY) @@ -1496,7 +1496,7 @@ pattern_match_restrict(Cpattern p, Cpattern wp, convchar_t *wsc, int wsclen, * characters. We're matching two patterns against * one another to generate a character to insert. * This is a bit too psychedelic, so I'm going to - * bale out now. See you on the ground. + * bail out now. See you on the ground. */ return 0; } @@ -1564,7 +1564,7 @@ pattern_match(Cpattern p, char *s, Cpattern wp, char *ws) c = unmeta_one(s, &len); /* * If either is "?", they match each other; no further tests. - * Apply this even if the character wasn't convertable; + * Apply this even if the character wasn't convertible; * there's no point trying to be clever in that case. */ if (p->tp != CPAT_ANY || wp->tp != CPAT_ANY) @@ -1934,7 +1934,7 @@ bld_line(Cmatcher mp, ZLE_STRING_T line, char *mword, char *word, * This is the nightmare case: we have line and * and word matchers and some pattern which restricts * the value on the line without us knowing exactly - * what it is. Despatch to the special function + * what it is. Dispatch to the special function * for that. */ if (mp && !mp->flags && mp->wlen <= wlen && diff --git a/Src/exec.c b/Src/exec.c index 1f23a862d..1860a10ed 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -3954,7 +3954,7 @@ execcmd_exec(Estate state, Execcmd_params eparams, if (type == WC_AUTOFN) { /* * We pre-loaded this to get any redirs. - * So we execuate a simplified function here. + * So we execute a simplified function here. */ lastval = execautofn_basic(state, do_exec); } else diff --git a/Src/math.c b/Src/math.c index ade02d80c..4f24361a4 100644 --- a/Src/math.c +++ b/Src/math.c @@ -162,7 +162,7 @@ static int unary = 1; #define TOKCOUNT 53 /* - * Opeator recedences: in reverse order, i.e. lower number, high precedence. + * Operator precedences: in reverse order, i.e. lower number, high precedence. * These are the C precedences. * * 0 Non-operators: NUM (numeric constant), ID (identifier), @@ -219,7 +219,7 @@ static int c_prec[TOKCOUNT] = }; /* - * Opeator recedences: in reverse order, i.e. lower number, high precedence. + * Operator precedences: in reverse order, i.e. lower number, high precedence. * These are the default zsh precedences. * * 0 Non-operators: NUM (numeric constant), ID (identifier), diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst index c114ff103..ac39a4eea 100644 --- a/Test/A01grammar.ztst +++ b/Test/A01grammar.ztst @@ -922,7 +922,7 @@ F:Note that the behaviour of 'exit' inside try-list inside a function is unspeci x=1 x=2 | echo $x echo $x -0:Assignment-only current shell commands in LHS of pipelin +0:Assignment-only current shell commands in LHS of pipeline >1 >1 diff --git a/Test/B12limit.ztst b/Test/B12limit.ztst index 48d33e6e3..9dce59824 100644 --- a/Test/B12limit.ztst +++ b/Test/B12limit.ztst @@ -11,7 +11,7 @@ %test limit | grep UNKNOWN || print OK -0:Check if there is unknown resouce(s) in the system +0:Check if there is unknown resource(s) in the system >OK F:A failure here does not indicate any error in zsh. It just means there F:is a resource in your system that is unknown to zsh developers. Please -- cgit v1.2.3