@@ -791,18 +791,26 @@ func (c *coster) functionCost(e ast.Expr, function, overloadID string, target *A
791791 return CallEstimate {CostEstimate : c .sizeOrUnknown (args [1 ]).MultiplyByCostFactor (1 ).Add (argCostSum ())}
792792 }
793793 // O(nm) functions
794- case overloads .MatchesString :
794+ case overloads .Matches , overloads . MatchesString :
795795 // https://swtch.com/~rsc/regexp/regexp1.html applies to RE2 implementation supported by CEL
796- if target != nil && len (args ) == 1 {
796+ var strNode , regexNode AstNode
797+ if overloadID == overloads .MatchesString && target != nil && len (args ) == 1 {
798+ strNode = * target
799+ regexNode = args [0 ]
800+ } else if overloadID == overloads .Matches && target == nil && len (args ) == 2 {
801+ strNode = args [0 ]
802+ regexNode = args [1 ]
803+ }
804+ if strNode != nil && regexNode != nil {
797805 // Add one to string length for purposes of cost calculation to prevent product of string and regex to be 0
798806 // in case where string is empty but regex is still expensive.
799- strCost := c .sizeOrUnknown (* target ).Add (SizeEstimate {Min : 1 , Max : 1 }).MultiplyByCostFactor (common .StringTraversalCostFactor )
807+ strCost := c .sizeOrUnknown (strNode ).Add (SizeEstimate {Min : 1 , Max : 1 }).MultiplyByCostFactor (common .StringTraversalCostFactor )
800808 // We don't know how many expressions are in the regex, just the string length (a huge
801809 // improvement here would be to somehow get a count the number of expressions in the regex or
802810 // how many states are in the regex state machine and use that to measure regex cost).
803811 // For now, we're making a guess that each expression in a regex is typically at least 4 chars
804812 // in length.
805- regexCost := c .sizeOrUnknown (args [ 0 ] ).MultiplyByCostFactor (common .RegexStringLengthCostFactor )
813+ regexCost := c .sizeOrUnknown (regexNode ).MultiplyByCostFactor (common .RegexStringLengthCostFactor )
806814 return CallEstimate {CostEstimate : strCost .Multiply (regexCost ).Add (argCostSum ())}
807815 }
808816 case overloads .ContainsString :
0 commit comments