diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/leb128/src/bin/leb128-repl.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/leb128/src/bin/leb128-repl.rs')
-rw-r--r-- | third_party/rust/leb128/src/bin/leb128-repl.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/third_party/rust/leb128/src/bin/leb128-repl.rs b/third_party/rust/leb128/src/bin/leb128-repl.rs new file mode 100644 index 0000000000..4f6c43e367 --- /dev/null +++ b/third_party/rust/leb128/src/bin/leb128-repl.rs @@ -0,0 +1,94 @@ +use leb128; + +use std::io::{self, BufRead, Write}; +use std::str; + +fn display(bytes: &[u8]) -> String { + let mut s = vec![]; + + // Base 10. + write!(&mut s, "{:?}\n", bytes).unwrap(); + + // Hex. + write!(&mut s, "[").unwrap(); + for (i, b) in bytes.iter().enumerate() { + if i != 0 { + write!(&mut s, ", ").unwrap(); + } + write!(&mut s, "{:0x}", b).unwrap(); + } + writeln!(&mut s, "]").unwrap(); + + // Binary. + write!(&mut s, "[").unwrap(); + for (i, b) in bytes.iter().enumerate() { + if i != 0 { + write!(&mut s, ", ").unwrap(); + } + write!(&mut s, "{:08b}", b).unwrap(); + } + writeln!(&mut s, "]").unwrap(); + + String::from_utf8(s).unwrap() +} + +fn main() { + println!( + " +LEB128 Read-Eval-Print-Loop! + +Converts numbers to signed and unsigned LEB128 and displays the results in +base-10, hex, and binary. +" + ); + + let mut stdin = io::BufReader::new(io::stdin()); + let mut stdout = io::stdout(); + + let mut buf = vec![]; + + loop { + stdout.write_all(b"> ").expect("failed to write to stdout"); + stdout.flush().expect("failed to flush stdout"); + + buf.clear(); + let n = stdin + .read_until(b'\n', &mut buf) + .expect("failed to read line from stdin"); + if n == 0 { + break; + } + + let uleb = str::from_utf8(&buf) + .ok() + .and_then(|s| s.trim().parse().ok()) + .and_then(|n: u64| { + let mut s = vec![]; + leb128::write::unsigned(&mut s, n).ok()?; + Some(display(&s)) + }) + .unwrap_or_else(|| "error\n".into()); + stdout + .write_all(b"# unsigned LEB128\n") + .and_then(|_| stdout.write_all(uleb.as_bytes())) + .and_then(|_| stdout.write_all(b"\n")) + .expect("failed to write to stdout"); + + let leb = str::from_utf8(&buf) + .ok() + .and_then(|s| s.trim().parse().ok()) + .and_then(|n: i64| { + let mut s = vec![]; + leb128::write::signed(&mut s, n).ok()?; + Some(display(&s)) + }) + .unwrap_or_else(|| "error\n".into()); + stdout + .write_all(b"# signed LEB128\n") + .and_then(|_| stdout.write_all(leb.as_bytes())) + .and_then(|_| stdout.write_all(b"\n")) + .expect("failed to write to stdout"); + + stdout.flush().expect("failed to flush stdout"); + } +} |