//Replace constants in expressions for its values precalculated
//using AST nodes with values.
481126
func resolveConstants(expr ast.Node) ast.Node {
	switch expr.(type) {
	case *ast.BasicLit:
		v, e := evalBasicLit(expr.(*ast.BasicLit), nil)
		if e != nil {
			return expr
		}
		return &constNodeBasicLit{*expr.(*ast.BasicLit), v}
	case *ast.BinaryExpr:
		bexp := expr.(*ast.BinaryExpr)
		bexp.X = resolveConstants(bexp.X).(ast.Expr)
		bexp.Y = resolveConstants(bexp.Y).(ast.Expr)
		v, e := evalBinaryExpr(expr.(*ast.BinaryExpr), nilContext{})
		if e != nil {
			return expr
		}
		return &constNodeBinaryExpr{*expr.(*ast.BinaryExpr), v}
	case *ast.ParenExpr:
		pexp := expr.(*ast.ParenExpr)
		pexp.X = resolveConstants(pexp.X).(ast.Expr)
		v, e := eval(pexp.X, nilContext{})
		if e != nil {
			return expr
		}
		return &constNodeParenExpr{*expr.(*ast.ParenExpr), v}

	case *ast.Ident:
		v, e := evalIdent(expr.(*ast.Ident), nilContext{})
		if e != nil {
			return expr
		}
		return &constNodeIdent{*expr.(*ast.Ident), v}
	case *ast.UnaryExpr:
		v, e := evalUnaryExpr(expr.(*ast.UnaryExpr), nilContext{})
		if e != nil {
			fmt.Println(e)
			return expr
		}
		return &constNodeUnaryExpr{*expr.(*ast.UnaryExpr), v}
	case *ast.CallExpr:
		callexp := expr.(*ast.CallExpr)
		for key, value := range callexp.Args {
			callexp.Args[key] = resolveConstants(value).(ast.Expr)
		}
		return expr
	case *ast.IndexExpr:
		idexpr := expr.(*ast.IndexExpr)
		idexpr.Index = resolveConstants(idexpr.Index).(ast.Expr)
		return expr
	case *ast.SliceExpr:
		slexpr := expr.(*ast.SliceExpr)
		slexpr.Low = resolveConstants(slexpr.Low).(ast.Expr)
		slexpr.High = resolveConstants(slexpr.High).(ast.Expr)
		slexpr.X = resolveConstants(slexpr.X).(ast.Expr)
		return expr
	default:
		return expr
	}
}
783773
func replaceExprs(from, to Expr, exprs ...*Expr) bool {
	for _, expr := range exprs {
		if *expr == nil {
			continue
		}
		if *expr == from {
			*expr = to
			return true
		}
		if (*expr).replace(from, to) {
			return true
		}
	}
	return false
}
923692
func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection {
	s.AfterNodes(ns...)
	return s.Remove()
}
191127
func replacePrefix(s string, args ...string) string {
	if len(args) != 2 {
		return s
	}
	if strings.HasPrefix(s, args[0]) {
		return strings.Replace(s, args[0], args[1], 1)
	}
	return s
}
956701
func (s *SecondaryCache) Replace(secondary string, value interface{}) bool {
	item := s.Get(secondary)
	if item == nil {
		return false
	}
	s.Set(secondary, value, item.TTL())
	return true
}
544707
func (s *StateSettings) ReplaceSettings(key string, settings map[string]interface{}) error {
	return replaceSettings(s.backend.db(), s.collection, key, settings)
}
1018252
func (nz *normalizer) convertComparison(node *ComparisonExpr) {
	if node.Operator != InStr && node.Operator != NotInStr {
		return
	}
	tupleVals, ok := node.Right.(ValTuple)
	if !ok {
		return
	}
	// The RHS is a tuple of values.
	// Make a list bindvar.
	bvals := &querypb.BindVariable{
		Type: querypb.Type_TUPLE,
	}
	for _, val := range tupleVals {
		bval := nz.sqlToBindvar(val)
		if bval == nil {
			return
		}
		bvals.Values = append(bvals.Values, &querypb.Value{
			Type:  bval.Type,
			Value: bval.Value,
		})
	}
	bvname := nz.newName()
	nz.bindVars[bvname] = bvals
	// Modify RHS to be a list bindvar.
	node.Right = ListArg(append([]byte("::"), bvname...))
}
244812
func (ref Ref) ConstantPrefix() Ref {
	ref = ref.Copy()

	i := ref.Dynamic()
	if i < 0 {
		return ref
	}
	return ref[:i]
}
693834
func (f *ReFuncs) ReplaceLiteral(re, replacement, input interface{}) (string, error) {
	return regexp.ReplaceLiteral(conv.ToString(re),
		conv.ToString(replacement),
		conv.ToString(input))
}
902287
func (t *Translator) substitute(str string, substitutions map[string]string) (substituted string, errors []error) {

	substituted = str

	for find, replace := range substitutions {
		if strings.Index(str, "{"+find+"}") == -1 {
			errors = append(errors, translatorError{translator: t, message: "substitution not found: " + str + ", " + replace})
		}
		substituted = strings.Replace(substituted, "{"+find+"}", replace, -1)
	}

	return
}
565516
func (t Term) Replace(arg interface{}, optArgs ...ReplaceOpts) Term {
	opts := map[string]interface{}{}
	if len(optArgs) >= 1 {
		opts = optArgs[0].toMap()
	}
	return constructMethodTerm(t, "Replace", p.Term_REPLACE, []interface{}{funcWrap(arg)}, opts)
}
665658
func replaceBetweenQuotes(text string) string {
	text = subPat(text, "single", betweenSingleQuotesRE)
	text = subPat(text, "double", betweenDoubleQuotesRE)
	text = subPat(text, "double", betweenSquareBracketsRE)
	text = subPat(text, "double", betweenParensRE)
	text = subPat(text, "double", betweenArrowQuotesRE)
	text = subPat(text, "double", betweenSmartQuotesRE)
	return text
}
143147
func replaceOrAppendEnvValues(defaults, overrides []string) []string {
	cache := make(map[string]int, len(defaults))
	results := make([]string, 0, len(defaults))
	for i, e := range defaults {
		parts := strings.SplitN(e, "=", 2)
		results = append(results, e)
		cache[parts[0]] = i
	}

	for _, value := range overrides {
		// Values w/o = means they want this env to be removed/unset.
		if !strings.Contains(value, "=") {
			if i, exists := cache[value]; exists {
				results[i] = "" // Used to indicate it should be removed
			}
			continue
		}

		// Just do a normal set/update
		parts := strings.SplitN(value, "=", 2)
		if i, exists := cache[parts[0]]; exists {
			results[i] = value
		} else {
			results = append(results, value)
		}
	}

	// Now remove all entries that we want to "unset"
	for i := 0; i < len(results); i++ {
		if results[i] == "" {
			results = append(results[:i], results[i+1:]...)
			i--
		}
	}

	return results
}
140835
func FoldConstant(ctx context.Context, expr Expression) Expression {
	scalarFunc, ok := expr.(*ScalarFunction)
	if !ok {
		return expr
	}
	if _, isDynamic := DynamicFuncs[scalarFunc.FuncName.L]; isDynamic {
		return expr
	}
	args := scalarFunc.GetArgs()
	canFold := true
	for i := 0; i < len(args); i++ {
		foldedArg := FoldConstant(ctx, args[i])
		scalarFunc.GetArgs()[i] = foldedArg
		if _, ok := foldedArg.(*Constant); !ok {
			canFold = false
		}
	}
	if !canFold {
		return expr
	}
	value, err := scalarFunc.Eval(nil, ctx)
	if err != nil {
		log.Printf("There may exist an error during constant folding. The function name is %s, args are %s", scalarFunc.FuncName, args)
		return expr
	}
	return &Constant{
		Value:   value,
		RetType: scalarFunc.RetType,
	}
}
1089950
func (expr *Expr) Operands() []*Term {
	terms, ok := expr.Terms.([]*Term)
	if !ok {
		return nil
	}
	return terms[1:]
}
937367
func (v *VerbalExpression) Replace(src string, dst string) string {
	return v.Regex().ReplaceAllString(src, dst)
}
303300
func ruleConstantFalse(ctx sessionctx.Context, i, j int, exprs *exprSet) {
	cond := exprs.data[i]
	if cons, ok := cond.(*Constant); ok {
		v, isNull, err := cons.EvalInt(ctx, chunk.Row{})
		if err != nil {
			logutil.Logger(context.Background()).Warn("eval constant", zap.Error(err))
			return
		}
		if !isNull && v == 0 {
			exprs.SetConstFalse()
		}
	}
}