Rework MediaType to be an atom-based struct instead of an enum.

MozReview-Commit-ID: 1Tfrs9PBkhA
This commit is contained in:
Brad Werth 2017-08-07 10:30:48 -07:00 committed by Brad Werth
parent 77cb5371b3
commit 557ffa979d
9 changed files with 92 additions and 72 deletions

View file

@ -8,6 +8,7 @@
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;
@ -151,3 +152,13 @@ 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)
}
}