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
//! This module provides an iterator over strings that splits on whitespace
//! but doesn't throw the whitespace away, like the version in
//! [std](https://doc.rust-lang.org/std/primitive.str.html#method.split_whitespace)
//! does.

//! An iterator over the whitespace and non-whitespace sub-strings of a string, separated by any
//! amount of whitespace.
pub struct SplitPreserveWS<'a> {
    string: Option<Token<'a>>,
}

/// The token returned by the `SplitPreserveWS` iterator. It can be either
/// `Whitespace` or `Other`
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Token<'a> {
    Whitespace(&'a str),
    Other(&'a str),
}

impl<'a> SplitPreserveWS<'a> {
    /// Splits a string slice by whitespace.
    ///
    /// The iterator returned will return string slices that are sub-slices of the original string
    /// slice, annotated as `Whitespace` or `Other` using the `Token` enum.
    ///
    /// 'Whitespace' is defined according to the terms of the Unicode Derived Core Property
    /// `White_Space`.
    ///
    /// ```rust
    /// use split_preserve::{SplitPreserveWS, Token};
    ///
    /// assert_eq!(SplitPreserveWS::new("aa  ").next(), Some(Token::Other("aa")))
    /// ```
    pub fn new(string: &'a str) -> Self {
        if string.is_empty() {
            Self { string: None }
        } else if string.starts_with(char::is_whitespace) {
            Self {
                string: Some(Token::Whitespace(string)),
            }
        } else {
            Self {
                string: Some(Token::Other(string)),
            }
        }
    }

    /// Maps over the `Token::Other` elements of the iterator.
    ///
    /// This will allocate a new string for each of the tokens in the iterator
    ///
    /// ```rust
    /// use split_preserve::{SplitPreserveWS, Token};
    ///
    /// assert_eq!(
    ///     SplitPreserveWS::new("Line\twith\nweird whitespace")
    ///         .map_words(|f| f.chars().rev().collect::<String>())
    ///         .collect::<String>(),
    ///     "eniL\thtiw\ndriew ecapsetihw"
    /// )
    /// ```
    pub fn map_words<S>(self, mut f: S) -> std::iter::Map<Self, impl FnMut(Token<'a>) -> String>
    where
        S: FnMut(&str) -> String,
    {
        self.map(move |t: Token<'a>| match t {
            Token::Other(s) => f(s),
            Token::Whitespace(s) => s.to_string(),
        })
    }

    /// Maps over the `Token::Whitespace` elements of the iterator.
    ///
    /// This will allocate a new string for each of the tokens in the iterator
    ///
    /// ```rust
    /// use split_preserve::{SplitPreserveWS, Token};
    ///
    /// assert_eq!(
    ///     SplitPreserveWS::new("Line\twith\nweird whitespace")
    ///         .map_whitespace(|_| String::from(" "))
    ///         .collect::<String>(),
    ///     "Line with weird whitespace"
    /// )
    /// ```
    pub fn map_whitespace<S>(
        self,
        mut f: S,
    ) -> std::iter::Map<Self, impl FnMut(Token<'a>) -> String>
    where
        S: FnMut(&str) -> String,
    {
        self.map(move |t: Token<'a>| match t {
            Token::Other(s) => s.to_string(),
            Token::Whitespace(s) => f(s),
        })
    }
}

impl<'a> Iterator for SplitPreserveWS<'a> {
    type Item = Token<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.string.take().map(|t| match t {
            Token::Whitespace(s) => {
                let (token, rest) = match s.find(|c: char| !c.is_whitespace()) {
                    Some(i) => {
                        let (a, b) = s.split_at(i);
                        (a, Some(Token::Other(b)))
                    }
                    None => (s, None),
                };
                self.string = rest;
                Token::Whitespace(token)
            }
            Token::Other(s) => {
                let (token, rest) = match s.find(char::is_whitespace) {
                    Some(i) => {
                        let (a, b) = s.split_at(i);
                        (a, Some(Token::Whitespace(b)))
                    }
                    None => (s, None),
                };
                self.string = rest;
                Token::Other(token)
            }
        })
    }
}