style/properties_and_values/syntax/
ascii.rs1pub fn trim_ascii_whitespace(input: &str) -> &str {
7 if input.is_empty() {
8 return input;
9 }
10
11 let mut start = 0;
12 {
13 let mut iter = input.as_bytes().iter();
14 loop {
15 let byte = match iter.next() {
16 Some(b) => b,
17 None => return "",
18 };
19
20 if !byte.is_ascii_whitespace() {
21 break;
22 }
23 start += 1;
24 }
25 }
26
27 let mut end = input.len();
28 assert!(start < end);
29 {
30 let mut iter = input.as_bytes()[start..].iter().rev();
31 loop {
32 let byte = match iter.next() {
33 Some(b) => b,
34 None => {
35 debug_assert!(false, "We should have caught this in the loop above!");
36 return "";
37 },
38 };
39
40 if !byte.is_ascii_whitespace() {
41 break;
42 }
43 end -= 1;
44 }
45 }
46
47 &input[start..end]
48}
49
50#[test]
51fn trim_ascii_whitespace_test() {
52 fn test(i: &str, o: &str) {
53 assert_eq!(trim_ascii_whitespace(i), o)
54 }
55
56 test("", "");
57 test(" ", "");
58 test(" a b c ", "a b c");
59 test(" \t \t \ta b c \t \t \t \t", "a b c");
60}