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
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use core::mem::MaybeUninit;
use crate::auto::*;
use crate::{AsciiCase, Error, OutBuf, ERROR};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
pub use simd_abstraction::common::hex::Hex;
#[cfg(feature = "alloc")]
use simd_abstraction::tools::alloc_uninit_bytes;
#[cfg(feature = "alloc")]
#[inline]
pub fn encode_to_boxed_str(src: &[u8], case: AsciiCase) -> Box<str> {
use core::slice;
use core::str;
if src.is_empty() {
return Box::from("");
}
unsafe {
assert!(src.len() * 2 <= (isize::MAX as usize));
let mut uninit_buf = alloc_uninit_bytes(src.len() * 2);
encode(src, OutBuf::uninit(&mut *uninit_buf), case).unwrap();
let len = uninit_buf.len();
let ptr = Box::into_raw(uninit_buf).cast::<u8>();
let buf = slice::from_raw_parts_mut(ptr, len);
Box::from_raw(str::from_utf8_unchecked_mut(buf))
}
}
#[cfg(feature = "alloc")]
#[inline]
pub fn decode_to_boxed_bytes(src: &[u8]) -> Result<Box<[u8]>, Error> {
use core::slice;
if src.is_empty() {
return Ok(Box::from([]));
}
unsafe {
if src.len() % 2 != 0 {
return Err(ERROR);
}
let mut uninit_buf = alloc_uninit_bytes(src.len() / 2);
decode(src, OutBuf::uninit(&mut *uninit_buf))?;
let len = uninit_buf.len();
let ptr = Box::into_raw(uninit_buf).cast::<u8>();
let buf = slice::from_raw_parts_mut(ptr, len);
Ok(Box::from_raw(buf))
}
}
#[inline]
pub fn encode_as_str<'s, 'd>(
src: &'s [u8],
dst: OutBuf<'d>,
case: AsciiCase,
) -> Result<&'d mut str, Error> {
let ans = encode(src, dst, case)?;
Ok(unsafe { core::str::from_utf8_unchecked_mut(ans) })
}
#[test]
fn test_alloc() {
let src = "hello".as_bytes();
let ans = encode_to_boxed_str(src, AsciiCase::Lower);
assert_eq!(&*ans, "68656c6c6f");
let ans = decode_to_boxed_bytes(ans.as_bytes()).unwrap();
assert_eq!(&*ans, src);
}
#[test]
fn test_str() {
use core::mem::MaybeUninit;
let src = "hello";
let mut dst = [MaybeUninit::uninit(); 10];
let ans = {
let src = src.as_bytes();
let dst = OutBuf::uninit(&mut dst);
let case = AsciiCase::Lower;
encode_as_str(src, dst, case).unwrap()
};
assert_eq!(ans, "68656c6c6f");
}
#[inline]
pub fn encode_u64(src: u64, case: AsciiCase) -> Hex<16> {
unsafe {
let mut this: MaybeUninit<Hex<16>> = MaybeUninit::uninit();
let src = &src.to_be_bytes();
let dst: *mut u8 = this.as_mut_ptr().cast();
let table = match case {
AsciiCase::Lower => crate::fallback::FULL_LOWER_TABLE,
AsciiCase::Upper => crate::fallback::FULL_UPPER_TABLE,
};
crate::fallback::encode_unchecked(src, dst, table);
this.assume_init()
}
}
#[test]
fn test_hex_u64() {
let src = 0x1234_5678_9abc_def0;
let hex = encode_u64(src, AsciiCase::Lower);
assert_eq!(hex.as_str(), format!("{:x}", src));
let hex = encode_u64(src, AsciiCase::Upper);
assert_eq!(hex.as_str(), format!("{:X}", src));
}