File: regexp.go

package info (click to toggle)
mongo-tools 3.4.14-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,612 kB
  • sloc: python: 9,281; asm: 4,229; ansic: 1,606; sh: 817; makefile: 67; cpp: 59; lisp: 19
file content (275 lines) | stat: -rw-r--r-- 7,016 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package json

import (
	"fmt"
	"reflect"
	"unicode"
	"unicode/utf16"
	"unicode/utf8"
)

// Transition functions for recognizing RegExp.
// Adapted from encoding/json/scanner.go.

// stateR is the state after reading `R`.
func stateR(s *scanner, c int) int {
	if c == 'e' {
		s.step = generateState("RegExp", []byte("gExp"), stateConstructor)
		return scanContinue
	}
	return s.error(c, "in literal RegExp (expecting 'e')")
}

// stateInRegexpPattern is the state after reading `/`.
func stateInRegexpPattern(s *scanner, c int) int {
	if c == '/' {
		s.step = stateInRegexpOptions
		return scanRegexpOptions
	}
	if c == '\\' {
		s.step = stateInRegexpPatternEsc
		return scanRegexpPattern
	}
	if c < 0x20 {
		return s.error(c, "in regular expression literal")
	}
	return scanRegexpPattern
}

// stateInRegexpPatternEsc is the state after reading `'\` during a regex pattern.
func stateInRegexpPatternEsc(s *scanner, c int) int {
	switch c {
	case 'b', 'f', 'n', 'r', 't', '\\', '/', '\'':
		s.step = stateInRegexpPattern
		return scanRegexpPattern
	}
	if c == 'u' {
		s.step = stateInRegexpPatternEscU
		return scanRegexpPattern
	}
	return s.error(c, "in string escape code")
}

// stateInRegexpPatternEscU is the state after reading `'\u` during a regex pattern.
func stateInRegexpPatternEscU(s *scanner, c int) int {
	if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
		s.step = stateInRegexpPatternEscU1
		return scanRegexpPattern
	}
	// numbers
	return s.error(c, "in \\u hexadecimal character escape")
}

// stateInRegexpPatternEscU1 is the state after reading `'\u1` during a regex pattern.
func stateInRegexpPatternEscU1(s *scanner, c int) int {
	if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
		s.step = stateInRegexpPatternEscU12
		return scanRegexpPattern
	}
	// numbers
	return s.error(c, "in \\u hexadecimal character escape")
}

// stateInRegexpPatternEscU12 is the state after reading `'\u12` during a regex pattern.
func stateInRegexpPatternEscU12(s *scanner, c int) int {
	if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
		s.step = stateInRegexpPatternEscU123
		return scanRegexpPattern
	}
	// numbers
	return s.error(c, "in \\u hexadecimal character escape")
}

// stateInRegexpPatternEscU123 is the state after reading `'\u123` during a regex pattern.
func stateInRegexpPatternEscU123(s *scanner, c int) int {
	if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
		s.step = stateInRegexpPattern
		return scanRegexpPattern
	}
	// numbers
	return s.error(c, "in \\u hexadecimal character escape")
}

// stateInRegexpOptions is the state after reading `/foo/`.
func stateInRegexpOptions(s *scanner, c int) int {
	switch c {
	case 'g', 'i', 'm', 's':
		return scanRegexpOptions
	}
	return stateEndValue(s, c)
}

// Decodes a RegExp literal stored in the underlying byte data into v.
func (d *decodeState) storeRegexp(v reflect.Value) {
	op := d.scanWhile(scanSkipSpace)
	if op != scanBeginCtor {
		d.error(fmt.Errorf("expected beginning of constructor"))
	}

	args, err := d.ctor("RegExp", []reflect.Type{stringType, stringType})
	if err != nil {
		d.error(err)
	}
	switch kind := v.Kind(); kind {
	case reflect.Interface:
		arg0 := args[0].String()
		arg1 := args[1].String()
		v.Set(reflect.ValueOf(RegExp{arg0, arg1}))
	default:
		d.error(fmt.Errorf("cannot store %v value into %v type", regexpType, kind))
	}
}

// Returns a RegExp literal from the underlying byte data.
func (d *decodeState) getRegexp() interface{} {
	op := d.scanWhile(scanSkipSpace)
	if op != scanBeginCtor {
		d.error(fmt.Errorf("expected beginning of constructor"))
	}

	args := d.ctorInterface()
	if err := ctorNumArgsMismatch("RegExp", 2, len(args)); err != nil {
		d.error(err)
	}
	arg0, ok := args[0].(string)
	if !ok {
		d.error(fmt.Errorf("expected string for first argument of RegExp constructor"))
	}
	arg1, ok := args[1].(string)
	if !ok {
		d.error(fmt.Errorf("expected string for second argument of RegExp constructor"))
	}
	return RegExp{arg0, arg1}
}

// Decoder function that breaks a regular expression literal into its pattern and options.
// Adapted from encoding/json/decode.go.

// regexp consumes a regular expression from d.data[d.off-1:].
// the two bytes of the regexp ("/a") have been read already.
func (d *decodeState) regexp() (string, string, error) {
	start := d.off - 1

	// Look ahead for /.
	op := d.scanWhile(scanRegexpPattern)
	if op != scanRegexpOptions {
		return "", "", fmt.Errorf("expected beginning of regular expression options")
	}
	pattern := d.data[start : d.off-1]

	start = d.off
	op = d.scanWhile(scanRegexpOptions)

	// Back up so caller can have the byte we just read.
	d.off--
	d.scan.undo(op)

	options := d.data[start:d.off]

	// Check for unusual characters. If there are none,
	// then no copying is needed, so return string of the
	// original bytes.
	r := 0
	for r < len(pattern) {
		c := pattern[r]
		if c == '\\' || c == '/' || c < ' ' {
			break
		}
		if c < utf8.RuneSelf {
			r++
			continue
		}
		rr, size := utf8.DecodeRune(pattern[r:])
		if rr == utf8.RuneError && size == 1 {
			break
		}
		r += size
	}
	if r == len(pattern) {
		return string(pattern), string(options), nil
	}

	b := make([]byte, len(pattern)+2*utf8.UTFMax)
	w := copy(b, pattern[0:r])
	for r < len(pattern) {
		// Out of room?  Can only happen if pattern is full of
		// malformed UTF-8 and we're replacing each
		// byte with RuneError.
		if w >= len(b)-2*utf8.UTFMax {
			nb := make([]byte, (len(b)+utf8.UTFMax)*2)
			copy(nb, b[0:w])
			b = nb
		}
		switch c := pattern[r]; {
		case c == '\\':
			r++
			if r >= len(pattern) {
				return "", "", errPhase
			}
			switch pattern[r] {
			default:
				return "", "", fmt.Errorf("invalid escape character")
			case '"', '\\', '/', '\'':
				b[w] = pattern[r]
				r++
				w++
			case 'b':
				b[w] = '\b'
				r++
				w++
			case 'f':
				b[w] = '\f'
				r++
				w++
			case 'n':
				b[w] = '\n'
				r++
				w++
			case 'r':
				b[w] = '\r'
				r++
				w++
			case 't':
				b[w] = '\t'
				r++
				w++
			case 'u':
				r--
				rr := getu4(pattern[r:])
				if rr < 0 {
					return "", "", fmt.Errorf("non-hexadecimal character found")
				}
				r += 6
				if utf16.IsSurrogate(rr) {
					rr1 := getu4(pattern[r:])
					if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
						// A valid pair; consume.
						r += 6
						w += utf8.EncodeRune(b[w:], dec)
						break
					}
					// Invalid surrogate; fall back to replacement rune.
					rr = unicode.ReplacementChar
				}
				w += utf8.EncodeRune(b[w:], rr)
			}

		// Forward slash, control characters are invalid.
		case c == '/', c < ' ':
			d.error(fmt.Errorf("regular expression pattern cannot contain unescaped '/'"))

		// ASCII
		case c < utf8.RuneSelf:
			b[w] = c
			r++
			w++

		// Coerce to well-formed UTF-8.
		default:
			rr, size := utf8.DecodeRune(pattern[r:])
			r += size
			w += utf8.EncodeRune(b[w:], rr)
		}
	}
	return string(b[0:w]), string(options), nil
}