diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2016-10-06 18:39:03 +0200 |
---|---|---|
committer | Simon Sapin <simon.sapin@exyr.org> | 2016-12-09 10:56:20 -1000 |
commit | 97344b150dc3532a881f3476d398c38c8305b8ae (patch) | |
tree | c13155f53011a7bc07aead496380667afbe29bdc /components/style/str.rs | |
parent | 120b003195383041b9f182fc03103e50f9249e49 (diff) | |
download | servo-97344b150dc3532a881f3476d398c38c8305b8ae.tar.gz servo-97344b150dc3532a881f3476d398c38c8305b8ae.zip |
cow_to_ascii_lowercase()
Diffstat (limited to 'components/style/str.rs')
-rw-r--r-- | components/style/str.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/components/style/str.rs b/components/style/str.rs index 73518625b6e..be763892118 100644 --- a/components/style/str.rs +++ b/components/style/str.rs @@ -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 +} |