2017-01-03 03:32:29 +00:00
|
|
|
//! Contains types that encapsulate uncased ASCII strings.
|
|
|
|
//!
|
|
|
|
//! An 'uncased' ASCII string is case-preserving. That is, the string itself
|
|
|
|
//! contains cased charaters, but comparison (including ordering, equaility, and
|
|
|
|
//! hashing) is case-insensitive.
|
|
|
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::borrow::{Cow, Borrow};
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::ascii::AsciiExt;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
/// A reference to an uncased (case-preserving) ASCII string. This is typically
|
|
|
|
/// created from an `&str` as follows:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
2017-03-30 22:38:51 +00:00
|
|
|
/// use rocket::http::ascii::UncasedStr;
|
2017-01-03 03:32:29 +00:00
|
|
|
///
|
2017-03-30 22:38:51 +00:00
|
|
|
/// let ascii_ref: &UncasedStr = "Hello, world!".into();
|
2017-01-03 03:32:29 +00:00
|
|
|
/// ```
|
|
|
|
#[derive(Debug)]
|
2017-03-30 22:38:51 +00:00
|
|
|
pub struct UncasedStr(str);
|
2017-01-03 03:32:29 +00:00
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl UncasedStr {
|
2017-03-31 00:52:02 +00:00
|
|
|
#[inline(always)]
|
2017-03-21 22:42:11 +00:00
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl PartialEq for UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &UncasedStr) -> bool {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.0.eq_ignore_ascii_case(&other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl PartialEq<str> for UncasedStr {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
self.0.eq_ignore_ascii_case(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl PartialEq<UncasedStr> for str {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &UncasedStr) -> bool {
|
2017-01-05 08:14:44 +00:00
|
|
|
other.0.eq_ignore_ascii_case(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> PartialEq<&'a str> for UncasedStr {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: & &'a str) -> bool {
|
|
|
|
self.0.eq_ignore_ascii_case(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> PartialEq<UncasedStr> for &'a str {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &UncasedStr) -> bool {
|
2017-01-05 08:14:44 +00:00
|
|
|
other.0.eq_ignore_ascii_case(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> From<&'a str> for &'a UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn from(string: &'a str) -> &'a UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
unsafe { ::std::mem::transmute(string) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl Eq for UncasedStr { }
|
2017-01-03 03:32:29 +00:00
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl Hash for UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
for byte in self.0.bytes() {
|
|
|
|
hasher.write_u8(byte.to_ascii_lowercase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl PartialOrd for UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn partial_cmp(&self, other: &UncasedStr) -> Option<Ordering> {
|
2017-01-03 03:32:29 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl Ord for UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
let self_chars = self.0.chars().map(|c| c.to_ascii_lowercase());
|
|
|
|
let other_chars = other.0.chars().map(|c| c.to_ascii_lowercase());
|
|
|
|
self_chars.cmp(other_chars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl fmt::Display for UncasedStr {
|
2017-03-21 09:04:07 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:32:29 +00:00
|
|
|
/// An uncased (case-preserving) ASCII string.
|
|
|
|
#[derive(Clone, Debug)]
|
2017-03-30 22:38:51 +00:00
|
|
|
pub struct Uncased<'s> {
|
2017-01-05 08:14:44 +00:00
|
|
|
pub string: Cow<'s, str>
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s> Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
/// Creates a new UncaseAscii string.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
2017-03-30 22:38:51 +00:00
|
|
|
/// use rocket::http::ascii::Uncased;
|
2017-01-03 03:32:29 +00:00
|
|
|
///
|
|
|
|
/// let uncased_ascii = UncasedAScii::new("Content-Type");
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
pub fn new<S: Into<Cow<'s, str>>>(string: S) -> Uncased<'s> {
|
|
|
|
Uncased { string: string.into() }
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts `self` into an owned `String`, allocating if necessary,
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
self.string.into_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the inner string.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
self.string.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the inner `Cow`.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_cow(self) -> Cow<'s, str> {
|
|
|
|
self.string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> Deref for Uncased<'a> {
|
|
|
|
type Target = UncasedStr;
|
2017-01-03 03:32:29 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn deref(&self) -> &UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.as_str().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> AsRef<UncasedStr> for Uncased<'a>{
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn as_ref(&self) -> &UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.as_str().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> Borrow<UncasedStr> for Uncased<'a> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn borrow(&self) -> &UncasedStr {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.as_str().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s, 'c: 's> From<&'c str> for Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn from(string: &'c str) -> Self {
|
2017-03-30 22:38:51 +00:00
|
|
|
Uncased::new(string)
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl From<String> for Uncased<'static> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn from(string: String) -> Self {
|
2017-03-30 22:38:51 +00:00
|
|
|
Uncased::new(string)
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s, 'c: 's> From<Cow<'c, str>> for Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn from(string: Cow<'c, str>) -> Self {
|
2017-03-30 22:38:51 +00:00
|
|
|
Uncased::new(string)
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s, 'c: 's, T: Into<Cow<'c, str>>> From<T> for Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
default fn from(string: T) -> Self {
|
2017-03-30 22:38:51 +00:00
|
|
|
Uncased::new(string)
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a, 'b> PartialOrd<Uncased<'b>> for Uncased<'a> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn partial_cmp(&self, other: &Uncased<'b>) -> Option<Ordering> {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.as_ref().partial_cmp(other.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> Ord for Uncased<'a> {
|
2017-01-03 03:32:29 +00:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.as_ref().cmp(other.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s> fmt::Display for Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.string.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a, 'b> PartialEq<Uncased<'b>> for Uncased<'a> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &Uncased<'b>) -> bool {
|
2017-01-03 03:32:29 +00:00
|
|
|
self.as_ref().eq(other.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a> PartialEq<str> for Uncased<'a> {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
self.as_ref().eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'b> PartialEq<Uncased<'b>> for str {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &Uncased<'b>) -> bool {
|
2017-01-05 08:14:44 +00:00
|
|
|
other.as_ref().eq(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a, 'b> PartialEq<&'b str> for Uncased<'a> {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: & &'b str) -> bool {
|
|
|
|
self.as_ref().eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'a, 'b> PartialEq<Uncased<'b>> for &'a str {
|
2017-01-05 08:14:44 +00:00
|
|
|
#[inline(always)]
|
2017-03-30 22:38:51 +00:00
|
|
|
fn eq(&self, other: &Uncased<'b>) -> bool {
|
2017-01-05 08:14:44 +00:00
|
|
|
other.as_ref().eq(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s> Eq for Uncased<'s> { }
|
2017-01-03 03:32:29 +00:00
|
|
|
|
2017-03-30 22:38:51 +00:00
|
|
|
impl<'s> Hash for Uncased<'s> {
|
2017-01-03 03:32:29 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
self.as_ref().hash(hasher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if `s1` and `s2` are equal without considering case. That is,
|
|
|
|
/// for ASCII strings, this function returns s1.to_lower() == s2.to_lower(), but
|
|
|
|
/// does it in a much faster way.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn uncased_eq<S1: AsRef<str>, S2: AsRef<str>>(s1: S1, s2: S2) -> bool {
|
2017-03-30 22:38:51 +00:00
|
|
|
let ascii_ref_1: &UncasedStr = s1.as_ref().into();
|
|
|
|
let ascii_ref_2: &UncasedStr = s2.as_ref().into();
|
2017-01-03 03:32:29 +00:00
|
|
|
ascii_ref_1 == ascii_ref_2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-03-30 22:38:51 +00:00
|
|
|
use super::Uncased;
|
2017-01-03 03:32:29 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
|
|
|
|
|
|
fn hash<T: Hash>(t: &T) -> u64 {
|
|
|
|
let mut s = DefaultHasher::new();
|
|
|
|
t.hash(&mut s);
|
|
|
|
s.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_uncased_eq {
|
|
|
|
($($string:expr),+) => ({
|
|
|
|
let mut strings = Vec::new();
|
2017-01-05 08:14:44 +00:00
|
|
|
$(strings.push($string);)+
|
2017-01-03 03:32:29 +00:00
|
|
|
|
|
|
|
for i in 0..strings.len() {
|
|
|
|
for j in i..strings.len() {
|
2017-01-05 08:14:44 +00:00
|
|
|
let (str_a, str_b) = (strings[i], strings[j]);
|
2017-03-30 22:38:51 +00:00
|
|
|
let ascii_a = Uncased::from(str_a);
|
|
|
|
let ascii_b = Uncased::from(str_b);
|
2017-01-05 08:14:44 +00:00
|
|
|
assert_eq!(ascii_a, ascii_b);
|
|
|
|
assert_eq!(hash(&ascii_a), hash(&ascii_b));
|
|
|
|
assert_eq!(ascii_a, str_a);
|
|
|
|
assert_eq!(ascii_b, str_b);
|
|
|
|
assert_eq!(ascii_a, str_b);
|
|
|
|
assert_eq!(ascii_b, str_a);
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_case_insensitive() {
|
|
|
|
assert_uncased_eq!["a", "A"];
|
|
|
|
assert_uncased_eq!["foobar", "FOOBAR", "FooBar", "fOObAr", "fooBAR"];
|
|
|
|
assert_uncased_eq!["", ""];
|
|
|
|
assert_uncased_eq!["content-type", "Content-Type", "CONTENT-TYPE"];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_case_cmp() {
|
2017-03-30 22:38:51 +00:00
|
|
|
assert!(Uncased::from("foobar") == Uncased::from("FOOBAR"));
|
|
|
|
assert!(Uncased::from("a") == Uncased::from("A"));
|
|
|
|
|
|
|
|
assert!(Uncased::from("a") < Uncased::from("B"));
|
|
|
|
assert!(Uncased::from("A") < Uncased::from("B"));
|
|
|
|
assert!(Uncased::from("A") < Uncased::from("b"));
|
|
|
|
|
|
|
|
assert!(Uncased::from("aa") > Uncased::from("a"));
|
|
|
|
assert!(Uncased::from("aa") > Uncased::from("A"));
|
|
|
|
assert!(Uncased::from("AA") > Uncased::from("a"));
|
|
|
|
assert!(Uncased::from("AA") > Uncased::from("a"));
|
|
|
|
assert!(Uncased::from("Aa") > Uncased::from("a"));
|
|
|
|
assert!(Uncased::from("Aa") > Uncased::from("A"));
|
|
|
|
assert!(Uncased::from("aA") > Uncased::from("a"));
|
|
|
|
assert!(Uncased::from("aA") > Uncased::from("A"));
|
2017-01-03 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|