Skip to content

Commit f081c67

Browse files
committed
remove a few string copies
1 parent cd2e414 commit f081c67

4 files changed

Lines changed: 42 additions & 44 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,9 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
10411041
ConvertCondition(stmt->getCond());
10421042
{
10431043
PushBrace brace(*this);
1044-
curr_for_inc_.emplace(nullptr);
1044+
curr_for_inc_.emplace_back(nullptr);
10451045
Convert(stmt->getBody());
1046-
curr_for_inc_.pop();
1046+
curr_for_inc_.pop_back();
10471047
}
10481048
return false;
10491049
}
@@ -1054,9 +1054,9 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
10541054
StrCat(keyword::kLoop);
10551055
{
10561056
PushBrace loop_brace(*this);
1057-
curr_for_inc_.emplace(nullptr);
1057+
curr_for_inc_.emplace_back(nullptr);
10581058
Convert(stmt->getBody());
1059-
curr_for_inc_.pop();
1059+
curr_for_inc_.pop_back();
10601060
StrCat(keyword::kIf, token::kNot);
10611061
{
10621062
PushParen paren(*this);
@@ -1082,9 +1082,9 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
10821082
}
10831083
{
10841084
PushBrace brace(*this);
1085-
curr_for_inc_.emplace(stmt->getInc());
1085+
curr_for_inc_.emplace_back(stmt->getInc());
10861086
Convert(stmt->getBody());
1087-
curr_for_inc_.pop();
1087+
curr_for_inc_.pop_back();
10881088
Convert(stmt->getInc());
10891089
StrCat(token::kSemiColon);
10901090
}
@@ -1120,9 +1120,9 @@ void Converter::ConvertForRangeBody(clang::CXXForRangeStmt *stmt,
11201120
std::optional<ScopedMapIterDecl> skip;
11211121
if (map_iter_decl)
11221122
skip.emplace(*this, map_iter_decl);
1123-
curr_for_inc_.emplace(nullptr);
1123+
curr_for_inc_.emplace_back(nullptr);
11241124
Convert(stmt->getBody());
1125-
curr_for_inc_.pop();
1125+
curr_for_inc_.pop_back();
11261126
}
11271127

11281128
bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
@@ -1213,7 +1213,7 @@ bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
12131213

12141214
bool Converter::VisitContinueStmt([[maybe_unused]] clang::ContinueStmt *stmt) {
12151215
if (!curr_for_inc_.empty()) {
1216-
Convert(curr_for_inc_.top());
1216+
Convert(curr_for_inc_.back());
12171217
StrCat(token::kSemiColon);
12181218
}
12191219
StrCat(keyword::kContinue);
@@ -1256,27 +1256,26 @@ bool Converter::IsSubExprOf(const clang::Expr *sub_expr,
12561256
}
12571257

12581258
bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
1259-
std::string &fmt_args, std::string &fmt_trait,
1259+
std::string &fmt_args, const char* &fmt_trait,
12601260
std::string &fmt_width) {
12611261
std::string arg_str = Mapper::ToString(arg);
12621262
if (clang::isa<clang::StringLiteral>(arg->IgnoreImplicit())) {
12631263
auto str = GetEscapedStringLiteral(arg);
1264+
std::string_view trim(str);
12641265
// Delete " from string
1265-
str.erase(std::remove(str.begin(), str.end(), '"'), str.end());
1266-
fmt += std::move(str);
1266+
trim.remove_prefix(1);
1267+
trim.remove_suffix(1);
1268+
fmt += trim;
12671269
} else if (auto ch = GetEscapedUTF8CharLiteral(arg); !ch.empty()) {
12681270
fmt += std::move(ch);
12691271
} else if (arg_str.contains("std::endl")) {
12701272
fmt += "\\n";
12711273
} else if (arg_str.contains("std::hex")) {
1272-
fmt_trait = 'x';
1274+
fmt_trait = "x";
12731275
} else if (arg_str.contains("std::dec")) {
12741276
fmt_trait = "";
12751277
} else if (arg_str.contains("Setw")) {
1276-
fmt_width = ToString(arg);
1277-
// Delete leading and trailing whitespaces
1278-
fmt_width.erase(0, fmt_width.find_first_not_of(' '));
1279-
fmt_width.erase(fmt_width.find_last_not_of(' ') + 1);
1278+
fmt_width = Trim(ToString(arg));
12801279
} else if (!arg->getType()->isCharType() &&
12811280
Mapper::Map(arg->getType()) != "Vec<u8>") {
12821281
fmt += ("{:" + fmt_width + fmt_trait + "}");
@@ -1343,7 +1342,7 @@ void Converter::ConvertCallToOstream(clang::CallExpr *expr) {
13431342
}
13441343

13451344
std::string fmt;
1346-
std::string fmt_trait;
1345+
const char *fmt_trait = "";
13471346
std::string fmt_width;
13481347
std::string fmt_args;
13491348
std::string raw_args;
@@ -1379,7 +1378,7 @@ void Converter::ConvertCallToOstream(clang::CallExpr *expr) {
13791378
write_raw_args();
13801379
}
13811380

1382-
assert(fmt_trait == "" && "Stream state was not restored after call");
1381+
assert(*fmt_trait == '\0' && "Stream state was not restored after call");
13831382
}
13841383

13851384
void Converter::ConvertPrintf(clang::CallExpr *expr) {
@@ -1766,8 +1765,8 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17661765
}
17671766

17681767
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
1769-
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
1770-
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
1768+
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
1769+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
17711770
uint64_t arr_size = arr_ty->getSize().getZExtValue();
17721771
if (expr->getString().empty()) {
17731772
StrCat(std::format("[0u8; {}]", arr_size));
@@ -2077,8 +2076,8 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
20772076
Convert(lhs);
20782077
ConvertCast(computation_result_type);
20792078
}
2080-
std::string op(opcode_as_string);
2081-
op.erase(std::remove(op.begin(), op.end(), '='), op.end());
2079+
auto op = opcode_as_string;
2080+
op.remove_suffix(1); // remove '=' from operator
20822081
StrCat(op);
20832082
Convert(rhs);
20842083
}
@@ -2778,7 +2777,7 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
27782777
alloc_type_as_string);
27792778
StrCat(new_array_as_string);
27802779
}
2781-
if (!curr_init_type_.empty() && curr_init_type_.top()->isPointerType()) {
2780+
if (!curr_init_type_.empty() && curr_init_type_.back()->isPointerType()) {
27822781
StrCat(".as_mut_ptr()");
27832782
}
27842783
} else {

cpp2rust/converter/converter.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <functional>
1111
#include <optional>
12-
#include <stack>
1312
#include <string>
1413
#include <unordered_map>
1514
#include <unordered_set>
@@ -157,7 +156,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
157156
virtual bool VisitContinueStmt(clang::ContinueStmt *stmt);
158157

159158
bool GetFmtArg(clang::Expr *arg, std::string &fmt, std::string &fmt_args,
160-
std::string &fmt_trait, std::string &fmt_width);
159+
const char* &fmt_trait, std::string &fmt_width);
161160

162161
bool GetRawArg(clang::Expr *arg, std::string &raw_args);
163162

@@ -606,36 +605,36 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
606605
}
607606
}
608607
};
609-
std::stack<clang::Expr *> curr_for_inc_;
610-
std::stack<clang::QualType> curr_init_type_;
608+
std::vector<clang::Expr *> curr_for_inc_;
609+
std::vector<clang::QualType> curr_init_type_;
611610

612-
enum class BreakTarget { Loop, FallthroughSwitch, Switch };
613-
std::stack<BreakTarget> break_target_;
611+
enum class BreakTarget : int8_t { Loop, FallthroughSwitch, Switch };
612+
std::vector<BreakTarget> break_target_;
614613

615614
bool isSwitchBreak() const {
616-
return !break_target_.empty() && break_target_.top() == BreakTarget::Switch;
615+
return !break_target_.empty() && break_target_.back() == BreakTarget::Switch;
617616
}
618617

619618
class PushBreakTarget {
620619
public:
621-
PushBreakTarget(std::stack<BreakTarget> &stack, BreakTarget target)
620+
PushBreakTarget(std::vector<BreakTarget> &stack, BreakTarget target)
622621
: stack_(stack) {
623-
stack_.push(target);
622+
stack_.push_back(target);
624623
}
625-
~PushBreakTarget() { stack_.pop(); }
624+
~PushBreakTarget() { stack_.pop_back(); }
626625
PushBreakTarget(const PushBreakTarget &) = delete;
627626
PushBreakTarget &operator=(const PushBreakTarget &) = delete;
628627

629628
private:
630-
std::stack<BreakTarget> &stack_;
629+
std::vector<BreakTarget> &stack_;
631630
};
632631

633632
class PushInitType {
634633
public:
635634
PushInitType(Converter &c, clang::QualType type) : c_(c) {
636-
c_.curr_init_type_.push(type);
635+
c_.curr_init_type_.emplace_back(type);
637636
}
638-
~PushInitType() { c_.curr_init_type_.pop(); }
637+
~PushInitType() { c_.curr_init_type_.pop_back(); }
639638
PushInitType(const PushInitType &) = delete;
640639
PushInitType &operator=(const PushInitType &) = delete;
641640

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,9 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
10101010
}
10111011

10121012
bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
1013-
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
1013+
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
10141014
uint64_t pad = 1;
1015-
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
1015+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
10161016
uint64_t arr_size = arr_ty->getSize().getZExtValue();
10171017
if (expr->getString().empty()) {
10181018
StrCat(std::format("vec![0u8; {}].into_boxed_slice()", arr_size));
@@ -1281,8 +1281,8 @@ void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
12811281
StrCat(ConvertRValue(lhs));
12821282
ConvertCast(computation_result_type);
12831283
}
1284-
std::string op(opcode_as_string);
1285-
op.erase(std::remove(op.begin(), op.end(), '='), op.end());
1284+
auto op = opcode_as_string;
1285+
op.remove_suffix(1); // remove '=' from operator
12861286
StrCat(op);
12871287
Convert(rhs);
12881288
}

cpp2rust/converter/models/converter_refcount.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class ConverterRefCount final : public Converter {
228228
FullRefCount,
229229
};
230230

231-
const char *ConversionKindToString(ConversionKind k) {
231+
static const char *ConversionKindToString(ConversionKind k) {
232232
switch (k) {
233233
case ConversionKind::Unboxed:
234234
return "Unboxed";
@@ -254,7 +254,7 @@ class ConverterRefCount final : public Converter {
254254
}
255255
log() << "[PushConversionKind:" << line << "] ";
256256
for (auto ck : c.conversion_kind_) {
257-
log() << c.ConversionKindToString(ck) << ", ";
257+
log() << ConversionKindToString(ck) << ", ";
258258
}
259259
log() << '\n';
260260
}
@@ -264,7 +264,7 @@ class ConverterRefCount final : public Converter {
264264
}
265265
log() << "[PopConversionKind] ";
266266
for (auto ck : c.conversion_kind_) {
267-
log() << c.ConversionKindToString(ck) << ", ";
267+
log() << ConversionKindToString(ck) << ", ";
268268
}
269269
log() << '\n';
270270
}

0 commit comments

Comments
 (0)