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

Function estimateStringReplaceCost

ext/strings.go:965–984  ·  view source on GitHub ↗

estimateStringReplaceCost estimates cost for string replace operations. The cost accounts for search (O(n*m)) and potential output size growth.

(estimator checker.CostEstimator, target *checker.AstNode, args []checker.AstNode)

Source from the content-addressed store, hash-verified

963// estimateStringReplaceCost estimates cost for string replace operations.
964// The cost accounts for search (O(n*m)) and potential output size growth.
965func estimateStringReplaceCost(estimator checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate {
966 if target == nil || len(args) < 2 {
967 return nil
968 }
969 // Compute the search for the replacement string, by 'm' times
970 targetSize := estimateSize(estimator, *target)
971 needleSize := atLeastOne(estimateSize(estimator, args[0]))
972 searchCost := atLeastOne(targetSize).Multiply(needleSize).MultiplyByCostFactor(stringCostFactor)
973
974 replacementSize := estimateSize(estimator, args[1]).Add(fixedSizeEstimate(1))
975 allReplacedSize := safeMul(safeAdd(targetSize.Max, 1), replacementSize.Max)
976 resultMinSize := targetSize.Min
977 if resultMinSize > replacementSize.Min {
978 resultMinSize = replacementSize.Min
979 }
980 resultSize := rangedSizeEstimate(resultMinSize, allReplacedSize)
981 return callEstimate(
982 searchCost.Add(resultSize.AsCost()).Add(callCostEstimate), &resultSize,
983 )
984}
985
986// estimateStringSplitCost estimates cost for string split operations.
987// Split creates a list of substrings, so cost includes both traversal and

Callers

nothing calls this directly

Calls 11

atLeastOneFunction · 0.85
fixedSizeEstimateFunction · 0.85
safeMulFunction · 0.85
safeAddFunction · 0.85
rangedSizeEstimateFunction · 0.85
callEstimateFunction · 0.85
AsCostMethod · 0.80
estimateSizeFunction · 0.70
MultiplyMethod · 0.65
AddMethod · 0.65
MultiplyByCostFactorMethod · 0.45

Tested by

no test coverage detected