Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c2rust translate unicode to utf8 but Rust doesn't support #762

Closed
wain303009 opened this issue Dec 30, 2022 · 1 comment
Closed

c2rust translate unicode to utf8 but Rust doesn't support #762

wain303009 opened this issue Dec 30, 2022 · 1 comment

Comments

@wain303009
Copy link

printf("\n  \e[32m\u2713 \e[90mok\e[0m\n\n");;

after translation of c2rust:

printf(
        b"\n  \x1B[32m\xE2\x9C\x93 \x1B[90mok\x1B[0m\n\n\0" as *const u8
            as *const libc::c_char,
    );

Though both kind of string are supported by C, Rust only support the first unicode other than the second utf8, and compiler will report error as following:
print! ("\n \x1B[32m\xE2\x9C\x93 \x1B[90mok\x1B[0m\n\n\0"); ^^^^ must be a character in the range [\x00-\x7f]

@kkysen
Copy link
Contributor

kkysen commented Jan 4, 2023

@wain303009, I'm unsure what the error is here.

#include <stdio.h>

int main() {
    printf("\n  \e[32m\u2713 \e[90mok\e[0m\n\n");
}

turns into (playground)

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
extern "C" {
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn main_0() -> libc::c_int {
    printf(
        b"\n  \x1B[32m\xE2\x9C\x93 \x1B[90mok\x1B[0m\n\n\0" as *const u8
            as *const libc::c_char,
    );
    return 0;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

which compiles and runs fine.

Perhaps you meant that

print!("\n  \x1B[32m\xE2\x9C\x93 \x1B[90mok\x1B[0m\n\n\0");

doesn't compile, but that's because that's a "" &str, which must be UTF-8, not a b"" &[u8] byte string, as it is in the c2rust-translated version.

c2rust translates C strings into byte strings as they may not be valid UTF-8, and Rust byte strings do not allow Unicode specifiers like \u{2713}, so we must translate it into its raw byte sequence. In the future, c"" C strings may be used, which I think may fix this issue, but that RFC (rust-lang/rfcs#3348, rust-lang/rust#105723) was just recently accepted. In the meantime, you'll need to re-encode the byte string into a UTF-8 string if you want to refactor it as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants