use crate::Utf16CharsWithHandler;
use crate::Utf16Handler;
use core::iter::FusedIterator;
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Utf16CharIndicesWithHandler<'a, H>
where
H: Utf16Handler,
{
front_offset: usize,
iter: Utf16CharsWithHandler<'a, H>,
}
impl<'a, H> Iterator for Utf16CharIndicesWithHandler<'a, H>
where
H: Utf16Handler,
{
type Item = (usize, H::Output);
#[inline(always)]
fn next(&mut self) -> Option<(usize, H::Output)> {
let pre_len = self.as_slice().len();
match self.iter.next() {
None => None,
Some(ch) => {
let index = self.front_offset;
let len = self.as_slice().len();
self.front_offset += pre_len - len;
Some((index, ch))
}
}
}
#[inline(always)]
fn count(self) -> usize {
self.iter.count()
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline(always)]
fn last(mut self) -> Option<(usize, H::Output)> {
self.next_back()
}
}
impl<'a, H> DoubleEndedIterator for Utf16CharIndicesWithHandler<'a, H>
where
H: Utf16Handler,
{
#[inline(always)]
fn next_back(&mut self) -> Option<(usize, H::Output)> {
self.iter.next_back().map(|ch| {
let index = self.front_offset + self.as_slice().len();
(index, ch)
})
}
}
impl<H> FusedIterator for Utf16CharIndicesWithHandler<'_, H> where H: Utf16Handler {}
impl<'a, H> Utf16CharIndicesWithHandler<'a, H>
where
H: Utf16Handler,
{
#[inline(always)]
pub fn new(code_units: &'a [u16], handler: H) -> Self {
Utf16CharIndicesWithHandler::<'a, H> {
front_offset: 0,
iter: Utf16CharsWithHandler::new(code_units, handler),
}
}
#[inline(always)]
pub fn handler(&self) -> &H {
self.iter.handler()
}
#[must_use]
#[inline(always)]
pub fn as_slice(&self) -> &'a [u16] {
self.iter.as_slice()
}
#[inline(always)]
#[must_use]
pub fn offset(&self) -> usize {
self.front_offset
}
}