| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package regexpscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/ |
| 6 | |
| 7 | import ( |
| 8 | "regexp/syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/" |
| 9 | "slicesscip-go gomod github.com/golang/go/src go1.21 slices/" |
| 10 | "stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 11 | "unicodescip-go gomod github.com/golang/go/src go1.21 unicode/" |
| 12 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/" |
| 13 | ) |
| 14 | |
| 15 | // "One-pass" regexp execution. |
| 16 | // Some regexps can be analyzed to determine that they never need |
| 17 | // backtracking: they are guaranteed to run in one pass over the string |
| 18 | // without bothering to save all the usual NFA state. |
| 19 | // Detect those and execute them more quickly. |
| 20 | |
| 21 | // A onePassProg is a compiled one-pass regular expression program. |
| 22 | // It is the same as syntax.Prog except for the use of onePassInst. |
| 23 | type onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg# struct { |
| 24 | Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst. []onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst# |
| 25 | Startscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Start. int // index of start instruction |
| 26 | NumCapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#NumCap. int // number of InstCapture insts in re |
| 27 | } |
| 28 | |
| 29 | // A onePassInst is a single instruction in a one-pass regular expression program. |
| 30 | // It is the same as syntax.Inst except for the new 'Next' field. |
| 31 | type onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst# struct { |
| 32 | syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Inst. |
| 33 | Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. []uint32 |
| 34 | } |
| 35 | |
| 36 | // onePassPrefix returns a literal string that all matches for the |
| 37 | // regexp must start with. Complete is true if the prefix |
| 38 | // is the entire match. Pc is the index of the last rune instruction |
| 39 | // in the string. The onePassPrefix skips over the mandatory |
| 40 | // EmptyBeginText. |
| 41 | func onePassPrefixscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassPrefix().(p *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) (prefix string, complete bool, pc uint32) { |
| 42 | i := &p.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[p.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.] |
| 43 | if i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. != syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth. || (syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(i.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.))&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyBeginTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyBeginText. == 0 { |
| 44 | return "", i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch., uint32(p.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.) |
| 45 | } |
| 46 | pc = i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 47 | i = &p.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[pc] |
| 48 | for i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstNopscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstNop. { |
| 49 | pc = i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 50 | i = &p.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[pc] |
| 51 | } |
| 52 | // Avoid allocation of buffer if prefix is empty. |
| 53 | if iopscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/iop().(i) != syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune. || len(i.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.) != 1 { |
| 54 | return "", i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch., uint32(p.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.) |
| 55 | } |
| 56 | |
| 57 | // Have prefix; gather characters. |
| 58 | var buf stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Builderscip-go gomod github.com/golang/go/src go1.21 strings/Builder# |
| 59 | for iopscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/iop().(i) == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune. && len(i.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.) == 1 && syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Flagsscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Flags#(i.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.FoldCasescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/FoldCase. == 0 && i.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0] != utf8scip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/.RuneErrorscip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/RuneError. { |
| 60 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.21 strings/Builder#WriteRune().(i.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0]) |
| 61 | pc, i = i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., &p.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.] |
| 62 | } |
| 63 | if i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth. && |
| 64 | syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(i.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyEndTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyEndText. != 0 && |
| 65 | p.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. { |
| 66 | complete = true |
| 67 | } |
| 68 | return buf.Stringscip-go gomod github.com/golang/go/src go1.21 strings/Builder#String().(), complete, pc |
| 69 | } |
| 70 | |
| 71 | // onePassNext selects the next actionable state of the prog, based on the input character. |
| 72 | // It should only be called when i.Op == InstAlt or InstAltMatch, and from the one-pass machine. |
| 73 | // One of the alternates may ultimately lead without input to end of line. If the instruction |
| 74 | // is InstAltMatch the path to the InstMatch is in i.Out, the normal node in i.Next. |
| 75 | func onePassNextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassNext().(i *onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#, r rune) uint32 { |
| 76 | next := i.MatchRunePosscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#MatchRunePos().(r) |
| 77 | if next >= 0 { |
| 78 | return i.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[next] |
| 79 | } |
| 80 | if i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch. { |
| 81 | return i.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 82 | } |
| 83 | return 0 |
| 84 | } |
| 85 | |
| 86 | func iopscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/iop().(i *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#) syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstOp# { |
| 87 | op := i.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. |
| 88 | switch op { |
| 89 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRune1scip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune1., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAny., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyNotNLscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAnyNotNL.: |
| 90 | op = syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune. |
| 91 | } |
| 92 | return op |
| 93 | } |
| 94 | |
| 95 | // Sparse Array implementation is used as a queueOnePass. |
| 96 | type queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass# struct { |
| 97 | sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse. []uint32 |
| 98 | densescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#dense. []uint32 |
| 99 | sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size., nextIndexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#nextIndex. uint32 |
| 100 | } |
| 101 | |
| 102 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) emptyscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#empty().() bool { |
| 103 | return q.nextIndexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#nextIndex. >= q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size. |
| 104 | } |
| 105 | |
| 106 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#next().() (n uint32) { |
| 107 | n = q.densescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#dense.[q.nextIndexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#nextIndex.] |
| 108 | q.nextIndexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#nextIndex.++ |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) clearscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#clear().() { |
| 113 | q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size. = 0 |
| 114 | q.nextIndexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#nextIndex. = 0 |
| 115 | } |
| 116 | |
| 117 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) containsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#contains().(u uint32) bool { |
| 118 | if u >= uint32(len(q.sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.)) { |
| 119 | return false |
| 120 | } |
| 121 | return q.sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.[u] < q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size. && q.densescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#dense.[q.sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.[u]] == u |
| 122 | } |
| 123 | |
| 124 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(u uint32) { |
| 125 | if !q.containsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#contains().(u) { |
| 126 | q.insertNewscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insertNew().(u) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) insertNewscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insertNew().(u uint32) { |
| 131 | if u >= uint32(len(q.sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.)) { |
| 132 | return |
| 133 | } |
| 134 | q.sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.[u] = q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size. |
| 135 | q.densescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#dense.[q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size.] = u |
| 136 | q.sizescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#size.++ |
| 137 | } |
| 138 | |
| 139 | func newQueuescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/newQueue().(size int) (q *queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#) { |
| 140 | return &queueOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#{ |
| 141 | sparsescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#sparse.: make([]uint32, size), |
| 142 | densescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#dense.: make([]uint32, size), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // mergeRuneSets merges two non-intersecting runesets, and returns the merged result, |
| 147 | // and a NextIp array. The idea is that if a rune matches the OnePassRunes at index |
| 148 | // i, NextIp[i/2] is the target. If the input sets intersect, an empty runeset and a |
| 149 | // NextIp array with the single element mergeFailed is returned. |
| 150 | // The code assumes that both inputs contain ordered and non-intersecting rune pairs. |
| 151 | const mergeFailedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/mergeFailed. = uint32(0xffffffff) |
| 152 | |
| 153 | var ( |
| 154 | noRunescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/noRune. = []rune{} |
| 155 | noNextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/noNext. = []uint32{mergeFailedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/mergeFailed.} |
| 156 | ) |
| 157 | |
| 158 | func mergeRuneSetsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/mergeRuneSets().(leftRunes, rightRunes *[]rune, leftPC, rightPC uint32) ([]rune, []uint32) { |
| 159 | leftLen := len(*leftRunes) |
| 160 | rightLen := len(*rightRunes) |
| 161 | if leftLen&0x1 != 0 || rightLen&0x1 != 0 { |
| 162 | panic("mergeRuneSets odd length []rune") |
| 163 | } |
| 164 | var ( |
| 165 | lx, rx int |
| 166 | ) |
| 167 | merged := make([]rune, 0) |
| 168 | next := make([]uint32, 0) |
| 169 | ok := true |
| 170 | defer func() { |
| 171 | if !ok { |
| 172 | merged = nil |
| 173 | next = nil |
| 174 | } |
| 175 | }() |
| 176 | |
| 177 | ix := -1 |
| 178 | extend := func(newLow *int, newArray *[]rune, pc uint32) bool { |
| 179 | if ix > 0 && (*newArray)[*newLow] <= merged[ix] { |
| 180 | return false |
| 181 | } |
| 182 | merged = append(merged, (*newArray)[*newLow], (*newArray)[*newLow+1]) |
| 183 | *newLow += 2 |
| 184 | ix += 2 |
| 185 | next = append(next, pc) |
| 186 | return true |
| 187 | } |
| 188 | |
| 189 | for lx < leftLen || rx < rightLen { |
| 190 | switch { |
| 191 | case rx >= rightLen: |
| 192 | ok = extend(&lx, leftRunes, leftPC) |
| 193 | case lx >= leftLen: |
| 194 | ok = extend(&rx, rightRunes, rightPC) |
| 195 | case (*rightRunes)[rx] < (*leftRunes)[lx]: |
| 196 | ok = extend(&rx, rightRunes, rightPC) |
| 197 | default: |
| 198 | ok = extend(&lx, leftRunes, leftPC) |
| 199 | } |
| 200 | if !ok { |
| 201 | return noRunescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/noRune., noNextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/noNext. |
| 202 | } |
| 203 | } |
| 204 | return merged, next |
| 205 | } |
| 206 | |
| 207 | // cleanupOnePass drops working memory, and restores certain shortcut instructions. |
| 208 | func cleanupOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/cleanupOnePass().(prog *onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#, original *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) { |
| 209 | for ix, instOriginal := range original.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst. { |
| 210 | switch instOriginal.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. { |
| 211 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune.: |
| 212 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstCapturescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstCapture., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstNopscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstNop., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstFailscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstFail.: |
| 213 | prog.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[ix].Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = nil |
| 214 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRune1scip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune1., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAny., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyNotNLscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAnyNotNL.: |
| 215 | prog.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[ix].Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = nil |
| 216 | prog.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[ix] = onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#{Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Inst.: instOriginal} |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // onePassCopy creates a copy of the original Prog, as we'll be modifying it. |
| 222 | func onePassCopyscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassCopy().(prog *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) *onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg# { |
| 223 | p := &onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#{ |
| 224 | Startscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Start.: prog.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start., |
| 225 | NumCapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#NumCap.: prog.NumCapscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#NumCap., |
| 226 | Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.: make([]onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#, len(prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.)), |
| 227 | } |
| 228 | for i, inst := range prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst. { |
| 229 | p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[i] = onePassInstscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#{Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Inst.: inst} |
| 230 | } |
| 231 | |
| 232 | // rewrites one or more common Prog constructs that enable some otherwise |
| 233 | // non-onepass Progs to be onepass. A:BD (for example) means an InstAlt at |
| 234 | // ip A, that points to ips B & C. |
| 235 | // A:BC + B:DA => A:BC + B:CD |
| 236 | // A:BC + B:DC => A:DC + B:DC |
| 237 | for pc := range p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst. { |
| 238 | switch p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[pc].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. { |
| 239 | default: |
| 240 | continue |
| 241 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.: |
| 242 | // A:Bx + B:Ay |
| 243 | p_A_Other := &p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[pc].Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 244 | p_A_Alt := &p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[pc].Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg. |
| 245 | // make sure a target is another Alt |
| 246 | instAlt := p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[*p_A_Alt] |
| 247 | if !(instAlt.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt. || instAlt.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.) { |
| 248 | p_A_Alt, p_A_Other = p_A_Other, p_A_Alt |
| 249 | instAlt = p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[*p_A_Alt] |
| 250 | if !(instAlt.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt. || instAlt.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.) { |
| 251 | continue |
| 252 | } |
| 253 | } |
| 254 | instOther := p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[*p_A_Other] |
| 255 | // Analyzing both legs pointing to Alts is for another day |
| 256 | if instOther.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt. || instOther.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch. { |
| 257 | // too complicated |
| 258 | continue |
| 259 | } |
| 260 | // simple empty transition loop |
| 261 | // A:BC + B:DA => A:BC + B:DC |
| 262 | p_B_Alt := &p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[*p_A_Alt].Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 263 | p_B_Other := &p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[*p_A_Alt].Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg. |
| 264 | patch := false |
| 265 | if instAlt.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. == uint32(pc) { |
| 266 | patch = true |
| 267 | } else if instAlt.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg. == uint32(pc) { |
| 268 | patch = true |
| 269 | p_B_Alt, p_B_Other = p_B_Other, p_B_Alt |
| 270 | } |
| 271 | if patch { |
| 272 | *p_B_Alt = *p_A_Other |
| 273 | } |
| 274 | |
| 275 | // empty transition to common target |
| 276 | // A:BC + B:DC => A:DC + B:DC |
| 277 | if *p_A_Other == *p_B_Alt { |
| 278 | *p_A_Alt = *p_B_Other |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | return p |
| 283 | } |
| 284 | |
| 285 | var anyRuneNotNLscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/anyRuneNotNL. = []rune{0, '\n' - 1, '\n' + 1, unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.MaxRunescip-go gomod github.com/golang/go/src go1.21 unicode/MaxRune.} |
| 286 | var anyRunescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/anyRune. = []rune{0, unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.MaxRunescip-go gomod github.com/golang/go/src go1.21 unicode/MaxRune.} |
| 287 | |
| 288 | // makeOnePass creates a onepass Prog, if possible. It is possible if at any alt, |
| 289 | // the match engine can always tell which branch to take. The routine may modify |
| 290 | // p if it is turned into a onepass Prog. If it isn't possible for this to be a |
| 291 | // onepass Prog, the Prog nil is returned. makeOnePass is recursive |
| 292 | // to the size of the Prog. |
| 293 | func makeOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/makeOnePass().(p *onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#) *onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg# { |
| 294 | // If the machine is very long, it's not worth the time to check if we can use one pass. |
| 295 | if len(p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.) >= 1000 { |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | var ( |
| 300 | instQueue = newQueuescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/newQueue().(len(p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.)) |
| 301 | visitQueue = newQueuescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/newQueue().(len(p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.)) |
| 302 | check func(uint32, []bool) bool |
| 303 | onePassRunes = make([][]rune, len(p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.)) |
| 304 | ) |
| 305 | |
| 306 | // check that paths from Alt instructions are unambiguous, and rebuild the new |
| 307 | // program as a onepass program |
| 308 | check = func(pc uint32, m []bool) (ok bool) { |
| 309 | ok = true |
| 310 | inst := &p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[pc] |
| 311 | if visitQueue.containsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#contains().(pc) { |
| 312 | return |
| 313 | } |
| 314 | visitQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(pc) |
| 315 | switch inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. { |
| 316 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.: |
| 317 | ok = check(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., m) && check(inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg., m) |
| 318 | // check no-input paths to InstMatch |
| 319 | matchOut := m[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.] |
| 320 | matchArg := m[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.] |
| 321 | if matchOut && matchArg { |
| 322 | ok = false |
| 323 | break |
| 324 | } |
| 325 | // Match on empty goes in inst.Out |
| 326 | if matchArg { |
| 327 | inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg. = inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg., inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 328 | matchOut, matchArg = matchArg, matchOut |
| 329 | } |
| 330 | if matchOut { |
| 331 | m[pc] = true |
| 332 | inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. = syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch. |
| 333 | } |
| 334 | |
| 335 | // build a dispatch operator from the two legs of the alt. |
| 336 | onePassRunes[pc], inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = mergeRuneSetsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/mergeRuneSets().( |
| 337 | &onePassRunes[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.], &onePassRunes[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.], inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.) |
| 338 | if len(inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.) > 0 && inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[0] == mergeFailedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/mergeFailed. { |
| 339 | ok = false |
| 340 | break |
| 341 | } |
| 342 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstCapturescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstCapture., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstNopscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstNop.: |
| 343 | ok = check(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., m) |
| 344 | m[pc] = m[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.] |
| 345 | // pass matching runes back through these no-ops. |
| 346 | onePassRunes[pc] = append([]rune{}, onePassRunes[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.]...) |
| 347 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = make([]uint32, len(onePassRunes[pc])/2+1) |
| 348 | for i := range inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. { |
| 349 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[i] = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 350 | } |
| 351 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth.: |
| 352 | ok = check(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., m) |
| 353 | m[pc] = m[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.] |
| 354 | onePassRunes[pc] = append([]rune{}, onePassRunes[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.]...) |
| 355 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = make([]uint32, len(onePassRunes[pc])/2+1) |
| 356 | for i := range inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. { |
| 357 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[i] = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 358 | } |
| 359 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstFailscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstFail.: |
| 360 | m[pc] = inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. |
| 361 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune.: |
| 362 | m[pc] = false |
| 363 | if len(inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.) > 0 { |
| 364 | break |
| 365 | } |
| 366 | instQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.) |
| 367 | if len(inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.) == 0 { |
| 368 | onePassRunes[pc] = []rune{} |
| 369 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = []uint32{inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.} |
| 370 | break |
| 371 | } |
| 372 | runes := make([]rune, 0) |
| 373 | if len(inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.) == 1 && syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Flagsscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Flags#(inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.FoldCasescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/FoldCase. != 0 { |
| 374 | r0 := inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0] |
| 375 | runes = append(runes, r0, r0) |
| 376 | for r1 := unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.SimpleFoldscip-go gomod github.com/golang/go/src go1.21 unicode/SimpleFold().(r0); r1 != r0; r1 = unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.SimpleFoldscip-go gomod github.com/golang/go/src go1.21 unicode/SimpleFold().(r1) { |
| 377 | runes = append(runes, r1, r1) |
| 378 | } |
| 379 | slicesscip-go gomod github.com/golang/go/src go1.21 slices/.Sortscip-go gomod github.com/golang/go/src go1.21 slices/Sort().(runes) |
| 380 | } else { |
| 381 | runes = append(runes, inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune....) |
| 382 | } |
| 383 | onePassRunes[pc] = runes |
| 384 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = make([]uint32, len(onePassRunes[pc])/2+1) |
| 385 | for i := range inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. { |
| 386 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[i] = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 387 | } |
| 388 | inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. = syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune. |
| 389 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRune1scip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune1.: |
| 390 | m[pc] = false |
| 391 | if len(inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.) > 0 { |
| 392 | break |
| 393 | } |
| 394 | instQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.) |
| 395 | runes := []rune{} |
| 396 | // expand case-folded runes |
| 397 | if syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Flagsscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Flags#(inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.FoldCasescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/FoldCase. != 0 { |
| 398 | r0 := inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0] |
| 399 | runes = append(runes, r0, r0) |
| 400 | for r1 := unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.SimpleFoldscip-go gomod github.com/golang/go/src go1.21 unicode/SimpleFold().(r0); r1 != r0; r1 = unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.SimpleFoldscip-go gomod github.com/golang/go/src go1.21 unicode/SimpleFold().(r1) { |
| 401 | runes = append(runes, r1, r1) |
| 402 | } |
| 403 | slicesscip-go gomod github.com/golang/go/src go1.21 slices/.Sortscip-go gomod github.com/golang/go/src go1.21 slices/Sort().(runes) |
| 404 | } else { |
| 405 | runes = append(runes, inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0], inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0]) |
| 406 | } |
| 407 | onePassRunes[pc] = runes |
| 408 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = make([]uint32, len(onePassRunes[pc])/2+1) |
| 409 | for i := range inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. { |
| 410 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[i] = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 411 | } |
| 412 | inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. = syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune. |
| 413 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAny.: |
| 414 | m[pc] = false |
| 415 | if len(inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.) > 0 { |
| 416 | break |
| 417 | } |
| 418 | instQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.) |
| 419 | onePassRunes[pc] = append([]rune{}, anyRunescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/anyRune....) |
| 420 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = []uint32{inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.} |
| 421 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyNotNLscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAnyNotNL.: |
| 422 | m[pc] = false |
| 423 | if len(inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.) > 0 { |
| 424 | break |
| 425 | } |
| 426 | instQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.) |
| 427 | onePassRunes[pc] = append([]rune{}, anyRuneNotNLscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/anyRuneNotNL....) |
| 428 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. = make([]uint32, len(onePassRunes[pc])/2+1) |
| 429 | for i := range inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next. { |
| 430 | inst.Nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassInst#Next.[i] = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out. |
| 431 | } |
| 432 | } |
| 433 | return |
| 434 | } |
| 435 | |
| 436 | instQueue.clearscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#clear().() |
| 437 | instQueue.insertscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#insert().(uint32(p.Startscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Start.)) |
| 438 | m := make([]bool, len(p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.)) |
| 439 | for !instQueue.emptyscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#empty().() { |
| 440 | visitQueue.clearscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#clear().() |
| 441 | pc := instQueue.nextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/queueOnePass#next().() |
| 442 | if !check(pc, m) { |
| 443 | p = nil |
| 444 | break |
| 445 | } |
| 446 | } |
| 447 | if p != nil { |
| 448 | for i := range p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst. { |
| 449 | p.Instscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#Inst.[i].Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune. = onePassRunes[i] |
| 450 | } |
| 451 | } |
| 452 | return p |
| 453 | } |
| 454 | |
| 455 | // compileOnePass returns a new *syntax.Prog suitable for onePass execution if the original Prog |
| 456 | // can be recharacterized as a one-pass regexp program, or syntax.nil if the |
| 457 | // Prog cannot be converted. For a one pass prog, the fundamental condition that must |
| 458 | // be true is: at any InstAlt, there must be no ambiguity about what branch to take. |
| 459 | func compileOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/compileOnePass().(prog *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) (p *onePassProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassProg#) { |
| 460 | if prog.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start. == 0 { |
| 461 | return nil |
| 462 | } |
| 463 | // onepass regexp is anchored |
| 464 | if prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[prog.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. != syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth. || |
| 465 | syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[prog.Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.].Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyBeginTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyBeginText. != syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyBeginTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyBeginText. { |
| 466 | return nil |
| 467 | } |
| 468 | // every instruction leading to InstMatch must be EmptyEndText |
| 469 | for _, inst := range prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst. { |
| 470 | opOut := prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. |
| 471 | switch inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. { |
| 472 | default: |
| 473 | if opOut == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. { |
| 474 | return nil |
| 475 | } |
| 476 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.: |
| 477 | if opOut == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. || prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. { |
| 478 | return nil |
| 479 | } |
| 480 | case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth.: |
| 481 | if opOut == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch. { |
| 482 | if syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyEndTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyEndText. == syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyEndTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyEndText. { |
| 483 | continue |
| 484 | } |
| 485 | return nil |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | // Creates a slightly optimized copy of the original Prog |
| 490 | // that cleans up some Prog idioms that block valid onepass programs |
| 491 | p = onePassCopyscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/onePassCopy().(prog) |
| 492 | |
| 493 | // checkAmbiguity on InstAlts, build onepass Prog if possible |
| 494 | p = makeOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/makeOnePass().(p) |
| 495 | |
| 496 | if p != nil { |
| 497 | cleanupOnePassscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/cleanupOnePass().(p, prog) |
| 498 | } |
| 499 | return p |
| 500 | } |