Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.h"
24
#include "runtime/function_registry.h"
25
#include "runtime/runtime_options.h"
26
#include "re2/re2.h"
27
28
namespace cel {
29
namespace {}  // namespace
30
31
absl::Status RegisterRegexFunctions(FunctionRegistry& registry,
32
22.7k
                                    const RuntimeOptions& options) {
33
22.7k
  if (options.enable_regex) {
34
22.7k
    auto regex_matches = [max_size = options.regex_max_program_size](
35
22.7k
                             const StringValue& target,
36
22.7k
                             const StringValue& regex,
37
22.7k
                             const Function::InvokeContext& context) -> Value {
38
0
      RE2 re2(regex.ToString(), cel::internal::MakeRE2Options());
39
0
      CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, max_size))
40
0
          .With(ErrorValueReturn(context.arena()));
41
0
      return BoolValue(RE2::PartialMatch(target.ToString(), re2));
42
0
    };
43
44
    // bind str.matches(re) and matches(str, re)
45
45.4k
    for (bool receiver_style : {true, false}) {
46
45.4k
      using MatchFnAdapter =
47
45.4k
          BinaryFunctionAdapter<Value, const StringValue&, const StringValue&>;
48
45.4k
      CEL_RETURN_IF_ERROR(
49
45.4k
          registry.Register(MatchFnAdapter::CreateDescriptor(
50
45.4k
                                cel::builtin::kRegexMatch, receiver_style),
51
45.4k
                            MatchFnAdapter::WrapFunction(regex_matches)));
52
45.4k
    }
53
22.7k
  }  // if options.enable_regex
54
55
22.7k
  return absl::OkStatus();
56
22.7k
}
57
58
}  // namespace cel