Use AsciiExt in ByteString implementations.

This commit is contained in:
Ms2ger 2015-05-01 21:20:16 +02:00
parent 5f6b791e62
commit bd77acfbb8

View file

@ -4,6 +4,7 @@
//! The `ByteString` struct. //! The `ByteString` struct.
use std::ascii::AsciiExt;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::ops; use std::ops;
@ -36,20 +37,12 @@ impl ByteString {
/// Compare `self` to `other`, matching AZ and az as equal. /// Compare `self` to `other`, matching AZ and az as equal.
pub fn eq_ignore_case(&self, other: &ByteString) -> bool { pub fn eq_ignore_case(&self, other: &ByteString) -> bool {
// XXXManishearth make this more efficient self.0.eq_ignore_ascii_case(&other.0)
self.to_lower() == other.to_lower()
} }
/// Returns `self` with AZ replaced by az. /// Returns `self` with AZ replaced by az.
pub fn to_lower(&self) -> ByteString { pub fn to_lower(&self) -> ByteString {
let ByteString(ref vec) = *self; ByteString::new(self.0.to_ascii_lowercase())
ByteString::new(vec.iter().map(|&x| {
if x > 'A' as u8 && x < 'Z' as u8 {
x + ('a' as u8) - ('A' as u8)
} else {
x
}
}).collect())
} }
/// Returns whether `self` is a `token`, as defined by /// Returns whether `self` is a `token`, as defined by