decRegexOptimizer compiles regex pattern string constants.
(regexOptimizations ...*RegexOptimization)
| 129 | |
| 130 | // decRegexOptimizer compiles regex pattern string constants. |
| 131 | func decRegexOptimizer(regexOptimizations ...*RegexOptimization) InterpretableDecoratorV2 { |
| 132 | functionMatchMap := make(map[string]*RegexOptimization) |
| 133 | overloadMatchMap := make(map[string]*RegexOptimization) |
| 134 | for _, m := range regexOptimizations { |
| 135 | functionMatchMap[m.Function] = m |
| 136 | if m.OverloadID != "" { |
| 137 | overloadMatchMap[m.OverloadID] = m |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return func(i InterpretableV2) (InterpretableV2, error) { |
| 142 | call, ok := i.(InterpretableCall) |
| 143 | if !ok { |
| 144 | return i, nil |
| 145 | } |
| 146 | |
| 147 | var matcher *RegexOptimization |
| 148 | var found bool |
| 149 | if call.OverloadID() != "" { |
| 150 | matcher, found = overloadMatchMap[call.OverloadID()] |
| 151 | } |
| 152 | if !found { |
| 153 | matcher, found = functionMatchMap[call.Function()] |
| 154 | } |
| 155 | if !found || matcher.RegexIndex >= len(call.Args()) { |
| 156 | return i, nil |
| 157 | } |
| 158 | args := call.Args() |
| 159 | regexArg := args[matcher.RegexIndex] |
| 160 | regexStr, isConst := regexArg.(InterpretableConst) |
| 161 | if !isConst { |
| 162 | return i, nil |
| 163 | } |
| 164 | pattern, ok := regexStr.Value().(types.String) |
| 165 | if !ok { |
| 166 | return i, nil |
| 167 | } |
| 168 | return matcher.Factory(call, string(pattern)) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func maybeOptimizeConstUnary(i InterpretableV2, call InterpretableCall) (InterpretableV2, error) { |
| 173 | args := call.Args() |
no test coverage detected