scolapasta_int_parse/
whitespace.rs1pub fn trim_leading(bytes: &[u8]) -> &[u8] {
2 if let Some(idx) = bytes.iter().position(|&b| !posix_space::is_space(b)) {
3 &bytes[idx..]
4 } else {
5 bytes
6 }
7}
8
9pub fn trim_trailing(bytes: &[u8]) -> &[u8] {
10 if let Some(idx) = bytes.iter().rposition(|&b| !posix_space::is_space(b)) {
11 &bytes[..=idx]
12 } else {
13 bytes
14 }
15}
16
17pub fn trim(bytes: &[u8]) -> &[u8] {
18 let bytes = trim_leading(bytes);
19 trim_trailing(bytes)
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25
26 #[test]
27 fn trim_leading_trims_leading_whitespace() {
28 assert_eq!(trim_leading(b"abc"), b"abc");
29 assert_eq!(trim_leading(b" abc"), b"abc");
30 assert_eq!(trim_leading(b" abc"), b"abc");
31 assert_eq!(trim_leading(b"\tabc"), b"abc");
32 assert_eq!(trim_leading(b"\nabc"), b"abc");
33 assert_eq!(trim_leading(b"\x0Aabc"), b"abc");
34 assert_eq!(trim_leading(b"\x0Cabc"), b"abc");
35 assert_eq!(trim_leading(b" \t\n\x0A\x0Cabc"), b"abc");
36 assert_eq!(
37 trim_leading(b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc"),
38 b"abc"
39 );
40 }
41
42 #[test]
43 fn trim_leading_preserves_trailing_whitespace() {
44 assert_eq!(trim_leading(b"abc "), b"abc ");
45 assert_eq!(trim_leading(b" abc "), b"abc ");
46 assert_eq!(trim_leading(b" abc "), b"abc ");
47 assert_eq!(trim_leading(b"\tabc\t"), b"abc\t");
48 assert_eq!(trim_leading(b"\nabc\n"), b"abc\n");
49 assert_eq!(trim_leading(b"\x0Aabc\x0A"), b"abc\x0A");
50 assert_eq!(trim_leading(b"\x0Cabc\x0C"), b"abc\x0C");
51 assert_eq!(trim_leading(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b"abc \t\n\x0A\x0C");
52 assert_eq!(
53 trim_leading(b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"),
54 b"abc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"
55 );
56 }
57
58 #[test]
59 fn trim_trailing_trims_trailing_whitespace() {
60 assert_eq!(trim_trailing(b"abc"), b"abc");
61 assert_eq!(trim_trailing(b"abc "), b"abc");
62 assert_eq!(trim_trailing(b"abc "), b"abc");
63 assert_eq!(trim_trailing(b"abc\t"), b"abc");
64 assert_eq!(trim_trailing(b"abc\n"), b"abc");
65 assert_eq!(trim_trailing(b"abc\x0A"), b"abc");
66 assert_eq!(trim_trailing(b"abc\x0C"), b"abc");
67 assert_eq!(trim_trailing(b"abc \t\n\x0A\x0C"), b"abc");
68 assert_eq!(
69 trim_trailing(b"abc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"),
70 b"abc"
71 );
72 }
73
74 #[test]
75 fn trim_leading_preserves_leading_whitespace() {
76 assert_eq!(trim_trailing(b" abc"), b" abc");
77 assert_eq!(trim_trailing(b" abc "), b" abc");
78 assert_eq!(trim_trailing(b" abc "), b" abc");
79 assert_eq!(trim_trailing(b"\tabc\t"), b"\tabc");
80 assert_eq!(trim_trailing(b"\nabc\n"), b"\nabc");
81 assert_eq!(trim_trailing(b"\x0Aabc\x0A"), b"\x0Aabc");
82 assert_eq!(trim_trailing(b"\x0Cabc\x0C"), b"\x0Cabc");
83 assert_eq!(trim_trailing(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b" \t\n\x0A\x0Cabc");
84 assert_eq!(
85 trim_trailing(b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"),
86 b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc"
87 );
88 }
89
90 #[test]
91 fn trim_trims_all_whitespace() {
92 assert_eq!(trim(b" abc "), b"abc");
93 assert_eq!(trim(b" abc "), b"abc");
94 assert_eq!(trim(b" abc "), b"abc");
95 assert_eq!(trim(b"\tabc\t"), b"abc");
96 assert_eq!(trim(b"\nabc\n"), b"abc");
97 assert_eq!(trim(b"\x0Aabc\x0A"), b"abc");
98 assert_eq!(trim(b"\x0Cabc\x0C"), b"abc");
99 assert_eq!(trim(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b"abc");
100 assert_eq!(
101 trim(b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"),
102 b"abc"
103 );
104 }
105
106 #[test]
107 fn vertical_tab_is_whitespace() {
108 assert_eq!(trim_leading(b"\x0Babc"), b"abc");
113 assert_eq!(trim_trailing(b"abc\x0B"), b"abc");
114 assert_eq!(trim(b"\x0Babc\x0B"), b"abc");
115 }
116}