MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ExtractString

Function ExtractString

extensions/regex_functions.cc:54–76  ·  view source on GitHub ↗

Extract matched group values from the given target string and rewrite the string

Source from the content-addressed store, hash-verified

52// Extract matched group values from the given target string and rewrite the
53// string
54Value ExtractString(int regex_max_program_size, const StringValue& target,
55 const StringValue& regex, const StringValue& rewrite,
56 const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
57 google::protobuf::MessageFactory* absl_nonnull message_factory,
58 google::protobuf::Arena* absl_nonnull arena) {
59 std::string regex_scratch;
60 std::string target_scratch;
61 std::string rewrite_scratch;
62 absl::string_view regex_view = regex.ToStringView(&regex_scratch);
63 absl::string_view target_view = target.ToStringView(&target_scratch);
64 absl::string_view rewrite_view = rewrite.ToStringView(&rewrite_scratch);
65
66 RE2 re2(regex_view, cel::internal::MakeRE2Options());
67 CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, regex_max_program_size))
68 .With(ErrorValueReturn());
69 std::string output;
70 bool result = RE2::Extract(target_view, re2, rewrite_view, &output);
71 if (!result) {
72 return ErrorValue(absl::InvalidArgumentError(
73 "Unable to extract string for the given regex"));
74 }
75 return StringValue::From(std::move(output), arena);
76}
77
78// Captures the first unnamed/named group value
79// NOTE: For capturing all the groups, use CaptureStringN instead

Callers

nothing calls this directly

Calls 7

MakeRE2OptionsFunction · 0.85
CheckRE2Function · 0.85
ErrorValueReturnClass · 0.85
ExtractFunction · 0.85
ErrorValueFunction · 0.85
ToStringViewMethod · 0.80
WithMethod · 0.80

Tested by

no test coverage detected