2014-02-14 12:25:58 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
//! String utils for attributes and similar stuff.
|
|
|
|
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
2016-05-18 01:15:15 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2017-04-21 05:17:06 +00:00
|
|
|
use std::ascii::AsciiExt;
|
2015-11-04 11:17:41 +00:00
|
|
|
use std::convert::AsRef;
|
2015-10-05 08:33:02 +00:00
|
|
|
use std::iter::{Filter, Peekable};
|
2016-06-05 11:57:18 +00:00
|
|
|
use std::str::Split;
|
2015-11-13 01:47:30 +00:00
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// A static slice of characters.
|
2014-04-05 08:04:34 +00:00
|
|
|
pub type StaticCharVec = &'static [char];
|
2016-12-31 11:19:02 +00:00
|
|
|
|
|
|
|
/// A static slice of `str`s.
|
2014-03-26 15:46:47 +00:00
|
|
|
pub type StaticStringVec = &'static [&'static str];
|
2014-02-14 12:25:58 +00:00
|
|
|
|
2014-03-14 14:16:57 +00:00
|
|
|
/// A "space character" according to:
|
|
|
|
///
|
2015-04-14 22:16:55 +00:00
|
|
|
/// https://html.spec.whatwg.org/multipage/#space-character
|
2014-04-05 08:04:34 +00:00
|
|
|
pub static HTML_SPACE_CHARACTERS: StaticCharVec = &[
|
2015-01-08 15:00:57 +00:00
|
|
|
'\u{0020}',
|
|
|
|
'\u{0009}',
|
|
|
|
'\u{000a}',
|
|
|
|
'\u{000c}',
|
|
|
|
'\u{000d}',
|
2014-03-14 14:16:57 +00:00
|
|
|
];
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Whether a character is a HTML whitespace character.
|
2016-04-20 08:54:53 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn char_is_whitespace(c: char) -> bool {
|
|
|
|
HTML_SPACE_CHARACTERS.contains(&c)
|
|
|
|
}
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Whether all the string is HTML whitespace.
|
|
|
|
#[inline]
|
2016-04-20 08:54:53 +00:00
|
|
|
pub fn is_whitespace(s: &str) -> bool {
|
|
|
|
s.chars().all(char_is_whitespace)
|
|
|
|
}
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
#[inline]
|
|
|
|
fn not_empty(&split: &&str) -> bool { !split.is_empty() }
|
|
|
|
|
|
|
|
/// Split a string on HTML whitespace.
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
pub fn split_html_space_chars<'a>(s: &'a str) ->
|
2015-02-12 00:24:45 +00:00
|
|
|
Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> {
|
2015-01-28 01:15:50 +00:00
|
|
|
s.split(HTML_SPACE_CHARACTERS).filter(not_empty as fn(&&str) -> bool)
|
2014-04-05 08:04:34 +00:00
|
|
|
}
|
2014-09-04 07:20:54 +00:00
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Split a string on commas.
|
|
|
|
#[inline]
|
2016-05-24 14:42:23 +00:00
|
|
|
pub fn split_commas<'a>(s: &'a str) -> Filter<Split<'a, char>, fn(&&str) -> bool> {
|
|
|
|
s.split(',').filter(not_empty as fn(&&str) -> bool)
|
|
|
|
}
|
2015-10-05 08:33:02 +00:00
|
|
|
|
|
|
|
fn is_ascii_digit(c: &char) -> bool {
|
|
|
|
match *c {
|
|
|
|
'0'...'9' => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:59:25 +00:00
|
|
|
fn is_decimal_point(c: char) -> bool {
|
|
|
|
c == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_exponent_char(c: char) -> bool {
|
|
|
|
match c {
|
|
|
|
'e' | 'E' => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 08:33:02 +00:00
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Read a set of ascii digits and read them into a number.
|
2016-04-26 18:59:25 +00:00
|
|
|
pub fn read_numbers<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> (Option<i64>, usize) {
|
2015-10-05 08:33:02 +00:00
|
|
|
match iter.peek() {
|
|
|
|
Some(c) if is_ascii_digit(c) => (),
|
2016-04-26 18:59:25 +00:00
|
|
|
_ => return (None, 0),
|
2015-10-05 08:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iter.take_while(is_ascii_digit).map(|d| {
|
|
|
|
d as i64 - '0' as i64
|
2016-04-26 18:59:25 +00:00
|
|
|
}).fold((Some(0i64), 0), |accumulator, d| {
|
|
|
|
let digits = accumulator.0.and_then(|accumulator| {
|
2015-10-05 08:33:02 +00:00
|
|
|
accumulator.checked_mul(10)
|
|
|
|
}).and_then(|accumulator| {
|
|
|
|
accumulator.checked_add(d)
|
2016-04-26 18:59:25 +00:00
|
|
|
});
|
|
|
|
(digits, accumulator.1 + 1)
|
2015-10-05 08:33:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Read a decimal fraction.
|
2016-04-26 18:59:25 +00:00
|
|
|
pub fn read_fraction<I: Iterator<Item=char>>(mut iter: Peekable<I>,
|
|
|
|
mut divisor: f64,
|
|
|
|
value: f64) -> (f64, usize) {
|
|
|
|
match iter.peek() {
|
|
|
|
Some(c) if is_decimal_point(*c) => (),
|
|
|
|
_ => return (value, 0),
|
|
|
|
}
|
|
|
|
iter.next();
|
|
|
|
|
|
|
|
iter.take_while(is_ascii_digit).map(|d|
|
|
|
|
d as i64 - '0' as i64
|
|
|
|
).fold((value, 1), |accumulator, d| {
|
|
|
|
divisor *= 10f64;
|
2016-12-31 11:19:02 +00:00
|
|
|
(accumulator.0 + d as f64 / divisor, accumulator.1 + 1)
|
2016-04-26 18:59:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Reads an exponent from an iterator over chars, for example `e100`.
|
2016-04-26 18:59:25 +00:00
|
|
|
pub fn read_exponent<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> Option<i32> {
|
|
|
|
match iter.peek() {
|
|
|
|
Some(c) if is_exponent_char(*c) => (),
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
iter.next();
|
|
|
|
|
|
|
|
match iter.peek() {
|
|
|
|
None => None,
|
|
|
|
Some(&'-') => {
|
|
|
|
iter.next();
|
|
|
|
read_numbers(iter).0.map(|exp| -exp.to_i32().unwrap_or(0))
|
|
|
|
}
|
|
|
|
Some(&'+') => {
|
|
|
|
iter.next();
|
|
|
|
read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0))
|
|
|
|
}
|
|
|
|
Some(_) => read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 11:19:02 +00:00
|
|
|
/// Join a set of strings with a given delimiter `join`.
|
2015-09-29 22:59:14 +00:00
|
|
|
pub fn str_join<I, T>(strs: I, join: &str) -> String
|
2016-12-31 11:19:02 +00:00
|
|
|
where I: IntoIterator<Item=T>,
|
|
|
|
T: AsRef<str>,
|
2015-09-29 22:59:14 +00:00
|
|
|
{
|
2015-10-01 01:34:44 +00:00
|
|
|
strs.into_iter().enumerate().fold(String::new(), |mut acc, (i, s)| {
|
|
|
|
if i > 0 { acc.push_str(join); }
|
2015-07-07 14:58:35 +00:00
|
|
|
acc.push_str(s.as_ref());
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2017-04-21 05:17:06 +00:00
|
|
|
|
|
|
|
/// Returns true if a given string has a given prefix with case-insensitive match.
|
|
|
|
pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool {
|
|
|
|
string.len() > prefix.len() &&
|
|
|
|
string[0..prefix.len()].eq_ignore_ascii_case(prefix)
|
|
|
|
}
|