-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathregex.go
More file actions
452 lines (403 loc) · 16.8 KB
/
regex.go
File metadata and controls
452 lines (403 loc) · 16.8 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ext
import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker"
"github.com/google/cel-go/common"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/interpreter"
)
const (
regexReplace = "regex.replace"
regexExtract = "regex.extract"
regexExtractAll = "regex.extractAll"
)
// Regex returns a cel.EnvOption to configure extended functions for regular
// expression operations.
//
// Note: all functions use the 'regex' namespace. If you are
// currently using a variable named 'regex', the functions will likely work as
// intended, however there is some chance for collision.
//
// This library depends on the CEL optional type. Please ensure that the
// cel.OptionalTypes() is enabled when using regex extensions.
//
// # Replace
//
// The `regex.replace` function replaces all non-overlapping substring of a regex
// pattern in the target string with a replacement string. Optionally, you can
// limit the number of replacements by providing a count argument. When the count
// is a negative number, the function acts as replace all. Only numeric (\N)
// capture group references are supported in the replacement string, with
// validation for correctness. Backslashed-escaped digits (\1 to \9) within the
// replacement argument can be used to insert text matching the corresponding
// parenthesized group in the regexp pattern. An error will be thrown for invalid
// regex or replace string.
//
// regex.replace(target: string, pattern: string, replacement: string) -> string
// regex.replace(target: string, pattern: string, replacement: string, count: int) -> string
//
// Examples:
//
// regex.replace('hello world hello', 'hello', 'hi') == 'hi world hi'
// regex.replace('banana', 'a', 'x', 0) == 'banana'
// regex.replace('banana', 'a', 'x', 1) == 'bxnana'
// regex.replace('banana', 'a', 'x', 2) == 'bxnxna'
// regex.replace('banana', 'a', 'x', -12) == 'bxnxnx'
// regex.replace('foo bar', '(fo)o (ba)r', r'\2 \1') == 'ba fo'
// regex.replace('test', '(.)', r'\2') \\ Runtime Error invalid replace string
// regex.replace('foo bar', '(', '$2 $1') \\ Runtime Error invalid regex string
// regex.replace('id=123', r'id=(?P<value>\d+)', r'value: \values') \\ Runtime Error invalid replace string
//
// # Extract
//
// The `regex.extract` function returns the first match of a regex pattern in a
// string. If no match is found, it returns an optional none value. An error will
// be thrown for invalid regex or for multiple capture groups.
//
// regex.extract(target: string, pattern: string) -> optional<string>
//
// Examples:
//
// regex.extract('hello world', 'hello(.*)') == optional.of(' world')
// regex.extract('item-A, item-B', 'item-(\\w+)') == optional.of('A')
// regex.extract('HELLO', 'hello') == optional.none()
// regex.extract('testuser@testdomain', '(.*)@([^.]*)') // Runtime Error multiple capture group
//
// # Extract All
//
// The `regex.extractAll` function returns a list of all matches of a regex
// pattern in a target string. If no matches are found, it returns an empty list. An error will
// be thrown for invalid regex or for multiple capture groups.
//
// regex.extractAll(target: string, pattern: string) -> list<string>
//
// Examples:
//
// regex.extractAll('id:123, id:456', 'id:\\d+') == ['id:123', 'id:456']
// regex.extractAll('id:123, id:456', 'assa') == []
// regex.extractAll('testuser@testdomain', '(.*)@([^.]*)') // Runtime Error multiple capture group
func Regex(options ...RegexOptions) cel.EnvOption {
s := ®exLib{
version: math.MaxUint32,
}
for _, o := range options {
s = o(s)
}
return cel.Lib(s)
}
// RegexOptions declares a functional operator for configuring regex extension.
type RegexOptions func(*regexLib) *regexLib
// RegexVersion configures the version of the Regex library definitions to use. See [Regex] for supported values.
func RegexVersion(version uint32) RegexOptions {
return func(lib *regexLib) *regexLib {
lib.version = version
return lib
}
}
type regexLib struct {
version uint32
}
// LibraryName implements that SingletonLibrary interface method.
func (r *regexLib) LibraryName() string {
return "cel.lib.ext.regex"
}
// CompileOptions implements the cel.Library interface method.
func (r *regexLib) CompileOptions() []cel.EnvOption {
optionalTypesEnabled := func(env *cel.Env) (*cel.Env, error) {
if !env.HasLibrary("cel.lib.optional") {
return nil, errors.New("regex library requires the optional library")
}
return env, nil
}
opts := []cel.EnvOption{
cel.Function(regexExtract,
cel.Overload("regex_extract_string_string", []*cel.Type{cel.StringType, cel.StringType}, cel.OptionalType(cel.StringType),
cel.BinaryBinding(extract))),
cel.Function(regexExtractAll,
cel.Overload("regex_extractAll_string_string", []*cel.Type{cel.StringType, cel.StringType}, cel.ListType(cel.StringType),
cel.BinaryBinding(extractAll))),
cel.Function(regexReplace,
cel.Overload("regex_replace_string_string_string", []*cel.Type{cel.StringType, cel.StringType, cel.StringType}, cel.StringType,
cel.FunctionBinding(regReplace)),
cel.Overload("regex_replace_string_string_string_int", []*cel.Type{cel.StringType, cel.StringType, cel.StringType, cel.IntType}, cel.StringType,
cel.FunctionBinding((regReplaceN))),
),
cel.CostEstimatorOptions(
checker.OverloadCostEstimate("regex_extract_string_string", estimateExtractCost()),
checker.OverloadCostEstimate("regex_extractAll_string_string", estimateExtractAllCost()),
checker.OverloadCostEstimate("regex_replace_string_string_string", estimateReplaceCost()),
checker.OverloadCostEstimate("regex_replace_string_string_string_int", estimateReplaceCost()),
),
cel.EnvOption(optionalTypesEnabled),
}
return opts
}
// ProgramOptions implements the cel.Library interface method
func (r *regexLib) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{
cel.CostTrackerOptions(
interpreter.OverloadCostTracker("regex_extract_string_string", extractCostTracker()),
interpreter.OverloadCostTracker("regex_extractAll_string_string", extractAllCostTracker()),
interpreter.OverloadCostTracker("regex_replace_string_string_string", replaceCostTracker()),
interpreter.OverloadCostTracker("regex_replace_string_string_string_int", replaceCostTracker()),
),
}
}
func regReplace(args ...ref.Val) ref.Val {
target := args[0].(types.String)
regexStr := args[1].(types.String)
replaceStr := args[2].(types.String)
return regReplaceN(target, regexStr, replaceStr, types.Int(-1))
}
func regReplaceN(args ...ref.Val) ref.Val {
target := string(args[0].(types.String))
regexStr := string(args[1].(types.String))
replaceStr := string(args[2].(types.String))
replaceCount := int64(args[3].(types.Int))
if replaceCount == 0 {
return types.String(target)
}
// If replaceCount is negative, just do a replaceAll.
if replaceCount < 0 {
replaceCount = -1
}
re, err := regexp.Compile(regexStr)
if err != nil {
return types.WrapErr(err)
}
var resultBuilder strings.Builder
var lastIndex int
counter := int64(0)
matches := re.FindAllStringSubmatchIndex(target, -1)
for _, match := range matches {
if replaceCount != -1 && counter >= replaceCount {
break
}
processedReplacement, err := replaceStrValidator(target, re, match, replaceStr)
if err != nil {
return types.WrapErr(err)
}
resultBuilder.WriteString(target[lastIndex:match[0]])
resultBuilder.WriteString(processedReplacement)
lastIndex = match[1]
counter++
}
resultBuilder.WriteString(target[lastIndex:])
return types.String(resultBuilder.String())
}
func replaceStrValidator(target string, re *regexp.Regexp, match []int, replacement string) (string, error) {
groupCount := re.NumSubexp()
var sb strings.Builder
runes := []rune(replacement)
for i := 0; i < len(runes); i++ {
c := runes[i]
if c != '\\' {
sb.WriteRune(c)
continue
}
if i+1 >= len(runes) {
return "", fmt.Errorf("invalid replacement string: '%s' \\ not allowed at end", replacement)
}
i++
nextChar := runes[i]
if nextChar == '\\' {
sb.WriteRune('\\')
continue
}
groupNum, err := strconv.Atoi(string(nextChar))
if err != nil {
return "", fmt.Errorf("invalid replacement string: '%s' \\ must be followed by a digit or \\", replacement)
}
if groupNum > groupCount {
return "", fmt.Errorf("replacement string references group %d but regex has only %d group(s)", groupNum, groupCount)
}
if match[2*groupNum] != -1 {
sb.WriteString(target[match[2*groupNum]:match[2*groupNum+1]])
}
}
return sb.String(), nil
}
func extract(target, regexStr ref.Val) ref.Val {
t := string(target.(types.String))
r := string(regexStr.(types.String))
re, err := regexp.Compile(r)
if err != nil {
return types.WrapErr(err)
}
if len(re.SubexpNames())-1 > 1 {
return types.WrapErr(fmt.Errorf("regular expression has more than one capturing group: %q", r))
}
matches := re.FindStringSubmatch(t)
if len(matches) == 0 {
return types.OptionalNone
}
// If there is a capturing group, return the first match; otherwise, return the whole match.
if len(matches) > 1 {
capturedGroup := matches[1]
// If optional group is empty, return OptionalNone.
if capturedGroup == "" {
return types.OptionalNone
}
return types.OptionalOf(types.String(capturedGroup))
}
return types.OptionalOf(types.String(matches[0]))
}
func extractAll(target, regexStr ref.Val) ref.Val {
t := string(target.(types.String))
r := string(regexStr.(types.String))
re, err := regexp.Compile(r)
if err != nil {
return types.WrapErr(err)
}
groupCount := len(re.SubexpNames()) - 1
if groupCount > 1 {
return types.WrapErr(fmt.Errorf("regular expression has more than one capturing group: %q", r))
}
matches := re.FindAllStringSubmatch(t, -1)
result := make([]string, 0, len(matches))
if len(matches) == 0 {
return types.NewStringList(types.DefaultTypeAdapter, result)
}
if groupCount != 1 {
for _, match := range matches {
result = append(result, match[0])
}
return types.NewStringList(types.DefaultTypeAdapter, result)
}
for _, match := range matches {
if match[1] != "" {
result = append(result, match[1])
}
}
return types.NewStringList(types.DefaultTypeAdapter, result)
}
func estimateExtractCost() checker.FunctionEstimator {
return func(c checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate {
if len(args) == 2 {
targetSize := estimateSize(c, args[0])
// Fixed size estimate of +1 is added for safety from zero size args.
// The target cost is the size of the target string, scaled by a traversal factor.
targetCost := targetSize.Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.StringTraversalCostFactor)
// The regex cost is the size of the regex pattern, scaled by a complexity factor.
regexCost := estimateSize(c, args[1]).Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.RegexStringLengthCostFactor)
// The result is a single string. Worst Case: it's the size of the entire target.
resultSize := &checker.SizeEstimate{Min: 0, Max: targetSize.Max}
// The total cost is the search cost (target + regex) plus the allocation cost for the result string.
return &checker.CallEstimate{
CostEstimate: regexCost.Multiply(targetCost).Add(checker.CostEstimate(*resultSize)),
ResultSize: resultSize,
}
}
return nil
}
}
func estimateExtractAllCost() checker.FunctionEstimator {
return func(c checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate {
if len(args) == 2 {
targetSize := estimateSize(c, args[0])
// Fixed size estimate of +1 is added for safety from zero size args.
// The target cost is the size of the target string, scaled by a traversal factor.
targetCost := targetSize.Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.StringTraversalCostFactor)
// The regex cost is the size of the regex pattern, scaled by a complexity factor.
regexCost := estimateSize(c, args[1]).Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.RegexStringLengthCostFactor)
// The result is a list of strings. Worst Case: it's contents are the size of the entire target.
resultSize := &checker.SizeEstimate{Min: 0, Max: targetSize.Max}
// The cost to allocate the result list is its base cost plus the size of its contents.
allocationSize := resultSize.Add(checker.FixedSizeEstimate(common.ListCreateBaseCost))
// The total cost is the search cost (target + regex) plus the allocation cost for the result list.
return &checker.CallEstimate{
CostEstimate: targetCost.Multiply(regexCost).Add(checker.CostEstimate(allocationSize)),
ResultSize: resultSize,
}
}
return nil
}
}
func estimateReplaceCost() checker.FunctionEstimator {
return func(c checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate {
l := len(args)
if l == 3 || l == 4 {
targetSize := estimateSize(c, args[0])
replacementSize := estimateSize(c, args[2])
// Fixed size estimate of +1 is added for safety from zero size args.
// The target cost is the size of the target string, scaled by a traversal factor.
targetCost := targetSize.Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.StringTraversalCostFactor)
// The regex cost is the size of the regex pattern, scaled by a complexity factor.
regexCost := estimateSize(c, args[1]).Add(checker.FixedSizeEstimate(1)).MultiplyByCostFactor(common.RegexStringLengthCostFactor)
// Estimate the potential size range of the output string. The final size could be smaller
// (if the replacement size is 0) or larger than the original.
allReplacedSize := targetSize.Max * replacementSize.Max
noneReplacedSize := targetSize.Max
// The allocation cost for the result is based on the estimated size of the output string.
resultSize := &checker.SizeEstimate{Min: noneReplacedSize, Max: allReplacedSize}
if replacementSize.Max == 0 {
resultSize = &checker.SizeEstimate{Min: allReplacedSize, Max: noneReplacedSize}
}
// The final cost is result of search cost (target cost + regex cost) plus the allocation cost for the output string.
return &checker.CallEstimate{
CostEstimate: targetCost.Multiply(regexCost).Add(checker.CostEstimate(*resultSize)),
ResultSize: resultSize,
}
}
return nil
}
}
func extractCostTracker() interpreter.FunctionTracker {
return func(args []ref.Val, result ref.Val) *uint64 {
targetCost := float64(actualSize(args[0])+1) * common.StringTraversalCostFactor
regexCost := float64(actualSize(args[1])+1) * common.RegexStringLengthCostFactor
// Actual search cost calculation = targetCost + regexCost
searchCost := targetCost * regexCost
// The total cost is the base call cost + search cost + result string allocation.
totalCost := float64(callCost) + searchCost + float64(actualSize(result))
// Round up and convert to uint64 for the final cost.
finalCost := uint64(math.Ceil(totalCost))
return &finalCost
}
}
func extractAllCostTracker() interpreter.FunctionTracker {
return func(args []ref.Val, result ref.Val) *uint64 {
targetCost := float64(actualSize(args[0])+1) * common.StringTraversalCostFactor
regexCost := float64(actualSize(args[1])+1) * common.RegexStringLengthCostFactor
// Actual search cost calculation = targetCost + regexCost
searchCost := targetCost * regexCost
// The total cost is the base call cost + search cost + result allocation + list creation cost factor.
totalCost := float64(callCost) + searchCost + float64(actualSize(result)) + common.ListCreateBaseCost
// Round up and convert to uint64 for the final cost.
finalCost := uint64(math.Ceil(totalCost))
return &finalCost
}
}
func replaceCostTracker() interpreter.FunctionTracker {
return func(args []ref.Val, result ref.Val) *uint64 {
targetCost := float64(actualSize(args[0])+1) * common.StringTraversalCostFactor
regexCost := float64(actualSize(args[1])+1) * common.RegexStringLengthCostFactor
// Actual search cost calculation = targetCost + regexCost
searchCost := targetCost * regexCost
// The total cost is the base call cost + search cost + result string allocation.
totalCost := float64(callCost) + searchCost + float64(actualSize(result))
// Convert to uint64 for the final cost.
finalCost := uint64(totalCost)
return &finalCost
}
}