about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-06-14 01:54:40 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-06-16 10:50:28 +1000
commit42974d3bc4ee091c6d6c586ff3c8568231290cd8 (patch)
tree12e8aa3574e6aa0b9413356f082a5a8a8ae1597c /src/libstd
parentf1886680e00850843e2524fba609ddba6a13180b (diff)
downloadrust-42974d3bc4ee091c6d6c586ff3c8568231290cd8.tar.gz
rust-42974d3bc4ee091c6d6c586ff3c8568231290cd8.zip
std: simplify the string comparison implementations, using iterators.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs44
1 files changed, 14 insertions, 30 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index d5beb755a27..1f823a078c4 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -530,54 +530,38 @@ pub fn eq(a: &~str, b: &~str) -> bool {
     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))]
 impl<'self> TotalOrd for &'self str {
     #[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))]
 impl TotalOrd for ~str {
     #[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))]
 impl TotalOrd for @str {
     #[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
 #[inline]
 fn lt(a: &str, b: &str) -> bool {
-    let (a_len, b_len) = (a.len(), b.len());
-    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;
+    a.cmp(& b) == Less
 }
 
 /// Bytewise less than or equal