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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{AppendHexDecode, AppendHexEncode, AsciiCase, Error, FromHexDecode, FromHexEncode};
use vsimd::tools::{alloc_uninit_bytes, assume_init, boxed_str, slice_parts};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
#[inline]
fn decode_to_boxed_bytes(src: &[u8]) -> Result<Box<[u8]>, Error> {
if src.is_empty() {
return Ok(Box::from([]));
}
ensure!(src.len() % 2 == 0);
unsafe {
let mut buf = alloc_uninit_bytes(src.len() / 2);
{
let (src, len) = slice_parts(src);
let dst: *mut u8 = buf.as_mut_ptr().cast();
crate::multiversion::decode::auto(src, len, dst)?;
}
Ok(assume_init(buf))
}
}
#[inline]
fn decode_append_vec(src: &[u8], buf: &mut Vec<u8>) -> Result<(), Error> {
if src.is_empty() {
return Ok(());
}
ensure!(src.len() % 2 == 0);
let m = src.len() / 2;
buf.reserve_exact(m);
let prev_len = buf.len();
unsafe {
let (src, len) = slice_parts(src);
let dst = buf.as_mut_ptr().add(prev_len);
crate::multiversion::decode::auto(src, len, dst)?;
buf.set_len(prev_len + m);
Ok(())
}
}
#[inline]
fn encode_to_boxed_str(src: &[u8], case: AsciiCase) -> Box<str> {
if src.is_empty() {
return Box::from("");
}
unsafe {
let m = src.len() * 2;
assert!(m <= usize::MAX / 2);
let mut buf = alloc_uninit_bytes(m);
{
let (src, len) = slice_parts(src);
let dst: *mut u8 = buf.as_mut_ptr().cast();
crate::multiversion::encode::auto(src, len, dst, case);
}
boxed_str(assume_init(buf))
}
}
#[inline]
fn encode_append_vec(src: &[u8], buf: &mut Vec<u8>, case: AsciiCase) {
if src.is_empty() {
return;
}
unsafe {
let m = src.len() * 2;
assert!(m <= usize::MAX / 2);
buf.reserve_exact(m);
let prev_len = buf.len();
let (src, len) = slice_parts(src);
let dst = buf.as_mut_ptr().add(prev_len);
crate::multiversion::encode::auto(src, len, dst, case);
buf.set_len(prev_len + m);
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexDecode for Box<[u8]> {
#[inline]
fn from_hex_decode(data: &[u8]) -> Result<Self, Error> {
decode_to_boxed_bytes(data)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexDecode for Vec<u8> {
#[inline]
fn from_hex_decode(data: &[u8]) -> Result<Self, Error> {
let ans = decode_to_boxed_bytes(data)?;
Ok(Vec::from(ans))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexEncode for Box<[u8]> {
#[inline]
#[must_use]
fn from_hex_encode(data: &[u8], case: AsciiCase) -> Self {
let ans = encode_to_boxed_str(data, case);
ans.into_boxed_bytes()
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexEncode for Box<str> {
#[inline]
#[must_use]
fn from_hex_encode(data: &[u8], case: AsciiCase) -> Self {
encode_to_boxed_str(data, case)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexEncode for Vec<u8> {
#[inline]
#[must_use]
fn from_hex_encode(data: &[u8], case: AsciiCase) -> Self {
let ans = encode_to_boxed_str(data, case);
Vec::from(ans.into_boxed_bytes())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromHexEncode for String {
#[inline]
#[must_use]
fn from_hex_encode(data: &[u8], case: AsciiCase) -> Self {
let ans = encode_to_boxed_str(data, case);
String::from(ans)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendHexEncode for Vec<u8> {
#[inline]
fn append_hex_encode(src: &[u8], dst: &mut Self, case: AsciiCase) {
encode_append_vec(src, dst, case);
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendHexEncode for String {
#[inline]
fn append_hex_encode(src: &[u8], dst: &mut Self, case: AsciiCase) {
unsafe { encode_append_vec(src, dst.as_mut_vec(), case) }
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendHexDecode for Vec<u8> {
#[inline]
fn append_hex_decode(src: &[u8], dst: &mut Self) -> Result<(), Error> {
decode_append_vec(src, dst)
}
}