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 fn to_escaped_string<T: fmt::Debug>(x: &T) -> String {
13 // FIXME: don't allocate twice
14 let string: String = format!("{:?}", x);
15 string.chars().flat_map(|c: char| c.escape_default()).collect()
16}
17
18/// If `c` is an ASCII letter, return the corresponding lowercase
19/// letter, otherwise None.
20pub 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 use mac::test_eq;
33
34 test_eq!(lower_letter_a_is_a, lower_ascii_letter('a'), Some('a'));
35 test_eq!(lower_letter_A_is_a, lower_ascii_letter('A'), Some('a'));
36 test_eq!(lower_letter_symbol_is_None, lower_ascii_letter('!'), None);
37 test_eq!(
38 lower_letter_nonascii_is_None,
39 lower_ascii_letter('\u{a66e}'),
40 None
41 );
42}
43