| 1 | use leb128rust-analyzer cargo leb128 0.2.5 crate/; |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/; |
| 5 | |
| 6 | fn displayrust-analyzer cargo leb128 0.2.5 display().(bytes: &[u8]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 7 | let mut s = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 8 | |
| 9 | // Base 10. |
| 10 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, "{:?}\n", bytes).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 11 | |
| 12 | // Hex. |
| 13 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, "[").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 14 | for (i, b) in bytes.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 15 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 16 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, ", ").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 17 | } |
| 18 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, "{:0x}", b).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 19 | } |
| 20 | writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(&mut s, "]").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 21 | |
| 22 | // Binary. |
| 23 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, "[").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 24 | for (i, b) in bytes.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 25 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 26 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, ", ").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 27 | } |
| 28 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut s, "{:08b}", b).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 29 | } |
| 30 | writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(&mut s, "]").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 31 | |
| 32 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 33 | } |
| 34 | |
| 35 | fn mainrust-analyzer cargo leb128 0.2.5 main().() { |
| 36 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 37 | " |
| 38 | LEB128 Read-Eval-Print-Loop! |
| 39 | |
| 40 | Converts numbers to signed and unsigned LEB128 and displays the results in |
| 41 | base-10, hex, and binary. |
| 42 | " |
| 43 | ); |
| 44 | |
| 45 | let mut stdin = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdin().()); |
| 46 | let mut stdout = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().(); |
| 47 | |
| 48 | let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 49 | |
| 50 | loop { |
| 51 | stdout.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(b"> ").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write to stdout"); |
| 52 | stdout.flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]flush().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to flush stdout"); |
| 53 | |
| 54 | buf.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().(); |
| 55 | let n = stdin |
| 56 | .read_untilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#read_until().(b'\n', &mut buf) |
| 57 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read line from stdin"); |
| 58 | if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | let uleb = strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&buf) |
| 63 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 64 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 65 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|n: u64| { |
| 66 | let mut s = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 67 | leb128rust-analyzer cargo leb128 0.2.5 crate/::writerust-analyzer cargo leb128 0.2.5 write/::unsignedrust-analyzer cargo leb128 0.2.5 write/unsigned().(&mut s, n).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 68 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(displayrust-analyzer cargo leb128 0.2.5 display().(&s)) |
| 69 | }) |
| 70 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| "error\n".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 71 | stdout |
| 72 | .write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(b"# unsigned LEB128\n") |
| 73 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|_| stdout.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(uleb.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().())) |
| 74 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|_| stdout.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(b"\n")) |
| 75 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write to stdout"); |
| 76 | |
| 77 | let leb = strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&buf) |
| 78 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 79 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 80 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|n: i64| { |
| 81 | let mut s = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 82 | leb128rust-analyzer cargo leb128 0.2.5 crate/::writerust-analyzer cargo leb128 0.2.5 write/::signedrust-analyzer cargo leb128 0.2.5 write/signed().(&mut s, n).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 83 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(displayrust-analyzer cargo leb128 0.2.5 display().(&s)) |
| 84 | }) |
| 85 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| "error\n".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 86 | stdout |
| 87 | .write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(b"# signed LEB128\n") |
| 88 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|_| stdout.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(leb.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().())) |
| 89 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|_| stdout.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(b"\n")) |
| 90 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write to stdout"); |
| 91 | |
| 92 | stdout.flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]flush().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to flush stdout"); |
| 93 | } |
| 94 | } |