usesuper::Utf8Chars; use core::iter::FusedIterator;
/// An iterator over the [`char`]s and their positions. #[derive(Clone, Debug)] #[must_use = "iterators are lazy and do nothing unless consumed"] pubstruct Utf8CharIndices<'a> {
front_offset: usize,
iter: Utf8Chars<'a>,
}
impl<'a> Iterator for Utf8CharIndices<'a> { type Item = (usize, char);
#[inline] fn next(&mutself) -> Option<(usize, char)> { let pre_len = self.as_slice().len(); matchself.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] fn last(mutself) -> Option<(usize, char)> { // No need to go through the entire string. self.next_back()
}
}
impl<'a> DoubleEndedIterator for Utf8CharIndices<'a> { #[inline] fn next_back(&mutself) -> Option<(usize, char)> { self.iter.next_back().map(|ch| { let index = self.front_offset + self.as_slice().len();
(index, ch)
})
}
}
impl FusedIterator for Utf8CharIndices<'_> {}
impl<'a> Utf8CharIndices<'a> { #[inline(always)] /// Creates the iterator from a byte slice. pubfn new(bytes: &'a [u8]) -> Self {
Utf8CharIndices::<'a> {
front_offset: 0,
iter: Utf8Chars::new(bytes),
}
}
/// Views the underlying data as a subslice of the original data. /// /// This has the same lifetime as the original slice, and so the /// iterator can continue to be used while this exists. #[must_use] #[inline] pubfn as_slice(&self) -> &'a [u8] { self.iter.as_slice()
}
/// Returns the byte position of the next character, or the length /// of the underlying string if there are no more characters. /// /// # Examples /// /// ``` /// use utf8_iter::Utf8CharsEx; /// let mut chars = "a楽".as_bytes().char_indices(); /// /// assert_eq!(chars.offset(), 0); /// assert_eq!(chars.next(), Some((0, 'a'))); /// /// assert_eq!(chars.offset(), 1); /// assert_eq!(chars.next(), Some((1, '楽'))); /// /// assert_eq!(chars.offset(), 4); /// assert_eq!(chars.next(), None); /// ``` #[inline] #[must_use] pubfn offset(&self) -> usize { self.front_offset
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-22)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.