]> git.proxmox.com Git - rustc.git/blob - src/vendor/html5ever/src/util/str.rs
New upstream version 1.31.0+dfsg1
[rustc.git] / src / vendor / html5ever / src / 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
10 use std::fmt;
11
12 pub fn to_escaped_string<T: fmt::Debug>(x: &T) -> String {
13 use std::fmt::Write;
14
15 // FIXME: don't allocate twice
16 let mut buf = String::new();
17 let _ = buf.write_fmt(format_args!("{:?}", x));
18 buf.shrink_to_fit();
19 buf.chars().flat_map(|c| c.escape_default()).collect()
20 }
21
22 /// If `c` is an ASCII letter, return the corresponding lowercase
23 /// letter, otherwise None.
24 pub fn lower_ascii_letter(c: char) -> Option<char> {
25 match c {
26 'a' ... 'z' => Some(c),
27 'A' ... 'Z' => Some((c as u8 - b'A' + b'a') as char),
28 _ => None
29 }
30 }
31
32 /// Is the character an ASCII alphanumeric character?
33 pub fn is_ascii_alnum(c: char) -> bool {
34 matches!(c, '0'...'9' | 'a'...'z' | 'A'...'Z')
35 }
36
37 /// ASCII whitespace characters, as defined by
38 /// tree construction modes that treat them specially.
39 pub fn is_ascii_whitespace(c: char) -> bool {
40 matches!(c, '\t' | '\r' | '\n' | '\x0C' | ' ')
41 }
42
43 #[cfg(test)]
44 #[allow(non_snake_case)]
45 mod test {
46 use super::{is_ascii_alnum, lower_ascii_letter};
47
48 test_eq!(lower_letter_a_is_a, lower_ascii_letter('a'), Some('a'));
49 test_eq!(lower_letter_A_is_a, lower_ascii_letter('A'), Some('a'));
50 test_eq!(lower_letter_symbol_is_None, lower_ascii_letter('!'), None);
51 test_eq!(lower_letter_nonascii_is_None, lower_ascii_letter('\u{a66e}'), None);
52
53 test_eq!(is_alnum_a, is_ascii_alnum('a'), true);
54 test_eq!(is_alnum_A, is_ascii_alnum('A'), true);
55 test_eq!(is_alnum_1, is_ascii_alnum('1'), true);
56 test_eq!(is_not_alnum_symbol, is_ascii_alnum('!'), false);
57 test_eq!(is_not_alnum_nonascii, is_ascii_alnum('\u{a66e}'), false);
58 }