content_security_policy/
text_util.rs

1pub(crate) fn is_char_ascii_whitespace(c: char) -> bool {
2    c == '\u{09}' || c == '\u{0A}' || c == '\u{0C}' || c == '\u{0D}' || c == '\u{20}'
3}
4
5pub(crate) fn strip_leading_and_trailing_ascii_whitespace(string: &str) -> &str {
6    string.trim_matches(is_char_ascii_whitespace)
7}
8
9pub(crate) fn collect_a_sequence_of_non_ascii_white_space_code_points(string: &str)
10                                                           -> (&str, &str) {
11    match string.find(is_char_ascii_whitespace) {
12        Some(i) => string.split_at(i),
13        None => (string, "")
14    }
15}
16
17pub(crate) struct SplitAsciiWhitespace<'a>(&'a str);
18
19impl<'a> Iterator for SplitAsciiWhitespace<'a> {
20    type Item = &'a str;
21    fn next(&mut self) -> Option<Self::Item> {
22        self.0 = self.0.trim_start_matches(is_char_ascii_whitespace);
23        let mut s = self.0.splitn(2, is_char_ascii_whitespace);
24        let next = s.next().unwrap_or("");
25        self.0 = s.next().unwrap_or("");
26        match (next.is_empty(), self.0.is_empty()) {
27            (true, true) => None,
28            (false, _) => Some(next),
29            _ => self.next(),
30        }
31    }
32}
33
34pub(crate) fn split_ascii_whitespace(string: &str) -> SplitAsciiWhitespace {
35    SplitAsciiWhitespace(string)
36}
37
38pub(crate) struct SplitCommas<'a>(&'a str);
39
40impl<'a> Iterator for SplitCommas<'a> {
41    type Item = &'a str;
42    fn next(&mut self) -> Option<Self::Item> {
43        let mut s = self.0.splitn(2, ',');
44        let next = s.next().unwrap_or("");
45        self.0 = s.next().unwrap_or("");
46        match (next.is_empty(), self.0.is_empty()) {
47            (true, true) => None,
48            (true, false) => Some(""),
49            (false, _) => Some(next),
50        }
51    }
52}
53
54pub(crate) fn split_commas(string: &str) -> SplitCommas {
55    SplitCommas(string)
56}
57
58pub(crate) fn ascii_case_insensitive_match(a: &str, b: &str) -> bool {
59    a.to_ascii_lowercase() == b.to_ascii_lowercase()
60}