1use super::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
4use super::validations::{next_code_point, next_code_point_reverse};
5use super::{
6 BytesIsNotEmpty, CharEscapeDebugContinue, CharEscapeDefault, CharEscapeUnicode,
7 IsAsciiWhitespace, IsNotEmpty, IsWhitespace, LinesMap, UnsafeBytesToStr, from_utf8_unchecked,
8};
9use crate::fmt::{self, Write};
10use crate::iter::{
11 Chain, Copied, Filter, FlatMap, Flatten, FusedIterator, Map, TrustedLen, TrustedRandomAccess,
12 TrustedRandomAccessNoCoerce,
13};
14use crate::num::NonZero;
15use crate::ops::Try;
16use crate::slice::{self, Split as SliceSplit};
17use crate::{char as char_mod, option};
18
19#[derive(Clone)]
28#[must_use = "iterators are lazy and do nothing unless consumed"]
29#[stable(feature = "rust1", since = "1.0.0")]
30pub struct Chars<'a> {
31 pub(super) iter: slice::Iter<'a, u8>,
32}
33
34#[stable(feature = "rust1", since = "1.0.0")]
35impl<'a> Iterator for Chars<'a> {
36 type Item = char;
37
38 #[inline]
39 fn next(&mut self) -> Option<char> {
40 unsafe { next_code_point(&mut self.iter).map(|ch| char::from_u32_unchecked(ch)) }
43 }
44
45 #[inline]
46 fn count(self) -> usize {
47 super::count::count_chars(self.as_str())
48 }
49
50 #[inline]
51 fn advance_by(&mut self, mut remainder: usize) -> Result<(), NonZero<usize>> {
52 const CHUNK_SIZE: usize = 32;
53
54 if remainder >= CHUNK_SIZE {
55 let mut chunks = self.iter.as_slice().as_chunks::<CHUNK_SIZE>().0.iter();
56 let mut bytes_skipped: usize = 0;
57
58 while remainder > CHUNK_SIZE
59 && let Some(chunk) = chunks.next()
60 {
61 bytes_skipped += CHUNK_SIZE;
62
63 let mut start_bytes = [false; CHUNK_SIZE];
64
65 for i in 0..CHUNK_SIZE {
66 start_bytes[i] = !super::validations::utf8_is_cont_byte(chunk[i]);
67 }
68
69 remainder -= start_bytes.into_iter().map(|i| i as u8).sum::<u8>() as usize;
70 }
71
72 unsafe { self.iter.advance_by(bytes_skipped).unwrap_unchecked() };
75
76 while self.iter.len() > 0 {
78 let b = self.iter.as_slice()[0];
79 if !super::validations::utf8_is_cont_byte(b) {
80 break;
81 }
82 unsafe { self.iter.advance_by(1).unwrap_unchecked() };
84 }
85 }
86
87 while (remainder > 0) && (self.iter.len() > 0) {
88 remainder -= 1;
89 let b = self.iter.as_slice()[0];
90 let slurp = super::validations::utf8_char_width(b);
91 unsafe { self.iter.advance_by(slurp).unwrap_unchecked() };
94 }
95
96 NonZero::new(remainder).map_or(Ok(()), Err)
97 }
98
99 #[inline]
100 fn size_hint(&self) -> (usize, Option<usize>) {
101 let len = self.iter.len();
102 (len.div_ceil(4), Some(len))
103 }
104
105 #[inline]
106 fn last(mut self) -> Option<char> {
107 self.next_back()
109 }
110}
111
112#[stable(feature = "chars_debug_impl", since = "1.38.0")]
113impl fmt::Debug for Chars<'_> {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 write!(f, "Chars(")?;
116 f.debug_list().entries(self.clone()).finish()?;
117 write!(f, ")")?;
118 Ok(())
119 }
120}
121
122#[stable(feature = "rust1", since = "1.0.0")]
123impl<'a> DoubleEndedIterator for Chars<'a> {
124 #[inline]
125 fn next_back(&mut self) -> Option<char> {
126 unsafe { next_code_point_reverse(&mut self.iter).map(|ch| char::from_u32_unchecked(ch)) }
129 }
130}
131
132#[stable(feature = "fused", since = "1.26.0")]
133impl FusedIterator for Chars<'_> {}
134
135impl<'a> Chars<'a> {
136 #[stable(feature = "iter_to_slice", since = "1.4.0")]
154 #[must_use]
155 #[inline]
156 pub fn as_str(&self) -> &'a str {
157 unsafe { from_utf8_unchecked(self.iter.as_slice()) }
159 }
160}
161
162#[derive(Clone, Debug)]
170#[must_use = "iterators are lazy and do nothing unless consumed"]
171#[stable(feature = "rust1", since = "1.0.0")]
172pub struct CharIndices<'a> {
173 pub(super) front_offset: usize,
174 pub(super) iter: Chars<'a>,
175}
176
177#[stable(feature = "rust1", since = "1.0.0")]
178impl<'a> Iterator for CharIndices<'a> {
179 type Item = (usize, char);
180
181 #[inline]
182 fn next(&mut self) -> Option<(usize, char)> {
183 let pre_len = self.iter.iter.len();
184 match self.iter.next() {
185 None => None,
186 Some(ch) => {
187 let index = self.front_offset;
188 let len = self.iter.iter.len();
189 self.front_offset += pre_len - len;
190 Some((index, ch))
191 }
192 }
193 }
194
195 #[inline]
196 fn count(self) -> usize {
197 self.iter.count()
198 }
199
200 #[inline]
201 fn size_hint(&self) -> (usize, Option<usize>) {
202 self.iter.size_hint()
203 }
204
205 #[inline]
206 fn last(mut self) -> Option<(usize, char)> {
207 self.next_back()
209 }
210}
211
212#[stable(feature = "rust1", since = "1.0.0")]
213impl<'a> DoubleEndedIterator for CharIndices<'a> {
214 #[inline]
215 fn next_back(&mut self) -> Option<(usize, char)> {
216 self.iter.next_back().map(|ch| {
217 let index = self.front_offset + self.iter.iter.len();
218 (index, ch)
219 })
220 }
221}
222
223#[stable(feature = "fused", since = "1.26.0")]
224impl FusedIterator for CharIndices<'_> {}
225
226impl<'a> CharIndices<'a> {
227 #[stable(feature = "iter_to_slice", since = "1.4.0")]
232 #[must_use]
233 #[inline]
234 pub fn as_str(&self) -> &'a str {
235 self.iter.as_str()
236 }
237
238 #[inline]
268 #[must_use]
269 #[stable(feature = "char_indices_offset", since = "1.82.0")]
270 pub fn offset(&self) -> usize {
271 self.front_offset
272 }
273}
274
275#[must_use = "iterators are lazy and do nothing unless consumed"]
282#[stable(feature = "rust1", since = "1.0.0")]
283#[derive(Clone, Debug)]
284pub struct Bytes<'a>(pub(super) Copied<slice::Iter<'a, u8>>);
285
286#[stable(feature = "rust1", since = "1.0.0")]
287impl Iterator for Bytes<'_> {
288 type Item = u8;
289
290 #[inline]
291 fn next(&mut self) -> Option<u8> {
292 self.0.next()
293 }
294
295 #[inline]
296 fn size_hint(&self) -> (usize, Option<usize>) {
297 self.0.size_hint()
298 }
299
300 #[inline]
301 fn count(self) -> usize {
302 self.0.count()
303 }
304
305 #[inline]
306 fn last(self) -> Option<Self::Item> {
307 self.0.last()
308 }
309
310 #[inline]
311 fn nth(&mut self, n: usize) -> Option<Self::Item> {
312 self.0.nth(n)
313 }
314
315 #[inline]
316 fn all<F>(&mut self, f: F) -> bool
317 where
318 F: FnMut(Self::Item) -> bool,
319 {
320 self.0.all(f)
321 }
322
323 #[inline]
324 fn any<F>(&mut self, f: F) -> bool
325 where
326 F: FnMut(Self::Item) -> bool,
327 {
328 self.0.any(f)
329 }
330
331 #[inline]
332 fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
333 where
334 P: FnMut(&Self::Item) -> bool,
335 {
336 self.0.find(predicate)
337 }
338
339 #[inline]
340 fn position<P>(&mut self, predicate: P) -> Option<usize>
341 where
342 P: FnMut(Self::Item) -> bool,
343 {
344 self.0.position(predicate)
345 }
346
347 #[inline]
348 fn rposition<P>(&mut self, predicate: P) -> Option<usize>
349 where
350 P: FnMut(Self::Item) -> bool,
351 {
352 self.0.rposition(predicate)
353 }
354
355 #[inline]
356 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> u8 {
357 unsafe { self.0.__iterator_get_unchecked(idx) }
360 }
361}
362
363#[stable(feature = "rust1", since = "1.0.0")]
364impl DoubleEndedIterator for Bytes<'_> {
365 #[inline]
366 fn next_back(&mut self) -> Option<u8> {
367 self.0.next_back()
368 }
369
370 #[inline]
371 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
372 self.0.nth_back(n)
373 }
374
375 #[inline]
376 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
377 where
378 P: FnMut(&Self::Item) -> bool,
379 {
380 self.0.rfind(predicate)
381 }
382}
383
384#[stable(feature = "rust1", since = "1.0.0")]
385impl ExactSizeIterator for Bytes<'_> {
386 #[inline]
387 fn len(&self) -> usize {
388 self.0.len()
389 }
390
391 #[inline]
392 fn is_empty(&self) -> bool {
393 self.0.is_empty()
394 }
395}
396
397#[stable(feature = "fused", since = "1.26.0")]
398impl FusedIterator for Bytes<'_> {}
399
400#[unstable(feature = "trusted_len", issue = "37572")]
401unsafe impl TrustedLen for Bytes<'_> {}
402
403#[doc(hidden)]
404#[unstable(feature = "trusted_random_access", issue = "none")]
405unsafe impl TrustedRandomAccess for Bytes<'_> {}
406
407#[doc(hidden)]
408#[unstable(feature = "trusted_random_access", issue = "none")]
409unsafe impl TrustedRandomAccessNoCoerce for Bytes<'_> {
410 const MAY_HAVE_SIDE_EFFECT: bool = false;
411}
412
413macro_rules! derive_pattern_clone {
416 (clone $t:ident with |$s:ident| $e:expr) => {
417 impl<'a, P> Clone for $t<'a, P>
418 where
419 P: Pattern<Searcher<'a>: Clone>,
420 {
421 fn clone(&self) -> Self {
422 let $s = self;
423 $e
424 }
425 }
426 };
427}
428
429macro_rules! generate_pattern_iterators {
468 {
469 forward:
471 $(#[$forward_iterator_attribute:meta])*
472 struct $forward_iterator:ident;
473
474 reverse:
476 $(#[$reverse_iterator_attribute:meta])*
477 struct $reverse_iterator:ident;
478
479 stability:
481 $(#[$common_stability_attribute:meta])*
482
483 internal:
485 $internal_iterator:ident yielding ($iterty:ty);
486
487 delegate $($t:tt)*
489 } => {
490 $(#[$forward_iterator_attribute])*
491 $(#[$common_stability_attribute])*
492 pub struct $forward_iterator<'a, P: Pattern>(pub(super) $internal_iterator<'a, P>);
493
494 $(#[$common_stability_attribute])*
495 impl<'a, P> fmt::Debug for $forward_iterator<'a, P>
496 where
497 P: Pattern<Searcher<'a>: fmt::Debug>,
498 {
499 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
500 f.debug_tuple(stringify!($forward_iterator))
501 .field(&self.0)
502 .finish()
503 }
504 }
505
506 $(#[$common_stability_attribute])*
507 impl<'a, P: Pattern> Iterator for $forward_iterator<'a, P> {
508 type Item = $iterty;
509
510 #[inline]
511 fn next(&mut self) -> Option<$iterty> {
512 self.0.next()
513 }
514 }
515
516 $(#[$common_stability_attribute])*
517 impl<'a, P> Clone for $forward_iterator<'a, P>
518 where
519 P: Pattern<Searcher<'a>: Clone>,
520 {
521 fn clone(&self) -> Self {
522 $forward_iterator(self.0.clone())
523 }
524 }
525
526 $(#[$reverse_iterator_attribute])*
527 $(#[$common_stability_attribute])*
528 pub struct $reverse_iterator<'a, P: Pattern>(pub(super) $internal_iterator<'a, P>);
529
530 $(#[$common_stability_attribute])*
531 impl<'a, P> fmt::Debug for $reverse_iterator<'a, P>
532 where
533 P: Pattern<Searcher<'a>: fmt::Debug>,
534 {
535 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
536 f.debug_tuple(stringify!($reverse_iterator))
537 .field(&self.0)
538 .finish()
539 }
540 }
541
542 $(#[$common_stability_attribute])*
543 impl<'a, P> Iterator for $reverse_iterator<'a, P>
544 where
545 P: Pattern<Searcher<'a>: ReverseSearcher<'a>>,
546 {
547 type Item = $iterty;
548
549 #[inline]
550 fn next(&mut self) -> Option<$iterty> {
551 self.0.next_back()
552 }
553 }
554
555 $(#[$common_stability_attribute])*
556 impl<'a, P> Clone for $reverse_iterator<'a, P>
557 where
558 P: Pattern<Searcher<'a>: Clone>,
559 {
560 fn clone(&self) -> Self {
561 $reverse_iterator(self.0.clone())
562 }
563 }
564
565 #[stable(feature = "fused", since = "1.26.0")]
566 impl<'a, P: Pattern> FusedIterator for $forward_iterator<'a, P> {}
567
568 #[stable(feature = "fused", since = "1.26.0")]
569 impl<'a, P> FusedIterator for $reverse_iterator<'a, P>
570 where
571 P: Pattern<Searcher<'a>: ReverseSearcher<'a>>,
572 {}
573
574 generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
575 $forward_iterator,
576 $reverse_iterator, $iterty);
577 };
578 {
579 double ended; with $(#[$common_stability_attribute:meta])*,
580 $forward_iterator:ident,
581 $reverse_iterator:ident, $iterty:ty
582 } => {
583 $(#[$common_stability_attribute])*
584 impl<'a, P> DoubleEndedIterator for $forward_iterator<'a, P>
585 where
586 P: Pattern<Searcher<'a>: DoubleEndedSearcher<'a>>,
587 {
588 #[inline]
589 fn next_back(&mut self) -> Option<$iterty> {
590 self.0.next_back()
591 }
592 }
593
594 $(#[$common_stability_attribute])*
595 impl<'a, P> DoubleEndedIterator for $reverse_iterator<'a, P>
596 where
597 P: Pattern<Searcher<'a>: DoubleEndedSearcher<'a>>,
598 {
599 #[inline]
600 fn next_back(&mut self) -> Option<$iterty> {
601 self.0.next()
602 }
603 }
604 };
605 {
606 single ended; with $(#[$common_stability_attribute:meta])*,
607 $forward_iterator:ident,
608 $reverse_iterator:ident, $iterty:ty
609 } => {}
610}
611
612derive_pattern_clone! {
613 clone SplitInternal
614 with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
615}
616
617pub(super) struct SplitInternal<'a, P: Pattern> {
618 pub(super) start: usize,
619 pub(super) end: usize,
620 pub(super) matcher: P::Searcher<'a>,
621 pub(super) allow_trailing_empty: bool,
622 pub(super) finished: bool,
623}
624
625impl<'a, P> fmt::Debug for SplitInternal<'a, P>
626where
627 P: Pattern<Searcher<'a>: fmt::Debug>,
628{
629 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
630 f.debug_struct("SplitInternal")
631 .field("start", &self.start)
632 .field("end", &self.end)
633 .field("matcher", &self.matcher)
634 .field("allow_trailing_empty", &self.allow_trailing_empty)
635 .field("finished", &self.finished)
636 .finish()
637 }
638}
639
640impl<'a, P: Pattern> SplitInternal<'a, P> {
641 #[inline]
642 fn get_end(&mut self) -> Option<&'a str> {
643 if !self.finished {
644 self.finished = true;
645
646 if self.allow_trailing_empty || self.end - self.start > 0 {
647 let string = unsafe { self.matcher.haystack().get_unchecked(self.start..self.end) };
649 return Some(string);
650 }
651 }
652
653 None
654 }
655
656 #[inline]
657 fn next(&mut self) -> Option<&'a str> {
658 if self.finished {
659 return None;
660 }
661
662 let haystack = self.matcher.haystack();
663 match self.matcher.next_match() {
664 Some((a, b)) => unsafe {
666 let elt = haystack.get_unchecked(self.start..a);
667 self.start = b;
668 Some(elt)
669 },
670 None => self.get_end(),
671 }
672 }
673
674 #[inline]
675 fn next_inclusive(&mut self) -> Option<&'a str> {
676 if self.finished {
677 return None;
678 }
679
680 let haystack = self.matcher.haystack();
681 match self.matcher.next_match() {
682 Some((_, b)) => unsafe {
686 let elt = haystack.get_unchecked(self.start..b);
687 self.start = b;
688 Some(elt)
689 },
690 None => self.get_end(),
691 }
692 }
693
694 #[inline]
695 fn next_back(&mut self) -> Option<&'a str>
696 where
697 P::Searcher<'a>: ReverseSearcher<'a>,
698 {
699 if self.finished {
700 return None;
701 }
702
703 if !self.allow_trailing_empty {
704 self.allow_trailing_empty = true;
705 match self.next_back() {
706 Some(elt) if !elt.is_empty() => return Some(elt),
707 _ => {
708 if self.finished {
709 return None;
710 }
711 }
712 }
713 }
714
715 let haystack = self.matcher.haystack();
716 match self.matcher.next_match_back() {
717 Some((a, b)) => unsafe {
719 let elt = haystack.get_unchecked(b..self.end);
720 self.end = a;
721 Some(elt)
722 },
723 None => unsafe {
725 self.finished = true;
726 Some(haystack.get_unchecked(self.start..self.end))
727 },
728 }
729 }
730
731 #[inline]
732 fn next_back_inclusive(&mut self) -> Option<&'a str>
733 where
734 P::Searcher<'a>: ReverseSearcher<'a>,
735 {
736 if self.finished {
737 return None;
738 }
739
740 if !self.allow_trailing_empty {
741 self.allow_trailing_empty = true;
742 match self.next_back_inclusive() {
743 Some(elt) if !elt.is_empty() => return Some(elt),
744 _ => {
745 if self.finished {
746 return None;
747 }
748 }
749 }
750 }
751
752 let haystack = self.matcher.haystack();
753 match self.matcher.next_match_back() {
754 Some((_, b)) => unsafe {
758 let elt = haystack.get_unchecked(b..self.end);
759 self.end = b;
760 Some(elt)
761 },
762 None => unsafe {
768 self.finished = true;
769 Some(haystack.get_unchecked(self.start..self.end))
770 },
771 }
772 }
773
774 #[inline]
775 fn remainder(&self) -> Option<&'a str> {
776 if self.finished {
778 return None;
779 }
780
781 Some(unsafe { self.matcher.haystack().get_unchecked(self.start..self.end) })
783 }
784}
785
786generate_pattern_iterators! {
787 forward:
788 struct Split;
792 reverse:
793 struct RSplit;
797 stability:
798 #[stable(feature = "rust1", since = "1.0.0")]
799 internal:
800 SplitInternal yielding (&'a str);
801 delegate double ended;
802}
803
804impl<'a, P: Pattern> Split<'a, P> {
805 #[inline]
821 #[unstable(feature = "str_split_remainder", issue = "77998")]
822 pub fn remainder(&self) -> Option<&'a str> {
823 self.0.remainder()
824 }
825}
826
827impl<'a, P: Pattern> RSplit<'a, P> {
828 #[inline]
844 #[unstable(feature = "str_split_remainder", issue = "77998")]
845 pub fn remainder(&self) -> Option<&'a str> {
846 self.0.remainder()
847 }
848}
849
850generate_pattern_iterators! {
851 forward:
852 struct SplitTerminator;
856 reverse:
857 struct RSplitTerminator;
861 stability:
862 #[stable(feature = "rust1", since = "1.0.0")]
863 internal:
864 SplitInternal yielding (&'a str);
865 delegate double ended;
866}
867
868impl<'a, P: Pattern> SplitTerminator<'a, P> {
869 #[inline]
885 #[unstable(feature = "str_split_remainder", issue = "77998")]
886 pub fn remainder(&self) -> Option<&'a str> {
887 self.0.remainder()
888 }
889}
890
891impl<'a, P: Pattern> RSplitTerminator<'a, P> {
892 #[inline]
908 #[unstable(feature = "str_split_remainder", issue = "77998")]
909 pub fn remainder(&self) -> Option<&'a str> {
910 self.0.remainder()
911 }
912}
913
914derive_pattern_clone! {
915 clone SplitNInternal
916 with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
917}
918
919pub(super) struct SplitNInternal<'a, P: Pattern> {
920 pub(super) iter: SplitInternal<'a, P>,
921 pub(super) count: usize,
923}
924
925impl<'a, P> fmt::Debug for SplitNInternal<'a, P>
926where
927 P: Pattern<Searcher<'a>: fmt::Debug>,
928{
929 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
930 f.debug_struct("SplitNInternal")
931 .field("iter", &self.iter)
932 .field("count", &self.count)
933 .finish()
934 }
935}
936
937impl<'a, P: Pattern> SplitNInternal<'a, P> {
938 #[inline]
939 fn next(&mut self) -> Option<&'a str> {
940 match self.count {
941 0 => None,
942 1 => {
943 self.count = 0;
944 self.iter.get_end()
945 }
946 _ => {
947 self.count -= 1;
948 self.iter.next()
949 }
950 }
951 }
952
953 #[inline]
954 fn next_back(&mut self) -> Option<&'a str>
955 where
956 P::Searcher<'a>: ReverseSearcher<'a>,
957 {
958 match self.count {
959 0 => None,
960 1 => {
961 self.count = 0;
962 self.iter.get_end()
963 }
964 _ => {
965 self.count -= 1;
966 self.iter.next_back()
967 }
968 }
969 }
970
971 #[inline]
972 fn remainder(&self) -> Option<&'a str> {
973 self.iter.remainder()
974 }
975}
976
977generate_pattern_iterators! {
978 forward:
979 struct SplitN;
983 reverse:
984 struct RSplitN;
988 stability:
989 #[stable(feature = "rust1", since = "1.0.0")]
990 internal:
991 SplitNInternal yielding (&'a str);
992 delegate single ended;
993}
994
995impl<'a, P: Pattern> SplitN<'a, P> {
996 #[inline]
1012 #[unstable(feature = "str_split_remainder", issue = "77998")]
1013 pub fn remainder(&self) -> Option<&'a str> {
1014 self.0.remainder()
1015 }
1016}
1017
1018impl<'a, P: Pattern> RSplitN<'a, P> {
1019 #[inline]
1035 #[unstable(feature = "str_split_remainder", issue = "77998")]
1036 pub fn remainder(&self) -> Option<&'a str> {
1037 self.0.remainder()
1038 }
1039}
1040
1041derive_pattern_clone! {
1042 clone MatchIndicesInternal
1043 with |s| MatchIndicesInternal(s.0.clone())
1044}
1045
1046pub(super) struct MatchIndicesInternal<'a, P: Pattern>(pub(super) P::Searcher<'a>);
1047
1048impl<'a, P> fmt::Debug for MatchIndicesInternal<'a, P>
1049where
1050 P: Pattern<Searcher<'a>: fmt::Debug>,
1051{
1052 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1053 f.debug_tuple("MatchIndicesInternal").field(&self.0).finish()
1054 }
1055}
1056
1057impl<'a, P: Pattern> MatchIndicesInternal<'a, P> {
1058 #[inline]
1059 fn next(&mut self) -> Option<(usize, &'a str)> {
1060 self.0
1061 .next_match()
1062 .map(|(start, end)| unsafe { (start, self.0.haystack().get_unchecked(start..end)) })
1064 }
1065
1066 #[inline]
1067 fn next_back(&mut self) -> Option<(usize, &'a str)>
1068 where
1069 P::Searcher<'a>: ReverseSearcher<'a>,
1070 {
1071 self.0
1072 .next_match_back()
1073 .map(|(start, end)| unsafe { (start, self.0.haystack().get_unchecked(start..end)) })
1075 }
1076}
1077
1078generate_pattern_iterators! {
1079 forward:
1080 struct MatchIndices;
1084 reverse:
1085 struct RMatchIndices;
1089 stability:
1090 #[stable(feature = "str_match_indices", since = "1.5.0")]
1091 internal:
1092 MatchIndicesInternal yielding ((usize, &'a str));
1093 delegate double ended;
1094}
1095
1096derive_pattern_clone! {
1097 clone MatchesInternal
1098 with |s| MatchesInternal(s.0.clone())
1099}
1100
1101pub(super) struct MatchesInternal<'a, P: Pattern>(pub(super) P::Searcher<'a>);
1102
1103impl<'a, P> fmt::Debug for MatchesInternal<'a, P>
1104where
1105 P: Pattern<Searcher<'a>: fmt::Debug>,
1106{
1107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1108 f.debug_tuple("MatchesInternal").field(&self.0).finish()
1109 }
1110}
1111
1112impl<'a, P: Pattern> MatchesInternal<'a, P> {
1113 #[inline]
1114 fn next(&mut self) -> Option<&'a str> {
1115 self.0.next_match().map(|(a, b)| unsafe {
1117 self.0.haystack().get_unchecked(a..b)
1119 })
1120 }
1121
1122 #[inline]
1123 fn next_back(&mut self) -> Option<&'a str>
1124 where
1125 P::Searcher<'a>: ReverseSearcher<'a>,
1126 {
1127 self.0.next_match_back().map(|(a, b)| unsafe {
1129 self.0.haystack().get_unchecked(a..b)
1131 })
1132 }
1133}
1134
1135generate_pattern_iterators! {
1136 forward:
1137 struct Matches;
1141 reverse:
1142 struct RMatches;
1146 stability:
1147 #[stable(feature = "str_matches", since = "1.2.0")]
1148 internal:
1149 MatchesInternal yielding (&'a str);
1150 delegate double ended;
1151}
1152
1153#[stable(feature = "rust1", since = "1.0.0")]
1160#[must_use = "iterators are lazy and do nothing unless consumed"]
1161#[derive(Clone, Debug)]
1162pub struct Lines<'a>(pub(super) Map<SplitInclusive<'a, char>, LinesMap>);
1163
1164#[stable(feature = "rust1", since = "1.0.0")]
1165impl<'a> Iterator for Lines<'a> {
1166 type Item = &'a str;
1167
1168 #[inline]
1169 fn next(&mut self) -> Option<&'a str> {
1170 self.0.next()
1171 }
1172
1173 #[inline]
1174 fn size_hint(&self) -> (usize, Option<usize>) {
1175 self.0.size_hint()
1176 }
1177
1178 #[inline]
1179 fn last(mut self) -> Option<&'a str> {
1180 self.next_back()
1181 }
1182}
1183
1184#[stable(feature = "rust1", since = "1.0.0")]
1185impl<'a> DoubleEndedIterator for Lines<'a> {
1186 #[inline]
1187 fn next_back(&mut self) -> Option<&'a str> {
1188 self.0.next_back()
1189 }
1190}
1191
1192#[stable(feature = "fused", since = "1.26.0")]
1193impl FusedIterator for Lines<'_> {}
1194
1195impl<'a> Lines<'a> {
1196 #[inline]
1213 #[must_use]
1214 #[unstable(feature = "str_lines_remainder", issue = "77998")]
1215 pub fn remainder(&self) -> Option<&'a str> {
1216 self.0.iter.remainder()
1217 }
1218}
1219
1220#[stable(feature = "rust1", since = "1.0.0")]
1224#[deprecated(since = "1.4.0", note = "use lines()/Lines instead now")]
1225#[must_use = "iterators are lazy and do nothing unless consumed"]
1226#[derive(Clone, Debug)]
1227#[allow(deprecated)]
1228pub struct LinesAny<'a>(pub(super) Lines<'a>);
1229
1230#[stable(feature = "rust1", since = "1.0.0")]
1231#[allow(deprecated)]
1232impl<'a> Iterator for LinesAny<'a> {
1233 type Item = &'a str;
1234
1235 #[inline]
1236 fn next(&mut self) -> Option<&'a str> {
1237 self.0.next()
1238 }
1239
1240 #[inline]
1241 fn size_hint(&self) -> (usize, Option<usize>) {
1242 self.0.size_hint()
1243 }
1244}
1245
1246#[stable(feature = "rust1", since = "1.0.0")]
1247#[allow(deprecated)]
1248impl<'a> DoubleEndedIterator for LinesAny<'a> {
1249 #[inline]
1250 fn next_back(&mut self) -> Option<&'a str> {
1251 self.0.next_back()
1252 }
1253}
1254
1255#[stable(feature = "fused", since = "1.26.0")]
1256#[allow(deprecated)]
1257impl FusedIterator for LinesAny<'_> {}
1258
1259#[stable(feature = "split_whitespace", since = "1.1.0")]
1267#[derive(Clone, Debug)]
1268pub struct SplitWhitespace<'a> {
1269 pub(super) inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
1270}
1271
1272#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1280#[derive(Clone, Debug)]
1281pub struct SplitAsciiWhitespace<'a> {
1282 pub(super) inner:
1283 Map<Filter<SliceSplit<'a, u8, IsAsciiWhitespace>, BytesIsNotEmpty>, UnsafeBytesToStr>,
1284}
1285
1286#[stable(feature = "split_inclusive", since = "1.51.0")]
1296pub struct SplitInclusive<'a, P: Pattern>(pub(super) SplitInternal<'a, P>);
1297
1298#[stable(feature = "split_whitespace", since = "1.1.0")]
1299impl<'a> Iterator for SplitWhitespace<'a> {
1300 type Item = &'a str;
1301
1302 #[inline]
1303 fn next(&mut self) -> Option<&'a str> {
1304 self.inner.next()
1305 }
1306
1307 #[inline]
1308 fn size_hint(&self) -> (usize, Option<usize>) {
1309 self.inner.size_hint()
1310 }
1311
1312 #[inline]
1313 fn last(mut self) -> Option<&'a str> {
1314 self.next_back()
1315 }
1316}
1317
1318#[stable(feature = "split_whitespace", since = "1.1.0")]
1319impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
1320 #[inline]
1321 fn next_back(&mut self) -> Option<&'a str> {
1322 self.inner.next_back()
1323 }
1324}
1325
1326#[stable(feature = "fused", since = "1.26.0")]
1327impl FusedIterator for SplitWhitespace<'_> {}
1328
1329impl<'a> SplitWhitespace<'a> {
1330 #[inline]
1347 #[must_use]
1348 #[unstable(feature = "str_split_whitespace_remainder", issue = "77998")]
1349 pub fn remainder(&self) -> Option<&'a str> {
1350 self.inner.iter.remainder()
1351 }
1352}
1353
1354#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1355impl<'a> Iterator for SplitAsciiWhitespace<'a> {
1356 type Item = &'a str;
1357
1358 #[inline]
1359 fn next(&mut self) -> Option<&'a str> {
1360 self.inner.next()
1361 }
1362
1363 #[inline]
1364 fn size_hint(&self) -> (usize, Option<usize>) {
1365 self.inner.size_hint()
1366 }
1367
1368 #[inline]
1369 fn last(mut self) -> Option<&'a str> {
1370 self.next_back()
1371 }
1372}
1373
1374#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1375impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> {
1376 #[inline]
1377 fn next_back(&mut self) -> Option<&'a str> {
1378 self.inner.next_back()
1379 }
1380}
1381
1382#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1383impl FusedIterator for SplitAsciiWhitespace<'_> {}
1384
1385impl<'a> SplitAsciiWhitespace<'a> {
1386 #[inline]
1405 #[must_use]
1406 #[unstable(feature = "str_split_whitespace_remainder", issue = "77998")]
1407 pub fn remainder(&self) -> Option<&'a str> {
1408 if self.inner.iter.iter.finished {
1409 return None;
1410 }
1411
1412 Some(unsafe { crate::str::from_utf8_unchecked(&self.inner.iter.iter.v) })
1414 }
1415}
1416
1417#[stable(feature = "split_inclusive", since = "1.51.0")]
1418impl<'a, P: Pattern> Iterator for SplitInclusive<'a, P> {
1419 type Item = &'a str;
1420
1421 #[inline]
1422 fn next(&mut self) -> Option<&'a str> {
1423 self.0.next_inclusive()
1424 }
1425}
1426
1427#[stable(feature = "split_inclusive", since = "1.51.0")]
1428impl<'a, P: Pattern<Searcher<'a>: fmt::Debug>> fmt::Debug for SplitInclusive<'a, P> {
1429 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1430 f.debug_struct("SplitInclusive").field("0", &self.0).finish()
1431 }
1432}
1433
1434#[stable(feature = "split_inclusive", since = "1.51.0")]
1436impl<'a, P: Pattern<Searcher<'a>: Clone>> Clone for SplitInclusive<'a, P> {
1437 fn clone(&self) -> Self {
1438 SplitInclusive(self.0.clone())
1439 }
1440}
1441
1442#[stable(feature = "split_inclusive", since = "1.51.0")]
1443impl<'a, P: Pattern<Searcher<'a>: DoubleEndedSearcher<'a>>> DoubleEndedIterator
1444 for SplitInclusive<'a, P>
1445{
1446 #[inline]
1447 fn next_back(&mut self) -> Option<&'a str> {
1448 self.0.next_back_inclusive()
1449 }
1450}
1451
1452#[stable(feature = "split_inclusive", since = "1.51.0")]
1453impl<'a, P: Pattern> FusedIterator for SplitInclusive<'a, P> {}
1454
1455impl<'a, P: Pattern> SplitInclusive<'a, P> {
1456 #[inline]
1472 #[unstable(feature = "str_split_inclusive_remainder", issue = "77998")]
1473 pub fn remainder(&self) -> Option<&'a str> {
1474 self.0.remainder()
1475 }
1476}
1477
1478#[derive(Clone)]
1485#[stable(feature = "encode_utf16", since = "1.8.0")]
1486pub struct EncodeUtf16<'a> {
1487 pub(super) chars: Chars<'a>,
1488 pub(super) extra: u16,
1489}
1490
1491#[stable(feature = "collection_debug", since = "1.17.0")]
1492impl fmt::Debug for EncodeUtf16<'_> {
1493 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1494 f.debug_struct("EncodeUtf16").finish_non_exhaustive()
1495 }
1496}
1497
1498#[stable(feature = "encode_utf16", since = "1.8.0")]
1499impl<'a> Iterator for EncodeUtf16<'a> {
1500 type Item = u16;
1501
1502 #[inline]
1503 fn next(&mut self) -> Option<u16> {
1504 if self.extra != 0 {
1505 let tmp = self.extra;
1506 self.extra = 0;
1507 return Some(tmp);
1508 }
1509
1510 let mut buf = [0; 2];
1511 self.chars.next().map(|ch| {
1512 let n = ch.encode_utf16(&mut buf).len();
1513 if n == 2 {
1514 self.extra = buf[1];
1515 }
1516 buf[0]
1517 })
1518 }
1519
1520 #[inline]
1521 fn size_hint(&self) -> (usize, Option<usize>) {
1522 let len = self.chars.iter.len();
1523 if self.extra == 0 {
1529 (len.div_ceil(3), Some(len))
1530 } else {
1531 (len.div_ceil(3) + 1, Some(len + 1))
1534 }
1535 }
1536}
1537
1538#[stable(feature = "fused", since = "1.26.0")]
1539impl FusedIterator for EncodeUtf16<'_> {}
1540
1541#[stable(feature = "str_escape", since = "1.34.0")]
1543#[derive(Clone, Debug)]
1544pub struct EscapeDebug<'a> {
1545 pub(super) inner: Chain<
1546 Flatten<option::IntoIter<char_mod::EscapeDebug>>,
1547 FlatMap<Chars<'a>, char_mod::EscapeDebug, CharEscapeDebugContinue>,
1548 >,
1549}
1550
1551#[stable(feature = "str_escape", since = "1.34.0")]
1553#[derive(Clone, Debug)]
1554pub struct EscapeDefault<'a> {
1555 pub(super) inner: FlatMap<Chars<'a>, char_mod::EscapeDefault, CharEscapeDefault>,
1556}
1557
1558#[stable(feature = "str_escape", since = "1.34.0")]
1560#[derive(Clone, Debug)]
1561pub struct EscapeUnicode<'a> {
1562 pub(super) inner: FlatMap<Chars<'a>, char_mod::EscapeUnicode, CharEscapeUnicode>,
1563}
1564
1565macro_rules! escape_types_impls {
1566 ($( $Name: ident ),+) => {$(
1567 #[stable(feature = "str_escape", since = "1.34.0")]
1568 impl<'a> fmt::Display for $Name<'a> {
1569 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1570 self.clone().try_for_each(|c| f.write_char(c))
1571 }
1572 }
1573
1574 #[stable(feature = "str_escape", since = "1.34.0")]
1575 impl<'a> Iterator for $Name<'a> {
1576 type Item = char;
1577
1578 #[inline]
1579 fn next(&mut self) -> Option<char> { self.inner.next() }
1580
1581 #[inline]
1582 fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1583
1584 #[inline]
1585 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
1586 Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Output = Acc>
1587 {
1588 self.inner.try_fold(init, fold)
1589 }
1590
1591 #[inline]
1592 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
1593 where Fold: FnMut(Acc, Self::Item) -> Acc,
1594 {
1595 self.inner.fold(init, fold)
1596 }
1597 }
1598
1599 #[stable(feature = "str_escape", since = "1.34.0")]
1600 impl<'a> FusedIterator for $Name<'a> {}
1601 )+}
1602}
1603
1604escape_types_impls!(EscapeDebug, EscapeDefault, EscapeUnicode);