#[derive(Clone, Debug)]
pub struct Latin1Indices<'a> {
front_offset: usize,
iter: &'a [u8],
}
impl<'a> Latin1Indices<'a> {
pub fn new(input: &'a [u8]) -> Self {
Self {
front_offset: 0,
iter: input,
}
}
}
impl<'a> Iterator for Latin1Indices<'a> {
type Item = (usize, u8);
#[inline]
fn next(&mut self) -> Option<(usize, u8)> {
self.iter.get(self.front_offset).map(|ch| {
self.front_offset += 1;
(self.front_offset - 1, *ch)
})
}
}
#[derive(Clone, Debug)]
pub struct Utf16Indices<'a> {
front_offset: usize,
iter: &'a [u16],
}
impl<'a> Utf16Indices<'a> {
pub fn new(input: &'a [u16]) -> Self {
Self {
front_offset: 0,
iter: input,
}
}
}
impl<'a> Iterator for Utf16Indices<'a> {
type Item = (usize, u32);
#[inline]
fn next(&mut self) -> Option<(usize, u32)> {
let (index, ch) = self.iter.get(self.front_offset).map(|ch| {
self.front_offset += 1;
(self.front_offset - 1, *ch)
})?;
let mut ch = ch as u32;
if (ch & 0xfc00) != 0xd800 {
return Some((index, ch));
}
if let Some(next) = self.iter.get(self.front_offset) {
let next = *next as u32;
if (next & 0xfc00) == 0xdc00 {
ch = ((ch & 0x3ff) << 10) + (next & 0x3ff) + 0x10000;
self.front_offset += 1;
}
}
Some((index, ch))
}
}
#[cfg(test)]
mod tests {
use crate::indices::*;
#[test]
fn latin1_indices() {
let latin1 = [0x30, 0x31, 0x32];
let mut indices = Latin1Indices::new(&latin1);
let n = indices.next().unwrap();
assert_eq!(n.0, 0);
assert_eq!(n.1, 0x30);
let n = indices.next().unwrap();
assert_eq!(n.0, 1);
assert_eq!(n.1, 0x31);
let n = indices.next().unwrap();
assert_eq!(n.0, 2);
assert_eq!(n.1, 0x32);
let n = indices.next();
assert_eq!(n, None);
}
#[test]
fn utf16_indices() {
let utf16 = [0xd83d, 0xde03, 0x0020, 0xd83c, 0xdf00, 0xd800, 0x0020];
let mut indices = Utf16Indices::new(&utf16);
let n = indices.next().unwrap();
assert_eq!(n.0, 0);
assert_eq!(n.1, 0x1f603);
let n = indices.next().unwrap();
assert_eq!(n.0, 2);
assert_eq!(n.1, 0x20);
let n = indices.next().unwrap();
assert_eq!(n.0, 3);
assert_eq!(n.1, 0x1f300);
let n = indices.next().unwrap();
assert_eq!(n.0, 5);
assert_eq!(n.1, 0xd800);
let n = indices.next().unwrap();
assert_eq!(n.0, 6);
assert_eq!(n.1, 0x0020);
let n = indices.next();
assert_eq!(n, None);
}
}