Skip to content

Commit 29aa19a

Browse files
Copilotnunoplopes
andauthored
revert: restore normalizeTranslationRule to use std::regex_replace
Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/0703124b-eddd-4240-bcdd-b114812b1b87 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent 6cf16b5 commit 29aa19a

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <llvm/Support/ThreadPool.h>
99

1010
#include <atomic>
11-
#include <cctype>
1211
#include <format>
1312
#include <mutex>
13+
#include <regex>
1414
#include <utility>
1515
#include <vector>
1616

@@ -546,41 +546,19 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {
546546
return instantiateTgt(subs, rule->second.type_info.type);
547547
}
548548

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);
574560
}
575-
return result;
576-
}
577561

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);
584562
return rule;
585563
}
586564

0 commit comments

Comments
 (0)