/proc/self/cwd/runtime/standard/regex_functions.cc
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | #include "runtime/standard/regex_functions.h" |
15 | | |
16 | | #include "absl/status/status.h" |
17 | | #include "absl/strings/string_view.h" |
18 | | #include "base/builtins.h" |
19 | | #include "base/function_adapter.h" |
20 | | #include "common/value.h" |
21 | | #include "internal/re2_options.h" |
22 | | #include "internal/status_macros.h" |
23 | | #include "runtime/function_registry.h" |
24 | | #include "runtime/runtime_options.h" |
25 | | #include "re2/re2.h" |
26 | | |
27 | | namespace cel { |
28 | | namespace {} // namespace |
29 | | |
30 | | absl::Status RegisterRegexFunctions(FunctionRegistry& registry, |
31 | 14.5k | const RuntimeOptions& options) { |
32 | 14.5k | if (options.enable_regex) { |
33 | 14.5k | auto regex_matches = [max_size = options.regex_max_program_size]( |
34 | 14.5k | const StringValue& target, |
35 | 14.5k | const StringValue& regex) -> Value { |
36 | 0 | RE2 re2(regex.ToString(), cel::internal::MakeRE2Options()); |
37 | 0 | CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, max_size)) |
38 | 0 | .With(ErrorValueReturn()); |
39 | 0 | return BoolValue(RE2::PartialMatch(target.ToString(), re2)); |
40 | 0 | }; |
41 | | |
42 | | // bind str.matches(re) and matches(str, re) |
43 | 29.0k | for (bool receiver_style : {true, false}) { |
44 | 29.0k | using MatchFnAdapter = |
45 | 29.0k | BinaryFunctionAdapter<Value, const StringValue&, const StringValue&>; |
46 | 29.0k | CEL_RETURN_IF_ERROR( |
47 | 29.0k | registry.Register(MatchFnAdapter::CreateDescriptor( |
48 | 29.0k | cel::builtin::kRegexMatch, receiver_style), |
49 | 29.0k | MatchFnAdapter::WrapFunction(regex_matches))); |
50 | 29.0k | } |
51 | 14.5k | } // if options.enable_regex |
52 | | |
53 | 14.5k | return absl::OkStatus(); |
54 | 14.5k | } |
55 | | |
56 | | } // namespace cel |