| 28 | namespace {} // namespace |
| 29 | |
| 30 | absl::Status RegisterRegexFunctions(FunctionRegistry& registry, |
| 31 | const RuntimeOptions& options) { |
| 32 | if (options.enable_regex) { |
| 33 | auto regex_matches = [max_size = options.regex_max_program_size]( |
| 34 | const StringValue& target, |
| 35 | const StringValue& regex) -> Value { |
| 36 | RE2 re2(regex.ToString(), cel::internal::MakeRE2Options()); |
| 37 | CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, max_size)) |
| 38 | .With(ErrorValueReturn()); |
| 39 | return BoolValue(RE2::PartialMatch(target.ToString(), re2)); |
| 40 | }; |
| 41 | |
| 42 | // bind str.matches(re) and matches(str, re) |
| 43 | for (bool receiver_style : {true, false}) { |
| 44 | using MatchFnAdapter = |
| 45 | BinaryFunctionAdapter<Value, const StringValue&, const StringValue&>; |
| 46 | CEL_RETURN_IF_ERROR( |
| 47 | registry.Register(MatchFnAdapter::CreateDescriptor( |
| 48 | cel::builtin::kRegexMatch, receiver_style), |
| 49 | MatchFnAdapter::WrapFunction(regex_matches))); |
| 50 | } |
| 51 | } // if options.enable_regex |
| 52 | |
| 53 | return absl::OkStatus(); |
| 54 | } |
| 55 | |
| 56 | } // namespace cel |