-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf32le.rs
More file actions
285 lines (243 loc) · 8 KB
/
utf32le.rs
File metadata and controls
285 lines (243 loc) · 8 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use alloc::vec::Vec;
use crate::encoding::Encoding;
use crate::error::EncodingError;
/// UTF-32 Little Endian encoding marker.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Utf32Le;
// Surrogate range constants (invalid in UTF-32)
const SURROGATE_START: u32 = 0xD800;
const SURROGATE_END: u32 = 0xDFFF;
const MAX_CODE_POINT: u32 = 0x10FFFF;
impl Encoding for Utf32Le {
const NAME: &'static str = "UTF-32LE";
const IS_FIXED_WIDTH: bool = true;
const BYTES_PER_CHAR: Option<usize> = Some(4);
const MAX_CHAR_LEN: usize = 4;
const NULL_LEN: usize = 4;
fn validate(bytes: &[u8]) -> Result<(), EncodingError> {
// Must have length divisible by 4
if bytes.len() % 4 != 0 {
return Err(EncodingError::new(
bytes.len() - (bytes.len() % 4),
Some(bytes.len() % 4),
));
}
let mut offset = 0;
while offset < bytes.len() {
let cp = u32::from_le_bytes([
bytes[offset],
bytes[offset + 1],
bytes[offset + 2],
bytes[offset + 3],
]);
// Check for surrogate range (invalid in UTF-32)
if (SURROGATE_START..=SURROGATE_END).contains(&cp) {
return Err(EncodingError::new(offset, Some(4)));
}
// Check for out-of-range code point
if cp > MAX_CODE_POINT {
return Err(EncodingError::new(offset, Some(4)));
}
offset += 4;
}
Ok(())
}
fn decode_char_at(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset + 4 > bytes.len() {
return None;
}
let cp = u32::from_le_bytes([
bytes[offset],
bytes[offset + 1],
bytes[offset + 2],
bytes[offset + 3],
]);
let c = char::from_u32(cp)?;
Some((c, offset + 4))
}
fn encoded_len(_c: char) -> usize {
4
}
fn encode_char(c: char, buf: &mut [u8]) -> usize {
let bytes = (c as u32).to_le_bytes();
buf[0] = bytes[0];
buf[1] = bytes[1];
buf[2] = bytes[2];
buf[3] = bytes[3];
4
}
fn is_char_boundary(bytes: &[u8], index: usize) -> bool {
index % 4 == 0 && index <= bytes.len()
}
fn decode_char_before(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset < 4 || offset > bytes.len() {
return None;
}
// Must be on 4-byte boundary
if offset % 4 != 0 {
return None;
}
let cp = u32::from_le_bytes([
bytes[offset - 4],
bytes[offset - 3],
bytes[offset - 2],
bytes[offset - 1],
]);
let c = char::from_u32(cp)?;
Some((c, offset - 4))
}
}
impl crate::encoding::UniversalEncoding for Utf32Le {}
// === Registry registration ===
#[cfg(feature = "registry")]
inventory::submit! {
crate::registry::EncodingEntry {
name: "UTF-32LE",
aliases: &["UTF32LE", "utf-32le", "utf32le", "UCS-4LE", "ucs-4le"],
is_unicode: true,
decode: |bytes| {
Utf32Le::validate(bytes)?;
let mut chars = Vec::new();
let mut offset = 0;
while let Some((c, next)) = Utf32Le::decode_char_at(bytes, offset) {
chars.push(c);
offset = next;
}
Ok(chars)
},
try_encode_char: |c| {
let mut buf = [0u8; 4];
let len = Utf32Le::encode_char(c, &mut buf);
Some(buf[..len].to_vec())
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_ascii() {
// "hello" in UTF-32LE
let bytes = [
0x68, 0x00, 0x00, 0x00, // 'h'
0x65, 0x00, 0x00, 0x00, // 'e'
0x6C, 0x00, 0x00, 0x00, // 'l'
0x6C, 0x00, 0x00, 0x00, // 'l'
0x6F, 0x00, 0x00, 0x00, // 'o'
];
assert!(Utf32Le::validate(&bytes).is_ok());
}
#[test]
fn test_validate_wrong_length() {
let bytes = [0x68, 0x00, 0x00]; // Only 3 bytes
assert!(Utf32Le::validate(&bytes).is_err());
let bytes = [0x68, 0x00, 0x00, 0x00, 0x65]; // 5 bytes
assert!(Utf32Le::validate(&bytes).is_err());
}
#[test]
fn test_validate_emoji() {
// U+1F600 (😀)
let bytes = [0x00, 0xF6, 0x01, 0x00];
assert!(Utf32Le::validate(&bytes).is_ok());
}
#[test]
fn test_validate_surrogate() {
// Surrogate code points are invalid in UTF-32
// U+D800 (high surrogate start)
let bytes = [0x00, 0xD8, 0x00, 0x00];
assert!(Utf32Le::validate(&bytes).is_err());
// U+DFFF (low surrogate end)
let bytes = [0xFF, 0xDF, 0x00, 0x00];
assert!(Utf32Le::validate(&bytes).is_err());
}
#[test]
fn test_validate_out_of_range() {
// Code points above U+10FFFF are invalid
let bytes = [0x00, 0x00, 0x11, 0x00]; // U+110000
assert!(Utf32Le::validate(&bytes).is_err());
}
#[test]
fn test_decode_ascii() {
let bytes = [0x68, 0x00, 0x00, 0x00]; // 'h'
let (c, next) = Utf32Le::decode_char_at(&bytes, 0).unwrap();
assert_eq!(c, 'h');
assert_eq!(next, 4);
}
#[test]
fn test_decode_emoji() {
// U+1F600 (😀)
let bytes = [0x00, 0xF6, 0x01, 0x00];
let (c, next) = Utf32Le::decode_char_at(&bytes, 0).unwrap();
assert_eq!(c, '😀');
assert_eq!(next, 4);
}
#[test]
fn test_encode_ascii() {
let mut buf = [0u8; 4];
let len = Utf32Le::encode_char('h', &mut buf);
assert_eq!(len, 4);
assert_eq!(&buf, &[0x68, 0x00, 0x00, 0x00]);
}
#[test]
fn test_encode_emoji() {
let mut buf = [0u8; 4];
let len = Utf32Le::encode_char('😀', &mut buf);
assert_eq!(len, 4);
assert_eq!(&buf, &[0x00, 0xF6, 0x01, 0x00]);
}
#[test]
fn test_is_char_boundary() {
// "hi" in UTF-32LE
let bytes = [
0x68, 0x00, 0x00, 0x00, // 'h'
0x69, 0x00, 0x00, 0x00, // 'i'
];
assert!(Utf32Le::is_char_boundary(&bytes, 0));
assert!(!Utf32Le::is_char_boundary(&bytes, 1));
assert!(!Utf32Le::is_char_boundary(&bytes, 2));
assert!(!Utf32Le::is_char_boundary(&bytes, 3));
assert!(Utf32Le::is_char_boundary(&bytes, 4));
assert!(!Utf32Le::is_char_boundary(&bytes, 5));
assert!(!Utf32Le::is_char_boundary(&bytes, 6));
assert!(!Utf32Le::is_char_boundary(&bytes, 7));
assert!(Utf32Le::is_char_boundary(&bytes, 8));
}
#[test]
fn test_decode_char_before() {
// "hi" in UTF-32LE
let bytes = [
0x68, 0x00, 0x00, 0x00, // 'h'
0x69, 0x00, 0x00, 0x00, // 'i'
];
let (c, start) = Utf32Le::decode_char_before(&bytes, 8).unwrap();
assert_eq!(c, 'i');
assert_eq!(start, 4);
let (c, start) = Utf32Le::decode_char_before(&bytes, 4).unwrap();
assert_eq!(c, 'h');
assert_eq!(start, 0);
}
#[test]
fn test_roundtrip_all_bmp() {
let mut buf = [0u8; 4];
for cp in 0u32..0x10000 {
// Skip surrogates
if (0xD800..=0xDFFF).contains(&cp) {
continue;
}
let c = char::from_u32(cp).unwrap();
let len = Utf32Le::encode_char(c, &mut buf);
let (decoded, _) = Utf32Le::decode_char_at(&buf[..len], 0).unwrap();
assert_eq!(decoded, c, "roundtrip failed for U+{:04X}", cp);
}
}
#[test]
fn test_roundtrip_supplementary() {
let mut buf = [0u8; 4];
for cp in 0x10000u32..=0x10FFFF {
let c = char::from_u32(cp).unwrap();
let len = Utf32Le::encode_char(c, &mut buf);
let (decoded, _) = Utf32Le::decode_char_at(&buf[..len], 0).unwrap();
assert_eq!(decoded, c, "roundtrip failed for U+{:04X}", cp);
}
}
}