zsh-workers
 help / color / mirror / code / Atom feed
41ea9d79ee5021fd9a62ef78d68f6bde96f6eb62 blob 2283 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
# Replace all occurrences of a regular expression in a variable.  The
# variable is modified directly.  Respects the setting of the
# option RE_MATCH_PCRE.
#
# First argument: *name* (not contents) of variable.
# Second argument: regular expression
# Third argument: replacement string.  This can contain all forms of
# $ and backtick substitutions; in particular, $MATCH will be replaced
# by the portion of the string matched by the regular expression.

# 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")

# $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


local MATCH MBEGIN MEND
local -a match mbegin mend

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
debug log:

solving 41ea9d79e ...
found 41ea9d79e in https://inbox.vuxu.org/zsh-workers/20191217111113.z242f4g6sx7xdwru@chaz.gmail.com/
found dec105524 in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 dec105524410acb6fed788fb372d95914a54abfd	Functions/Misc/regexp-replace

applying [1/1] https://inbox.vuxu.org/zsh-workers/20191217111113.z242f4g6sx7xdwru@chaz.gmail.com/
diff --git a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace
index dec105524..41ea9d79e 100644

Checking patch Functions/Misc/regexp-replace...
Applied patch Functions/Misc/regexp-replace cleanly.

index at:
100644 41ea9d79ee5021fd9a62ef78d68f6bde96f6eb62	Functions/Misc/regexp-replace

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).