Skip to content

Commit 39c5102

Browse files
nunoplopeslucic71
authored andcommitted
remove a few string copies
1 parent 968f1ca commit 39c5102

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,9 +1116,9 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
11161116
ConvertCondition(stmt->getCond());
11171117
{
11181118
PushBrace brace(*this);
1119-
curr_for_inc_.emplace(nullptr);
1119+
curr_for_inc_.emplace_back(nullptr);
11201120
Convert(stmt->getBody());
1121-
curr_for_inc_.pop();
1121+
curr_for_inc_.pop_back();
11221122
}
11231123
return false;
11241124
}
@@ -1129,9 +1129,9 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
11291129
StrCat(keyword::kLoop);
11301130
{
11311131
PushBrace loop_brace(*this);
1132-
curr_for_inc_.emplace(nullptr);
1132+
curr_for_inc_.emplace_back(nullptr);
11331133
Convert(stmt->getBody());
1134-
curr_for_inc_.pop();
1134+
curr_for_inc_.pop_back();
11351135
StrCat(keyword::kIf, token::kNot);
11361136
{
11371137
PushParen paren(*this);
@@ -1157,9 +1157,9 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
11571157
}
11581158
{
11591159
PushBrace brace(*this);
1160-
curr_for_inc_.emplace(stmt->getInc());
1160+
curr_for_inc_.emplace_back(stmt->getInc());
11611161
Convert(stmt->getBody());
1162-
curr_for_inc_.pop();
1162+
curr_for_inc_.pop_back();
11631163
Convert(stmt->getInc());
11641164
StrCat(token::kSemiColon);
11651165
}
@@ -1195,9 +1195,9 @@ void Converter::ConvertForRangeBody(clang::CXXForRangeStmt *stmt,
11951195
std::optional<ScopedMapIterDecl> skip;
11961196
if (map_iter_decl)
11971197
skip.emplace(*this, map_iter_decl);
1198-
curr_for_inc_.emplace(nullptr);
1198+
curr_for_inc_.emplace_back(nullptr);
11991199
Convert(stmt->getBody());
1200-
curr_for_inc_.pop();
1200+
curr_for_inc_.pop_back();
12011201
}
12021202

12031203
bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
@@ -1288,7 +1288,7 @@ bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
12881288

12891289
bool Converter::VisitContinueStmt([[maybe_unused]] clang::ContinueStmt *stmt) {
12901290
if (!curr_for_inc_.empty()) {
1291-
Convert(curr_for_inc_.top());
1291+
Convert(curr_for_inc_.back());
12921292
StrCat(token::kSemiColon);
12931293
}
12941294
StrCat(keyword::kContinue);
@@ -1331,7 +1331,7 @@ bool Converter::IsSubExprOf(const clang::Expr *sub_expr,
13311331
}
13321332

13331333
bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
1334-
std::string &fmt_args, std::string &fmt_trait,
1334+
std::string &fmt_args, const char *&fmt_trait,
13351335
std::string &fmt_width) {
13361336
std::string arg_str = Mapper::ToString(arg);
13371337
if (auto *str_lit =
@@ -1340,22 +1340,21 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
13401340
return false;
13411341
}
13421342
auto str = GetEscapedStringLiteral(arg);
1343+
std::string_view trim(str);
13431344
// Delete " from string
1344-
str.erase(std::remove(str.begin(), str.end(), '"'), str.end());
1345-
fmt += std::move(str);
1345+
trim.remove_prefix(1);
1346+
trim.remove_suffix(1);
1347+
fmt += trim;
13461348
} else if (auto ch = GetEscapedUTF8CharLiteral(arg); !ch.empty()) {
13471349
fmt += std::move(ch);
13481350
} else if (arg_str.contains("std::endl")) {
13491351
fmt += "\\n";
13501352
} else if (arg_str.contains("std::hex")) {
1351-
fmt_trait = 'x';
1353+
fmt_trait = "x";
13521354
} else if (arg_str.contains("std::dec")) {
13531355
fmt_trait = "";
13541356
} else if (arg_str.contains("Setw")) {
1355-
fmt_width = ToString(arg);
1356-
// Delete leading and trailing whitespaces
1357-
fmt_width.erase(0, fmt_width.find_first_not_of(' '));
1358-
fmt_width.erase(fmt_width.find_last_not_of(' ') + 1);
1357+
fmt_width = Trim(ToString(arg));
13591358
} else if (!arg->getType()->isCharType() &&
13601359
Mapper::Map(arg->getType()) != "Vec<u8>") {
13611360
fmt += ("{:" + fmt_width + fmt_trait + "}");
@@ -1424,7 +1423,7 @@ void Converter::ConvertCallToOstream(clang::CallExpr *expr) {
14241423
}
14251424

14261425
std::string fmt;
1427-
std::string fmt_trait;
1426+
const char *fmt_trait = "";
14281427
std::string fmt_width;
14291428
std::string fmt_args;
14301429
std::string raw_args;
@@ -1460,7 +1459,7 @@ void Converter::ConvertCallToOstream(clang::CallExpr *expr) {
14601459
write_raw_args();
14611460
}
14621461

1463-
assert(fmt_trait == "" && "Stream state was not restored after call");
1462+
assert(*fmt_trait == '\0' && "Stream state was not restored after call");
14641463
}
14651464

14661465
void Converter::ConvertPrintf(clang::CallExpr *expr) {
@@ -1882,8 +1881,8 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
18821881
}
18831882

18841883
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
1885-
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
1886-
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
1884+
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
1885+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
18871886
uint64_t arr_size = arr_ty->getSize().getZExtValue();
18881887
if (expr->getString().empty()) {
18891888
StrCat(std::format("[0u8; {}]", arr_size));
@@ -2202,8 +2201,8 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
22022201
Convert(lhs);
22032202
ConvertCast(computation_result_type);
22042203
}
2205-
std::string op(opcode_as_string);
2206-
op.erase(std::remove(op.begin(), op.end(), '='), op.end());
2204+
auto op = opcode_as_string;
2205+
op.remove_suffix(1); // remove '=' from operator
22072206
StrCat(op);
22082207
Convert(rhs);
22092208
}
@@ -2908,7 +2907,7 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
29082907
alloc_type_as_string);
29092908
StrCat(new_array_as_string);
29102909
}
2911-
if (!curr_init_type_.empty() && curr_init_type_.top()->isPointerType()) {
2910+
if (!curr_init_type_.empty() && curr_init_type_.back()->isPointerType()) {
29122911
StrCat(".as_mut_ptr()");
29132912
}
29142913
} else {

0 commit comments

Comments
 (0)