Bug 1371395 Part 1: Create a Servo method for cheaply getting an ascii lowercase converted string.
MozReview-Commit-ID: 1Tfrs9PBkhA
--- a/servo/components/style/str.rs
+++ b/servo/components/style/str.rs
@@ -3,16 +3,17 @@
* 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::borrow::Cow;
use std::convert::AsRef;
use std::iter::{Filter, Peekable};
use std::str::Split;
/// A static slice of characters.
pub type StaticCharVec = &'static [char];
/// A static slice of `str`s.
@@ -146,8 +147,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)
+ }
+}