use super::ReadChar;
use std::io;
#[derive(Debug)]
pub struct UTF16Input<'a> {
input: &'a [u16],
index: usize,
}
impl<'a> UTF16Input<'a> {
#[must_use]
pub const fn new(input: &'a [u16]) -> Self {
Self { input, index: 0 }
}
#[cold]
fn handle_surrogate_pair(&mut self, u1: u16) -> u32 {
let Some(u2) = self.input.get(self.index).copied() else {
return u1.into();
};
if !is_low_surrogate(u2) {
return u1.into();
}
self.index += 1;
code_point_from_surrogates(u1, u2)
}
}
impl ReadChar for UTF16Input<'_> {
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 !is_high_surrogate(u1) {
return Ok(Some(u1.into()));
}
Ok(Some(self.handle_surrogate_pair(u1)))
}
}
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
}