Skip to content

Commit cf75ab6

Browse files
committed
Emit array full of 0 on empty string
1 parent 28fa3ec commit cf75ab6

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,15 +1707,20 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17071707

17081708
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
17091709
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
1710-
StrCat(token::kStar);
17111710
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
1712-
uint64_t target = arr_ty->getSize().getZExtValue();
1713-
uint64_t pad = target > expr->getString().size()
1714-
? target - expr->getString().size()
1711+
uint64_t arr_size = arr_ty->getSize().getZExtValue();
1712+
if (expr->getString().empty()) {
1713+
StrCat(std::format("[0u8; {}]", arr_size));
1714+
return false;
1715+
}
1716+
uint64_t pad = arr_size > expr->getString().size()
1717+
? arr_size - expr->getString().size()
17151718
: 0;
1716-
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, pad)));
1719+
StrCat(token::kStar,
1720+
std::format("b{}", GetEscapedStringLiteral(expr, pad)));
17171721
return false;
17181722
}
1723+
StrCat(token::kStar);
17191724
}
17201725
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1)));
17211726
return false;

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,9 +949,13 @@ bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
949949
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
950950
uint64_t pad = 1;
951951
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
952-
uint64_t target = arr_ty->getSize().getZExtValue();
953-
pad = target > expr->getString().size()
954-
? target - expr->getString().size()
952+
uint64_t arr_size = arr_ty->getSize().getZExtValue();
953+
if (expr->getString().empty()) {
954+
StrCat(std::format("vec![0u8; {}].into_boxed_slice()", arr_size));
955+
return false;
956+
}
957+
pad = arr_size > expr->getString().size()
958+
? arr_size - expr->getString().size()
955959
: 0;
956960
}
957961
StrCat(std::format("Box::<[u8]>::from(b{}.as_slice())",

0 commit comments

Comments
 (0)