diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5cad686c..6db513fb 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -9,7 +9,6 @@ #include #include -#include #include "compiler.h" #include "converter/converter_lib.h" @@ -381,7 +380,7 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { } if (decl->isFileVarDecl()) { - name = std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_"); + name = ReplaceAll(Mapper::ToString(decl), "::", "_"); if ((decl->isExternallyDeclarable() && !decl->hasInit()) || !globals_.insert(name).second) { return false; @@ -2135,8 +2134,7 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) { enum_constant->getDeclContext())), enum_constant->getName().str()); } else if (IsGlobalVar(expr)) { - return std::regex_replace(Mapper::ToString(expr->getDecl()), - std::regex("::"), "_"); + return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_"); } return GetNamedDeclAsString(decl); @@ -2884,7 +2882,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const { if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) { return it->second; } - return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_"); + return ReplaceAll(Mapper::ToString(decl), "::", "_"); } std::vector diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 88a4c9b1..a16fd19d 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -762,4 +762,14 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) { } } +std::string ReplaceAll(std::string str, std::string_view from, + std::string_view to) { + size_t pos = 0; + while ((pos = str.find(from, pos)) != std::string::npos) { + str.replace(pos, from.size(), to); + pos += to.size(); + } + return str; +} + } // namespace cpp2rust diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index c82dbb3c..71219c8e 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -167,4 +166,7 @@ std::vector GetSwitchCaseBody(clang::CompoundStmt *body, void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix); +std::string ReplaceAll(std::string str, std::string_view from, + std::string_view to); + } // namespace cpp2rust diff --git a/cpp2rust/converter/mapper.cpp b/cpp2rust/converter/mapper.cpp index 9193b32f..434d18c4 100644 --- a/cpp2rust/converter/mapper.cpp +++ b/cpp2rust/converter/mapper.cpp @@ -548,7 +548,7 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) { std::string normalizeTranslationRule(std::string rule) { const std::array, 2> normalization_rules{{ - // Dettach pointer from double reference. Useful for matching translation + // Detach pointer from double reference. Useful for matching translation // rules. {std::regex(R"(\*\&\&)"), "* &&"}, // Ignore constant template parameters, i.e. replace them with _. @@ -674,7 +674,7 @@ bool ParamIsPointer(const clang::Expr *expr, unsigned index) { void AddRuleForUserDefinedType(clang::NamedDecl *decl) { auto cpp_name = ToString(decl); - auto rs_name = std::regex_replace(cpp_name, std::regex("::"), "_"); + auto rs_name = ReplaceAll(cpp_name, "::", "_"); if (types_.contains(cpp_name)) { return; diff --git a/cpp2rust/converter/plugins/emplace_back.cpp b/cpp2rust/converter/plugins/emplace_back.cpp index 26cf371f..9c156af5 100644 --- a/cpp2rust/converter/plugins/emplace_back.cpp +++ b/cpp2rust/converter/plugins/emplace_back.cpp @@ -3,8 +3,6 @@ #include -#include - #include "converter/converter_lib.h" #include "converter/mapper.h" #include "converter/models/converter_refcount.h" @@ -139,8 +137,7 @@ clang::CXXConstructExpr *buildConstructExpr(clang::CXXMemberCallExpr *call, void Converter::emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) { { PushExprKind push(*this, ExprKind::LValue); - StrCat(std::regex_replace(ToString(call->getCallee()), - std::regex("emplace_back"), "push")); + StrCat(ReplaceAll(ToString(call->getCallee()), "emplace_back", "push")); } StrCat("("); } diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index f3433bbd..b39bb178 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include