A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
regex_literal.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors
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// http://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#ifndef A11_FLOW_REGEX_LITERAL_H_
16#define A11_FLOW_REGEX_LITERAL_H_
17
18#include <cstddef>
19#include <string>
20#include <string_view>
21
22#include <absl/status/status.h>
23#include <absl/status/statusor.h>
24
25namespace a11::flow {
26
28inline absl::StatusOr<std::string> RegexExpression(std::string_view literal) {
29 if (literal.size() < 2 || literal.front() != '/') {
30 return absl::InvalidArgumentError("Expected a regex literal.");
31 }
32 const size_t close = literal.rfind('/');
33 if (close == 0) {
34 return absl::InvalidArgumentError("Unterminated regex literal.");
35 }
36 const std::string_view body = literal.substr(1, close - 1);
37 const std::string_view flags = literal.substr(close + 1);
38 bool seen_i = false;
39 bool seen_m = false;
40 bool seen_s = false;
41 for (const char flag : flags) {
42 bool* seen = flag == 'i' ? &seen_i
43 : flag == 'm' ? &seen_m
44 : flag == 's' ? &seen_s
45 : nullptr;
46 if (seen == nullptr || *seen) {
47 return absl::InvalidArgumentError(
48 "Regex flags must be unique i, m or s.");
49 }
50 *seen = true;
51 }
52 std::string expression;
53 expression.reserve(body.size() + flags.size() + 4);
54 if (!flags.empty()) {
55 expression.append("(?");
56 expression.append(flags);
57 expression.push_back(')');
58 }
59 expression.append(body);
60 return expression;
61}
62
63} // namespace a11::flow
64
65#endif // A11_FLOW_REGEX_LITERAL_H_
Definition casefold.cc:28
absl::StatusOr< std::string > RegexExpression(std::string_view literal)
Converts a slash-delimited Flow literal to an RE2 expression.
Definition regex_literal.h:28