aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util/str.rs
blob: bd89ad9ea6f443be1dc366153c249c73dcef070f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

pub type DOMString = ~str;

pub fn null_str_as_empty(s: &Option<DOMString>) -> DOMString {
    // We don't use map_default because it would allocate ~"" even for Some.
    match *s {
        Some(ref s) => s.clone(),
        None => ~""
    }
}

pub fn null_str_as_empty_ref<'a>(s: &'a Option<DOMString>) -> &'a str {
    match *s {
        Some(ref s) => s.as_slice(),
        None => &'a ""
    }
}

pub fn is_whitespace(s: &str) -> bool {
    s.chars().all(|c| match c {
        '\u0020' | '\u0009' | '\u000D' | '\u000A' => true,
        _ => false
    })
}

/// A "space character" according to:
///
///     http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#
///     space-character
pub static HTML_SPACE_CHARACTERS: [char, ..5] = [
    '\u0020',
    '\u0009',
    '\u000a',
    '\u000c',
    '\u000d',
];