/proc/self/cwd/eval/eval/regex_match_step.cc
Line | Count | Source |
1 | | // Copyright 2022 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 | | |
15 | | #include "eval/eval/regex_match_step.h" |
16 | | |
17 | | #include <cstdio> |
18 | | #include <memory> |
19 | | #include <string> |
20 | | #include <utility> |
21 | | |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/strings/cord.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "common/value.h" |
26 | | #include "eval/eval/evaluator_core.h" |
27 | | #include "eval/eval/expression_step_base.h" |
28 | | #include "eval/eval/expression_step_logic.h" |
29 | | #include "re2/re2.h" |
30 | | |
31 | | namespace google::api::expr::runtime { |
32 | | |
33 | | namespace { |
34 | | |
35 | | using ::cel::BoolValue; |
36 | | using ::cel::StringValue; |
37 | | |
38 | | inline constexpr int kNumRegexMatchArguments = 1; |
39 | | inline constexpr size_t kRegexMatchStepSubject = 0; |
40 | | |
41 | | struct MatchesVisitor final { |
42 | | const RE2& re; |
43 | | |
44 | 0 | bool operator()(const absl::Cord& value) const { |
45 | 0 | if (auto flat = value.TryFlat(); flat.has_value()) { |
46 | 0 | return RE2::PartialMatch(*flat, re); |
47 | 0 | } |
48 | 0 | return RE2::PartialMatch(static_cast<std::string>(value), re); |
49 | 0 | } |
50 | | |
51 | 0 | bool operator()(absl::string_view value) const { |
52 | 0 | return RE2::PartialMatch(value, re); |
53 | 0 | } |
54 | | }; |
55 | | |
56 | | class RegexMatchStep final : public ExpressionStepBase { |
57 | | public: |
58 | | explicit RegexMatchStep(std::shared_ptr<const RE2> re2) |
59 | 0 | : ExpressionStepBase(), re2_(std::move(re2)) {} |
60 | | |
61 | 0 | void Evaluate(ExecutionFrame* frame) const override { |
62 | 0 | if (!frame->value_stack().HasEnough(kNumRegexMatchArguments)) { |
63 | 0 | frame->Abort(absl::InternalError( |
64 | 0 | "Insufficient arguments supplied for regular expression match")); |
65 | 0 | return; |
66 | 0 | } |
67 | 0 | auto input_args = frame->value_stack().GetSpan(kNumRegexMatchArguments); |
68 | 0 | const auto& subject = input_args[kRegexMatchStepSubject]; |
69 | 0 | if (subject.IsUnknown() || subject.IsError()) { |
70 | | // Propagate unknowns and errors. |
71 | 0 | return; |
72 | 0 | } |
73 | 0 | if (!subject->Is<cel::StringValue>()) { |
74 | 0 | frame->Abort(absl::InternalError( |
75 | 0 | "First argument for regular expression match must be a string")); |
76 | 0 | return; |
77 | 0 | } |
78 | 0 | bool match = subject.GetString().NativeValue(MatchesVisitor{*re2_}); |
79 | 0 | frame->value_stack().Pop(kNumRegexMatchArguments); |
80 | 0 | frame->value_stack().Push(cel::BoolValue(match)); |
81 | 0 | } |
82 | | |
83 | | private: |
84 | | const std::shared_ptr<const RE2> re2_; |
85 | | }; |
86 | | |
87 | | } // namespace |
88 | | |
89 | | absl::StatusOr<std::unique_ptr<ExpressionStepLogic>> CreateRegexMatchStep( |
90 | 0 | std::shared_ptr<const RE2> re2) { |
91 | 0 | return std::make_unique<RegexMatchStep>(std::move(re2)); |
92 | 0 | } |
93 | | |
94 | | } // namespace google::api::expr::runtime |