|
12 | 12 |
|
13 | 13 | //! Operations on ASCII strings and characters
|
14 | 14 |
|
| 15 | +#![feature(ascii,std_misc)] |
| 16 | + |
15 | 17 | use std::fmt;
|
16 | 18 | use std::mem;
|
17 | 19 | use std::borrow::Borrow;
|
18 |
| -use std::ascii::AsciiExt; |
| 20 | +use std::ascii::{AsciiExt, OwnedAsciiExt}; |
19 | 21 |
|
20 | 22 | /// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
|
21 | 23 | #[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Hash, Copy)]
|
@@ -152,6 +154,89 @@ impl fmt::Debug for Ascii {
|
152 | 154 | // }
|
153 | 155 | // }
|
154 | 156 |
|
| 157 | +impl AsciiExt for Ascii { |
| 158 | + type Owned = Ascii; |
| 159 | + |
| 160 | + #[inline] |
| 161 | + fn is_ascii(&self) -> bool { |
| 162 | + true |
| 163 | + } |
| 164 | + |
| 165 | + fn to_ascii_uppercase(&self) -> Ascii { |
| 166 | + self.to_uppercase() |
| 167 | + } |
| 168 | + |
| 169 | + fn to_ascii_lowercase(&self) -> Ascii { |
| 170 | + self.to_lowercase() |
| 171 | + } |
| 172 | + |
| 173 | + fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 174 | + self.chr.eq_ignore_ascii_case(&other.chr) |
| 175 | + } |
| 176 | + |
| 177 | + #[inline] |
| 178 | + fn make_ascii_uppercase(&mut self) { |
| 179 | + self.chr.make_ascii_uppercase() |
| 180 | + } |
| 181 | + |
| 182 | + #[inline] |
| 183 | + fn make_ascii_lowercase(&mut self) { |
| 184 | + self.chr.make_ascii_lowercase() |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +impl AsciiExt for [Ascii] { |
| 189 | + type Owned = Vec<Ascii>; |
| 190 | + |
| 191 | + #[inline] |
| 192 | + fn is_ascii(&self) -> bool { |
| 193 | + true |
| 194 | + } |
| 195 | + |
| 196 | + fn to_ascii_uppercase(&self) -> Vec<Ascii> { |
| 197 | + let mut vec = self.to_vec(); |
| 198 | + vec.make_ascii_uppercase(); |
| 199 | + vec |
| 200 | + } |
| 201 | + |
| 202 | + fn to_ascii_lowercase(&self) -> Vec<Ascii> { |
| 203 | + let mut vec = self.to_vec(); |
| 204 | + vec.make_ascii_lowercase(); |
| 205 | + vec |
| 206 | + } |
| 207 | + |
| 208 | + fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 209 | + self.len() == other.len() && |
| 210 | + self.iter().zip(other.iter()).all(|(a, b)| a.eq_ignore_ascii_case(b)) |
| 211 | + } |
| 212 | + |
| 213 | + fn make_ascii_uppercase(&mut self) { |
| 214 | + for ascii in self { |
| 215 | + ascii.make_ascii_uppercase(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + fn make_ascii_lowercase(&mut self) { |
| 220 | + for ascii in self { |
| 221 | + ascii.make_ascii_lowercase(); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +impl OwnedAsciiExt for Vec<Ascii> { |
| 227 | + #[inline] |
| 228 | + fn into_ascii_uppercase(mut self) -> Vec<Ascii> { |
| 229 | + self.make_ascii_uppercase(); |
| 230 | + self |
| 231 | + } |
| 232 | + |
| 233 | + #[inline] |
| 234 | + fn into_ascii_lowercase(mut self) -> Vec<Ascii> { |
| 235 | + self.make_ascii_lowercase(); |
| 236 | + self |
| 237 | + } |
| 238 | +} |
| 239 | + |
155 | 240 | /// Trait for converting into an ascii type.
|
156 | 241 | pub trait AsciiCast : AsciiExt {
|
157 | 242 | type Target;
|
|
0 commit comments