| 16622 | } |
| 16623 | |
| 16624 | std::string escape_pattern_string(std::string_view input) { |
| 16625 | ada_log("escape_pattern_string called with input=", input); |
| 16626 | if (input.empty()) [[unlikely]] { |
| 16627 | return ""; |
| 16628 | } |
| 16629 | // Assert: input is an ASCII string. |
| 16630 | ADA_ASSERT_TRUE(ada::idna::is_ascii(input)); |
| 16631 | // Let result be the empty string. |
| 16632 | std::string result{}; |
| 16633 | result.reserve(input.size()); |
| 16634 | |
| 16635 | // TODO: Optimization opportunity: Use a lookup table |
| 16636 | constexpr auto should_escape = [](const char c) { |
| 16637 | return c == '+' || c == '*' || c == '?' || c == ':' || c == '{' || |
| 16638 | c == '}' || c == '(' || c == ')' || c == '\\'; |
| 16639 | }; |
| 16640 | |
| 16641 | // While index is less than input's length: |
| 16642 | for (const auto& c : input) { |
| 16643 | if (should_escape(c)) { |
| 16644 | // then append U+005C (\) to the end of result. |
| 16645 | result.append("\\"); |
| 16646 | } |
| 16647 | |
| 16648 | // Append c to the end of result. |
| 16649 | result += c; |
| 16650 | } |
| 16651 | // Return result. |
| 16652 | return result; |
| 16653 | } |
| 16654 | |
| 16655 | namespace { |
| 16656 | constexpr std::array<uint8_t, 256> escape_regexp_table = []() consteval { |