Extract matched group values from the given target string and rewrite the string
| 52 | // Extract matched group values from the given target string and rewrite the |
| 53 | // string |
| 54 | Value 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(®ex_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 |
nothing calls this directly
no test coverage detected