|
| 1 | +//! PrintableBinary — Rust implementation of the raw byte<->printable-UTF-8 codec. |
| 2 | +//! |
| 3 | +//! Produces byte-identical output to the Zig/C/Lua/JS implementations (same |
| 4 | +//! `character_map.txt`). Built for a Rust-GUI <-> Zig-core transport: the |
| 5 | +//! `*_into` variants reuse a caller-owned buffer for zero per-message allocation. |
| 6 | +include!(concat!(env!("OUT_DIR"), "/map.rs")); |
| 7 | + |
| 8 | +/// Worst-case encoded length: every byte maps to at most a 3-byte glyph. |
| 9 | +#[inline] |
| 10 | +pub fn encode_bound(len: usize) -> usize { |
| 11 | + len * 3 |
| 12 | +} |
| 13 | + |
| 14 | +/// Encode raw bytes to printable-binary UTF-8 (allocating). |
| 15 | +pub fn encode(input: &[u8]) -> Vec<u8> { |
| 16 | + let mut out = Vec::with_capacity(encode_bound(input.len())); |
| 17 | + encode_into(input, &mut out); |
| 18 | + out |
| 19 | +} |
| 20 | + |
| 21 | +/// Encode into a caller-owned buffer (cleared, then filled). Reuses the buffer's |
| 22 | +/// allocation across calls — zero allocations per message after warm-up. |
| 23 | +pub fn encode_into(input: &[u8], out: &mut Vec<u8>) { |
| 24 | + out.clear(); |
| 25 | + out.reserve(encode_bound(input.len())); |
| 26 | + for &b in input { |
| 27 | + let (off, len) = ENCODE[b as usize]; |
| 28 | + let (off, len) = (off as usize, len as usize); |
| 29 | + out.extend_from_slice(&MAP_DATA[off..off + len]); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[inline] |
| 34 | +fn utf8_len(b: u8) -> usize { |
| 35 | + if b < 0x80 { 1 } else if b < 0xE0 { 2 } else if b < 0xF0 { 3 } else { 4 } |
| 36 | +} |
| 37 | + |
| 38 | +/// Decode printable-binary UTF-8 back to raw bytes (allocating). Unrecognized |
| 39 | +/// sequences pass through unchanged, matching the other implementations. |
| 40 | +pub fn decode(input: &[u8]) -> Vec<u8> { |
| 41 | + let mut out = Vec::with_capacity(input.len()); |
| 42 | + decode_into(input, &mut out); |
| 43 | + out |
| 44 | +} |
| 45 | + |
| 46 | +/// Decode into a caller-owned buffer (cleared, then filled). |
| 47 | +pub fn decode_into(input: &[u8], out: &mut Vec<u8>) { |
| 48 | + out.clear(); |
| 49 | + let mut i = 0; |
| 50 | + while i < input.len() { |
| 51 | + let b0 = input[i]; |
| 52 | + let mut len = utf8_len(b0); |
| 53 | + if i + len > input.len() { |
| 54 | + len = input.len() - i; |
| 55 | + } |
| 56 | + let mut matched = false; |
| 57 | + match len { |
| 58 | + 1 => { |
| 59 | + let v = DECODE_1[b0 as usize]; |
| 60 | + if v >= 0 { |
| 61 | + out.push(v as u8); |
| 62 | + i += 1; |
| 63 | + matched = true; |
| 64 | + } |
| 65 | + } |
| 66 | + 2 => { |
| 67 | + let v = DECODE_2[(b0 & 0x1F) as usize][(input[i + 1] & 0x3F) as usize]; |
| 68 | + if v >= 0 { |
| 69 | + out.push(v as u8); |
| 70 | + i += 2; |
| 71 | + matched = true; |
| 72 | + } |
| 73 | + } |
| 74 | + 3 => { |
| 75 | + let key = ((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8) | input[i + 2] as u32; |
| 76 | + if let Ok(idx) = DECODE_3.binary_search_by_key(&key, |&(k, _)| k) { |
| 77 | + out.push(DECODE_3[idx].1); |
| 78 | + i += 3; |
| 79 | + matched = true; |
| 80 | + } |
| 81 | + } |
| 82 | + _ => {} |
| 83 | + } |
| 84 | + if !matched { |
| 85 | + out.extend_from_slice(&input[i..i + len]); |
| 86 | + i += len; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn round_trip_all_256_single_bytes() { |
| 97 | + for b in 0u16..256 { |
| 98 | + let b = b as u8; |
| 99 | + let dec = decode(&encode(&[b])); |
| 100 | + assert_eq!(dec, vec![b], "byte {b:#04x} did not round-trip"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn round_trip_mixed_buffer() { |
| 106 | + let mut data: Vec<u8> = (0u16..256).map(|x| x as u8).collect(); |
| 107 | + data.extend_from_slice(b"Hello, World!\x00\xff\n\t"); |
| 108 | + assert_eq!(decode(&encode(&data)), data); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn zero_alloc_into_matches_allocating() { |
| 113 | + let data = b"transport \x00\x01\xfe\xff frame"; |
| 114 | + let mut ebuf = Vec::new(); |
| 115 | + encode_into(data, &mut ebuf); |
| 116 | + assert_eq!(ebuf, encode(data)); |
| 117 | + let mut dbuf = Vec::new(); |
| 118 | + decode_into(&ebuf, &mut dbuf); |
| 119 | + assert_eq!(dbuf, data.to_vec()); |
| 120 | + } |
| 121 | +} |
0 commit comments