1use core::iter::FusedIterator;
2
3use crate::internal::{InternalCharIndices, InternalChars};
4
5use super::JavaStr;
6
7#[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 #[inline]
22 #[must_use]
23 pub fn as_str(&self) -> &JavaStr {
24 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#[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 #[inline]
74 #[must_use]
75 pub fn offset(&self) -> usize {
76 self.iter.offset()
77 }
78
79 #[inline]
81 #[must_use]
82 pub fn as_str(&self) -> &JavaStr {
83 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<'_> {}