func()

in lib/regexp.go [270:298]


func (l regexpLib) replaceAll(args ...ref.Val) ref.Val {
	if len(args) != 3 {
		return types.NoSuchOverloadErr()
	}
	patName, ok := args[1].(types.String)
	if !ok {
		return types.ValOrErr(patName, "no such overload")
	}
	re, ok := l[string(patName)]
	if !ok {
		return types.NewErr("no regexp %s", patName)
	}
	switch src := args[0].(type) {
	case types.Bytes:
		repl, ok := args[2].(types.Bytes)
		if !ok {
			return types.ValOrErr(repl, "no such overload")
		}
		return types.Bytes(re.ReplaceAll(src, repl))
	case types.String:
		repl, ok := args[2].(types.String)
		if !ok {
			return types.ValOrErr(repl, "no such overload")
		}
		return types.String(re.ReplaceAllString(string(src), string(repl)))
	default:
		return types.NewErr("invalid type for replace_all: %s", args[0].Type())
	}
}