Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <llvm/Support/ConvertUTF.h>

#include <format>
#include <regex>

#include "compiler.h"
#include "converter/converter_lib.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<const char *>
Expand Down
10 changes: 10 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <clang/AST/Type.h>

#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -167,4 +166,7 @@ std::vector<clang::Stmt *> 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
4 changes: 2 additions & 2 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {

std::string normalizeTranslationRule(std::string rule) {
const std::array<std::pair<std::regex, std::string>, 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 _.
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions cpp2rust/converter/plugins/emplace_back.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <clang/Sema/Initialization.h>

#include <regex>

#include "converter/converter_lib.h"
#include "converter/mapper.h"
#include "converter/models/converter_refcount.h"
Expand Down Expand Up @@ -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("(");
}
Expand Down
1 change: 0 additions & 1 deletion cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <algorithm>
#include <fstream>
#include <iterator>
#include <regex>
#include <string>
#include <vector>

Expand Down
Loading