html5ever/util/
str.rs

1// Copyright 2014-2017 The html5ever Project Developers. See the
2// COPYRIGHT file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::fmt;
11
12pub(crate) fn to_escaped_string<T: fmt::Debug>(x: &T) -> String {
13    // FIXME: don't allocate twice
14    let string = format!("{x:?}");
15    string.chars().flat_map(|c| c.escape_default()).collect()
16}
17
18/// If `c` is an ASCII letter, return the corresponding lowercase
19/// letter, otherwise None.
20pub(crate) fn lower_ascii_letter(c: char) -> Option<char> {
21    match c {
22        'a'..='z' => Some(c),
23        'A'..='Z' => Some((c as u8 - b'A' + b'a') as char),
24        _ => None,
25    }
26}
27
28#[cfg(test)]
29#[allow(non_snake_case)]
30mod test {
31    use super::lower_ascii_letter;
32
33    #[test]
34    fn lower_letter_a_is_a() {
35        assert_eq!(lower_ascii_letter('a'), Some('a'));
36    }
37
38    #[test]
39    fn lower_letter_A_is_a() {
40        assert_eq!(lower_ascii_letter('A'), Some('a'));
41    }
42
43    #[test]
44    fn lower_letter_symbol_is_None() {
45        assert_eq!(lower_ascii_letter('!'), None);
46    }
47
48    #[test]
49    fn lower_letter_nonascii_is_None() {
50        assert_eq!(lower_ascii_letter('\u{a66e}'), None);
51    }
52}