-
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathutf16.rs
More file actions
65 lines (52 loc) · 1.73 KB
/
utf16.rs
File metadata and controls
65 lines (52 loc) · 1.73 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
use super::ReadChar;
use std::io;
/// Input for UTF-16 encoded sources.
#[derive(Debug)]
pub struct UTF16Input<'a> {
input: &'a [u16],
index: usize,
}
impl<'a> UTF16Input<'a> {
/// Creates a new `UTF16Input` from a UTF-16 encoded slice e.g. <code>[&\[u16\]][slice]</code>.
///
/// [slice]: std::slice
#[must_use]
pub const fn new(input: &'a [u16]) -> Self {
Self { input, index: 0 }
}
}
impl ReadChar for UTF16Input<'_> {
/// Retrieves the next unchecked char in u32 code point.
fn next_char(&mut self) -> io::Result<Option<u32>> {
let Some(u1) = self.input.get(self.index).copied() else {
return Ok(None);
};
self.index += 1;
// If the code unit is not a high surrogate, it is not the start of a surrogate pair.
if !is_high_surrogate(u1) {
return Ok(Some(u1.into()));
}
let Some(u2) = self.input.get(self.index).copied() else {
return Ok(Some(u1.into()));
};
// If the code unit is not a low surrogate, it is not a surrogate pair.
if !is_low_surrogate(u2) {
return Ok(Some(u1.into()));
}
self.index += 1;
Ok(Some(code_point_from_surrogates(u1, u2)))
}
}
const SURROGATE_HIGH_START: u16 = 0xD800;
const SURROGATE_HIGH_END: u16 = 0xDBFF;
const SURROGATE_LOW_START: u16 = 0xDC00;
const SURROGATE_LOW_END: u16 = 0xDFFF;
fn is_high_surrogate(b: u16) -> bool {
(SURROGATE_HIGH_START..=SURROGATE_HIGH_END).contains(&b)
}
fn is_low_surrogate(b: u16) -> bool {
(SURROGATE_LOW_START..=SURROGATE_LOW_END).contains(&b)
}
fn code_point_from_surrogates(high: u16, low: u16) -> u32 {
(((u32::from(high & 0x3ff)) << 10) | u32::from(low & 0x3ff)) + 0x1_0000
}