std: continue improving the comparison trait impls for str.

This moves them all into the traits submodule, and delegates Ord
to the TotalOrd instance. It also deletes the stand-alone lt, gt,
ge and le functions.
This commit is contained in:
Huon Wilson
2013-06-14 13:37:47 +10:00
parent 42974d3bc4
commit 4686ed1a1d
2 changed files with 129 additions and 168 deletions

View File

@@ -26,7 +26,6 @@ use core::either;
use core::io; use core::io;
use core::option; use core::option;
use core::result; use core::result;
use core::str;
use core::task; use core::task;
use core::to_str::ToStr; use core::to_str::ToStr;
use core::uint; use core::uint;
@@ -542,7 +541,7 @@ pub fn filter_tests(
// Sort the tests alphabetically // Sort the tests alphabetically
fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool { fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
str::le(t1.desc.name.to_str(), t2.desc.name.to_str()) t1.desc.name.to_str() < t2.desc.name.to_str()
} }
sort::quick_sort(filtered, lteq); sort::quick_sort(filtered, lteq);

View File

@@ -23,7 +23,6 @@ use cast;
use char; use char;
use char::Char; use char::Char;
use clone::Clone; use clone::Clone;
use cmp::{TotalOrd, Ordering, Less, Equal, Greater};
use container::Container; use container::Container;
use iter::Times; use iter::Times;
use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator}; use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
@@ -37,8 +36,6 @@ use uint;
use vec; use vec;
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector}; use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector};
#[cfg(not(test))] use cmp::{Eq, Ord, Equiv, TotalEq};
/* /*
Section: Conditions Section: Conditions
*/ */
@@ -530,165 +527,6 @@ pub fn eq(a: &~str, b: &~str) -> bool {
eq_slice(*a, *b) eq_slice(*a, *b)
} }
#[cfg(not(test))]
impl<'self> TotalOrd for &'self str {
#[inline]
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 { self.as_slice().cmp(&other.as_slice()) }
}
#[cfg(not(test))]
impl TotalOrd for @str {
#[inline]
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 {
a.cmp(& b) == Less
}
/// Bytewise less than or equal
#[inline]
pub fn le(a: &str, b: &str) -> bool {
!lt(b, a)
}
/// Bytewise greater than or equal
#[inline]
fn ge(a: &str, b: &str) -> bool {
!lt(a, b)
}
/// Bytewise greater than
#[inline]
fn gt(a: &str, b: &str) -> bool {
!le(a, b)
}
#[cfg(not(test))]
impl<'self> Eq for &'self str {
#[inline(always)]
fn eq(&self, other: & &'self str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: & &'self str) -> bool { !(*self).eq(other) }
}
#[cfg(not(test))]
impl Eq for ~str {
#[inline(always)]
fn eq(&self, other: &~str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: &~str) -> bool { !(*self).eq(other) }
}
#[cfg(not(test))]
impl Eq for @str {
#[inline(always)]
fn eq(&self, other: &@str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: &@str) -> bool { !(*self).eq(other) }
}
#[cfg(not(test))]
impl<'self> TotalEq for &'self str {
#[inline(always)]
fn equals(&self, other: & &'self str) -> bool {
eq_slice((*self), (*other))
}
}
#[cfg(not(test))]
impl TotalEq for ~str {
#[inline(always)]
fn equals(&self, other: &~str) -> bool {
eq_slice((*self), (*other))
}
}
#[cfg(not(test))]
impl TotalEq for @str {
#[inline(always)]
fn equals(&self, other: &@str) -> bool {
eq_slice((*self), (*other))
}
}
#[cfg(not(test))]
impl Ord for ~str {
#[inline(always)]
fn lt(&self, other: &~str) -> bool { lt((*self), (*other)) }
#[inline(always)]
fn le(&self, other: &~str) -> bool { le((*self), (*other)) }
#[inline(always)]
fn ge(&self, other: &~str) -> bool { ge((*self), (*other)) }
#[inline(always)]
fn gt(&self, other: &~str) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<'self> Ord for &'self str {
#[inline(always)]
fn lt(&self, other: & &'self str) -> bool { lt((*self), (*other)) }
#[inline(always)]
fn le(&self, other: & &'self str) -> bool { le((*self), (*other)) }
#[inline(always)]
fn ge(&self, other: & &'self str) -> bool { ge((*self), (*other)) }
#[inline(always)]
fn gt(&self, other: & &'self str) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl Ord for @str {
#[inline(always)]
fn lt(&self, other: &@str) -> bool { lt((*self), (*other)) }
#[inline(always)]
fn le(&self, other: &@str) -> bool { le((*self), (*other)) }
#[inline(always)]
fn ge(&self, other: &@str) -> bool { ge((*self), (*other)) }
#[inline(always)]
fn gt(&self, other: &@str) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<'self, S: Str> Equiv<S> for &'self str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
#[cfg(not(test))]
impl<'self, S: Str> Equiv<S> for @str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
#[cfg(not(test))]
impl<'self, S: Str> Equiv<S> for ~str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
/* /*
Section: Searching Section: Searching
*/ */
@@ -1072,12 +910,136 @@ pub mod raw {
#[cfg(not(test))] #[cfg(not(test))]
pub mod traits { pub mod traits {
use ops::Add; use ops::Add;
use cmp::{TotalOrd, Ordering, Less, Equal, Greater, Eq, Ord, Equiv, TotalEq};
use super::{Str, eq_slice};
impl<'self> Add<&'self str,~str> for ~str { impl<'self> Add<&'self str,~str> for ~str {
#[inline(always)] #[inline(always)]
fn add(&self, rhs: & &'self str) -> ~str { fn add(&self, rhs: & &'self str) -> ~str {
self.append((*rhs)) self.append((*rhs))
} }
} }
impl<'self> TotalOrd for &'self str {
#[inline]
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())
}
}
impl TotalOrd for ~str {
#[inline]
fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl TotalOrd for @str {
#[inline]
fn cmp(&self, other: &@str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl<'self> Eq for &'self str {
#[inline(always)]
fn eq(&self, other: & &'self str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: & &'self str) -> bool { !(*self).eq(other) }
}
impl Eq for ~str {
#[inline(always)]
fn eq(&self, other: &~str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: &~str) -> bool { !(*self).eq(other) }
}
impl Eq for @str {
#[inline(always)]
fn eq(&self, other: &@str) -> bool {
eq_slice((*self), (*other))
}
#[inline(always)]
fn ne(&self, other: &@str) -> bool { !(*self).eq(other) }
}
impl<'self> TotalEq for &'self str {
#[inline(always)]
fn equals(&self, other: & &'self str) -> bool {
eq_slice((*self), (*other))
}
}
impl TotalEq for ~str {
#[inline(always)]
fn equals(&self, other: &~str) -> bool {
eq_slice((*self), (*other))
}
}
impl TotalEq for @str {
#[inline(always)]
fn equals(&self, other: &@str) -> bool {
eq_slice((*self), (*other))
}
}
impl<'self> Ord for &'self str {
#[inline(always)]
fn lt(&self, other: & &'self str) -> bool { self.cmp(other) == Less }
#[inline(always)]
fn le(&self, other: & &'self str) -> bool { self.cmp(other) != Greater }
#[inline(always)]
fn ge(&self, other: & &'self str) -> bool { self.cmp(other) != Less }
#[inline(always)]
fn gt(&self, other: & &'self str) -> bool { self.cmp(other) == Greater }
}
impl Ord for ~str {
#[inline(always)]
fn lt(&self, other: &~str) -> bool { self.cmp(other) == Less }
#[inline(always)]
fn le(&self, other: &~str) -> bool { self.cmp(other) != Greater }
#[inline(always)]
fn ge(&self, other: &~str) -> bool { self.cmp(other) != Less }
#[inline(always)]
fn gt(&self, other: &~str) -> bool { self.cmp(other) == Greater }
}
impl Ord for @str {
#[inline(always)]
fn lt(&self, other: &@str) -> bool { self.cmp(other) == Less }
#[inline(always)]
fn le(&self, other: &@str) -> bool { self.cmp(other) != Greater }
#[inline(always)]
fn ge(&self, other: &@str) -> bool { self.cmp(other) != Less }
#[inline(always)]
fn gt(&self, other: &@str) -> bool { self.cmp(other) == Greater }
}
impl<'self, S: Str> Equiv<S> for &'self str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
impl<'self, S: Str> Equiv<S> for @str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
impl<'self, S: Str> Equiv<S> for ~str {
#[inline(always)]
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}
} }
#[cfg(test)] #[cfg(test)]
@@ -2267,10 +2229,10 @@ mod tests {
#[test] #[test]
fn test_le() { fn test_le() {
assert!((le(&"", &""))); assert!("" <= "");
assert!((le(&"", &"foo"))); assert!("" <= "foo");
assert!((le(&"foo", &"foo"))); assert!("foo" <= "foo");
assert!((!eq(&~"foo", &~"bar"))); assert!("foo" != ~"bar");
} }
#[test] #[test]