scolapasta_int_parse/
whitespace.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pub fn trim_leading(bytes: &[u8]) -> &[u8] {
    if let Some(idx) = bytes.iter().position(|&b| !posix_space::is_space(b)) {
        &bytes[idx..]
    } else {
        bytes
    }
}

pub fn trim_trailing(bytes: &[u8]) -> &[u8] {
    if let Some(idx) = bytes.iter().rposition(|&b| !posix_space::is_space(b)) {
        &bytes[..=idx]
    } else {
        bytes
    }
}

pub fn trim(bytes: &[u8]) -> &[u8] {
    let bytes = trim_leading(bytes);
    trim_trailing(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trim_leading_trims_leading_whitespace() {
        assert_eq!(trim_leading(b"abc"), b"abc");
        assert_eq!(trim_leading(b" abc"), b"abc");
        assert_eq!(trim_leading(b"      abc"), b"abc");
        assert_eq!(trim_leading(b"\tabc"), b"abc");
        assert_eq!(trim_leading(b"\nabc"), b"abc");
        assert_eq!(trim_leading(b"\x0Aabc"), b"abc");
        assert_eq!(trim_leading(b"\x0Cabc"), b"abc");
        assert_eq!(trim_leading(b" \t\n\x0A\x0Cabc"), b"abc");
        assert_eq!(
            trim_leading(b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc"),
            b"abc"
        );
    }

    #[test]
    fn trim_leading_preserves_trailing_whitespace() {
        assert_eq!(trim_leading(b"abc "), b"abc ");
        assert_eq!(trim_leading(b" abc "), b"abc ");
        assert_eq!(trim_leading(b"      abc      "), b"abc      ");
        assert_eq!(trim_leading(b"\tabc\t"), b"abc\t");
        assert_eq!(trim_leading(b"\nabc\n"), b"abc\n");
        assert_eq!(trim_leading(b"\x0Aabc\x0A"), b"abc\x0A");
        assert_eq!(trim_leading(b"\x0Cabc\x0C"), b"abc\x0C");
        assert_eq!(trim_leading(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b"abc \t\n\x0A\x0C");
        assert_eq!(
            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"),
            b"abc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"
        );
    }

    #[test]
    fn trim_trailing_trims_trailing_whitespace() {
        assert_eq!(trim_trailing(b"abc"), b"abc");
        assert_eq!(trim_trailing(b"abc "), b"abc");
        assert_eq!(trim_trailing(b"abc      "), b"abc");
        assert_eq!(trim_trailing(b"abc\t"), b"abc");
        assert_eq!(trim_trailing(b"abc\n"), b"abc");
        assert_eq!(trim_trailing(b"abc\x0A"), b"abc");
        assert_eq!(trim_trailing(b"abc\x0C"), b"abc");
        assert_eq!(trim_trailing(b"abc \t\n\x0A\x0C"), b"abc");
        assert_eq!(
            trim_trailing(b"abc \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C"),
            b"abc"
        );
    }

    #[test]
    fn trim_leading_preserves_leading_whitespace() {
        assert_eq!(trim_trailing(b" abc"), b" abc");
        assert_eq!(trim_trailing(b" abc "), b" abc");
        assert_eq!(trim_trailing(b"      abc      "), b"      abc");
        assert_eq!(trim_trailing(b"\tabc\t"), b"\tabc");
        assert_eq!(trim_trailing(b"\nabc\n"), b"\nabc");
        assert_eq!(trim_trailing(b"\x0Aabc\x0A"), b"\x0Aabc");
        assert_eq!(trim_trailing(b"\x0Cabc\x0C"), b"\x0Cabc");
        assert_eq!(trim_trailing(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b" \t\n\x0A\x0Cabc");
        assert_eq!(
            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"),
            b" \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0C \t\n\x0A\x0Cabc"
        );
    }

    #[test]
    fn trim_trims_all_whitespace() {
        assert_eq!(trim(b" abc "), b"abc");
        assert_eq!(trim(b" abc "), b"abc");
        assert_eq!(trim(b"      abc      "), b"abc");
        assert_eq!(trim(b"\tabc\t"), b"abc");
        assert_eq!(trim(b"\nabc\n"), b"abc");
        assert_eq!(trim(b"\x0Aabc\x0A"), b"abc");
        assert_eq!(trim(b"\x0Cabc\x0C"), b"abc");
        assert_eq!(trim(b" \t\n\x0A\x0Cabc \t\n\x0A\x0C"), b"abc");
        assert_eq!(
            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"),
            b"abc"
        );
    }

    #[test]
    fn vertical_tab_is_whitespace() {
        // ```
        // [3.1.2] > Integer "    \u000B 27"
        // => 27
        // ```
        assert_eq!(trim_leading(b"\x0Babc"), b"abc");
        assert_eq!(trim_trailing(b"abc\x0B"), b"abc");
        assert_eq!(trim(b"\x0Babc\x0B"), b"abc");
    }
}