mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-28 00:15:23 +00:00
std: simplify the string comparison implementations, using iterators.
This commit is contained in:
parent
f1886680e0
commit
42974d3bc4
@ -530,54 +530,38 @@ pub fn eq(a: &~str, b: &~str) -> bool {
|
|||||||
eq_slice(*a, *b)
|
eq_slice(*a, *b)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn cmp(a: &str, b: &str) -> Ordering {
|
|
||||||
let low = uint::min(a.len(), b.len());
|
|
||||||
|
|
||||||
for uint::range(0, low) |idx| {
|
|
||||||
match a[idx].cmp(&b[idx]) {
|
|
||||||
Greater => return Greater,
|
|
||||||
Less => return Less,
|
|
||||||
Equal => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
a.len().cmp(&b.len())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl<'self> TotalOrd for &'self str {
|
impl<'self> TotalOrd for &'self str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cmp(&self, other: & &'self str) -> Ordering { cmp(*self, *other) }
|
fn cmp(&self, other: & &'self str) -> Ordering {
|
||||||
|
for self.bytes_iter().zip(other.bytes_iter()).advance |(s_b, o_b)| {
|
||||||
|
match s_b.cmp(&o_b) {
|
||||||
|
Greater => return Greater,
|
||||||
|
Less => return Less,
|
||||||
|
Equal => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.len().cmp(&other.len())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl TotalOrd for ~str {
|
impl TotalOrd for ~str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cmp(&self, other: &~str) -> Ordering { cmp(*self, *other) }
|
fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
impl TotalOrd for @str {
|
impl TotalOrd for @str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cmp(&self, other: &@str) -> Ordering { cmp(*self, *other) }
|
fn cmp(&self, other: &@str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bytewise slice less than
|
/// Bytewise slice less than
|
||||||
#[inline]
|
#[inline]
|
||||||
fn lt(a: &str, b: &str) -> bool {
|
fn lt(a: &str, b: &str) -> bool {
|
||||||
let (a_len, b_len) = (a.len(), b.len());
|
a.cmp(& b) == Less
|
||||||
let end = uint::min(a_len, b_len);
|
|
||||||
|
|
||||||
let mut i = 0;
|
|
||||||
while i < end {
|
|
||||||
let (c_a, c_b) = (a[i], b[i]);
|
|
||||||
if c_a < c_b { return true; }
|
|
||||||
if c_a > c_b { return false; }
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return a_len < b_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bytewise less than or equal
|
/// Bytewise less than or equal
|
||||||
|
Loading…
Reference in New Issue
Block a user