| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 1 | //@ args: run |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 2 | //@ cfg: feature=alloc |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 3 | // base16ct 1.0.0, transpiled as a multi-file crate. |
| 4 | // |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 5 | // `error.rs`, `lower.rs`, `upper.rs`, `mixed.rs` and `display.rs` are the |
| 6 | // crate's own files, byte-for-byte. |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 7 | // This file carries `lib.rs`'s core -- `decoded_len`, `encoded_len` and |
| 8 | // `decode_inner` verbatim -- plus a driver, because the runner needs a `main`. |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 9 | // The `alloc`-gated items are on, via `--cfg feature=alloc`. |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 10 | |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 11 | mod display; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 12 | mod error; |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago | 13 | mod lower; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 14 | mod mixed; |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago | 15 | mod upper; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 16 | |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 17 | // `lib.rs` re-exports these from `alloc` for its modules to `use crate::..`; |
| 18 | // this crate root stands in for it. |
| 19 | #[cfg(feature = "alloc")] |
| 20 | pub use std::{string::String, vec::Vec}; |
| 21 | |
| 22 | pub use crate::display::HexDisplay; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 23 | pub use crate::error::{Error, Result}; |
| 24 | |
| 25 | /// Compute decoded length of the given hex-encoded input. |
| 26 | #[inline(always)] |
| 27 | pub fn decoded_len(bytes: &[u8]) -> Result<usize> { |
| 28 | if bytes.len() & 1 == 0 { |
| 29 | Ok(bytes.len() / 2) |
| 30 | } else { |
| 31 | Err(Error::InvalidLength) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// Get the length of Base16 (hex) produced by encoding the given bytes. |
| 36 | #[inline(always)] |
| 37 | pub fn encoded_len(bytes: &[u8]) -> usize { |
| 38 | bytes.len() * 2 |
| 39 | } |
| 40 | |
| 41 | fn decode_inner<'a>( |
| 42 | src: &[u8], |
| 43 | dst: &'a mut [u8], |
| 44 | decode_nibble: impl Fn(u8) -> u16, |
| 45 | ) -> Result<&'a [u8]> { |
| 46 | let dst = dst |
| 47 | .get_mut(..decoded_len(src)?) |
| 48 | .ok_or(Error::InvalidLength)?; |
| 49 | |
| 50 | let mut err: u16 = 0; |
| 51 | for (src, dst) in src.chunks_exact(2).zip(dst.iter_mut()) { |
| 52 | let byte = (decode_nibble(src[0]) << 4) | decode_nibble(src[1]); |
| 53 | err |= byte >> 8; |
| 54 | *dst = byte as u8; |
| 55 | } |
| 56 | |
| 57 | match err { |
| 58 | 0 => Ok(dst), |
| 59 | _ => Err(Error::InvalidEncoding), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn show(tag: &str, r: Result<&[u8]>) { |
| 64 | match r { |
| 65 | Ok(v) => { |
| 66 | print!("{} ok ", tag); |
| 67 | for b in v.iter() { |
| 68 | print!("{:02x}", b); |
| 69 | } |
| 70 | println!(" len={}", v.len()); |
| 71 | } |
| 72 | Err(e) => println!("{} err {:?} / {}", tag, e, e), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn main() { |
| 77 | let mut buf = [0u8; 16]; |
| 78 | |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago | 79 | show("lower", lower::decode(b"abcd1234", &mut buf)); |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 80 | show("mixed-l", mixed::decode(b"abcd1234", &mut buf)); |
| 81 | show("mixed-u", mixed::decode(b"ABCD1234", &mut buf)); |
| 82 | show("mixed-m", mixed::decode(b"abCD1234", &mut buf)); |
| 83 | show("edge", mixed::decode(b"00ff7f80", &mut buf)); |
| 84 | show("oddlen", mixed::decode(b"abc", &mut buf)); |
| 85 | show("bad", mixed::decode(b"zzzz", &mut buf)); |
| 86 | show("empty", mixed::decode(b"", &mut buf)); |
| 87 | |
| 88 | let mut small = [0u8; 2]; |
| 89 | show("short-dst", mixed::decode(b"abcd1234", &mut small)); |
| 90 | |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago | 91 | show("upper", upper::decode(b"ABCD1234", &mut buf)); |
| 92 | show("upper-rej", upper::decode(b"abcd1234", &mut buf)); |
| 93 | |
| 94 | let mut enc = [0u8; 8]; |
| 95 | show("encode", lower::encode(b"\xab\xcd\x12\x34", &mut enc)); |
| 96 | let mut encu = [0u8; 8]; |
| 97 | show("encode-up", upper::encode(b"\xab\xcd\x12\x34", &mut encu)); |
| 98 | |
| 99 | // `encode_str` is a closure over an `unsafe` block returning a borrowed |
| 100 | // `&str` -- a view of the bytes just written, not a copy. |
| 101 | let mut enc2 = [0u8; 8]; |
| 102 | match lower::encode_str(b"\xab\xcd\x12\x34", &mut enc2) { |
| 103 | Ok(s) => println!("encode_str ok {} len={}", s, s.len()), |
| 104 | Err(e) => println!("encode_str err {:?}", e), |
| 105 | } |
| 106 | let mut tiny = [0u8; 2]; |
| 107 | match lower::encode_str(b"\xab\xcd", &mut tiny) { |
| 108 | Ok(s) => println!("tiny ok {}", s), |
| 109 | Err(e) => println!("tiny err {:?} / {}", e, e), |
| 110 | } |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 111 | |
| 112 | println!("{} {}", decoded_len(b"abcd").unwrap(), encoded_len(b"\xab\xcd")); |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago | 113 | |
| 114 | // The `alloc` half: `decode_vec` and `encode_string`. |
| 115 | println!("{:?}", lower::decode_vec(b"abcd1234")); |
| 116 | println!("{:?}", lower::decode_vec(b"abc")); |
| 117 | println!("{:?}", mixed::decode_vec(b"ABcd1234")); |
| 118 | println!("{}", lower::encode_string(b"\xab\xcd\x12\x34")); |
| 119 | println!("{}", upper::encode_string(b"\xab\xcd\x12\x34")); |
| 120 | println!("[{}]", lower::encode_string(b"")); |
| 121 | |
| 122 | // `HexDisplay` is a tuple struct holding a borrowed slice, and its |
| 123 | // `UpperHex`/`LowerHex` impls write once per byte into the formatter. |
| 124 | let raw = b"\xab\xcd\x12\x34"; |
| 125 | println!("{}", HexDisplay(raw)); |
| 126 | println!("{:X} {:x}", HexDisplay(raw), HexDisplay(raw)); |
| 127 | println!("{}", HexDisplay(b"")); |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 128 | } |