nandi/rustnimpublic Fork 0
99b837678a29fedd20bb68a5117a6621d8d178b4
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

main.rs · 128 lines · 4.1 KBRust Blame HistoryRaw
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago1//@ args: run
Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago2//@ cfg: feature=alloc
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago3// 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 ago5// `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 ago7// 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 ago9// 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 ago10
Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago11mod display;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago12mod error;
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago13mod lower;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago14mod mixed;
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago15mod upper;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago16
Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 17h ago17// `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")]
20pub use std::{string::String, vec::Vec};
21
22pub use crate::display::HexDisplay;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago23pub use crate::error::{Error, Result};
24
25/// Compute decoded length of the given hex-encoded input.
26#[inline(always)]
27pub 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)]
37pub fn encoded_len(bytes: &[u8]) -> usize {
38 bytes.len() * 2
39}
40
41fn 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
63fn 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
76fn main() {
77 let mut buf = [0u8; 16];
78
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 17h ago79 show("lower", lower::decode(b"abcd1234", &mut buf));
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago80 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 ago91 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 ago111
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 ago113
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 ago128}