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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/// An extension to
/// [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
/// allowing for inline trimming.
pub trait StringExt {
    /// Mutates a string in place with leading and trailing whitespace removed.
    ///
    /// 'Whitespace' is defined according to the terms of the Unicode Derived
    /// Core Property `White_Space`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("   Hello World!   ");
    ///
    /// test.trim();
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    fn trim(&mut self);

    /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("blahHello World!blah");
    ///
    /// test.trim_matches("blah");
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    ///
    /// # Note
    ///
    /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
    /// this will be changed to accept a pattern.
    fn trim_matches(&mut self, rem: &str);

    /// Mutates a string in place with leading whitespace removed.
    ///
    /// 'Whitespace' is defined according to the terms of the Unicode Derived
    /// Core Property `White_Space`.
    ///
    /// # Text directionality
    ///
    /// A string is a sequence of bytes. `start` in this context means the first
    /// position of that byte string; for a left-to-right language like English or
    /// Russian, this will be left side, and for right-to-left languages like
    /// Arabic or Hebrew, this will be the right side.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("   Hello World!");
    ///
    /// test.trim_start();
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    fn trim_start(&mut self);

    /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
    ///
    /// # Text directionality
    ///
    /// A string is a sequence of bytes. `start` in this context means the first
    /// position of that byte string; for a left-to-right language like English or
    /// Russian, this will be left side, and for right-to-left languages like
    /// Arabic or Hebrew, this will be the right side.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("blahHello World!");
    ///
    /// test.trim_start_matches("blah");
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    ///
    /// # Note
    ///
    /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
    /// this will be changed to accept a pattern.
    fn trim_start_matches(&mut self, rem: &str);

    /// Mutates a string in place with trailing whitespace removed.
    ///
    /// 'Whitespace' is defined according to the terms of the Unicode Derived
    /// Core Property `White_Space`.
    ///
    /// # Text directionality
    ///
    /// A string is a sequence of bytes. 'Left' in this context means the first
    /// position of that byte string; for a language like Arabic or Hebrew
    /// which are 'right to left' rather than 'left to right', this will be
    /// the _right_ side, not the left.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("Hello World!   ");
    ///
    /// test.trim_end();
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    fn trim_end(&mut self);

    /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
    ///
    /// # Text directionality
    ///
    /// A string is a sequence of bytes. 'Left' in this context means the first
    /// position of that byte string; for a language like Arabic or Hebrew
    /// which are 'right to left' rather than 'left to right', this will be
    /// the _right_ side, not the left.
    ///
    /// ```rust
    /// use fenn::StringExt;
    ///
    /// let mut test = String::from("Hello World!blah");
    ///
    /// test.trim_end_matches("blah");
    ///
    /// assert_eq!(test, String::from("Hello World!"));
    /// ```
    ///
    /// # Note
    ///
    /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
    /// this will be changed to accept a pattern.
    fn trim_end_matches(&mut self, rem: &str);
}

impl StringExt for super::lib::String {
    fn trim(&mut self) {
        self.trim_start();
        self.trim_end();
    }

    fn trim_matches(&mut self, rem: &str) {
        self.trim_start_matches(rem);
        self.trim_end_matches(rem);
    }

    fn trim_start(&mut self) {
        while self.starts_with(char::is_whitespace) {
            self.drain(..1);
        }
    }

    fn trim_start_matches(&mut self, rem: &str) {
        while self.starts_with(rem) {
            self.drain(..rem.len());
        }
    }

    fn trim_end(&mut self) {
        while self.ends_with(char::is_whitespace) {
            self.truncate(self.len().saturating_sub(1));
        }
    }

    fn trim_end_matches(&mut self, rem: &str) {
        while self.ends_with(rem) {
            self.truncate(self.len().saturating_sub(rem.len()));
        }
    }
}