stry_common/utils/fenn/string.rs
1/// An extension to
2/// [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
3/// allowing for inline trimming.
4pub trait StringExt {
5 /// Mutates a string in place with leading and trailing whitespace removed.
6 ///
7 /// 'Whitespace' is defined according to the terms of the Unicode Derived
8 /// Core Property `White_Space`.
9 ///
10 /// # Examples
11 ///
12 /// ```rust
13 /// use fenn::StringExt;
14 ///
15 /// let mut test = String::from(" Hello World! ");
16 ///
17 /// test.trim();
18 ///
19 /// assert_eq!(test, String::from("Hello World!"));
20 /// ```
21 fn trim(&mut self);
22
23 /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
24 ///
25 /// # Examples
26 ///
27 /// ```rust
28 /// use fenn::StringExt;
29 ///
30 /// let mut test = String::from("blahHello World!blah");
31 ///
32 /// test.trim_matches("blah");
33 ///
34 /// assert_eq!(test, String::from("Hello World!"));
35 /// ```
36 ///
37 /// # Note
38 ///
39 /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
40 /// this will be changed to accept a pattern.
41 fn trim_matches(&mut self, rem: &str);
42
43 /// Mutates a string in place with leading whitespace removed.
44 ///
45 /// 'Whitespace' is defined according to the terms of the Unicode Derived
46 /// Core Property `White_Space`.
47 ///
48 /// # Text directionality
49 ///
50 /// A string is a sequence of bytes. `start` in this context means the first
51 /// position of that byte string; for a left-to-right language like English or
52 /// Russian, this will be left side, and for right-to-left languages like
53 /// Arabic or Hebrew, this will be the right side.
54 ///
55 /// # Examples
56 ///
57 /// ```rust
58 /// use fenn::StringExt;
59 ///
60 /// let mut test = String::from(" Hello World!");
61 ///
62 /// test.trim_start();
63 ///
64 /// assert_eq!(test, String::from("Hello World!"));
65 /// ```
66 fn trim_start(&mut self);
67
68 /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
69 ///
70 /// # Text directionality
71 ///
72 /// A string is a sequence of bytes. `start` in this context means the first
73 /// position of that byte string; for a left-to-right language like English or
74 /// Russian, this will be left side, and for right-to-left languages like
75 /// Arabic or Hebrew, this will be the right side.
76 ///
77 /// # Examples
78 ///
79 /// ```rust
80 /// use fenn::StringExt;
81 ///
82 /// let mut test = String::from("blahHello World!");
83 ///
84 /// test.trim_start_matches("blah");
85 ///
86 /// assert_eq!(test, String::from("Hello World!"));
87 /// ```
88 ///
89 /// # Note
90 ///
91 /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
92 /// this will be changed to accept a pattern.
93 fn trim_start_matches(&mut self, rem: &str);
94
95 /// Mutates a string in place with trailing whitespace removed.
96 ///
97 /// 'Whitespace' is defined according to the terms of the Unicode Derived
98 /// Core Property `White_Space`.
99 ///
100 /// # Text directionality
101 ///
102 /// A string is a sequence of bytes. 'Left' in this context means the first
103 /// position of that byte string; for a language like Arabic or Hebrew
104 /// which are 'right to left' rather than 'left to right', this will be
105 /// the _right_ side, not the left.
106 ///
107 /// # Examples
108 ///
109 /// ```rust
110 /// use fenn::StringExt;
111 ///
112 /// let mut test = String::from("Hello World! ");
113 ///
114 /// test.trim_end();
115 ///
116 /// assert_eq!(test, String::from("Hello World!"));
117 /// ```
118 fn trim_end(&mut self);
119
120 /// Mutates a string in place with all suffixes that match a pattern repeatedly removed.
121 ///
122 /// # Text directionality
123 ///
124 /// A string is a sequence of bytes. 'Left' in this context means the first
125 /// position of that byte string; for a language like Arabic or Hebrew
126 /// which are 'right to left' rather than 'left to right', this will be
127 /// the _right_ side, not the left.
128 ///
129 /// ```rust
130 /// use fenn::StringExt;
131 ///
132 /// let mut test = String::from("Hello World!blah");
133 ///
134 /// test.trim_end_matches("blah");
135 ///
136 /// assert_eq!(test, String::from("Hello World!"));
137 /// ```
138 ///
139 /// # Note
140 ///
141 /// Once the Pattern API ([#27721](https://github.com/rust-lang/rust/issues/27721)) becomes stable
142 /// this will be changed to accept a pattern.
143 fn trim_end_matches(&mut self, rem: &str);
144}
145
146impl StringExt for super::lib::String {
147 fn trim(&mut self) {
148 self.trim_start();
149 self.trim_end();
150 }
151
152 fn trim_matches(&mut self, rem: &str) {
153 self.trim_start_matches(rem);
154 self.trim_end_matches(rem);
155 }
156
157 fn trim_start(&mut self) {
158 while self.starts_with(char::is_whitespace) {
159 self.drain(..1);
160 }
161 }
162
163 fn trim_start_matches(&mut self, rem: &str) {
164 while self.starts_with(rem) {
165 self.drain(..rem.len());
166 }
167 }
168
169 fn trim_end(&mut self) {
170 while self.ends_with(char::is_whitespace) {
171 self.truncate(self.len().saturating_sub(1));
172 }
173 }
174
175 fn trim_end_matches(&mut self, rem: &str) {
176 while self.ends_with(rem) {
177 self.truncate(self.len().saturating_sub(rem.len()));
178 }
179 }
180}