MCPcopy Create free account
hub / github.com/cel-expr/cel-go / replaceStrValidator

Function replaceStrValidator

ext/regex.go:225–264  ·  view source on GitHub ↗
(target string, re *regexp.Regexp, match []int, replacement string)

Source from the content-addressed store, hash-verified

223}
224
225func replaceStrValidator(target string, re *regexp.Regexp, match []int, replacement string) (string, error) {
226 groupCount := re.NumSubexp()
227 var sb strings.Builder
228 runes := []rune(replacement)
229
230 for i := 0; i < len(runes); i++ {
231 c := runes[i]
232
233 if c != '\\' {
234 sb.WriteRune(c)
235 continue
236 }
237
238 if i+1 >= len(runes) {
239 return "", fmt.Errorf("invalid replacement string: '%s' \\ not allowed at end", replacement)
240 }
241
242 i++
243 nextChar := runes[i]
244
245 if nextChar == '\\' {
246 sb.WriteRune('\\')
247 continue
248 }
249
250 groupNum, err := strconv.Atoi(string(nextChar))
251 if err != nil {
252 return "", fmt.Errorf("invalid replacement string: '%s' \\ must be followed by a digit or \\", replacement)
253 }
254
255 if groupNum > groupCount {
256 return "", fmt.Errorf("replacement string references group %d but regex has only %d group(s)", groupNum, groupCount)
257 }
258
259 if match[2*groupNum] != -1 {
260 sb.WriteString(target[match[2*groupNum]:match[2*groupNum+1]])
261 }
262 }
263 return sb.String(), nil
264}
265
266func extract(target, regexStr ref.Val) ref.Val {
267 t := string(target.(types.String))

Callers 1

regReplaceNFunction · 0.85

Calls 1

StringMethod · 0.65

Tested by

no test coverage detected