nandi/rustnimpublic Fork 0
0a375d8ec0bfffe1baadd340864ab1ffa92fdb98
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 · 105 lines · 3.2 KBRust Blame HistoryRaw
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago1//@ args: run
2// base16ct 1.0.0, transpiled as a multi-file crate.
3//
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago4// `error.rs`, `lower.rs`, `upper.rs` and `mixed.rs` are the crate's own
5// files, byte-for-byte.
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago6// This file carries `lib.rs`'s core -- `decoded_len`, `encoded_len` and
7// `decode_inner` verbatim -- plus a driver, because the runner needs a `main`.
8// The `alloc`-gated items are off, as they are by default in the crate.
9
10mod error;
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago11mod lower;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago12mod mixed;
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago13mod upper;
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago14
15pub use crate::error::{Error, Result};
16
17/// Compute decoded length of the given hex-encoded input.
18#[inline(always)]
19pub fn decoded_len(bytes: &[u8]) -> Result<usize> {
20 if bytes.len() & 1 == 0 {
21 Ok(bytes.len() / 2)
22 } else {
23 Err(Error::InvalidLength)
24 }
25}
26
27/// Get the length of Base16 (hex) produced by encoding the given bytes.
28#[inline(always)]
29pub fn encoded_len(bytes: &[u8]) -> usize {
30 bytes.len() * 2
31}
32
33fn decode_inner<'a>(
34 src: &[u8],
35 dst: &'a mut [u8],
36 decode_nibble: impl Fn(u8) -> u16,
37) -> Result<&'a [u8]> {
38 let dst = dst
39 .get_mut(..decoded_len(src)?)
40 .ok_or(Error::InvalidLength)?;
41
42 let mut err: u16 = 0;
43 for (src, dst) in src.chunks_exact(2).zip(dst.iter_mut()) {
44 let byte = (decode_nibble(src[0]) << 4) | decode_nibble(src[1]);
45 err |= byte >> 8;
46 *dst = byte as u8;
47 }
48
49 match err {
50 0 => Ok(dst),
51 _ => Err(Error::InvalidEncoding),
52 }
53}
54
55fn show(tag: &str, r: Result<&[u8]>) {
56 match r {
57 Ok(v) => {
58 print!("{} ok ", tag);
59 for b in v.iter() {
60 print!("{:02x}", b);
61 }
62 println!(" len={}", v.len());
63 }
64 Err(e) => println!("{} err {:?} / {}", tag, e, e),
65 }
66}
67
68fn main() {
69 let mut buf = [0u8; 16];
70
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago71 show("lower", lower::decode(b"abcd1234", &mut buf));
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago72 show("mixed-l", mixed::decode(b"abcd1234", &mut buf));
73 show("mixed-u", mixed::decode(b"ABCD1234", &mut buf));
74 show("mixed-m", mixed::decode(b"abCD1234", &mut buf));
75 show("edge", mixed::decode(b"00ff7f80", &mut buf));
76 show("oddlen", mixed::decode(b"abc", &mut buf));
77 show("bad", mixed::decode(b"zzzz", &mut buf));
78 show("empty", mixed::decode(b"", &mut buf));
79
80 let mut small = [0u8; 2];
81 show("short-dst", mixed::decode(b"abcd1234", &mut small));
82
Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago83 show("upper", upper::decode(b"ABCD1234", &mut buf));
84 show("upper-rej", upper::decode(b"abcd1234", &mut buf));
85
86 let mut enc = [0u8; 8];
87 show("encode", lower::encode(b"\xab\xcd\x12\x34", &mut enc));
88 let mut encu = [0u8; 8];
89 show("encode-up", upper::encode(b"\xab\xcd\x12\x34", &mut encu));
90
91 // `encode_str` is a closure over an `unsafe` block returning a borrowed
92 // `&str` -- a view of the bytes just written, not a copy.
93 let mut enc2 = [0u8; 8];
94 match lower::encode_str(b"\xab\xcd\x12\x34", &mut enc2) {
95 Ok(s) => println!("encode_str ok {} len={}", s, s.len()),
96 Err(e) => println!("encode_str err {:?}", e),
97 }
98 let mut tiny = [0u8; 2];
99 match lower::encode_str(b"\xab\xcd", &mut tiny) {
100 Ok(s) => println!("tiny ok {}", s),
101 Err(e) => println!("tiny err {:?} / {}", e, e),
102 }
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago103
104 println!("{} {}", decoded_len(b"abcd").unwrap(), encoded_len(b"\xab\xcd"));
105}