str_utils/
trim.rs

1use alloc::borrow::Cow;
2
3use crate::to_substring_in_place;
4
5/// To extend `Cow<str>` to have `trim_cow`, `trim_start_cow`, `trim_end_cow`, `trim_ascii_cow`, `trim_ascii_start_cow`, `trim_ascii_end_cow` 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 both leading and trailing **ASCII** whitespace characters.
17    fn trim_ascii_cow(self) -> Cow<'a, str>;
18
19    /// Trims leading (left) **ASCII** whitespace characters.
20    fn trim_ascii_start_cow(self) -> Cow<'a, str>;
21
22    /// Trims trailing (right) **ASCII** whitespace characters.
23    fn trim_ascii_end_cow(self) -> Cow<'a, str>;
24}
25
26impl<'a> Trim<'a> for Cow<'a, str> {
27    #[inline]
28    fn trim_cow(self) -> Cow<'a, str> {
29        match self {
30            Cow::Borrowed(s) => Cow::Borrowed(s.trim()),
31            Cow::Owned(s) => {
32                let ss = s.trim();
33
34                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
35            },
36        }
37    }
38
39    #[inline]
40    fn trim_start_cow(self) -> Cow<'a, str> {
41        match self {
42            Cow::Borrowed(s) => Cow::Borrowed(s.trim_start()),
43            Cow::Owned(s) => {
44                let ss = s.trim_start();
45
46                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
47            },
48        }
49    }
50
51    #[inline]
52    fn trim_end_cow(self) -> Cow<'a, str> {
53        match self {
54            Cow::Borrowed(s) => Cow::Borrowed(s.trim_end()),
55            Cow::Owned(s) => {
56                let ss = s.trim_end();
57
58                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
59            },
60        }
61    }
62
63    #[inline]
64    fn trim_ascii_cow(self) -> Cow<'a, str> {
65        match self {
66            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii()),
67            Cow::Owned(s) => {
68                let ss = s.trim_ascii();
69
70                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
71            },
72        }
73    }
74
75    #[inline]
76    fn trim_ascii_start_cow(self) -> Cow<'a, str> {
77        match self {
78            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii_start()),
79            Cow::Owned(s) => {
80                let ss = s.trim_ascii_start();
81
82                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
83            },
84        }
85    }
86
87    #[inline]
88    fn trim_ascii_end_cow(self) -> Cow<'a, str> {
89        match self {
90            Cow::Borrowed(s) => Cow::Borrowed(s.trim_ascii_end()),
91            Cow::Owned(s) => {
92                let ss = s.trim_ascii_end();
93
94                Cow::Owned(unsafe { to_substring_in_place!(s, ss) })
95            },
96        }
97    }
98}