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