Bug 1371395 Part 1: Create a Servo method for cheaply getting an ascii lowercase converted string.
MozReview-Commit-ID: 1Tfrs9PBkhA
--- a/servo/components/style/lib.rs
+++ b/servo/components/style/lib.rs
@@ -32,16 +32,18 @@
// code by default.
//
// [1] https://github.com/rust-lang/rust/issues/15701#issuecomment-251900615
//#![deny(unsafe_code)]
#![allow(unused_unsafe)]
#![recursion_limit = "500"] // For define_css_keyword_enum! in -moz-appearance
+#![feature(exclusive_range_pattern)] // Useful for some ascii char detection.
+
extern crate app_units;
extern crate arrayvec;
extern crate atomic_refcell;
extern crate bit_vec;
#[macro_use]
extern crate bitflags;
#[allow(unused_extern_crates)] extern crate byteorder;
#[cfg(feature = "gecko")] #[macro_use] #[no_link] extern crate cfg_if;
--- a/servo/components/style/str.rs
+++ b/servo/components/style/str.rs
@@ -1,21 +1,23 @@
/* 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/. */
//! String utils for attributes and similar stuff.
#![deny(missing_docs)]
+
use num_traits::ToPrimitive;
use std::ascii::AsciiExt;
use std::convert::AsRef;
use std::iter::{Filter, Peekable};
use std::str::Split;
+use std::borrow::Cow;
/// A static slice of characters.
pub type StaticCharVec = &'static [char];
/// A static slice of `str`s.
pub type StaticStringVec = &'static [&'static str];
/// A "space character" according to:
@@ -146,8 +148,18 @@ pub fn str_join<I, T>(strs: I, join: &st
})
}
/// 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.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
}
+
+/// Returns an ascii lowercase version of a string, only allocating if needed.
+pub fn string_as_ascii_lowercase<'a>(input: &'a str) -> Cow<'a, str> {
+ if input.bytes().any(|c| matches!(c, b'A'..b'Z')) {
+ input.to_ascii_lowercase().into()
+ } else {
+ // Already ascii lowercase.
+ Cow::Borrowed(input)
+ }
+}