Skip to content

Commit 4bce2c7

Browse files
Copilotnunoplopes
andauthored
Remove unnecessary uses of regexes (#40)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Nuno Lopes <nuno.lopes@tecnico.ulisboa.pt>
1 parent 81ca488 commit 4bce2c7

6 files changed

Lines changed: 19 additions & 13 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <llvm/Support/ConvertUTF.h>
1010

1111
#include <format>
12-
#include <regex>
1312

1413
#include "compiler.h"
1514
#include "converter/converter_lib.h"
@@ -381,7 +380,7 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
381380
}
382381

383382
if (decl->isFileVarDecl()) {
384-
name = std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_");
383+
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
385384
if ((decl->isExternallyDeclarable() && !decl->hasInit()) ||
386385
!globals_.insert(name).second) {
387386
return false;
@@ -2139,8 +2138,7 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
21392138
enum_constant->getDeclContext())),
21402139
enum_constant->getName().str());
21412140
} else if (IsGlobalVar(expr)) {
2142-
return std::regex_replace(Mapper::ToString(expr->getDecl()),
2143-
std::regex("::"), "_");
2141+
return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_");
21442142
}
21452143

21462144
return GetNamedDeclAsString(decl);
@@ -2888,7 +2886,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
28882886
if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) {
28892887
return it->second;
28902888
}
2891-
return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_");
2889+
return ReplaceAll(Mapper::ToString(decl), "::", "_");
28922890
}
28932891

28942892
std::vector<const char *>

cpp2rust/converter/converter_lib.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,14 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) {
762762
}
763763
}
764764

765+
std::string ReplaceAll(std::string str, std::string_view from,
766+
std::string_view to) {
767+
size_t pos = 0;
768+
while ((pos = str.find(from, pos)) != std::string::npos) {
769+
str.replace(pos, from.size(), to);
770+
pos += to.size();
771+
}
772+
return str;
773+
}
774+
765775
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <clang/AST/Type.h>
1111

1212
#include <optional>
13-
#include <regex>
1413
#include <string>
1514
#include <string_view>
1615
#include <vector>
@@ -167,4 +166,7 @@ std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,
167166

168167
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
169168

169+
std::string ReplaceAll(std::string str, std::string_view from,
170+
std::string_view to);
171+
170172
} // namespace cpp2rust

cpp2rust/converter/mapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {
548548

549549
std::string normalizeTranslationRule(std::string rule) {
550550
const std::array<std::pair<std::regex, std::string>, 2> normalization_rules{{
551-
// Dettach pointer from double reference. Useful for matching translation
551+
// Detach pointer from double reference. Useful for matching translation
552552
// rules.
553553
{std::regex(R"(\*\&\&)"), "* &&"},
554554
// Ignore constant template parameters, i.e. replace them with _.
@@ -674,7 +674,7 @@ bool ParamIsPointer(const clang::Expr *expr, unsigned index) {
674674

675675
void AddRuleForUserDefinedType(clang::NamedDecl *decl) {
676676
auto cpp_name = ToString(decl);
677-
auto rs_name = std::regex_replace(cpp_name, std::regex("::"), "_");
677+
auto rs_name = ReplaceAll(cpp_name, "::", "_");
678678

679679
if (types_.contains(cpp_name)) {
680680
return;

cpp2rust/converter/plugins/emplace_back.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <clang/Sema/Initialization.h>
55

6-
#include <regex>
7-
86
#include "converter/converter_lib.h"
97
#include "converter/mapper.h"
108
#include "converter/models/converter_refcount.h"
@@ -139,8 +137,7 @@ clang::CXXConstructExpr *buildConstructExpr(clang::CXXMemberCallExpr *call,
139137
void Converter::emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) {
140138
{
141139
PushExprKind push(*this, ExprKind::LValue);
142-
StrCat(std::regex_replace(ToString(call->getCallee()),
143-
std::regex("emplace_back"), "push"));
140+
StrCat(ReplaceAll(ToString(call->getCallee()), "emplace_back", "push"));
144141
}
145142
StrCat("(");
146143
}

cpp2rust/converter/translation_rule.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <algorithm>
2525
#include <fstream>
2626
#include <iterator>
27-
#include <regex>
2827
#include <string>
2928
#include <vector>
3029

0 commit comments

Comments
 (0)