From 54b6f764f8714fcced938f52e7a136a4ca4f3dc2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 3 Jun 2026 13:21:47 +0100 Subject: [PATCH 1/5] Fix escape of special char --- cpp2rust/converter/converter.cpp | 2 +- tests/unit/string_escape.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 8701a9b0..43375a39 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1728,7 +1728,7 @@ std::string Converter::GetEscapedCharLiteral(char character) const { return "\\0"; } auto uc = static_cast(character); - if (uc < 0x20 || uc == 0x7F) { + if (uc < 0x20 || uc >= 0x7F) { return std::format("\\x{:02x}", uc); } return std::string(1, character); diff --git a/tests/unit/string_escape.cpp b/tests/unit/string_escape.cpp index 039139b8..0e417820 100644 --- a/tests/unit/string_escape.cpp +++ b/tests/unit/string_escape.cpp @@ -2,11 +2,11 @@ #include int main() { - const char *special = "\a\b\t\n\v\f\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~"; + const char *special = "\a\b\t\n\v\f\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff"; static const char expected[] = { 7, 8, 9, 10, 11, 12, 13, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, - 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, + 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 255 }; for (int i = 0; i < (int)(sizeof(expected) / sizeof(expected[0])); i++) { assert(special[i] == expected[i]); From 1040e1c39e7da3779dda8c616b27439fa20f2b7d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 3 Jun 2026 14:31:24 +0100 Subject: [PATCH 2/5] Fix non-ascii chars in rust byte array --- cpp2rust/converter/converter.cpp | 20 +++++--- cpp2rust/converter/converter.h | 7 +-- .../converter/models/converter_refcount.cpp | 4 +- libcc2rs/src/rc.rs | 9 ++-- .../out/refcount/bool_condition_logical.rs | 2 +- .../out/refcount/bool_condition_logical_c.rs | 2 +- tests/unit/out/refcount/char_printing.rs | 2 +- tests/unit/out/refcount/char_printing_cerr.rs | 2 +- tests/unit/out/refcount/default_in_statics.rs | 6 +-- tests/unit/out/refcount/enum_int_interop.rs | 6 +-- tests/unit/out/refcount/enum_int_interop_c.rs | 6 +-- tests/unit/out/refcount/expr_as_bool_c.rs | 2 +- tests/unit/out/refcount/expr_as_bool_cpp.rs | 2 +- .../out/refcount/fn_ptr_stdlib_compare.rs | 24 ++++----- tests/unit/out/refcount/fopen.rs | 4 +- tests/unit/out/refcount/global_pointers.rs | 6 +-- tests/unit/out/refcount/iterators.rs | 2 +- tests/unit/out/refcount/macros.rs | 8 +-- tests/unit/out/refcount/map-reallocation.rs | 10 ++-- tests/unit/out/refcount/printfs.rs | 10 ++-- .../out/refcount/reinterpret_cast_string.rs | 2 +- tests/unit/out/refcount/stdcopy_ostream.rs | 2 +- tests/unit/out/refcount/string.rs | 24 ++++----- tests/unit/out/refcount/string2.rs | 4 +- tests/unit/out/refcount/string_escape.rs | 49 +++++++++++++++--- tests/unit/out/refcount/string_literals.rs | 15 +++--- tests/unit/out/refcount/string_literals_c.rs | 27 +++++----- .../out/refcount/string_literals_concat.rs | 5 +- .../out/refcount/string_literals_concat_c.rs | 3 +- .../out/refcount/string_literals_return.rs | 8 +-- .../out/refcount/string_literals_return_c.rs | 8 +-- .../unit/out/refcount/struct_default_ctor.rs | 2 +- .../out/refcount/user_defined_same_as_libc.rs | 4 +- tests/unit/out/refcount/va_arg_conditional.rs | 4 +- .../out/refcount/va_arg_non_primitive_ptrs.rs | 2 +- tests/unit/out/refcount/va_arg_printf.rs | 4 +- tests/unit/out/refcount/va_arg_snprintf.rs | 4 +- tests/unit/out/refcount/va_arg_struct_ctx.rs | 4 +- tests/unit/out/unsafe/string_escape.rs | 51 ++++++++++++++++--- tests/unit/string_escape.cpp | 10 ++-- 40 files changed, 225 insertions(+), 141 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 43375a39..975b04ae 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1700,8 +1700,10 @@ bool Converter::VisitFloatingLiteral(clang::FloatingLiteral *expr) { } bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { - std::string ch = GetEscapedCharLiteral(expr->getValue()); - ch = "'" + std::move(ch) + "'"; + auto uc = static_cast(expr->getValue()); + std::string ch = uc > 0x7F + ? std::format("'\\u{{{:x}}}'", uc) + : "'" + GetEscapedCharLiteral(expr->getValue()) + "'"; { PushParen paren(*this); StrCat(ch, keyword::kAs, ToStringBase(expr->getType())); @@ -1710,7 +1712,8 @@ bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { return false; } -std::string Converter::GetEscapedCharLiteral(char character) const { +std::string Converter::GetEscapedCharLiteral(char character, + bool byte_string) const { switch (character) { case '"': return "\\\""; @@ -1728,7 +1731,7 @@ std::string Converter::GetEscapedCharLiteral(char character) const { return "\\0"; } auto uc = static_cast(character); - if (uc < 0x20 || uc >= 0x7F) { + if (uc < 0x20 || uc == 0x7F || (byte_string && uc > 0x7F)) { return std::format("\\x{:02x}", uc); } return std::string(1, character); @@ -1747,14 +1750,15 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const { } std::string Converter::GetEscapedStringLiteral(clang::Expr *expr, - uint64_t pad_nulls) const { + uint64_t pad_nulls, + bool byte_string) const { auto str_expr = clang::dyn_cast(expr->IgnoreCasts()); assert(str_expr); auto raw = str_expr->getString(); std::string out; out.push_back('"'); for (unsigned char c : raw) { - out += GetEscapedCharLiteral(static_cast(c)); + out += GetEscapedCharLiteral(static_cast(c), byte_string); } for (uint64_t i = 0; i < pad_nulls; ++i) { out += "\\0"; @@ -1775,12 +1779,12 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) { ? arr_size - expr->getString().size() : 0; StrCat(token::kStar, - std::format("b{}", GetEscapedStringLiteral(expr, pad))); + std::format("b{}", GetEscapedStringLiteral(expr, pad, true))); return false; } StrCat(token::kStar); } - StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1))); + StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1, true))); return false; } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 1f0a113a..6c78c7c2 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -240,12 +240,13 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr); - std::string GetEscapedCharLiteral(char character) const; + std::string GetEscapedCharLiteral(char character, + bool byte_string = false) const; std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const; - std::string GetEscapedStringLiteral(clang::Expr *expr, - uint64_t pad_nulls = 0) const; + std::string GetEscapedStringLiteral(clang::Expr *expr, uint64_t pad_nulls = 0, + bool byte_string = false) const; virtual bool VisitStringLiteral(clang::StringLiteral *expr); virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index e3f3f353..76abc3b0 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1023,10 +1023,10 @@ bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) { : 0; } StrCat(std::format("Box::<[u8]>::from(b{}.as_slice())", - GetEscapedStringLiteral(expr, pad))); + GetEscapedStringLiteral(expr, pad, true))); return false; } - StrCat(GetEscapedStringLiteral(expr)); + StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0, true))); return false; } diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 631e49c5..d80a3ed7 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -940,9 +940,10 @@ impl fmt::Display for Ptr { } } +type StringLiteralMap = HashMap<&'static [u8], Rc>>>; + thread_local! { - static STRING_LITERALS: RefCell>>>> = - RefCell::new(HashMap::new()); + static STRING_LITERALS: RefCell = RefCell::new(HashMap::new()); } impl Ptr { @@ -1020,12 +1021,12 @@ impl Ptr { } #[inline] - pub fn from_string_literal(s: &'static str) -> Self { + pub fn from_string_literal(s: &'static [u8]) -> Self { STRING_LITERALS.with(|literals| { let mut literals = literals.borrow_mut(); let weak = Rc::downgrade(literals.entry(s).or_insert_with(|| { Rc::new(RefCell::new({ - let mut v = s.as_bytes().to_vec(); + let mut v = s.to_vec(); v.push(0); v })) diff --git a/tests/unit/out/refcount/bool_condition_logical.rs b/tests/unit/out/refcount/bool_condition_logical.rs index 7b263d30..727d1721 100644 --- a/tests/unit/out/refcount/bool_condition_logical.rs +++ b/tests/unit/out/refcount/bool_condition_logical.rs @@ -127,7 +127,7 @@ fn main_0() -> i32 { if ((*n.borrow()) != 0) || (((*bits.borrow()) & 256_i64) != 0) { assert!(true); } - let cp: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hi"))); + let cp: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi"))); let cnp: Value> = Rc::new(RefCell::new(Ptr::::null())); if ((*x.borrow()) > (*y.borrow())) && (!(*cp.borrow()).is_null()) { assert!(true); diff --git a/tests/unit/out/refcount/bool_condition_logical_c.rs b/tests/unit/out/refcount/bool_condition_logical_c.rs index d7cfa358..da893412 100644 --- a/tests/unit/out/refcount/bool_condition_logical_c.rs +++ b/tests/unit/out/refcount/bool_condition_logical_c.rs @@ -152,7 +152,7 @@ fn main_0() -> i32 { { assert!((1 != 0)); } - let cp: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hi"))); + let cp: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi"))); let cnp: Value> = Rc::new(RefCell::new(Ptr::::null())); if (((((((*x.borrow()) > (*y.borrow())) as i32) != 0) && (!(*cp.borrow()).is_null())) as i32) != 0) diff --git a/tests/unit/out/refcount/char_printing.rs b/tests/unit/out/refcount/char_printing.rs index 9213abb8..27ad1011 100644 --- a/tests/unit/out/refcount/char_printing.rs +++ b/tests/unit/out/refcount/char_printing.rs @@ -13,7 +13,7 @@ fn main_0() -> i32 { let vec_: Value> = Rc::new(RefCell::new(vec![195_u8, 167_u8])); let i: Value = Rc::new(RefCell::new(27)); let str: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("rdas.") + Ptr::from_string_literal(b"rdas.") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/char_printing_cerr.rs b/tests/unit/out/refcount/char_printing_cerr.rs index e8304ba6..6e7ffeb5 100644 --- a/tests/unit/out/refcount/char_printing_cerr.rs +++ b/tests/unit/out/refcount/char_printing_cerr.rs @@ -13,7 +13,7 @@ fn main_0() -> i32 { let vec_: Value> = Rc::new(RefCell::new(vec![195_u8, 167_u8])); let i: Value = Rc::new(RefCell::new(27)); let str: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("rdas.") + Ptr::from_string_literal(b"rdas.") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 6d4844c2..7936846d 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -111,7 +111,7 @@ thread_local!( ); thread_local!( pub static static_foo_3: Value = Rc::new(RefCell::new(Foo { - s1: Rc::new(RefCell::new(Ptr::from_string_literal("hello"))), + s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))), s2: Rc::new(RefCell::new(Ptr::::null())), fn1: Rc::new(RefCell::new(FnPtr::null())), fn2: Rc::new(RefCell::new(FnPtr::null())), @@ -121,14 +121,14 @@ thread_local!( thread_local!( pub static static_foo_array_4: Value> = Rc::new(RefCell::new(Box::new([ Foo { - s1: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), + s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"first"))), s2: Rc::new(RefCell::new(Ptr::::null())), fn1: Rc::new(RefCell::new(FnPtr::null())), fn2: Rc::new(RefCell::new(FnPtr::null())), n: Rc::new(RefCell::new(1)), }, Foo { - s1: Rc::new(RefCell::new(Ptr::from_string_literal("second"))), + s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"second"))), s2: Rc::new(RefCell::new(Ptr::::null())), fn1: Rc::new(RefCell::new(FnPtr::null())), fn2: Rc::new(RefCell::new(FnPtr::null())), diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index f8334281..930587cc 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -91,17 +91,17 @@ thread_local!( thread_local!( pub static entries_3: Value> = Rc::new(RefCell::new(Box::new([ Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"first"))), color: Rc::new(RefCell::new(Color::RED)), opt: Rc::new(RefCell::new(Option::OPT_NONE)), }, Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("second"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"second"))), color: Rc::new(RefCell::new(Color::GREEN)), opt: Rc::new(RefCell::new(Option::OPT_A)), }, Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("third"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"third"))), color: Rc::new(RefCell::new(Color::BLUE)), opt: Rc::new(RefCell::new(Option::OPT_C)), }, diff --git a/tests/unit/out/refcount/enum_int_interop_c.rs b/tests/unit/out/refcount/enum_int_interop_c.rs index 3cefe5ff..fc61b994 100644 --- a/tests/unit/out/refcount/enum_int_interop_c.rs +++ b/tests/unit/out/refcount/enum_int_interop_c.rs @@ -81,17 +81,17 @@ thread_local!( thread_local!( pub static entries_3: Value> = Rc::new(RefCell::new(Box::new([ Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"first"))), color: Rc::new(RefCell::new(Color::RED)), opt: Rc::new(RefCell::new(Option::OPT_NONE)), }, Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("second"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"second"))), color: Rc::new(RefCell::new(Color::GREEN)), opt: Rc::new(RefCell::new(Option::OPT_A)), }, Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("third"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"third"))), color: Rc::new(RefCell::new(Color::BLUE)), opt: Rc::new(RefCell::new(Option::OPT_C)), }, diff --git a/tests/unit/out/refcount/expr_as_bool_c.rs b/tests/unit/out/refcount/expr_as_bool_c.rs index 86c34956..b50641a3 100644 --- a/tests/unit/out/refcount/expr_as_bool_c.rs +++ b/tests/unit/out/refcount/expr_as_bool_c.rs @@ -71,7 +71,7 @@ fn main_0() -> i32 { assert!(((((*eq.borrow()) == 1) as i32) != 0)); assert!(((((*lt.borrow()) == 0) as i32) != 0)); assert!(((((*neq.borrow()) == 0) as i32) != 0)); - let p1: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hi"))); + let p1: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi"))); let p2: Value> = Rc::new(RefCell::new(Ptr::::null())); let either: Value = Rc::new(RefCell::new( (((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32).clone(), diff --git a/tests/unit/out/refcount/expr_as_bool_cpp.rs b/tests/unit/out/refcount/expr_as_bool_cpp.rs index 70acdf98..80509b93 100644 --- a/tests/unit/out/refcount/expr_as_bool_cpp.rs +++ b/tests/unit/out/refcount/expr_as_bool_cpp.rs @@ -66,7 +66,7 @@ fn main_0() -> i32 { assert!(((*eq.borrow()) == 1)); assert!(((*lt.borrow()) == 0)); assert!(((*neq.borrow()) == 0)); - let p1: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hi"))); + let p1: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi"))); let p2: Value> = Rc::new(RefCell::new(Ptr::::null())); let either: Value = Rc::new(RefCell::new( (((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32).clone(), diff --git a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs index 41214323..3402eca2 100644 --- a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs @@ -77,17 +77,17 @@ fn main_0() -> i32 { 'loop_: while __do_while || (0 != 0) { __do_while = false; let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal("rb").to_rust_string() { + match Ptr::from_string_literal(b"rb").to_rust_string() { v if v == "rb" => std::fs::OpenOptions::new() .read(true) - .open(Ptr::from_string_literal("/dev/zero").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), v if v == "wb" => std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(Ptr::from_string_literal("/dev/zero").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), _ => panic!("unsupported mode"), @@ -131,17 +131,17 @@ fn main_0() -> i32 { 'loop_: while __do_while || (0 != 0) { __do_while = false; let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal("rb").to_rust_string() { + match Ptr::from_string_literal(b"rb").to_rust_string() { v if v == "rb" => std::fs::OpenOptions::new() .read(true) - .open(Ptr::from_string_literal("/dev/zero").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), v if v == "wb" => std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(Ptr::from_string_literal("/dev/zero").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), _ => panic!("unsupported mode"), @@ -241,17 +241,17 @@ fn main_0() -> i32 { 'loop_: while __do_while || (0 != 0) { __do_while = false; let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal("wb").to_rust_string() { + match Ptr::from_string_literal(b"wb").to_rust_string() { v if v == "rb" => std::fs::OpenOptions::new() .read(true) - .open(Ptr::from_string_literal("/dev/null").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), v if v == "wb" => std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(Ptr::from_string_literal("/dev/null").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), _ => panic!("unsupported mode"), @@ -285,17 +285,17 @@ fn main_0() -> i32 { 'loop_: while __do_while || (0 != 0) { __do_while = false; let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal("wb").to_rust_string() { + match Ptr::from_string_literal(b"wb").to_rust_string() { v if v == "rb" => std::fs::OpenOptions::new() .read(true) - .open(Ptr::from_string_literal("/dev/null").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), v if v == "wb" => std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(Ptr::from_string_literal("/dev/null").to_rust_string()) + .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) .ok() .map_or(Ptr::null(), |f| Ptr::alloc(f)), _ => panic!("unsupported mode"), diff --git a/tests/unit/out/refcount/fopen.rs b/tests/unit/out/refcount/fopen.rs index 5d72fa0d..1bf595ef 100644 --- a/tests/unit/out/refcount/fopen.rs +++ b/tests/unit/out/refcount/fopen.rs @@ -10,8 +10,8 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fname: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("testfile.txt"))); - let mode: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("rb"))); + let fname: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"testfile.txt"))); + let mode: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"rb"))); let file_ptr: Value> = Rc::new(RefCell::new(match (*mode.borrow()).to_rust_string() { v if v == "rb" => std::fs::OpenOptions::new() diff --git a/tests/unit/out/refcount/global_pointers.rs b/tests/unit/out/refcount/global_pointers.rs index c017d0d8..13a87d48 100644 --- a/tests/unit/out/refcount/global_pointers.rs +++ b/tests/unit/out/refcount/global_pointers.rs @@ -23,18 +23,18 @@ impl Clone for Entry { impl ByteRepr for Entry {} thread_local!( pub static single_entry_0: Value = Rc::new(RefCell::new(Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("alone"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"alone"))), p: Rc::new(RefCell::new(Ptr::::null())), })); ); thread_local!( pub static entries_1: Value> = Rc::new(RefCell::new(Box::new([ Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"first"))), p: Rc::new(RefCell::new(Ptr::::null())), }, Entry { - name: Rc::new(RefCell::new(Ptr::from_string_literal("second"))), + name: Rc::new(RefCell::new(Ptr::from_string_literal(b"second"))), p: Rc::new(RefCell::new(Ptr::::null())), }, ]))); diff --git a/tests/unit/out/refcount/iterators.rs b/tests/unit/out/refcount/iterators.rs index fea6821a..54228911 100644 --- a/tests/unit/out/refcount/iterators.rs +++ b/tests/unit/out/refcount/iterators.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let x: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("hello") + Ptr::from_string_literal(b"hello") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/macros.rs b/tests/unit/out/refcount/macros.rs index f799d3ba..3231fb0f 100644 --- a/tests/unit/out/refcount/macros.rs +++ b/tests/unit/out/refcount/macros.rs @@ -23,14 +23,14 @@ pub fn main() { fn main_0() -> i32 { println!( "{} {} {}", - Ptr::from_string_literal("macros.cpp"), + Ptr::from_string_literal(b"macros.cpp"), 8, - Ptr::from_string_literal("main") + Ptr::from_string_literal(b"main") ); ({ - let _file: Ptr = Ptr::from_string_literal("macros.cpp"); + let _file: Ptr = Ptr::from_string_literal(b"macros.cpp"); let _line: i32 = 9; - let _func: Ptr = Ptr::from_string_literal("main"); + let _func: Ptr = Ptr::from_string_literal(b"main"); log_0(_file, _line, _func) }); return 0; diff --git a/tests/unit/out/refcount/map-reallocation.rs b/tests/unit/out/refcount/map-reallocation.rs index 727c38af..a7f44c8a 100644 --- a/tests/unit/out/refcount/map-reallocation.rs +++ b/tests/unit/out/refcount/map-reallocation.rs @@ -28,14 +28,14 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new(((*it.borrow()).second().as_pointer()))); assert!( ((*(*it.borrow()).second().borrow()) == (*sentinel.borrow())) - && (!(Ptr::from_string_literal("iterator does not have correct value before insert")) + && (!(Ptr::from_string_literal(b"iterator does not have correct value before insert")) .is_null()) ); assert!( ({ let _lhs = ((*p.borrow()).read()); _lhs == (*sentinel.borrow()) - }) && (!(Ptr::from_string_literal("pointer does not have correct value before insert")) + }) && (!(Ptr::from_string_literal(b"pointer does not have correct value before insert")) .is_null()) ); let i: Value = Rc::new(RefCell::new(0)); @@ -65,20 +65,20 @@ fn main_0() -> i32 { assert!( ((*(*it.borrow()).second().borrow()) != 0) && (!(Ptr::from_string_literal( - "in refcount, iterator points to index 0 instead of sentinel" + b"in refcount, iterator points to index 0 instead of sentinel" )) .is_null()) ); assert!( ((*(*it.borrow()).second().borrow()) == (*sentinel.borrow())) - && (!(Ptr::from_string_literal("iterator does not have correct value after insert")) + && (!(Ptr::from_string_literal(b"iterator does not have correct value after insert")) .is_null()) ); assert!( ({ let _lhs = ((*p.borrow()).read()); _lhs == (*sentinel.borrow()) - }) && (!(Ptr::from_string_literal("pointer does not have correct value after insert")) + }) && (!(Ptr::from_string_literal(b"pointer does not have correct value after insert")) .is_null()) ); (*(*it.borrow()).second().borrow_mut()) = 57005; diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index cd8156e2..d2e3c38d 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -11,7 +11,7 @@ pub fn fn_0(v: Vec) -> Vec { return { let mut r = (*v.borrow()).clone(); r.pop(); - r.extend(Ptr::from_string_literal(" str").to_c_string_iterator()); + r.extend(Ptr::from_string_literal(b" str").to_c_string_iterator()); r.push(0); r }; @@ -23,15 +23,15 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - println!("{}", Ptr::from_string_literal("fprintf stdout")); + println!("{}", Ptr::from_string_literal(b"fprintf stdout")); println!("{} {} {}", 1, 2_u32, 3_i64); print!("hello world"); let in_: Value> = Rc::new(RefCell::new((libcc2rs::cin()).clone())); assert!(!((*in_.borrow()).is_null())); - println!("{}", Ptr::from_string_literal("printf")); + println!("{}", Ptr::from_string_literal(b"printf")); print!("hello world"); let s: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("a string") + Ptr::from_string_literal(b"a string") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), @@ -41,7 +41,7 @@ fn main_0() -> i32 { "{}", (Rc::new(RefCell::new( ({ - let _v: Vec = Ptr::from_string_literal("foo") + let _v: Vec = Ptr::from_string_literal(b"foo") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(); diff --git a/tests/unit/out/refcount/reinterpret_cast_string.rs b/tests/unit/out/refcount/reinterpret_cast_string.rs index 5b1de317..3dfd8521 100644 --- a/tests/unit/out/refcount/reinterpret_cast_string.rs +++ b/tests/unit/out/refcount/reinterpret_cast_string.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let s: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("ABCD") + Ptr::from_string_literal(b"ABCD") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index 36133edf..972d3167 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let str: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("Hello, world!\n") + Ptr::from_string_literal(b"Hello, world!\n") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/string.rs b/tests/unit/out/refcount/string.rs index 5f958895..b52bacde 100644 --- a/tests/unit/out/refcount/string.rs +++ b/tests/unit/out/refcount/string.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let s1: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("hello") + Ptr::from_string_literal(b"hello") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), @@ -42,7 +42,7 @@ fn main_0() -> i32 { .iter() .copied() .take((*s1.borrow()).len() - 1) - .eq(Ptr::from_string_literal("hello").to_c_string_iterator())); + .eq(Ptr::from_string_literal(b"hello").to_c_string_iterator())); let p1: Value> = Rc::new(RefCell::new((s1.as_pointer() as Ptr))); assert!(((((*p1.borrow()).offset((0) as isize).read()) as i32) == (('h' as u8) as i32))); assert!(((((*p1.borrow()).offset((1) as isize).read()) as i32) == (('e' as u8) as i32))); @@ -125,7 +125,7 @@ fn main_0() -> i32 { .iter() .take((*s1.borrow()).len() - 1) .rposition(|&x| { - Ptr::from_string_literal("l") + Ptr::from_string_literal(b"l") .to_c_string_iterator() .position(|ch| ch == x) .is_some() @@ -155,7 +155,7 @@ fn main_0() -> i32 { let s5: Value> = Rc::new(RefCell::new({ let mut r = (*s1.borrow()).clone(); r.pop(); - r.extend(Ptr::from_string_literal(", world").to_c_string_iterator()); + r.extend(Ptr::from_string_literal(b", world").to_c_string_iterator()); r.push(0); r })); @@ -211,7 +211,7 @@ fn main_0() -> i32 { .iter() .copied() .take((*string.borrow()).len() - 1) - .eq(Ptr::from_string_literal("bar").to_c_string_iterator())); + .eq(Ptr::from_string_literal(b"bar").to_c_string_iterator())); { (*string.borrow_mut()).pop(); (*string.borrow_mut()).resize((3_u64) as usize, 0); @@ -240,7 +240,7 @@ fn main_0() -> i32 { .iter() .copied() .take((*string.borrow()).len() - 1) - .eq(Ptr::from_string_literal("bar").to_c_string_iterator())); + .eq(Ptr::from_string_literal(b"bar").to_c_string_iterator())); { (*string.borrow_mut()).pop(); (*string.borrow_mut()).resize((5_u64) as usize, 0); @@ -334,7 +334,7 @@ fn main_0() -> i32 { let result: Value> = Rc::new(RefCell::new({ let mut r = (*string.borrow()).clone(); r.pop(); - r.extend(Ptr::from_string_literal(" foo").to_c_string_iterator()); + r.extend(Ptr::from_string_literal(b" foo").to_c_string_iterator()); r.push(0); r })); @@ -512,7 +512,7 @@ fn main_0() -> i32 { .iter() .take((*result.borrow()).len() - 1) .rposition(|&x| { - Ptr::from_string_literal("b") + Ptr::from_string_literal(b"b") .to_c_string_iterator() .position(|ch| ch == x) .is_some() @@ -526,7 +526,7 @@ fn main_0() -> i32 { .iter() .take((*result.borrow()).len() - 1) .rposition(|&x| { - Ptr::from_string_literal("f") + Ptr::from_string_literal(b"f") .to_c_string_iterator() .position(|ch| ch == x) .is_some() @@ -539,7 +539,7 @@ fn main_0() -> i32 { .iter() .take((*result.borrow()).len() - 1) .rposition(|&x| { - Ptr::from_string_literal("o") + Ptr::from_string_literal(b"o") .to_c_string_iterator() .position(|ch| ch == x) .is_some() @@ -552,7 +552,7 @@ fn main_0() -> i32 { .iter() .take((*result.borrow()).len() - 1) .rposition(|&x| { - Ptr::from_string_literal("x") + Ptr::from_string_literal(b"x") .to_c_string_iterator() .position(|ch| ch == x) .is_some() @@ -562,7 +562,7 @@ fn main_0() -> i32 { }; assert!(((*pos.borrow()) == (-1_i64 as u64))); let string_to_cast: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("cast") + Ptr::from_string_literal(b"cast") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/string2.rs b/tests/unit/out/refcount/string2.rs index 9fbc33cd..2ed6f7aa 100644 --- a/tests/unit/out/refcount/string2.rs +++ b/tests/unit/out/refcount/string2.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let arr: Value> = Rc::new(RefCell::new( - Ptr::from_string_literal("foo") + Ptr::from_string_literal(b"foo") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), @@ -27,6 +27,6 @@ fn main_0() -> i32 { .iter() .copied() .take((*arr.borrow()).len() - 1) - .eq(Ptr::from_string_literal("fbo").to_c_string_iterator())); + .eq(Ptr::from_string_literal(b"fbo").to_c_string_iterator())); return 0; } diff --git a/tests/unit/out/refcount/string_escape.rs b/tests/unit/out/refcount/string_escape.rs index 83986880..648cff3a 100644 --- a/tests/unit/out/refcount/string_escape.rs +++ b/tests/unit/out/refcount/string_escape.rs @@ -11,19 +11,56 @@ pub fn main() { } fn main_0() -> i32 { let special: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - "\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~", + b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff", ))); thread_local!( static expected_0: Value> = Rc::new(RefCell::new(Box::new([ - 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, 13_u8, 32_u8, 33_u8, 34_u8, 35_u8, 36_u8, 37_u8, - 38_u8, 39_u8, 40_u8, 41_u8, 42_u8, 43_u8, 44_u8, 45_u8, 46_u8, 47_u8, 58_u8, 59_u8, - 60_u8, 61_u8, 62_u8, 63_u8, 64_u8, 91_u8, 92_u8, 93_u8, 94_u8, 95_u8, 96_u8, 123_u8, - 124_u8, 125_u8, 126_u8, + 7_u8, + 8_u8, + 9_u8, + 10_u8, + 11_u8, + 12_u8, + 13_u8, + 32_u8, + 33_u8, + 34_u8, + 35_u8, + 36_u8, + 37_u8, + 38_u8, + 39_u8, + 40_u8, + 41_u8, + 42_u8, + 43_u8, + 44_u8, + 45_u8, + 46_u8, + 47_u8, + 58_u8, + 59_u8, + 60_u8, + 61_u8, + 62_u8, + 63_u8, + 64_u8, + 91_u8, + 92_u8, + 93_u8, + 94_u8, + 95_u8, + 96_u8, + 123_u8, + 124_u8, + 125_u8, + 126_u8, + ('\u{ff}' as u8), ]))); ); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) - < (((::std::mem::size_of::<[u8; 40]>() as u64 as u64) + < (((::std::mem::size_of::<[u8; 41]>() as u64 as u64) .wrapping_div(::std::mem::size_of::() as u64 as u64)) as i32)) { assert!({ diff --git a/tests/unit/out/refcount/string_literals.rs b/tests/unit/out/refcount/string_literals.rs index 1cf23e03..ffa32466 100644 --- a/tests/unit/out/refcount/string_literals.rs +++ b/tests/unit/out/refcount/string_literals.rs @@ -17,16 +17,17 @@ pub fn main() { } fn main_0() -> i32 { let immutable_strings: Value]>> = Rc::new(RefCell::new(Box::new([ - Ptr::from_string_literal("a"), - Ptr::from_string_literal("b"), - Ptr::from_string_literal("c"), + Ptr::from_string_literal(b"a"), + Ptr::from_string_literal(b"b"), + Ptr::from_string_literal(b"c"), ]))); - let immutable_string: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hello"))); + let immutable_string: Value> = + Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); let mutable_string_arr: Value> = Rc::new(RefCell::new(Box::<[u8]>::from(b"papanasi\0".as_slice()))); let immutable_string_arr: Value> = Rc::new(RefCell::new(Box::<[u8]>::from(b"papanasi\0".as_slice()))); - let immutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(""))); + let immutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b""))); let mutable_empty_arr: Value> = Rc::new(RefCell::new(vec![0u8; 1].into_boxed_slice())); let immutable_empty_arr: Value> = @@ -36,7 +37,7 @@ fn main_0() -> i32 { foo_mut_0(_str) }); ({ - let _str: Ptr = Ptr::from_string_literal("world"); + let _str: Ptr = Ptr::from_string_literal(b"world"); foo_const_1(_str) }); ({ @@ -48,7 +49,7 @@ fn main_0() -> i32 { foo_const_1(_str) }); ({ - let _str: Ptr = Ptr::from_string_literal(""); + let _str: Ptr = Ptr::from_string_literal(b""); foo_const_1(_str) }); ({ diff --git a/tests/unit/out/refcount/string_literals_c.rs b/tests/unit/out/refcount/string_literals_c.rs index 011caf2a..9900b0c9 100644 --- a/tests/unit/out/refcount/string_literals_c.rs +++ b/tests/unit/out/refcount/string_literals_c.rs @@ -17,29 +17,30 @@ pub fn main() { } fn main_0() -> i32 { let mutable_strings: Value]>> = Rc::new(RefCell::new(Box::new([ - Ptr::from_string_literal("a"), - Ptr::from_string_literal("b"), - Ptr::from_string_literal("c"), + Ptr::from_string_literal(b"a"), + Ptr::from_string_literal(b"b"), + Ptr::from_string_literal(b"c"), ]))); let immutable_strings: Value]>> = Rc::new(RefCell::new(Box::new([ - Ptr::from_string_literal("a"), - Ptr::from_string_literal("b"), - Ptr::from_string_literal("c"), + Ptr::from_string_literal(b"a"), + Ptr::from_string_literal(b"b"), + Ptr::from_string_literal(b"c"), ]))); - let mutable_string: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hello"))); - let immutable_string: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("hello"))); + let mutable_string: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); + let immutable_string: Value> = + Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); let mutable_string_arr: Value> = Rc::new(RefCell::new(Box::<[u8]>::from(b"papanasi\0".as_slice()))); let immutable_string_arr: Value> = Rc::new(RefCell::new(Box::<[u8]>::from(b"papanasi\0".as_slice()))); - let mutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(""))); - let immutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(""))); + let mutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b""))); + let immutable_empty: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b""))); let mutable_empty_arr: Value> = Rc::new(RefCell::new(vec![0u8; 1].into_boxed_slice())); let immutable_empty_arr: Value> = Rc::new(RefCell::new(vec![0u8; 1].into_boxed_slice())); ({ - let _str: Ptr = Ptr::from_string_literal("world"); + let _str: Ptr = Ptr::from_string_literal(b"world"); foo_mut_0(_str) }); ({ @@ -51,7 +52,7 @@ fn main_0() -> i32 { foo_mut_0(_str) }); ({ - let _str: Ptr = Ptr::from_string_literal("world"); + let _str: Ptr = Ptr::from_string_literal(b"world"); foo_const_1(_str) }); ({ @@ -71,7 +72,7 @@ fn main_0() -> i32 { foo_const_1(_str) }); ({ - let _str: Ptr = Ptr::from_string_literal(""); + let _str: Ptr = Ptr::from_string_literal(b""); foo_const_1(_str) }); ({ diff --git a/tests/unit/out/refcount/string_literals_concat.rs b/tests/unit/out/refcount/string_literals_concat.rs index 3bfeb994..f9a457c2 100644 --- a/tests/unit/out/refcount/string_literals_concat.rs +++ b/tests/unit/out/refcount/string_literals_concat.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let joined: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - "alpha\nbeta\ngamma\n", + b"alpha\nbeta\ngamma\n", ))); assert!(((((*joined.borrow()).offset((0) as isize).read()) as i32) == (('a' as u8) as i32))); assert!(((((*joined.borrow()).offset((5) as isize).read()) as i32) == (('\n' as u8) as i32))); @@ -21,7 +21,8 @@ fn main_0() -> i32 { assert!((((*arr.borrow())[(3) as usize] as i32) == (('b' as u8) as i32))); assert!((((*arr.borrow())[(5) as usize] as i32) == (('r' as u8) as i32))); assert!((((*arr.borrow())[(6) as usize] as i32) == (('\0' as u8) as i32))); - let split_pieces: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("abcdefghi"))); + let split_pieces: Value> = + Rc::new(RefCell::new(Ptr::from_string_literal(b"abcdefghi"))); assert!( ((((*split_pieces.borrow()).offset((0) as isize).read()) as i32) == (('a' as u8) as i32)) ); diff --git a/tests/unit/out/refcount/string_literals_concat_c.rs b/tests/unit/out/refcount/string_literals_concat_c.rs index 1fce432d..7852a135 100644 --- a/tests/unit/out/refcount/string_literals_concat_c.rs +++ b/tests/unit/out/refcount/string_literals_concat_c.rs @@ -15,7 +15,8 @@ fn main_0() -> i32 { assert!((((((*arr.borrow())[(3) as usize] as i32) == ('b' as i32)) as i32) != 0)); assert!((((((*arr.borrow())[(5) as usize] as i32) == ('r' as i32)) as i32) != 0)); assert!((((((*arr.borrow())[(6) as usize] as i32) == ('\0' as i32)) as i32) != 0)); - let split_pieces: Value> = Rc::new(RefCell::new(Ptr::from_string_literal("abcdefghi"))); + let split_pieces: Value> = + Rc::new(RefCell::new(Ptr::from_string_literal(b"abcdefghi"))); assert!( ((((((*split_pieces.borrow()).offset((0) as isize).read()) as i32) == ('a' as i32)) as i32) diff --git a/tests/unit/out/refcount/string_literals_return.rs b/tests/unit/out/refcount/string_literals_return.rs index 506ba96c..ac18cff7 100644 --- a/tests/unit/out/refcount/string_literals_return.rs +++ b/tests/unit/out/refcount/string_literals_return.rs @@ -7,17 +7,17 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn get_greeting_0() -> Ptr { - return Ptr::from_string_literal("hello"); + return Ptr::from_string_literal(b"hello"); } pub fn get_empty_1() -> Ptr { - return Ptr::from_string_literal(""); + return Ptr::from_string_literal(b""); } pub fn get_branch_2(x: i32) -> Ptr { let x: Value = Rc::new(RefCell::new(x)); if ((*x.borrow()) > 0) { - return Ptr::from_string_literal("positive"); + return Ptr::from_string_literal(b"positive"); } - return Ptr::from_string_literal("non-positive"); + return Ptr::from_string_literal(b"non-positive"); } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/string_literals_return_c.rs b/tests/unit/out/refcount/string_literals_return_c.rs index ce265d4d..95cea21a 100644 --- a/tests/unit/out/refcount/string_literals_return_c.rs +++ b/tests/unit/out/refcount/string_literals_return_c.rs @@ -7,17 +7,17 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn get_greeting_0() -> Ptr { - return Ptr::from_string_literal("hello"); + return Ptr::from_string_literal(b"hello"); } pub fn get_empty_1() -> Ptr { - return Ptr::from_string_literal(""); + return Ptr::from_string_literal(b""); } pub fn get_branch_2(x: i32) -> Ptr { let x: Value = Rc::new(RefCell::new(x)); if ((((*x.borrow()) > 0) as i32) != 0) { - return Ptr::from_string_literal("positive"); + return Ptr::from_string_literal(b"positive"); } - return Ptr::from_string_literal("non-positive"); + return Ptr::from_string_literal(b"non-positive"); } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/struct_default_ctor.rs b/tests/unit/out/refcount/struct_default_ctor.rs index 7656c27b..2e56c376 100644 --- a/tests/unit/out/refcount/struct_default_ctor.rs +++ b/tests/unit/out/refcount/struct_default_ctor.rs @@ -16,7 +16,7 @@ impl WOFF2Params { pub fn WOFF2Params() -> Self { let mut this = Self { extended_metadata: Rc::new(RefCell::new( - Ptr::from_string_literal("") + Ptr::from_string_literal(b"") .to_c_string_iterator() .chain(std::iter::once(0)) .collect::>(), diff --git a/tests/unit/out/refcount/user_defined_same_as_libc.rs b/tests/unit/out/refcount/user_defined_same_as_libc.rs index 599f7bb3..15156ebb 100644 --- a/tests/unit/out/refcount/user_defined_same_as_libc.rs +++ b/tests/unit/out/refcount/user_defined_same_as_libc.rs @@ -19,8 +19,8 @@ pub fn main() { fn main_0() -> i32 { let fp: Value> = Rc::new(RefCell::new( ({ - let _path: Ptr = Ptr::from_string_literal("/tmp/irrelevant-file"); - let _mode: Ptr = Ptr::from_string_literal("r"); + let _path: Ptr = Ptr::from_string_literal(b"/tmp/irrelevant-file"); + let _mode: Ptr = Ptr::from_string_literal(b"r"); fopen_0(_path, _mode) }), )); diff --git a/tests/unit/out/refcount/va_arg_conditional.rs b/tests/unit/out/refcount/va_arg_conditional.rs index 011e3372..9673c8de 100644 --- a/tests/unit/out/refcount/va_arg_conditional.rs +++ b/tests/unit/out/refcount/va_arg_conditional.rs @@ -24,7 +24,7 @@ fn main_0() -> i32 { assert!( (((({ let _verbose: i32 = 1; - let _fmt: Ptr = Ptr::from_string_literal("%d"); + let _fmt: Ptr = Ptr::from_string_literal(b"%d"); conditional_log_0(_verbose, _fmt, &[42.into()]) }) == 42) as i32) != 0) @@ -32,7 +32,7 @@ fn main_0() -> i32 { assert!( (((({ let _verbose: i32 = 0; - let _fmt: Ptr = Ptr::from_string_literal("%d"); + let _fmt: Ptr = Ptr::from_string_literal(b"%d"); conditional_log_0(_verbose, _fmt, &[99.into()]) }) == -1_i32) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index 50935667..fab1c2a2 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -44,7 +44,7 @@ pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { let out: Value>> = Rc::new(RefCell::new( ((*ap.borrow_mut()).arg::>>()).clone(), )); - (*out.borrow()).write(Ptr::from_string_literal("hello")); + (*out.borrow()).write(Ptr::from_string_literal(b"hello")); (*result.borrow_mut()) = 1; break 'switch; } diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index 9eaa7262..6a2458b1 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -34,14 +34,14 @@ pub fn main() { fn main_0() -> i32 { assert!( (((({ - let _fmt: Ptr = Ptr::from_string_literal("hello %d %d"); + let _fmt: Ptr = Ptr::from_string_literal(b"hello %d %d"); logf_1(_fmt, &[10.into(), 32.into()]) }) == 42) as i32) != 0) ); assert!( (((({ - let _fmt: Ptr = Ptr::from_string_literal("x %d %d"); + let _fmt: Ptr = Ptr::from_string_literal(b"x %d %d"); logf_1(_fmt, &[1.into(), 2.into()]) }) == 3) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_snprintf.rs b/tests/unit/out/refcount/va_arg_snprintf.rs index d681d0c8..f52b086d 100644 --- a/tests/unit/out/refcount/va_arg_snprintf.rs +++ b/tests/unit/out/refcount/va_arg_snprintf.rs @@ -28,7 +28,7 @@ fn main_0() -> i32 { (((({ let _buf: Ptr = (buf.as_pointer() as Ptr); let _size: i32 = 1; - let _fmt: Ptr = Ptr::from_string_literal("%d"); + let _fmt: Ptr = Ptr::from_string_literal(b"%d"); extract_first_0(_buf, _size, _fmt, &[42.into()]) }) == 42) as i32) != 0) @@ -38,7 +38,7 @@ fn main_0() -> i32 { (((({ let _buf: Ptr = (buf.as_pointer() as Ptr); let _size: i32 = 1; - let _fmt: Ptr = Ptr::from_string_literal("%d"); + let _fmt: Ptr = Ptr::from_string_literal(b"%d"); extract_first_0(_buf, _size, _fmt, &[65.into()]) }) == 65) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index c74d7062..f5ccee40 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -42,14 +42,14 @@ fn main_0() -> i32 { (*(*ctx.borrow()).last_error.borrow_mut()) = 0; ({ let _ctx: Ptr = (ctx.as_pointer()); - let _fmt: Ptr = Ptr::from_string_literal("error %d"); + let _fmt: Ptr = Ptr::from_string_literal(b"error %d"); set_error_0(_ctx, _fmt, &[42.into()]) }); assert!(((((*(*ctx.borrow()).last_error.borrow()) == 42) as i32) != 0)); (*(*ctx.borrow()).verbose.borrow_mut()) = 0; ({ let _ctx: Ptr = (ctx.as_pointer()); - let _fmt: Ptr = Ptr::from_string_literal("error %d"); + let _fmt: Ptr = Ptr::from_string_literal(b"error %d"); set_error_0(_ctx, _fmt, &[99.into()]) }); assert!(((((*(*ctx.borrow()).last_error.borrow()) == 42) as i32) != 0)); diff --git a/tests/unit/out/unsafe/string_escape.rs b/tests/unit/out/unsafe/string_escape.rs index bc2721dc..81c1c23c 100644 --- a/tests/unit/out/unsafe/string_escape.rs +++ b/tests/unit/out/unsafe/string_escape.rs @@ -13,18 +13,55 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut special: *const u8 = - b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\0".as_ptr(); - static mut expected_0: [u8; 40] = unsafe { + b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff\0".as_ptr(); + static mut expected_0: [u8; 41] = unsafe { [ - 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, 13_u8, 32_u8, 33_u8, 34_u8, 35_u8, 36_u8, 37_u8, - 38_u8, 39_u8, 40_u8, 41_u8, 42_u8, 43_u8, 44_u8, 45_u8, 46_u8, 47_u8, 58_u8, 59_u8, - 60_u8, 61_u8, 62_u8, 63_u8, 64_u8, 91_u8, 92_u8, 93_u8, 94_u8, 95_u8, 96_u8, 123_u8, - 124_u8, 125_u8, 126_u8, + 7_u8, + 8_u8, + 9_u8, + 10_u8, + 11_u8, + 12_u8, + 13_u8, + 32_u8, + 33_u8, + 34_u8, + 35_u8, + 36_u8, + 37_u8, + 38_u8, + 39_u8, + 40_u8, + 41_u8, + 42_u8, + 43_u8, + 44_u8, + 45_u8, + 46_u8, + 47_u8, + 58_u8, + 59_u8, + 60_u8, + 61_u8, + 62_u8, + 63_u8, + 64_u8, + 91_u8, + 92_u8, + 93_u8, + 94_u8, + 95_u8, + 96_u8, + 123_u8, + 124_u8, + 125_u8, + 126_u8, + ('\u{ff}' as u8), ] };; let mut i: i32 = 0; 'loop_: while ((i) - < (((::std::mem::size_of::<[u8; 40]>() as u64 as u64) + < (((::std::mem::size_of::<[u8; 41]>() as u64 as u64) .wrapping_div(::std::mem::size_of::() as u64 as u64)) as i32)) { assert!((((*special.offset((i) as isize)) as i32) == (expected_0[(i) as usize] as i32))); diff --git a/tests/unit/string_escape.cpp b/tests/unit/string_escape.cpp index 0e417820..a0a4a325 100644 --- a/tests/unit/string_escape.cpp +++ b/tests/unit/string_escape.cpp @@ -2,12 +2,12 @@ #include int main() { - const char *special = "\a\b\t\n\v\f\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff"; + const char *special = + "\a\b\t\n\v\f\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff"; static const char expected[] = { - 7, 8, 9, 10, 11, 12, 13, 32, 33, 34, 35, 36, 37, 38, - 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, - 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 255 - }; + 7, 8, 9, 10, 11, 12, 13, 32, 33, 34, 35, 36, 37, 38, + 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, + 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, '\xff'}; for (int i = 0; i < (int)(sizeof(expected) / sizeof(expected[0])); i++) { assert(special[i] == expected[i]); } From b0df9d2848669f8d2b17f8adeac7a8891e6203be Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 3 Jun 2026 14:49:04 +0100 Subject: [PATCH 3/5] Drop the byte_string flag --- cpp2rust/converter/converter.cpp | 22 ++++++++------- cpp2rust/converter/converter.h | 7 +++-- cpp2rust/converter/converter_lib.cpp | 9 +++++++ cpp2rust/converter/converter_lib.h | 2 ++ .../converter/models/converter_refcount.cpp | 4 +-- tests/unit/out/refcount/char_printing.rs | 14 ++++++---- tests/unit/out/refcount/char_printing_cerr.rs | 14 ++++++---- tests/unit/out/unsafe/char_printing.rs | 27 ++++++++++++++++++- tests/unit/out/unsafe/char_printing_cerr.rs | 27 ++++++++++++++++++- 9 files changed, 99 insertions(+), 27 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 975b04ae..43fffaff 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1258,7 +1258,11 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt, std::string &fmt_args, const char *&fmt_trait, std::string &fmt_width) { std::string arg_str = Mapper::ToString(arg); - if (clang::isa(arg->IgnoreImplicit())) { + if (auto *str_lit = + clang::dyn_cast(arg->IgnoreImplicit())) { + if (!IsAsciiStringLiteral(str_lit)) { + return false; + } auto str = GetEscapedStringLiteral(arg); std::string_view trim(str); // Delete " from string @@ -1299,6 +1303,8 @@ bool Converter::GetRawArg(clang::Expr *arg, std::string &raw_args) { raw_args += "(&(" + str + ")[..(" + str + ").len() - 1]"; } else if (Mapper::ToString(arg).contains("std::endl")) { raw_args += "(&[b'\\n']"; + } else if (clang::isa(arg->IgnoreImplicit())) { + raw_args += "(b" + GetEscapedStringLiteral(arg); } else { return false; } @@ -1712,8 +1718,7 @@ bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { return false; } -std::string Converter::GetEscapedCharLiteral(char character, - bool byte_string) const { +std::string Converter::GetEscapedCharLiteral(char character) const { switch (character) { case '"': return "\\\""; @@ -1731,7 +1736,7 @@ std::string Converter::GetEscapedCharLiteral(char character, return "\\0"; } auto uc = static_cast(character); - if (uc < 0x20 || uc == 0x7F || (byte_string && uc > 0x7F)) { + if (uc < 0x20 || uc >= 0x7F) { return std::format("\\x{:02x}", uc); } return std::string(1, character); @@ -1750,15 +1755,14 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const { } std::string Converter::GetEscapedStringLiteral(clang::Expr *expr, - uint64_t pad_nulls, - bool byte_string) const { + uint64_t pad_nulls) const { auto str_expr = clang::dyn_cast(expr->IgnoreCasts()); assert(str_expr); auto raw = str_expr->getString(); std::string out; out.push_back('"'); for (unsigned char c : raw) { - out += GetEscapedCharLiteral(static_cast(c), byte_string); + out += GetEscapedCharLiteral(static_cast(c)); } for (uint64_t i = 0; i < pad_nulls; ++i) { out += "\\0"; @@ -1779,12 +1783,12 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) { ? arr_size - expr->getString().size() : 0; StrCat(token::kStar, - std::format("b{}", GetEscapedStringLiteral(expr, pad, true))); + std::format("b{}", GetEscapedStringLiteral(expr, pad))); return false; } StrCat(token::kStar); } - StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1, true))); + StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1))); return false; } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 6c78c7c2..1f0a113a 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -240,13 +240,12 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr); - std::string GetEscapedCharLiteral(char character, - bool byte_string = false) const; + std::string GetEscapedCharLiteral(char character) const; std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const; - std::string GetEscapedStringLiteral(clang::Expr *expr, uint64_t pad_nulls = 0, - bool byte_string = false) const; + std::string GetEscapedStringLiteral(clang::Expr *expr, + uint64_t pad_nulls = 0) const; virtual bool VisitStringLiteral(clang::StringLiteral *expr); virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr); diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 2d5731c9..557a35bf 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -246,6 +246,15 @@ bool IsCallToOstream(clang::CallExpr *expr) { return false; } +bool IsAsciiStringLiteral(const clang::StringLiteral *str) { + for (unsigned char c : str->getString()) { + if (c > 0x7F) { + return false; + } + } + return true; +} + std::vector GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) { std::vector out; diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 6a6a70ec..1730bca2 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -65,6 +65,8 @@ bool IsUniquePtr(clang::QualType type); bool IsCallToOstream(clang::CallExpr *expr); +bool IsAsciiStringLiteral(const clang::StringLiteral *str); + std::vector GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 76abc3b0..0abfb5b7 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1023,10 +1023,10 @@ bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) { : 0; } StrCat(std::format("Box::<[u8]>::from(b{}.as_slice())", - GetEscapedStringLiteral(expr, pad, true))); + GetEscapedStringLiteral(expr, pad))); return false; } - StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0, true))); + StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0))); return false; } diff --git a/tests/unit/out/refcount/char_printing.rs b/tests/unit/out/refcount/char_printing.rs index 27ad1011..8462570f 100644 --- a/tests/unit/out/refcount/char_printing.rs +++ b/tests/unit/out/refcount/char_printing.rs @@ -29,12 +29,16 @@ fn main_0() -> i32 { ] .concat()), ); - write!( - libcc2rs::cout(), - "0x{:x} açordas?\nSim, 0x{:x}.\n", - 27, - (*i.borrow()), + write!(libcc2rs::cout(), "0x{:x}", 27,); + libcc2rs::cout().write_all( + &([ + (b" a\xc3\xa7ordas?" as &[u8]), + (&[('\n' as u8)] as &[u8]), + (b"Sim, 0x" as &[u8]), + ] + .concat()), ); + write!(libcc2rs::cout(), "{:x}.\n", (*i.borrow()),); write!(libcc2rs::cout(), "Hello, World!\n",); libcc2rs::cout().write_all( &([ diff --git a/tests/unit/out/refcount/char_printing_cerr.rs b/tests/unit/out/refcount/char_printing_cerr.rs index 6e7ffeb5..b0b0b510 100644 --- a/tests/unit/out/refcount/char_printing_cerr.rs +++ b/tests/unit/out/refcount/char_printing_cerr.rs @@ -29,12 +29,16 @@ fn main_0() -> i32 { ] .concat()), ); - write!( - libcc2rs::cerr(), - "0x{:x} açordas?\nSim, 0x{:x}.\n", - 27, - (*i.borrow()), + write!(libcc2rs::cerr(), "0x{:x}", 27,); + libcc2rs::cerr().write_all( + &([ + (b" a\xc3\xa7ordas?" as &[u8]), + (&[('\n' as u8)] as &[u8]), + (b"Sim, 0x" as &[u8]), + ] + .concat()), ); + write!(libcc2rs::cerr(), "{:x}.\n", (*i.borrow()),); write!(libcc2rs::cerr(), "Hello, World!\n",); libcc2rs::cerr().write_all( &([ diff --git a/tests/unit/out/unsafe/char_printing.rs b/tests/unit/out/unsafe/char_printing.rs index 6aedf5f1..dc21e0c1 100644 --- a/tests/unit/out/unsafe/char_printing.rs +++ b/tests/unit/out/unsafe/char_printing.rs @@ -54,8 +54,33 @@ unsafe fn main_0() -> i32 { .unwrap() .into_raw_fd(), ), - "0x{:x} açordas?\nSim, 0x{:x}.\n", + "0x{:x}", 27, + ); + std::fs::File::from_raw_fd( + std::io::stdout() + .as_fd() + .try_clone_to_owned() + .unwrap() + .into_raw_fd(), + ) + .write_all( + &([ + (b" a\xc3\xa7ordas?" as &[u8]), + (&[('\n' as u8)] as &[u8]), + (b"Sim, 0x" as &[u8]), + ] + .concat()), + ); + write!( + std::fs::File::from_raw_fd( + std::io::stdout() + .as_fd() + .try_clone_to_owned() + .unwrap() + .into_raw_fd(), + ), + "{:x}.\n", i, ); write!( diff --git a/tests/unit/out/unsafe/char_printing_cerr.rs b/tests/unit/out/unsafe/char_printing_cerr.rs index 0d33b1b1..5b50c6da 100644 --- a/tests/unit/out/unsafe/char_printing_cerr.rs +++ b/tests/unit/out/unsafe/char_printing_cerr.rs @@ -54,8 +54,33 @@ unsafe fn main_0() -> i32 { .unwrap() .into_raw_fd(), ), - "0x{:x} açordas?\nSim, 0x{:x}.\n", + "0x{:x}", 27, + ); + std::fs::File::from_raw_fd( + std::io::stderr() + .as_fd() + .try_clone_to_owned() + .unwrap() + .into_raw_fd(), + ) + .write_all( + &([ + (b" a\xc3\xa7ordas?" as &[u8]), + (&[('\n' as u8)] as &[u8]), + (b"Sim, 0x" as &[u8]), + ] + .concat()), + ); + write!( + std::fs::File::from_raw_fd( + std::io::stderr() + .as_fd() + .try_clone_to_owned() + .unwrap() + .into_raw_fd(), + ), + "{:x}.\n", i, ); write!( From 2f9afefe615f50f209a627508dd8fd92c10b8d40 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 3 Jun 2026 14:53:32 +0100 Subject: [PATCH 4/5] Replace unicode with byte --- cpp2rust/converter/converter.cpp | 5 ++--- tests/unit/out/refcount/string_escape.rs | 2 +- tests/unit/out/unsafe/string_escape.rs | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 43fffaff..a77007ae 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1707,9 +1707,8 @@ bool Converter::VisitFloatingLiteral(clang::FloatingLiteral *expr) { bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { auto uc = static_cast(expr->getValue()); - std::string ch = uc > 0x7F - ? std::format("'\\u{{{:x}}}'", uc) - : "'" + GetEscapedCharLiteral(expr->getValue()) + "'"; + std::string ch = GetEscapedCharLiteral(expr->getValue()); + ch = (uc > 0x7F ? "b'" : "'") + std::move(ch) + "'"; { PushParen paren(*this); StrCat(ch, keyword::kAs, ToStringBase(expr->getType())); diff --git a/tests/unit/out/refcount/string_escape.rs b/tests/unit/out/refcount/string_escape.rs index 648cff3a..c7c9c888 100644 --- a/tests/unit/out/refcount/string_escape.rs +++ b/tests/unit/out/refcount/string_escape.rs @@ -55,7 +55,7 @@ fn main_0() -> i32 { 124_u8, 125_u8, 126_u8, - ('\u{ff}' as u8), + (b'\xff' as u8), ]))); ); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/unsafe/string_escape.rs b/tests/unit/out/unsafe/string_escape.rs index 81c1c23c..70ae0802 100644 --- a/tests/unit/out/unsafe/string_escape.rs +++ b/tests/unit/out/unsafe/string_escape.rs @@ -56,7 +56,7 @@ unsafe fn main_0() -> i32 { 124_u8, 125_u8, 126_u8, - ('\u{ff}' as u8), + (b'\xff' as u8), ] };; let mut i: i32 = 0; From be42fbea2c55137014d384a12ea083baa48c8ad8 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 3 Jun 2026 15:12:25 +0100 Subject: [PATCH 5/5] Fix character literal escaping in VisitCharacterLiteral --- cpp2rust/converter/converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index a77007ae..907f7d6f 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1708,7 +1708,7 @@ bool Converter::VisitFloatingLiteral(clang::FloatingLiteral *expr) { bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { auto uc = static_cast(expr->getValue()); std::string ch = GetEscapedCharLiteral(expr->getValue()); - ch = (uc > 0x7F ? "b'" : "'") + std::move(ch) + "'"; + ch = (uc > 0x7F ? "b'" : "'") + std::move(ch) + '\''; { PushParen paren(*this); StrCat(ch, keyword::kAs, ToStringBase(expr->getType()));