|
8 | 8 | #include <llvm/Support/ThreadPool.h> |
9 | 9 |
|
10 | 10 | #include <atomic> |
11 | | -#include <cctype> |
12 | 11 | #include <format> |
13 | 12 | #include <mutex> |
| 13 | +#include <regex> |
14 | 14 | #include <utility> |
15 | 15 | #include <vector> |
16 | 16 |
|
@@ -546,41 +546,19 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) { |
546 | 546 | return instantiateTgt(subs, rule->second.type_info.type); |
547 | 547 | } |
548 | 548 |
|
549 | | -// Replace digit sequences that are not adjacent to other word characters with |
550 | | -// '_'. Equivalent to the regex \b\d+\b. |
551 | | -static std::string replaceWordBoundaryDigits(std::string_view str) { |
552 | | - std::string result; |
553 | | - result.reserve(str.size()); |
554 | | - auto is_word_char = [](char c) { |
555 | | - return std::isalnum((unsigned char)c) || c == '_'; |
556 | | - }; |
557 | | - size_t i = 0; |
558 | | - while (i < str.size()) { |
559 | | - if (std::isdigit((unsigned char)str[i])) { |
560 | | - bool start_ok = (i == 0 || !is_word_char(str[i - 1])); |
561 | | - size_t start = i; |
562 | | - while (i < str.size() && std::isdigit((unsigned char)str[i])) { |
563 | | - ++i; |
564 | | - } |
565 | | - bool end_ok = (i == str.size() || !is_word_char(str[i])); |
566 | | - if (start_ok && end_ok) { |
567 | | - result += '_'; |
568 | | - } else { |
569 | | - result.append(str.data() + start, i - start); |
570 | | - } |
571 | | - } else { |
572 | | - result += str[i++]; |
573 | | - } |
| 549 | +std::string normalizeTranslationRule(std::string rule) { |
| 550 | + const std::array<std::pair<std::regex, std::string>, 2> normalization_rules{{ |
| 551 | + // Detach pointer from double reference. Useful for matching translation |
| 552 | + // rules. |
| 553 | + {std::regex(R"(\*\&\&)"), "* &&"}, |
| 554 | + // Ignore constant template parameters, i.e. replace them with _. |
| 555 | + {std::regex(R"(\b\d+\b)"), "_"}, |
| 556 | + }}; |
| 557 | + |
| 558 | + for (const auto &r : normalization_rules) { |
| 559 | + rule = std::regex_replace(rule, r.first, r.second); |
574 | 560 | } |
575 | | - return result; |
576 | | -} |
577 | 561 |
|
578 | | -std::string normalizeTranslationRule(std::string rule) { |
579 | | - // Detach pointer from double reference. Useful for matching translation |
580 | | - // rules. |
581 | | - rule = ReplaceAll(std::move(rule), "*&&", "* &&"); |
582 | | - // Ignore constant template parameters, i.e. replace them with _. |
583 | | - rule = replaceWordBoundaryDigits(rule); |
584 | 562 | return rule; |
585 | 563 | } |
586 | 564 |
|
|
0 commit comments