depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit d7e604f226a74e6a0b57382cedf687d651c3ca5e
parent c8831e052173e6eb637b33d95d6dacd22f7d3e39
Author: Antoine A <>
Date:   Wed,  1 Dec 2021 20:15:27 +0100

Remove internal enum
reduce packed char memory layout to a single u8

Diffstat:
Muri-pack/src/lib.rs | 158++++++++++++++-----------------------------------------------------------------
1 file changed, 28 insertions(+), 130 deletions(-)

diff --git a/uri-pack/src/lib.rs b/uri-pack/src/lib.rs @@ -1,139 +1,37 @@ -/// Reserved 5 bit code for extended packing -const EXTENDED: u8 = 31; - -/// Packed ascii as 5 or 11 bits -#[derive(Debug, Clone, Copy)] -pub enum Packed { - Simple(u8), // u5: 0..32 - Extended(u8), // u6: 0..54 -} - /// Pack an URI ascii char /// Panic if char not supported -fn pack_ascii(c: u8) -> Packed { - use Packed::{Extended as E, Simple as S}; +fn pack_ascii(c: u8) -> u8 { [ - E(63), - E(36), - E(37), - E(38), - E(39), - S(30), - E(40), - E(41), - E(42), - E(43), - E(44), - E(45), - E(46), - S(28), - S(26), - S(27), - E(26), - E(27), - E(28), - E(29), - E(30), - E(31), - E(32), - E(33), - E(34), - E(35), - E(47), - E(48), - E(49), - E(50), - E(51), - E(52), - E(53), - E(0), - E(1), - E(2), - E(3), - E(4), - E(5), - E(6), - E(7), - E(8), - E(9), - E(10), - E(11), - E(12), - E(13), - E(14), - E(15), - E(16), - E(17), - E(18), - E(19), - E(20), - E(21), - E(22), - E(23), - E(24), - E(25), - E(54), - E(55), - E(56), - E(57), - S(29), - E(58), - S(0), - S(1), - S(2), - S(3), - S(4), - S(5), - S(6), - S(7), - S(8), - S(9), - S(10), - S(11), - S(12), - S(13), - S(14), - S(15), - S(16), - S(17), - S(18), - S(19), - S(20), - S(21), - S(22), - S(23), - S(24), - S(25), - E(59), - E(60), - E(61), - E(62), + 94, 67, 68, 69, 70, 30, 71, 72, 73, 74, 75, 76, 77, 28, 26, 27, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 78, 79, 80, 81, 82, 83, 84, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 85, 86, 87, 88, 29, 89, 0, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 90, 91, 92, + 93, ][(c - b' ') as usize] } /// Unpack an URI ascii char /// Panic if char not supported -fn unpack_ascii(c: Packed) -> u8 { - match c { - Packed::Simple(c) => [ - b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', - b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'.', b'/', - b'-', b'_', b'%', - ][c as usize], - Packed::Extended(c) => [ - b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', - b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'0', b'1', - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'!', b'"', b'#', b'$', b'&', b'\'', - b'(', b')', b'*', b'+', b',', b':', b';', b'<', b'=', b'>', b'?', b'@', b'[', b'\\', - b']', b'^', b'`', b'{', b'|', b'}', b'~', b' ', - ][c as usize], - } +fn unpack_ascii(c: u8) -> u8 { + [ + b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', + b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'.', b'/', b'-', b'_', + b'%', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', + b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'0', b'1', b'2', + b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'!', b'"', b'#', b'$', b'&', b'\'', b'(', b')', + b'*', b'+', b',', b':', b';', b'<', b'=', b'>', b'?', b'@', b'[', b'\\', b']', b'^', b'`', + b'{', b'|', b'}', b'~', b' ', + ][c as usize] } + /// Check if an ascii char is packable fn supported_ascii(c: &u8) -> bool { (b' '..=b'~').contains(c) } +/// Extended packing limit +const EXTENDED: u8 = 31; + #[derive(Debug, Clone, Copy, thiserror::Error)] pub enum EncodeErr { #[error("{0} is not a valid uri char")] @@ -210,12 +108,12 @@ pub fn pack_uri(uri: &str) -> Result<(Vec<u8>, usize), EncodeErr> { }; for c in uri.bytes() { - match pack_ascii(c) { - Packed::Simple(nb) => write_bits(nb, 5), - Packed::Extended(nb) => { - write_bits(EXTENDED, 5); - write_bits(nb, 6); - } + let nb = pack_ascii(c); + if nb < EXTENDED { + write_bits(nb, 5) + } else { + write_bits(EXTENDED, 5); + write_bits(nb - EXTENDED, 6); } } @@ -279,8 +177,8 @@ pub fn unpack_uri(bytes: &[u8], len: usize) -> Result<String, DecodeErr> { for _ in 0..len { let encoded = match read_nb(5)? { - EXTENDED => Packed::Extended(read_nb(6)?), - nb => Packed::Simple(nb), + EXTENDED => read_nb(6)? + EXTENDED, + nb => nb, }; buf.push(unpack_ascii(encoded) as char); }