| 1 | // Copyright 2025 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package extscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/ |
| 16 | |
| 17 | import ( |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 20 | "mathscip-go gomod github.com/golang/go/src go1.23.0 math/" |
| 21 | "regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/" |
| 22 | "strconvscip-go gomod github.com/golang/go/src go1.23.0 strconv/" |
| 23 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 24 | |
| 25 | "github.com/google/cel-go/celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/" |
| 26 | "github.com/google/cel-go/checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/" |
| 27 | "github.com/google/cel-go/commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/" |
| 28 | "github.com/google/cel-go/common/typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/" |
| 29 | "github.com/google/cel-go/common/types/refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/" |
| 30 | "github.com/google/cel-go/interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/" |
| 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | regexReplacescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexReplace. = "regex.replace" |
| 35 | regexExtractscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexExtract. = "regex.extract" |
| 36 | regexExtractAllscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexExtractAll. = "regex.extractAll" |
| 37 | ) |
| 38 | |
| 39 | // Regex returns a cel.EnvOption to configure extended functions for regular |
| 40 | // expression operations. |
| 41 | // |
| 42 | // Note: all functions use the 'regex' namespace. If you are |
| 43 | // currently using a variable named 'regex', the functions will likely work as |
| 44 | // intended, however there is some chance for collision. |
| 45 | // |
| 46 | // This library depends on the CEL optional type. Please ensure that the |
| 47 | // cel.OptionalTypes() is enabled when using regex extensions. |
| 48 | // |
| 49 | // # Replace |
| 50 | // |
| 51 | // The `regex.replace` function replaces all non-overlapping substring of a regex |
| 52 | // pattern in the target string with a replacement string. Optionally, you can |
| 53 | // limit the number of replacements by providing a count argument. When the count |
| 54 | // is a negative number, the function acts as replace all. Only numeric (\N) |
| 55 | // capture group references are supported in the replacement string, with |
| 56 | // validation for correctness. Backslashed-escaped digits (\1 to \9) within the |
| 57 | // replacement argument can be used to insert text matching the corresponding |
| 58 | // parenthesized group in the regexp pattern. An error will be thrown for invalid |
| 59 | // regex or replace string. |
| 60 | // |
| 61 | // regex.replace(target: string, pattern: string, replacement: string) -> string |
| 62 | // regex.replace(target: string, pattern: string, replacement: string, count: int) -> string |
| 63 | // |
| 64 | // Examples: |
| 65 | // |
| 66 | // regex.replace('hello world hello', 'hello', 'hi') == 'hi world hi' |
| 67 | // regex.replace('banana', 'a', 'x', 0) == 'banana' |
| 68 | // regex.replace('banana', 'a', 'x', 1) == 'bxnana' |
| 69 | // regex.replace('banana', 'a', 'x', 2) == 'bxnxna' |
| 70 | // regex.replace('banana', 'a', 'x', -12) == 'bxnxnx' |
| 71 | // regex.replace('foo bar', '(fo)o (ba)r', r'\2 \1') == 'ba fo' |
| 72 | // regex.replace('test', '(.)', r'\2') \\ Runtime Error invalid replace string |
| 73 | // regex.replace('foo bar', '(', '$2 $1') \\ Runtime Error invalid regex string |
| 74 | // regex.replace('id=123', r'id=(?P<value>\d+)', r'value: \values') \\ Runtime Error invalid replace string |
| 75 | // |
| 76 | // # Extract |
| 77 | // |
| 78 | // The `regex.extract` function returns the first match of a regex pattern in a |
| 79 | // string. If no match is found, it returns an optional none value. An error will |
| 80 | // be thrown for invalid regex or for multiple capture groups. |
| 81 | // |
| 82 | // regex.extract(target: string, pattern: string) -> optional<string> |
| 83 | // |
| 84 | // Examples: |
| 85 | // |
| 86 | // regex.extract('hello world', 'hello(.*)') == optional.of(' world') |
| 87 | // regex.extract('item-A, item-B', 'item-(\\w+)') == optional.of('A') |
| 88 | // regex.extract('HELLO', 'hello') == optional.none() |
| 89 | // regex.extract('testuser@testdomain', '(.*)@([^.]*)') // Runtime Error multiple capture group |
| 90 | // |
| 91 | // # Extract All |
| 92 | // |
| 93 | // The `regex.extractAll` function returns a list of all matches of a regex |
| 94 | // pattern in a target string. If no matches are found, it returns an empty list. An error will |
| 95 | // be thrown for invalid regex or for multiple capture groups. |
| 96 | // |
| 97 | // regex.extractAll(target: string, pattern: string) -> list<string> |
| 98 | // |
| 99 | // Examples: |
| 100 | // |
| 101 | // regex.extractAll('id:123, id:456', 'id:\\d+') == ['id:123', 'id:456'] |
| 102 | // regex.extractAll('id:123, id:456', 'assa') == [] |
| 103 | // regex.extractAll('testuser@testdomain', '(.*)@([^.]*)') // Runtime Error multiple capture group |
| 104 | func Regexscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/Regex().(options ...RegexOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/RegexOptions#) celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.EnvOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/EnvOption# { |
| 105 | s := ®exLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#{ |
| 106 | versionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#version.: mathscip-go gomod github.com/golang/go/src go1.23.0 math/.MaxUint32scip-go gomod github.com/golang/go/src go1.23.0 math/MaxUint32., |
| 107 | } |
| 108 | for _, o := range options { |
| 109 | s = o(s) |
| 110 | } |
| 111 | return celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Libscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Lib().(s) |
| 112 | } |
| 113 | |
| 114 | // RegexOptions declares a functional operator for configuring regex extension. |
| 115 | type RegexOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/RegexOptions# func(*regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#) *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib# |
| 116 | |
| 117 | // RegexVersion configures the version of the Regex library definitions to use. See [Regex] for supported values. |
| 118 | func RegexVersionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/RegexVersion().(version uint32) RegexOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/RegexOptions# { |
| 119 | return func(lib *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#) *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib# { |
| 120 | lib.versionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#version. = version |
| 121 | return lib |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | type regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib# struct { |
| 126 | versionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#version. uint32 |
| 127 | } |
| 128 | |
| 129 | // LibraryName implements that SingletonLibrary interface method. |
| 130 | func (r *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#) LibraryNamescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#LibraryName().() string { |
| 131 | return "cel.lib.ext.regex" |
| 132 | } |
| 133 | |
| 134 | // CompileOptions implements the cel.Library interface method. |
| 135 | func (r *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#) CompileOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#CompileOptions().() []celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.EnvOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/EnvOption# { |
| 136 | optionalTypesEnabled := func(env *celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#) (*celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#, error) { |
| 137 | if !env.HasLibraryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#HasLibrary().("cel.lib.optional") { |
| 138 | return nil, errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("regex library requires the optional library") |
| 139 | } |
| 140 | return env, nil |
| 141 | } |
| 142 | opts := []celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.EnvOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/EnvOption#{ |
| 143 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Functionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Function().(regexExtractscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexExtract., |
| 144 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Overload().("regex_extract_string_string", []*celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Type#{celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType.}, celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.OptionalTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/OptionalType.(celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType.), |
| 145 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.BinaryBindingscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/BinaryBinding().(extractscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extract().))), |
| 146 | |
| 147 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Functionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Function().(regexExtractAllscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexExtractAll., |
| 148 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Overload().("regex_extractAll_string_string", []*celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Type#{celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType.}, celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.ListTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/ListType.(celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType.), |
| 149 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.BinaryBindingscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/BinaryBinding().(extractAllscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractAll().))), |
| 150 | |
| 151 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Functionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Function().(regexReplacescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexReplace., |
| 152 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Overload().("regex_replace_string_string_string", []*celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Type#{celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType.}, celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., |
| 153 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.FunctionBindingscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/FunctionBinding().(regReplacescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regReplace().)), |
| 154 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Overload().("regex_replace_string_string_string_int", []*celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Type#{celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.IntTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/IntType.}, celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.StringTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/StringType., |
| 155 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.FunctionBindingscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/FunctionBinding().((regReplaceNscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regReplaceN().))), |
| 156 | ), |
| 157 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.CostEstimatorOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/CostEstimatorOptions().( |
| 158 | checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.OverloadCostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/OverloadCostEstimate().("regex_extract_string_string", estimateExtractCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateExtractCost().()), |
| 159 | checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.OverloadCostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/OverloadCostEstimate().("regex_extractAll_string_string", estimateExtractAllCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateExtractAllCost().()), |
| 160 | checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.OverloadCostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/OverloadCostEstimate().("regex_replace_string_string_string", estimateReplaceCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateReplaceCost().()), |
| 161 | checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.OverloadCostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/OverloadCostEstimate().("regex_replace_string_string_string_int", estimateReplaceCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateReplaceCost().()), |
| 162 | ), |
| 163 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.EnvOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/EnvOption#(optionalTypesEnabled), |
| 164 | } |
| 165 | return opts |
| 166 | } |
| 167 | |
| 168 | // ProgramOptions implements the cel.Library interface method |
| 169 | func (r *regexLibscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#) ProgramOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regexLib#ProgramOptions().() []celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.ProgramOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/ProgramOption# { |
| 170 | return []celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.ProgramOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/ProgramOption#{ |
| 171 | celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/.CostTrackerOptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/CostTrackerOptions().( |
| 172 | interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.OverloadCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/OverloadCostTracker().("regex_extract_string_string", extractCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractCostTracker().()), |
| 173 | interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.OverloadCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/OverloadCostTracker().("regex_extractAll_string_string", extractAllCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractAllCostTracker().()), |
| 174 | interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.OverloadCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/OverloadCostTracker().("regex_replace_string_string_string", replaceCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/replaceCostTracker().()), |
| 175 | interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.OverloadCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/OverloadCostTracker().("regex_replace_string_string_string_int", replaceCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/replaceCostTracker().()), |
| 176 | ), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func regReplacescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regReplace().(args ...refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 181 | target := args[0].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#) |
| 182 | regexStr := args[1].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#) |
| 183 | replaceStr := args[2].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#) |
| 184 | |
| 185 | return regReplaceNscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regReplaceN().(target, regexStr, replaceStr, typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Intscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Int#(-1)) |
| 186 | } |
| 187 | |
| 188 | func regReplaceNscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/regReplaceN().(args ...refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 189 | target := string(args[0].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 190 | regexStr := string(args[1].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 191 | replaceStr := string(args[2].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 192 | replaceCount := int64(args[3].(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Intscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Int#)) |
| 193 | |
| 194 | if replaceCount == 0 { |
| 195 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#(target) |
| 196 | } |
| 197 | |
| 198 | // If replaceCount is negative, just do a replaceAll. |
| 199 | if replaceCount < 0 { |
| 200 | replaceCount = -1 |
| 201 | } |
| 202 | |
| 203 | re, err := regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.23.0 regexp/Compile().(regexStr) |
| 204 | if err != nil { |
| 205 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(err) |
| 206 | } |
| 207 | |
| 208 | var resultBuilder stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder# |
| 209 | var lastIndex int |
| 210 | counter := int64(0) |
| 211 | |
| 212 | matches := re.FindAllStringSubmatchIndexscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#FindAllStringSubmatchIndex().(target, -1) |
| 213 | |
| 214 | for _, match := range matches { |
| 215 | if replaceCount != -1 && counter >= replaceCount { |
| 216 | break |
| 217 | } |
| 218 | |
| 219 | processedReplacement, err := replaceStrValidatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/replaceStrValidator().(target, re, match, replaceStr) |
| 220 | if err != nil { |
| 221 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(err) |
| 222 | } |
| 223 | |
| 224 | resultBuilder.WriteStringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteString().(target[lastIndex:match[0]]) |
| 225 | resultBuilder.WriteStringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteString().(processedReplacement) |
| 226 | lastIndex = match[1] |
| 227 | counter++ |
| 228 | } |
| 229 | |
| 230 | resultBuilder.WriteStringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteString().(target[lastIndex:]) |
| 231 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#(resultBuilder.Stringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#String().()) |
| 232 | } |
| 233 | |
| 234 | func replaceStrValidatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/replaceStrValidator().(target string, re *regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#, match []int, replacement string) (string, error) { |
| 235 | groupCount := re.NumSubexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#NumSubexp().() |
| 236 | var sb stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder# |
| 237 | runes := []rune(replacement) |
| 238 | |
| 239 | for i := 0; i < len(runes); i++ { |
| 240 | c := runes[i] |
| 241 | |
| 242 | if c != '\\' { |
| 243 | sb.WriteRunescip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteRune().(c) |
| 244 | continue |
| 245 | } |
| 246 | |
| 247 | if i+1 >= len(runes) { |
| 248 | return "", fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("invalid replacement string: '%s' \\ not allowed at end", replacement) |
| 249 | } |
| 250 | |
| 251 | i++ |
| 252 | nextChar := runes[i] |
| 253 | |
| 254 | if nextChar == '\\' { |
| 255 | sb.WriteRunescip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteRune().('\\') |
| 256 | continue |
| 257 | } |
| 258 | |
| 259 | groupNum, err := strconvscip-go gomod github.com/golang/go/src go1.23.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.23.0 strconv/Atoi().(string(nextChar)) |
| 260 | if err != nil { |
| 261 | return "", fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("invalid replacement string: '%s' \\ must be followed by a digit or \\", replacement) |
| 262 | } |
| 263 | |
| 264 | if groupNum > groupCount { |
| 265 | return "", fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("replacement string references group %d but regex has only %d group(s)", groupNum, groupCount) |
| 266 | } |
| 267 | |
| 268 | if match[2*groupNum] != -1 { |
| 269 | sb.WriteStringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#WriteString().(target[match[2*groupNum]:match[2*groupNum+1]]) |
| 270 | } |
| 271 | } |
| 272 | return sb.Stringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#String().(), nil |
| 273 | } |
| 274 | |
| 275 | func extractscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extract().(target, regexStr refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 276 | t := string(target.(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 277 | r := string(regexStr.(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 278 | re, err := regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.23.0 regexp/Compile().(r) |
| 279 | if err != nil { |
| 280 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(err) |
| 281 | } |
| 282 | |
| 283 | if len(re.SubexpNamesscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#SubexpNames().())-1 > 1 { |
| 284 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("regular expression has more than one capturing group: %q", r)) |
| 285 | } |
| 286 | |
| 287 | matches := re.FindStringSubmatchscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#FindStringSubmatch().(t) |
| 288 | if len(matches) == 0 { |
| 289 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.OptionalNonescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/OptionalNone. |
| 290 | } |
| 291 | |
| 292 | // If there is a capturing group, return the first match; otherwise, return the whole match. |
| 293 | if len(matches) > 1 { |
| 294 | capturedGroup := matches[1] |
| 295 | // If optional group is empty, return OptionalNone. |
| 296 | if capturedGroup == "" { |
| 297 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.OptionalNonescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/OptionalNone. |
| 298 | } |
| 299 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.OptionalOfscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/OptionalOf().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#(capturedGroup)) |
| 300 | } |
| 301 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.OptionalOfscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/OptionalOf().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#(matches[0])) |
| 302 | } |
| 303 | |
| 304 | func extractAllscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractAll().(target, regexStr refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 305 | t := string(target.(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 306 | r := string(regexStr.(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/String#)) |
| 307 | re, err := regexpscip-go gomod github.com/golang/go/src go1.23.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.23.0 regexp/Compile().(r) |
| 308 | if err != nil { |
| 309 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(err) |
| 310 | } |
| 311 | |
| 312 | groupCount := len(re.SubexpNamesscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#SubexpNames().()) - 1 |
| 313 | if groupCount > 1 { |
| 314 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("regular expression has more than one capturing group: %q", r)) |
| 315 | } |
| 316 | |
| 317 | matches := re.FindAllStringSubmatchscip-go gomod github.com/golang/go/src go1.23.0 regexp/Regexp#FindAllStringSubmatch().(t, -1) |
| 318 | result := make([]string, 0, len(matches)) |
| 319 | if len(matches) == 0 { |
| 320 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.NewStringListscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewStringList().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.DefaultTypeAdapterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/DefaultTypeAdapter., result) |
| 321 | } |
| 322 | |
| 323 | if groupCount != 1 { |
| 324 | for _, match := range matches { |
| 325 | result = append(result, match[0]) |
| 326 | } |
| 327 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.NewStringListscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewStringList().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.DefaultTypeAdapterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/DefaultTypeAdapter., result) |
| 328 | } |
| 329 | |
| 330 | for _, match := range matches { |
| 331 | if match[1] != "" { |
| 332 | result = append(result, match[1]) |
| 333 | } |
| 334 | } |
| 335 | return typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.NewStringListscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewStringList().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.DefaultTypeAdapterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/DefaultTypeAdapter., result) |
| 336 | } |
| 337 | |
| 338 | func estimateExtractCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateExtractCost().() checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FunctionEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FunctionEstimator# { |
| 339 | return func(c checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimator#, target *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#, args []checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#) *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate# { |
| 340 | if len(args) == 2 { |
| 341 | targetSize := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[0]) |
| 342 | // Fixed size estimate of +1 is added for safety from zero size args. |
| 343 | // The target cost is the size of the target string, scaled by a traversal factor. |
| 344 | targetCost := targetSize.Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.) |
| 345 | // The regex cost is the size of the regex pattern, scaled by a complexity factor. |
| 346 | regexCost := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[1]).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor.) |
| 347 | // The result is a single string. Worst Case: it's the size of the entire target. |
| 348 | resultSize := &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.SizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#{Minscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Min.: 0, Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.: targetSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.} |
| 349 | // The total cost is the search cost (target + regex) plus the allocation cost for the result string. |
| 350 | return &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#{ |
| 351 | CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#CostEstimate.: regexCost.Multiplyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Multiply().(targetCost).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#(*resultSize)), |
| 352 | ResultSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#ResultSize.: resultSize, |
| 353 | } |
| 354 | } |
| 355 | return nil |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func estimateExtractAllCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateExtractAllCost().() checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FunctionEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FunctionEstimator# { |
| 360 | return func(c checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimator#, target *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#, args []checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#) *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate# { |
| 361 | if len(args) == 2 { |
| 362 | targetSize := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[0]) |
| 363 | // Fixed size estimate of +1 is added for safety from zero size args. |
| 364 | // The target cost is the size of the target string, scaled by a traversal factor. |
| 365 | targetCost := targetSize.Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.) |
| 366 | // The regex cost is the size of the regex pattern, scaled by a complexity factor. |
| 367 | regexCost := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[1]).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor.) |
| 368 | // The result is a list of strings. Worst Case: it's contents are the size of the entire target. |
| 369 | resultSize := &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.SizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#{Minscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Min.: 0, Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.: targetSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.} |
| 370 | // The cost to allocate the result list is its base cost plus the size of its contents. |
| 371 | allocationSize := resultSize.Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.ListCreateBaseCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ListCreateBaseCost.)) |
| 372 | // The total cost is the search cost (target + regex) plus the allocation cost for the result list. |
| 373 | return &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#{ |
| 374 | CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#CostEstimate.: targetCost.Multiplyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Multiply().(regexCost).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#(allocationSize)), |
| 375 | ResultSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#ResultSize.: resultSize, |
| 376 | } |
| 377 | } |
| 378 | return nil |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | func estimateReplaceCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateReplaceCost().() checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FunctionEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FunctionEstimator# { |
| 383 | return func(c checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimator#, target *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#, args []checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.AstNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/AstNode#) *checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate# { |
| 384 | l := len(args) |
| 385 | if l == 3 || l == 4 { |
| 386 | targetSize := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[0]) |
| 387 | replacementSize := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[2]) |
| 388 | // Fixed size estimate of +1 is added for safety from zero size args. |
| 389 | // The target cost is the size of the target string, scaled by a traversal factor. |
| 390 | targetCost := targetSize.Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.) |
| 391 | // The regex cost is the size of the regex pattern, scaled by a complexity factor. |
| 392 | regexCost := estimateSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/estimateSize().(c, args[1]).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.FixedSizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/FixedSizeEstimate().(1)).MultiplyByCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#MultiplyByCostFactor().(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor.) |
| 393 | // Estimate the potential size range of the output string. The final size could be smaller |
| 394 | // (if the replacement size is 0) or larger than the original. |
| 395 | allReplacedSize := targetSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max. * replacementSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max. |
| 396 | noneReplacedSize := targetSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max. |
| 397 | // The allocation cost for the result is based on the estimated size of the output string. |
| 398 | resultSize := &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.SizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#{Minscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Min.: noneReplacedSize, Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.: allReplacedSize} |
| 399 | if replacementSize.Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max. == 0 { |
| 400 | resultSize = &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.SizeEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#{Minscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Min.: allReplacedSize, Maxscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/SizeEstimate#Max.: noneReplacedSize} |
| 401 | } |
| 402 | // The final cost is result of search cost (target cost + regex cost) plus the allocation cost for the output string. |
| 403 | return &checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CallEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#{ |
| 404 | CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#CostEstimate.: targetCost.Multiplyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Multiply().(regexCost).Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#Add().(checkerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/.CostEstimatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CostEstimate#(*resultSize)), |
| 405 | ResultSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/checker`/CallEstimate#ResultSize.: resultSize, |
| 406 | } |
| 407 | } |
| 408 | return nil |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func extractCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractCostTracker().() interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker# { |
| 413 | return func(args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) *uint64 { |
| 414 | targetCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[0])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor. |
| 415 | regexCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[1])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor. |
| 416 | // Actual search cost calculation = targetCost + regexCost |
| 417 | searchCost := targetCost * regexCost |
| 418 | // The total cost is the base call cost + search cost + result string allocation. |
| 419 | totalCost := float64(callCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/callCost.) + searchCost + float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(result)) |
| 420 | // Round up and convert to uint64 for the final cost. |
| 421 | finalCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(totalCost)) |
| 422 | return &finalCost |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | func extractAllCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/extractAllCostTracker().() interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker# { |
| 427 | return func(args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) *uint64 { |
| 428 | targetCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[0])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor. |
| 429 | regexCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[1])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor. |
| 430 | // Actual search cost calculation = targetCost + regexCost |
| 431 | searchCost := targetCost * regexCost |
| 432 | // The total cost is the base call cost + search cost + result allocation + list creation cost factor. |
| 433 | totalCost := float64(callCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/callCost.) + searchCost + float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(result)) + commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.ListCreateBaseCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ListCreateBaseCost. |
| 434 | // Round up and convert to uint64 for the final cost. |
| 435 | finalCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(totalCost)) |
| 436 | return &finalCost |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | func replaceCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/replaceCostTracker().() interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/.FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker# { |
| 441 | return func(args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) *uint64 { |
| 442 | targetCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[0])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor. |
| 443 | regexCost := float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(args[1])+1) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor. |
| 444 | // Actual search cost calculation = targetCost + regexCost |
| 445 | searchCost := targetCost * regexCost |
| 446 | // The total cost is the base call cost + search cost + result string allocation. |
| 447 | totalCost := float64(callCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/callCost.) + searchCost + float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/ext`/actualSize().(result)) |
| 448 | // Convert to uint64 for the final cost. |
| 449 | finalCost := uint64(totalCost) |
| 450 | return &finalCost |
| 451 | } |
| 452 | } |