mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
cow_to_ascii_lowercase()
This commit is contained in:
parent
120b003195
commit
97344b150d
5 changed files with 27 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2636,6 +2636,7 @@ dependencies = [
|
|||
"cssparser 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"euclid 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"html5ever-atoms 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"owning_ref 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use num_traits::ToPrimitive;
|
||||
use std::ascii::AsciiExt;
|
||||
use std::borrow::Cow;
|
||||
use std::convert::AsRef;
|
||||
use std::iter::{Filter, Peekable};
|
||||
use std::str::Split;
|
||||
|
@ -125,3 +127,13 @@ pub fn str_join<I, T>(strs: I, join: &str) -> String
|
|||
acc
|
||||
})
|
||||
}
|
||||
|
||||
/// Like AsciiExt::to_ascii_lowercase, but avoids allocating when the input is already lower-case.
|
||||
pub fn cow_into_ascii_lowercase<'a, S: Into<Cow<'a, str>>>(s: S) -> Cow<'a, str> {
|
||||
let mut cow = s.into();
|
||||
match cow.bytes().position(|byte| byte >= b'A' && byte <= b'Z') {
|
||||
Some(first_uppercase) => cow.to_mut()[first_uppercase..].make_ascii_lowercase(),
|
||||
None => {}
|
||||
}
|
||||
cow
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ testing = ["style/testing"]
|
|||
app_units = "0.3"
|
||||
cssparser = {version = "0.7", features = ["heap_size"]}
|
||||
euclid = "0.10.1"
|
||||
matches = "0.1"
|
||||
owning_ref = "0.2.2"
|
||||
parking_lot = "0.3"
|
||||
rustc-serialize = "0.3"
|
||||
|
|
|
@ -10,6 +10,7 @@ extern crate app_units;
|
|||
extern crate cssparser;
|
||||
extern crate euclid;
|
||||
#[macro_use] extern crate html5ever_atoms;
|
||||
#[macro_use] #[allow(unused_extern_crates)] extern crate matches;
|
||||
extern crate owning_ref;
|
||||
extern crate parking_lot;
|
||||
extern crate rustc_serialize;
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
* 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/. */
|
||||
|
||||
use style::str::{split_html_space_chars, str_join};
|
||||
use std::borrow::Cow;
|
||||
use style::str::{split_html_space_chars, str_join, cow_into_ascii_lowercase};
|
||||
|
||||
#[test]
|
||||
pub fn split_html_space_chars_whitespace() {
|
||||
|
@ -33,3 +34,13 @@ pub fn test_str_join_many() {
|
|||
let expected = "-alpha--beta-gamma-";
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_cow_into_ascii_lowercase() {
|
||||
assert!(matches!(cow_into_ascii_lowercase("abc.d"), Cow::Borrowed("abc.d")));
|
||||
let string = String::from("abc.d");
|
||||
assert!(matches!(cow_into_ascii_lowercase(string), Cow::Owned(ref s) if s == "abc.d"));
|
||||
assert!(matches!(cow_into_ascii_lowercase("Abc.d"), Cow::Owned(ref s) if s == "abc.d"));
|
||||
assert!(matches!(cow_into_ascii_lowercase("aBC.D"), Cow::Owned(ref s) if s == "abc.d"));
|
||||
assert!(matches!(cow_into_ascii_lowercase("abc.D"), Cow::Owned(ref s) if s == "abc.d"));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue