|
1 | | -#[cfg(feature = "unstable")] |
2 | | -use std::str::pattern::{Pattern, Searcher}; |
3 | | - |
4 | 1 | #[derive(Copy, Clone, Debug, PartialEq)] |
5 | 2 | pub enum SplitType<'a> { |
6 | 3 | Match(&'a str), |
7 | 4 | Delimiter(&'a str), |
8 | 5 | } |
9 | 6 |
|
10 | | -#[cfg(feature = "unstable")] |
11 | | -pub struct SplitKeepingDelimiter<'p, P> |
12 | | -where |
13 | | - P: Pattern<'p>, |
14 | | -{ |
15 | | - searcher: P::Searcher, |
16 | | - start: usize, |
17 | | - saved: Option<usize>, |
18 | | -} |
19 | | - |
20 | | -#[cfg(feature = "unstable")] |
21 | | -impl<'p, P> Iterator for SplitKeepingDelimiter<'p, P> |
22 | | -where |
23 | | - P: Pattern<'p>, |
24 | | -{ |
25 | | - type Item = SplitType<'p>; |
26 | | - |
27 | | - fn next(&mut self) -> Option<SplitType<'p>> { |
28 | | - if self.start == self.searcher.haystack().len() { |
29 | | - return None; |
30 | | - } |
31 | | - |
32 | | - if let Some(end_of_match) = self.saved.take() { |
33 | | - let s = &self.searcher.haystack()[self.start..end_of_match]; |
34 | | - self.start = end_of_match; |
35 | | - return Some(SplitType::Delimiter(s)); |
36 | | - } |
37 | | - |
38 | | - match self.searcher.next_match() { |
39 | | - Some((start, end)) => { |
40 | | - if self.start == start { |
41 | | - let s = &self.searcher.haystack()[start..end]; |
42 | | - self.start = end; |
43 | | - Some(SplitType::Delimiter(s)) |
44 | | - } else { |
45 | | - let s = &self.searcher.haystack()[self.start..start]; |
46 | | - self.start = start; |
47 | | - self.saved = Some(end); |
48 | | - Some(SplitType::Match(s)) |
49 | | - } |
50 | | - } |
51 | | - None => { |
52 | | - let s = &self.searcher.haystack()[self.start..]; |
53 | | - self.start = self.searcher.haystack().len(); |
54 | | - Some(SplitType::Match(s)) |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -#[cfg(feature = "unstable")] |
61 | | -pub trait SplitKeepingDelimiterExt: ::std::ops::Index<::std::ops::RangeFull, Output = str> { |
62 | | - fn split_keeping_delimiter<P>(&self, pattern: P) -> SplitKeepingDelimiter<'_, P> |
63 | | - where |
64 | | - P: for<'a> Pattern<'a>, |
65 | | - { |
66 | | - SplitKeepingDelimiter { |
67 | | - searcher: pattern.into_searcher(&self[..]), |
68 | | - start: 0, |
69 | | - saved: None, |
70 | | - } |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -#[cfg(not(feature = "unstable"))] |
75 | 7 | pub struct SplitKeepingDelimiter<'a, F> { |
76 | 8 | haystack: &'a str, |
77 | 9 | chars: F, |
78 | 10 | start: usize, |
79 | 11 | saved: Option<usize>, |
80 | 12 | } |
81 | 13 |
|
82 | | -#[cfg(not(feature = "unstable"))] |
83 | 14 | impl<'a, F> Iterator for SplitKeepingDelimiter<'a, F> |
84 | 15 | where |
85 | 16 | F: Fn(char) -> bool, |
|
123 | 54 | } |
124 | 55 | } |
125 | 56 |
|
126 | | -#[cfg(not(feature = "unstable"))] |
127 | 57 | pub trait SplitKeepingDelimiterExt: ::std::ops::Index<::std::ops::RangeFull, Output = str> { |
128 | 58 | fn split_keeping_delimiter<F>(&self, chars: F) -> SplitKeepingDelimiter<'_, F> |
129 | 59 | where |
|
0 commit comments