Skip to content

Commit 42974d3

Browse files
committed
std: simplify the string comparison implementations, using iterators.
1 parent f188668 commit 42974d3

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/libstd/str.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -530,54 +530,38 @@ pub fn eq(a: &~str, b: &~str) -> bool {
530530
eq_slice(*a, *b)
531531
}
532532

533-
#[inline]
534-
fn cmp(a: &str, b: &str) -> Ordering {
535-
let low = uint::min(a.len(), b.len());
536-
537-
for uint::range(0, low) |idx| {
538-
match a[idx].cmp(&b[idx]) {
539-
Greater => return Greater,
540-
Less => return Less,
541-
Equal => ()
542-
}
543-
}
544-
545-
a.len().cmp(&b.len())
546-
}
547-
548533
#[cfg(not(test))]
549534
impl<'self> TotalOrd for &'self str {
550535
#[inline]
551-
fn cmp(&self, other: & &'self str) -> Ordering { cmp(*self, *other) }
536+
fn cmp(&self, other: & &'self str) -> Ordering {
537+
for self.bytes_iter().zip(other.bytes_iter()).advance |(s_b, o_b)| {
538+
match s_b.cmp(&o_b) {
539+
Greater => return Greater,
540+
Less => return Less,
541+
Equal => ()
542+
}
543+
}
544+
545+
self.len().cmp(&other.len())
546+
}
552547
}
553548

554549
#[cfg(not(test))]
555550
impl TotalOrd for ~str {
556551
#[inline]
557-
fn cmp(&self, other: &~str) -> Ordering { cmp(*self, *other) }
552+
fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
558553
}
559554

560555
#[cfg(not(test))]
561556
impl TotalOrd for @str {
562557
#[inline]
563-
fn cmp(&self, other: &@str) -> Ordering { cmp(*self, *other) }
558+
fn cmp(&self, other: &@str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
564559
}
565560

566561
/// Bytewise slice less than
567562
#[inline]
568563
fn lt(a: &str, b: &str) -> bool {
569-
let (a_len, b_len) = (a.len(), b.len());
570-
let end = uint::min(a_len, b_len);
571-
572-
let mut i = 0;
573-
while i < end {
574-
let (c_a, c_b) = (a[i], b[i]);
575-
if c_a < c_b { return true; }
576-
if c_a > c_b { return false; }
577-
i += 1;
578-
}
579-
580-
return a_len < b_len;
564+
a.cmp(& b) == Less
581565
}
582566

583567
/// Bytewise less than or equal

0 commit comments

Comments
 (0)