1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// License: see LICENSE file at root directory of `master` branch

//! # `SubStrIter`

use alloc::string::String;

use core::ops::Deref;

/// # A sub string
///
/// This struct is used by [`SubStrIter`][::SubStrIter], which can be made via [`sub_strs()`][::sub_strs()].
///
/// You can dereference this struct to `&str`. It can also give you start and end indexes of the original string.
///
/// [::SubStrIter]: struct.SubStrIter.html
/// [::sub_strs()]: fn.sub_strs.html
#[derive(Debug)]
pub struct SubStr<'a> {
    target: &'a str,
    start: usize,
    end: usize,
}

impl SubStr<'_> {

    /// # Start index
    pub fn start(&self) -> usize {
        self.start
    }

    /// # End index
    pub fn end(&self) -> usize {
        self.end
    }

}

impl Deref for SubStr<'_> {

    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.target
    }

}

impl PartialEq<str> for SubStr<'_> {

    fn eq(&self, other: &str) -> bool {
        self.target == other
    }

}

impl<'a> PartialEq<SubStr<'a>> for &str {

    fn eq(&self, other: &SubStr<'a>) -> bool {
        self == &other.target
    }

}

impl PartialEq<String> for SubStr<'_> {

    fn eq(&self, other: &String) -> bool {
        self.target == other
    }

}

impl<'a> PartialEq<SubStr<'a>> for String {

    fn eq(&self, other: &SubStr<'a>) -> bool {
        self == other.target
    }

}

/// # An iterator of [`SubStr`][::SubStr]
///
/// You can make an instance of this struct by [`sub_strs()`][::sub_strs()].
///
/// [::SubStr]: struct.SubStr.html
/// [::sub_strs()]: fn.sub_strs.html
#[derive(Debug)]
pub struct SubStrIter<'a, S> where S: AsRef<str> {
    src: &'a str,
    src_index: usize,
    start: S,
    end: S,
}

impl<'a, S> SubStrIter<'a, S> where S: AsRef<str> {

    /// # Makes new instance
    fn new(source: &'a str, start: S, end: S) -> Self {
        Self {
            src: source,
            src_index: 0,
            start,
            end,
        }
    }

}

impl<'a, S: AsRef<str>> Iterator for SubStrIter<'a, S> where S: AsRef<str> {

    type Item = SubStr<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let start = self.start.as_ref();

        let src = &self.src[self.src_index..];
        if let Some(start_idx) = src.find(start) {
            let end = self.end.as_ref();
            if let Some(end_idx) = src[start_idx + start.len()..].find(end) {
                let end_idx = start_idx + start.len() + end_idx + end.len();
                let sub_start = self.src_index + start_idx;
                self.src_index += end_idx;
                return Some(SubStr {
                    target: &src[start_idx..end_idx],
                    start: sub_start,
                    end: self.src_index,
                });
            }
        }

        None
    }

}

/// # Finds sub strings
///
/// ## Examples
///
/// ```
/// let sample = "test(0)     test(1)---test(2) some test(3)";
/// let start = "test(";
/// let end = ")";
/// for (i, s) in sub_strs::sub_strs(sample, start, end).enumerate() {
///     let expected = format!("{}{}{}", start, i, end);
///     assert_eq!(s, expected);
///     assert_eq!(&sample[s.start()..s.end()], expected);
/// }
/// ```
pub fn sub_strs<'a, S>(source: &'a str, start: S, end: S) -> SubStrIter<'a, S> where S: AsRef<str> {
    SubStrIter::new(source, start, end)
}