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)
| 963 | // estimateStringReplaceCost estimates cost for string replace operations. |
| 964 | // The cost accounts for search (O(n*m)) and potential output size growth. |
| 965 | func 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 |
nothing calls this directly
no test coverage detected