Skip to main content

str_utils/
trim.rs

1use alloc::borrow::Cow;
2
3use crate::{to_substring_in_place, Pattern};
4
5/// To extend `Cow<str>` with methods corresponding to the standard library trimming methods.
6pub trait Trim<'a> {
7    /// Trims both leading and trailing Unicode whitespace characters.
8    fn trim_cow(self) -> Cow<'a, str>;
9
10    /// Trims leading (left) Unicode whitespace characters.
11    fn trim_start_cow(self) -> Cow<'a, str>;
12
13    /// Trims trailing (right) Unicode whitespace characters.
14    fn trim_end_cow(self) -> Cow<'a, str>;
15
16    /// Trims all prefixes and suffixes that match a pattern.
17    fn trim_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str>;
18
19    /// Trims all prefixes that match a pattern.
20    fn trim_start_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str>;
21
22    /// Trims all suffixes that match a pattern.
23    fn trim_end_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str>;
24
25    /// Trims both leading and trailing **ASCII** whitespace characters.
26    fn trim_ascii_cow(self) -> Cow<'a, str>;
27
28    /// Trims leading (left) **ASCII** whitespace characters.
29    fn trim_ascii_start_cow(self) -> Cow<'a, str>;
30
31    /// Trims trailing (right) **ASCII** whitespace characters.
32    fn trim_ascii_end_cow(self) -> Cow<'a, str>;
33}
34
35impl<'a> Trim<'a> for Cow<'a, str> {
36    #[inline]
37    fn trim_cow(self) -> Cow<'a, str> {
38        match self {
39            Cow::Borrowed(s) => Cow::Borrowed(s.trim()),
40            Cow::Owned(s) => {
41                let ss = s.trim();
42
43                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
44            },
45        }
46    }
47
48    #[inline]
49    fn trim_start_cow(self) -> Cow<'a, str> {
50        match self {
51            Cow::Borrowed(s) => Cow::Borrowed(s.trim_start()),
52            Cow::Owned(s) => {
53                let ss = s.trim_start();
54
55                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
56            },
57        }
58    }
59
60    #[inline]
61    fn trim_end_cow(self) -> Cow<'a, str> {
62        match self {
63            Cow::Borrowed(s) => Cow::Borrowed(s.trim_end()),
64            Cow::Owned(s) => {
65                let ss = s.trim_end();
66
67                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
68            },
69        }
70    }
71
72    #[inline]
73    fn trim_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str> {
74        match self {
75            Cow::Borrowed(s) => Cow::Borrowed(pat.trim_matches_from(s)),
76            Cow::Owned(s) => {
77                let ss = pat.trim_matches_from(s.as_str());
78
79                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
80            },
81        }
82    }
83
84    #[inline]
85    fn trim_start_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str> {
86        match self {
87            Cow::Borrowed(s) => Cow::Borrowed(pat.trim_start_matches_from(s)),
88            Cow::Owned(s) => {
89                let ss = pat.trim_start_matches_from(s.as_str());
90
91                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
92            },
93        }
94    }
95
96    #[inline]
97    fn trim_end_matches_cow<P: Pattern>(self, pat: P) -> Cow<'a, str> {
98        match self {
99            Cow::Borrowed(s) => Cow::Borrowed(pat.trim_end_matches_from(s)),
100            Cow::Owned(s) => {
101                let ss = pat.trim_end_matches_from(s.as_str());
102
103                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
104            },
105        }
106    }
107
108    #[inline]
109    fn trim_ascii_cow(self) -> Cow<'a, str> {
110        match self {
111            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii()),
112            Cow::Owned(s) => {
113                let ss = s.trim_ascii();
114
115                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
116            },
117        }
118    }
119
120    #[inline]
121    fn trim_ascii_start_cow(self) -> Cow<'a, str> {
122        match self {
123            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii_start()),
124            Cow::Owned(s) => {
125                let ss = s.trim_ascii_start();
126
127                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
128            },
129        }
130    }
131
132    #[inline]
133    fn trim_ascii_end_cow(self) -> Cow<'a, str> {
134        match self {
135            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii_end()),
136            Cow::Owned(s) => {
137                let ss = s.trim_ascii_end();
138
139                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
140            },
141        }
142    }
143}