Skip to content

Commit 4b96bd0

Browse files
author
Thomas Bahn
committed
Implement AsciiExt and OwnedAsciiExt
Closes #3
1 parent dbd150f commit 4b96bd0

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

src/lib.rs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
//! Operations on ASCII strings and characters
1414
15+
#![feature(ascii,std_misc)]
16+
1517
use std::fmt;
1618
use std::mem;
1719
use std::borrow::Borrow;
18-
use std::ascii::AsciiExt;
20+
use std::ascii::{AsciiExt, OwnedAsciiExt};
1921

2022
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
2123
#[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Hash, Copy)]
@@ -152,6 +154,89 @@ impl fmt::Debug for Ascii {
152154
// }
153155
// }
154156

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+
155240
/// Trait for converting into an ascii type.
156241
pub trait AsciiCast : AsciiExt {
157242
type Target;

0 commit comments

Comments
 (0)