1 // Copyright 2014-2017 The html5ever Project Developers. See the
2 // COPYRIGHT file at the top-level directory of this distribution.
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.
12 pub fn to_escaped_string
<T
: fmt
::Debug
>(x
: &T
) -> String
{
15 // FIXME: don't allocate twice
16 let mut buf
= String
::new();
17 let _
= buf
.write_fmt(format_args
!("{:?}", x
));
19 buf
.chars().flat_map(|c
| c
.escape_default()).collect()
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> {
26 'a'
... 'z'
=> Some(c
),
27 'A'
... 'Z'
=> Some((c
as u8 - b'A'
+ b'a'
) as char),
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'
)
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'
| ' '
)
44 #[allow(non_snake_case)]
46 use super::{is_ascii_alnum, lower_ascii_letter}
;
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
);
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);