Skip to content

Commit 4686ed1

Browse files
committed
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.
1 parent 42974d3 commit 4686ed1

File tree

2 files changed

+129
-168
lines changed

2 files changed

+129
-168
lines changed

src/libextra/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use core::either;
2626
use core::io;
2727
use core::option;
2828
use core::result;
29-
use core::str;
3029
use core::task;
3130
use core::to_str::ToStr;
3231
use core::uint;
@@ -542,7 +541,7 @@ pub fn filter_tests(
542541
543542
// Sort the tests alphabetically
544543
fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
545-
str::le(t1.desc.name.to_str(), t2.desc.name.to_str())
544+
t1.desc.name.to_str() < t2.desc.name.to_str()
546545
}
547546
sort::quick_sort(filtered, lteq);
548547

src/libstd/str.rs

Lines changed: 128 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use cast;
2323
use char;
2424
use char::Char;
2525
use clone::Clone;
26-
use cmp::{TotalOrd, Ordering, Less, Equal, Greater};
2726
use container::Container;
2827
use iter::Times;
2928
use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
@@ -37,8 +36,6 @@ use uint;
3736
use vec;
3837
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector};
3938

40-
#[cfg(not(test))] use cmp::{Eq, Ord, Equiv, TotalEq};
41-
4239
/*
4340
Section: Conditions
4441
*/
@@ -530,165 +527,6 @@ pub fn eq(a: &~str, b: &~str) -> bool {
530527
eq_slice(*a, *b)
531528
}
532529

533-
#[cfg(not(test))]
534-
impl<'self> TotalOrd for &'self str {
535-
#[inline]
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-
}
547-
}
548-
549-
#[cfg(not(test))]
550-
impl TotalOrd for ~str {
551-
#[inline]
552-
fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
553-
}
554-
555-
#[cfg(not(test))]
556-
impl TotalOrd for @str {
557-
#[inline]
558-
fn cmp(&self, other: &@str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
559-
}
560-
561-
/// Bytewise slice less than
562-
#[inline]
563-
fn lt(a: &str, b: &str) -> bool {
564-
a.cmp(& b) == Less
565-
}
566-
567-
/// Bytewise less than or equal
568-
#[inline]
569-
pub fn le(a: &str, b: &str) -> bool {
570-
!lt(b, a)
571-
}
572-
573-
/// Bytewise greater than or equal
574-
#[inline]
575-
fn ge(a: &str, b: &str) -> bool {
576-
!lt(a, b)
577-
}
578-
579-
/// Bytewise greater than
580-
#[inline]
581-
fn gt(a: &str, b: &str) -> bool {
582-
!le(a, b)
583-
}
584-
585-
#[cfg(not(test))]
586-
impl<'self> Eq for &'self str {
587-
#[inline(always)]
588-
fn eq(&self, other: & &'self str) -> bool {
589-
eq_slice((*self), (*other))
590-
}
591-
#[inline(always)]
592-
fn ne(&self, other: & &'self str) -> bool { !(*self).eq(other) }
593-
}
594-
595-
#[cfg(not(test))]
596-
impl Eq for ~str {
597-
#[inline(always)]
598-
fn eq(&self, other: &~str) -> bool {
599-
eq_slice((*self), (*other))
600-
}
601-
#[inline(always)]
602-
fn ne(&self, other: &~str) -> bool { !(*self).eq(other) }
603-
}
604-
605-
#[cfg(not(test))]
606-
impl Eq for @str {
607-
#[inline(always)]
608-
fn eq(&self, other: &@str) -> bool {
609-
eq_slice((*self), (*other))
610-
}
611-
#[inline(always)]
612-
fn ne(&self, other: &@str) -> bool { !(*self).eq(other) }
613-
}
614-
615-
#[cfg(not(test))]
616-
impl<'self> TotalEq for &'self str {
617-
#[inline(always)]
618-
fn equals(&self, other: & &'self str) -> bool {
619-
eq_slice((*self), (*other))
620-
}
621-
}
622-
623-
#[cfg(not(test))]
624-
impl TotalEq for ~str {
625-
#[inline(always)]
626-
fn equals(&self, other: &~str) -> bool {
627-
eq_slice((*self), (*other))
628-
}
629-
}
630-
631-
#[cfg(not(test))]
632-
impl TotalEq for @str {
633-
#[inline(always)]
634-
fn equals(&self, other: &@str) -> bool {
635-
eq_slice((*self), (*other))
636-
}
637-
}
638-
639-
#[cfg(not(test))]
640-
impl Ord for ~str {
641-
#[inline(always)]
642-
fn lt(&self, other: &~str) -> bool { lt((*self), (*other)) }
643-
#[inline(always)]
644-
fn le(&self, other: &~str) -> bool { le((*self), (*other)) }
645-
#[inline(always)]
646-
fn ge(&self, other: &~str) -> bool { ge((*self), (*other)) }
647-
#[inline(always)]
648-
fn gt(&self, other: &~str) -> bool { gt((*self), (*other)) }
649-
}
650-
651-
#[cfg(not(test))]
652-
impl<'self> Ord for &'self str {
653-
#[inline(always)]
654-
fn lt(&self, other: & &'self str) -> bool { lt((*self), (*other)) }
655-
#[inline(always)]
656-
fn le(&self, other: & &'self str) -> bool { le((*self), (*other)) }
657-
#[inline(always)]
658-
fn ge(&self, other: & &'self str) -> bool { ge((*self), (*other)) }
659-
#[inline(always)]
660-
fn gt(&self, other: & &'self str) -> bool { gt((*self), (*other)) }
661-
}
662-
663-
#[cfg(not(test))]
664-
impl Ord for @str {
665-
#[inline(always)]
666-
fn lt(&self, other: &@str) -> bool { lt((*self), (*other)) }
667-
#[inline(always)]
668-
fn le(&self, other: &@str) -> bool { le((*self), (*other)) }
669-
#[inline(always)]
670-
fn ge(&self, other: &@str) -> bool { ge((*self), (*other)) }
671-
#[inline(always)]
672-
fn gt(&self, other: &@str) -> bool { gt((*self), (*other)) }
673-
}
674-
675-
#[cfg(not(test))]
676-
impl<'self, S: Str> Equiv<S> for &'self str {
677-
#[inline(always)]
678-
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
679-
}
680-
#[cfg(not(test))]
681-
impl<'self, S: Str> Equiv<S> for @str {
682-
#[inline(always)]
683-
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
684-
}
685-
686-
#[cfg(not(test))]
687-
impl<'self, S: Str> Equiv<S> for ~str {
688-
#[inline(always)]
689-
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
690-
}
691-
692530
/*
693531
Section: Searching
694532
*/
@@ -1072,12 +910,136 @@ pub mod raw {
1072910
#[cfg(not(test))]
1073911
pub mod traits {
1074912
use ops::Add;
913+
use cmp::{TotalOrd, Ordering, Less, Equal, Greater, Eq, Ord, Equiv, TotalEq};
914+
use super::{Str, eq_slice};
915+
1075916
impl<'self> Add<&'self str,~str> for ~str {
1076917
#[inline(always)]
1077918
fn add(&self, rhs: & &'self str) -> ~str {
1078919
self.append((*rhs))
1079920
}
1080921
}
922+
923+
impl<'self> TotalOrd for &'self str {
924+
#[inline]
925+
fn cmp(&self, other: & &'self str) -> Ordering {
926+
for self.bytes_iter().zip(other.bytes_iter()).advance |(s_b, o_b)| {
927+
match s_b.cmp(&o_b) {
928+
Greater => return Greater,
929+
Less => return Less,
930+
Equal => ()
931+
}
932+
}
933+
934+
self.len().cmp(&other.len())
935+
}
936+
}
937+
938+
impl TotalOrd for ~str {
939+
#[inline]
940+
fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
941+
}
942+
943+
impl TotalOrd for @str {
944+
#[inline]
945+
fn cmp(&self, other: &@str) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
946+
}
947+
948+
impl<'self> Eq for &'self str {
949+
#[inline(always)]
950+
fn eq(&self, other: & &'self str) -> bool {
951+
eq_slice((*self), (*other))
952+
}
953+
#[inline(always)]
954+
fn ne(&self, other: & &'self str) -> bool { !(*self).eq(other) }
955+
}
956+
957+
impl Eq for ~str {
958+
#[inline(always)]
959+
fn eq(&self, other: &~str) -> bool {
960+
eq_slice((*self), (*other))
961+
}
962+
#[inline(always)]
963+
fn ne(&self, other: &~str) -> bool { !(*self).eq(other) }
964+
}
965+
966+
impl Eq for @str {
967+
#[inline(always)]
968+
fn eq(&self, other: &@str) -> bool {
969+
eq_slice((*self), (*other))
970+
}
971+
#[inline(always)]
972+
fn ne(&self, other: &@str) -> bool { !(*self).eq(other) }
973+
}
974+
975+
impl<'self> TotalEq for &'self str {
976+
#[inline(always)]
977+
fn equals(&self, other: & &'self str) -> bool {
978+
eq_slice((*self), (*other))
979+
}
980+
}
981+
982+
impl TotalEq for ~str {
983+
#[inline(always)]
984+
fn equals(&self, other: &~str) -> bool {
985+
eq_slice((*self), (*other))
986+
}
987+
}
988+
989+
impl TotalEq for @str {
990+
#[inline(always)]
991+
fn equals(&self, other: &@str) -> bool {
992+
eq_slice((*self), (*other))
993+
}
994+
}
995+
996+
impl<'self> Ord for &'self str {
997+
#[inline(always)]
998+
fn lt(&self, other: & &'self str) -> bool { self.cmp(other) == Less }
999+
#[inline(always)]
1000+
fn le(&self, other: & &'self str) -> bool { self.cmp(other) != Greater }
1001+
#[inline(always)]
1002+
fn ge(&self, other: & &'self str) -> bool { self.cmp(other) != Less }
1003+
#[inline(always)]
1004+
fn gt(&self, other: & &'self str) -> bool { self.cmp(other) == Greater }
1005+
}
1006+
1007+
impl Ord for ~str {
1008+
#[inline(always)]
1009+
fn lt(&self, other: &~str) -> bool { self.cmp(other) == Less }
1010+
#[inline(always)]
1011+
fn le(&self, other: &~str) -> bool { self.cmp(other) != Greater }
1012+
#[inline(always)]
1013+
fn ge(&self, other: &~str) -> bool { self.cmp(other) != Less }
1014+
#[inline(always)]
1015+
fn gt(&self, other: &~str) -> bool { self.cmp(other) == Greater }
1016+
}
1017+
1018+
impl Ord for @str {
1019+
#[inline(always)]
1020+
fn lt(&self, other: &@str) -> bool { self.cmp(other) == Less }
1021+
#[inline(always)]
1022+
fn le(&self, other: &@str) -> bool { self.cmp(other) != Greater }
1023+
#[inline(always)]
1024+
fn ge(&self, other: &@str) -> bool { self.cmp(other) != Less }
1025+
#[inline(always)]
1026+
fn gt(&self, other: &@str) -> bool { self.cmp(other) == Greater }
1027+
}
1028+
1029+
impl<'self, S: Str> Equiv<S> for &'self str {
1030+
#[inline(always)]
1031+
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
1032+
}
1033+
1034+
impl<'self, S: Str> Equiv<S> for @str {
1035+
#[inline(always)]
1036+
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
1037+
}
1038+
1039+
impl<'self, S: Str> Equiv<S> for ~str {
1040+
#[inline(always)]
1041+
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
1042+
}
10811043
}
10821044
10831045
#[cfg(test)]
@@ -2267,10 +2229,10 @@ mod tests {
22672229
22682230
#[test]
22692231
fn test_le() {
2270-
assert!((le(&"", &"")));
2271-
assert!((le(&"", &"foo")));
2272-
assert!((le(&"foo", &"foo")));
2273-
assert!((!eq(&~"foo", &~"bar")));
2232+
assert!("" <= "");
2233+
assert!("" <= "foo");
2234+
assert!("foo" <= "foo");
2235+
assert!("foo" != ~"bar");
22742236
}
22752237
22762238
#[test]

0 commit comments

Comments
 (0)