Skip to content

Commit

Permalink
build.rs: imp. function to show weird paths as hex
Browse files Browse the repository at this point in the history
* previously char range 0x00 to 0x1F weren't hexified
* even if valid utf-8, still show \0 as \x00
(even though in our use case \0 wouldn't be seen due to being converted
by Command::arg/args earlier to "<string-with-nul>")

* passes the tests that were there thus far but got removed by prev. commit
  • Loading branch information
correabuscar committed May 29, 2024
1 parent 6550ad1 commit 4cb88d6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,16 +1162,32 @@ struct MyArgs<'a>(Vec<&'a OsStr>);

fn humanly_visible_os_chars(f: &mut fmt::Formatter<'_>, arg: &OsStr) -> fmt::Result {
if let Some(arg_str) = arg.to_str() {
// If the argument is valid UTF-8, simply display it
write!(f, "\"{}\"", arg_str)?;
// If the argument is valid UTF-8,
if arg_str.contains('\0') {
write!(f, "\"")?;
// has \0 in it, show them as \x00 but keep the rest as they are such as ♥
for c in arg_str.chars() {
if c == '\0' {
write!(f,"\\x00")?
} else {
write!(f,"{}",c)?
}
}
write!(f, "\"")?;
} else {
// has no \0 in it
write!(f, "\"{}\"", arg_str)?;
}
} else {
//None aka not fully utf8 arg
//then we show it as ascii + hex
write!(f, "\"")?;
#[cfg(not(target_os = "windows"))]
for byte in arg.as_bytes() {
match std::char::from_u32(u32::from(*byte)) {
Some(c) if c.is_ascii() => write!(f, "{}", c)?,
//chars in range 0x20..0x7E (presumably printable) are shown as they are
Some(c) if (*byte >= 0x20) && (*byte <= 0x7E) => write!(f, "{}", c)?,
//anything else including \0 and ♥ become hex, eg. ♥ is \xE2\x99\xA5
_ => {
write!(f, "\\x{:02X}", byte)?;
}
Expand Down

0 comments on commit 4cb88d6

Please sign in to comment.