Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5
//! String utils for attributes and similar stuff.
6
7
#![deny(missing_docs)]
8
9
use num_traits::ToPrimitive;
10
use std::borrow::Cow;
11
use std::convert::AsRef;
12
use std::fmt::{self, Write};
13
use std::iter::{Filter, Peekable};
14
use std::str::Split;
15
16
/// A static slice of characters.
17
pub type StaticCharVec = &'static [char];
18
19
/// A static slice of `str`s.
20
pub type StaticStringVec = &'static [&'static str];
21
22
/// A "space character" according to:
23
///
25
pub static HTML_SPACE_CHARACTERS: StaticCharVec =
26
&['\u{0020}', '\u{0009}', '\u{000a}', '\u{000c}', '\u{000d}'];
27
28
/// Whether a character is a HTML whitespace character.
29
#[inline]
30
pub fn char_is_whitespace(c: char) -> bool {
31
HTML_SPACE_CHARACTERS.contains(&c)
32
}
33
34
/// Whether all the string is HTML whitespace.
35
#[inline]
36
pub fn is_whitespace(s: &str) -> bool {
37
s.chars().all(char_is_whitespace)
38
}
39
40
#[inline]
41
fn not_empty(&split: &&str) -> bool {
42
!split.is_empty()
43
}
44
45
/// Split a string on HTML whitespace.
46
#[inline]
47
pub fn split_html_space_chars<'a>(
48
s: &'a str,
49
) -> Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> {
50
s.split(HTML_SPACE_CHARACTERS)
51
.filter(not_empty as fn(&&str) -> bool)
52
}
53
54
/// Split a string on commas.
55
#[inline]
56
pub fn split_commas<'a>(s: &'a str) -> Filter<Split<'a, char>, fn(&&str) -> bool> {
57
s.split(',').filter(not_empty as fn(&&str) -> bool)
58
}
59
60
/// Character is ascii digit
61
pub fn is_ascii_digit(c: &char) -> bool {
62
match *c {
63
'0'..='9' => true,
64
_ => false,
65
}
66
}
67
68
fn is_decimal_point(c: char) -> bool {
69
c == '.'
70
}
71
72
fn is_exponent_char(c: char) -> bool {
73
match c {
74
'e' | 'E' => true,
75
_ => false,
76
}
77
}
78
79
/// Read a set of ascii digits and read them into a number.
80
pub fn read_numbers<I: Iterator<Item = char>>(mut iter: Peekable<I>) -> (Option<i64>, usize) {
81
match iter.peek() {
82
Some(c) if is_ascii_digit(c) => (),
83
_ => return (None, 0),
84
}
85
86
iter.take_while(is_ascii_digit)
87
.map(|d| d as i64 - '0' as i64)
88
.fold((Some(0i64), 0), |accumulator, d| {
89
let digits = accumulator
90
.0
91
.and_then(|accumulator| accumulator.checked_mul(10))
92
.and_then(|accumulator| accumulator.checked_add(d));
93
(digits, accumulator.1 + 1)
94
})
95
}
96
97
/// Read a decimal fraction.
98
pub fn read_fraction<I: Iterator<Item = char>>(
99
mut iter: Peekable<I>,
100
mut divisor: f64,
101
value: f64,
102
) -> (f64, usize) {
103
match iter.peek() {
104
Some(c) if is_decimal_point(*c) => (),
105
_ => return (value, 0),
106
}
107
iter.next();
108
109
iter.take_while(is_ascii_digit)
110
.map(|d| d as i64 - '0' as i64)
111
.fold((value, 1), |accumulator, d| {
112
divisor *= 10f64;
113
(accumulator.0 + d as f64 / divisor, accumulator.1 + 1)
114
})
115
}
116
117
/// Reads an exponent from an iterator over chars, for example `e100`.
118
pub fn read_exponent<I: Iterator<Item = char>>(mut iter: Peekable<I>) -> Option<i32> {
119
match iter.peek() {
120
Some(c) if is_exponent_char(*c) => (),
121
_ => return None,
122
}
123
iter.next();
124
125
match iter.peek() {
126
None => None,
127
Some(&'-') => {
128
iter.next();
129
read_numbers(iter).0.map(|exp| -exp.to_i32().unwrap_or(0))
130
},
131
Some(&'+') => {
132
iter.next();
133
read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0))
134
},
135
Some(_) => read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0)),
136
}
137
}
138
139
/// Join a set of strings with a given delimiter `join`.
140
pub fn str_join<I, T>(strs: I, join: &str) -> String
141
where
142
I: IntoIterator<Item = T>,
143
T: AsRef<str>,
144
{
145
strs.into_iter()
146
.enumerate()
147
.fold(String::new(), |mut acc, (i, s)| {
148
if i > 0 {
149
acc.push_str(join);
150
}
151
acc.push_str(s.as_ref());
152
acc
153
})
154
}
155
156
/// Returns true if a given string has a given prefix with case-insensitive match.
157
pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool {
158
string.len() >= prefix.len() &&
159
string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
160
}
161
162
/// Returns an ascii lowercase version of a string, only allocating if needed.
163
pub fn string_as_ascii_lowercase<'a>(input: &'a str) -> Cow<'a, str> {
164
if input.bytes().any(|c| matches!(c, b'A'..=b'Z')) {
165
input.to_ascii_lowercase().into()
166
} else {
167
// Already ascii lowercase.
168
Cow::Borrowed(input)
169
}
170
}
171
172
/// To avoid accidentally instantiating multiple monomorphizations of large
173
/// serialization routines, we define explicit concrete types and require
174
/// them in those routines. This primarily avoids accidental mixing of UTF8
175
/// with UTF16 serializations in Gecko.
176
#[cfg(feature = "gecko")]
177
pub type CssStringWriter = ::nsstring::nsAString;
178
179
/// String type that coerces to CssStringWriter, used when serialization code
180
/// needs to allocate a temporary string.
181
#[cfg(feature = "gecko")]
182
pub type CssString = ::nsstring::nsString;
183
184
/// Certain serialization code needs to interact with borrowed strings, which
185
/// are sometimes native UTF8 Rust strings, and other times serialized UTF16
186
/// strings. This enum multiplexes the two cases.
187
#[cfg(feature = "gecko")]
188
pub enum CssStringBorrow<'a> {
189
/// A borrow of a UTF16 CssString.
190
UTF16(&'a ::nsstring::nsString),
191
/// A borrow of a regular Rust UTF8 string.
192
UTF8(&'a str),
193
}
194
195
#[cfg(feature = "gecko")]
196
impl<'a> CssStringBorrow<'a> {
197
/// Writes the borrowed string to the provided writer.
198
pub fn append_to(&self, dest: &mut CssStringWriter) -> fmt::Result {
199
match *self {
200
CssStringBorrow::UTF16(s) => {
201
dest.append(s);
202
Ok(())
203
},
204
CssStringBorrow::UTF8(s) => dest.write_str(s),
205
}
206
}
207
208
/// Returns true of the borrowed string is empty.
209
pub fn is_empty(&self) -> bool {
210
match *self {
211
CssStringBorrow::UTF16(s) => s.is_empty(),
212
CssStringBorrow::UTF8(s) => s.is_empty(),
213
}
214
}
215
}
216
217
#[cfg(feature = "gecko")]
218
impl<'a> From<&'a str> for CssStringBorrow<'a> {
219
fn from(s: &'a str) -> Self {
220
CssStringBorrow::UTF8(s)
221
}
222
}
223
224
#[cfg(feature = "gecko")]
225
impl<'a> From<&'a ::nsstring::nsString> for CssStringBorrow<'a> {
226
fn from(s: &'a ::nsstring::nsString) -> Self {
227
CssStringBorrow::UTF16(s)
228
}
229
}
230
231
/// String. The comments for the Gecko types explain the need for this abstraction.
232
#[cfg(feature = "servo")]
233
pub type CssStringWriter = String;
234
235
/// String. The comments for the Gecko types explain the need for this abstraction.
236
#[cfg(feature = "servo")]
237
pub type CssString = String;
238
239
/// Borrowed string. The comments for the Gecko types explain the need for this abstraction.
240
#[cfg(feature = "servo")]
241
pub struct CssStringBorrow<'a>(&'a str);
242
243
#[cfg(feature = "servo")]
244
impl<'a> CssStringBorrow<'a> {
245
/// Appends the borrowed string to the given string.
246
pub fn append_to(&self, dest: &mut CssStringWriter) -> fmt::Result {
247
dest.write_str(self.0)
248
}
249
250
/// Returns true if the borrowed string is empty.
251
pub fn is_empty(&self) -> bool {
252
self.0.is_empty()
253
}
254
}
255
256
#[cfg(feature = "servo")]
257
impl<'a> From<&'a str> for CssStringBorrow<'a> {
258
fn from(s: &'a str) -> Self {
259
CssStringBorrow(s)
260
}
261
}
262
263
#[cfg(feature = "servo")]
264
impl<'a> From<&'a String> for CssStringBorrow<'a> {
265
fn from(s: &'a String) -> Self {
266
CssStringBorrow(&*s)
267
}
268
}