cesu8_str/java/
iter.rs

1use core::iter::FusedIterator;
2
3use crate::internal::{InternalCharIndices, InternalChars};
4
5use super::JavaStr;
6
7/// An iterator over the [`char`]s of a Java CESU-8 string slice.
8///
9/// This struct is created by the `chars` method on the `JavaStr`. See its
10/// documentation for more detail.
11///
12/// [`chars`]: JavaStr::chars
13#[derive(Debug, Clone)]
14#[must_use = "iterators are lazy and do nothing unless consumed"]
15pub struct JavaChars<'a> {
16    pub(crate) iter: InternalChars<'a>,
17}
18
19impl<'a> JavaChars<'a> {
20    /// Views the underlying string as a subslice of the original string.
21    #[inline]
22    #[must_use]
23    pub fn as_str(&self) -> &JavaStr {
24        // SAFETY: The bytes come from a JavaStr, so they must be in a valid format.
25        unsafe { JavaStr::from_java_cesu8_unchecked(self.iter.as_bytes()) }
26    }
27}
28
29impl Iterator for JavaChars<'_> {
30    type Item = char;
31
32    #[inline]
33    #[must_use]
34    fn next(&mut self) -> Option<Self::Item> {
35        self.iter.next()
36    }
37
38    #[inline]
39    fn size_hint(&self) -> (usize, Option<usize>) {
40        self.iter.size_hint()
41    }
42
43    #[inline]
44    fn last(self) -> Option<char> {
45        self.iter.last()
46    }
47}
48
49impl DoubleEndedIterator for JavaChars<'_> {
50    #[inline]
51    fn next_back(&mut self) -> Option<Self::Item> {
52        self.iter.next_back()
53    }
54}
55
56impl FusedIterator for JavaChars<'_> {}
57
58/// An iterator over the [`char`]s of a Java CESU-8 string slice, and their
59/// positions.
60///
61/// This struct is created by the `char_indices` method on a `JavaStr`. See its
62/// documentation for more detail.
63#[derive(Debug, Clone)]
64#[must_use = "iterators are lazy and do nothing unless consumed"]
65pub struct JavaCharIndices<'a> {
66    pub(crate) iter: InternalCharIndices<'a>,
67}
68
69impl<'a> JavaCharIndices<'a> {
70    /// Returns the byte position of the next character, or
71    /// the length of the underlying string if there are no
72    /// more characters.
73    #[inline]
74    #[must_use]
75    pub fn offset(&self) -> usize {
76        self.iter.offset()
77    }
78
79    /// Views the underlying string as a subslice of the original string.
80    #[inline]
81    #[must_use]
82    pub fn as_str(&self) -> &JavaStr {
83        // SAFETY: The bytes come from a JavaStr, so they must be in a valid format.
84        unsafe { JavaStr::from_java_cesu8_unchecked(self.iter.as_bytes()) }
85    }
86}
87
88impl Iterator for JavaCharIndices<'_> {
89    type Item = (usize, char);
90
91    #[inline]
92    fn next(&mut self) -> Option<(usize, char)> {
93        self.iter.next()
94    }
95
96    #[inline]
97    fn size_hint(&self) -> (usize, Option<usize>) {
98        self.iter.size_hint()
99    }
100
101    #[inline]
102    fn last(self) -> Option<Self::Item> {
103        self.iter.last()
104    }
105}
106
107impl DoubleEndedIterator for JavaCharIndices<'_> {
108    #[inline]
109    fn next_back(&mut self) -> Option<Self::Item> {
110        self.iter.next_back()
111    }
112}
113
114impl FusedIterator for JavaCharIndices<'_> {}