Skip to content

Instantly share code, notes, and snippets.

@lukewilson2002
Created April 17, 2018 22:35
Show Gist options
  • Select an option

  • Save lukewilson2002/c9136595fcd9c985ece662e675fbf577 to your computer and use it in GitHub Desktop.

Select an option

Save lukewilson2002/c9136595fcd9c985ece662e675fbf577 to your computer and use it in GitHub Desktop.
Tiny Rust ASCII table printer
fn main() {
println!("| {:^5} | {:>3} | {:>4} | {:03} |", "Char", "Dec", "Hex", "Oct");
println!("|-------|-----|------|-----|");
for c in 0u8..128u8 {
println!("| {:^5} | {:>3} | {:>#4X} | {:03o} |", get_char_name(c), c, c, c);
}
}
fn get_char_name(c: u8) -> String {
assert!(c < 128);
if (c as char).is_control() || c == 0x20 /* space */ {
match c {
0 => "NUL",
1 => "SOH",
2 => "STX",
3 => "ETX",
4 => "EOT",
5 => "ENQ",
6 => "ACK",
7 => "BEL",
8 => "BS",
9 => "TAB",
10 => "LF",
11 => "VT",
12 => "FF",
13 => "CR",
14 => "SO",
15 => "SI",
16 => "DLE",
17 => "DC1",
18 => "DC2",
19 => "DC3",
20 => "DC4",
21 => "NAK",
22 => "SYN",
23 => "ETB",
24 => "CAN",
25 => "EM",
26 => "SUB",
27 => "ESC",
28 => "FS",
29 => "GS",
30 => "RS",
31 => "US",
32 => "Space",
127 => "DEL",
_ => panic!("Impossible! :)"),
}.to_owned()
} else {
(c as char).to_string()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment