Skip to content

Commit 6d589f8

Browse files
committed
implement Ord, TotalEq and TotalOrd for char
Closes #6063
1 parent 1d53bab commit 6d589f8

File tree

3 files changed

+56
-69
lines changed

3 files changed

+56
-69
lines changed

src/libcore/char.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! Utilities for manipulating the char type
1212
13+
use cmp::Ord;
1314
use option::{None, Option, Some};
1415
use str;
1516
use u32;
@@ -243,26 +244,26 @@ pub fn len_utf8_bytes(c: char) -> uint {
243244
else { fail!(~"invalid character!") }
244245
}
245246
246-
/**
247-
* Compare two chars
248-
*
249-
* # Return value
250-
*
251-
* -1 if a < b, 0 if a == b, +1 if a > b
252-
*/
253-
#[inline(always)]
254-
pub fn cmp(a: char, b: char) -> int {
255-
return if b > a { -1 }
256-
else if b < a { 1 }
257-
else { 0 }
258-
}
259-
260247
#[cfg(notest)]
261248
impl Eq for char {
249+
#[inline(always)]
262250
fn eq(&self, other: &char) -> bool { (*self) == (*other) }
251+
#[inline(always)]
263252
fn ne(&self, other: &char) -> bool { (*self) != (*other) }
264253
}
265254
255+
#[cfg(notest)]
256+
impl Ord for char {
257+
#[inline(always)]
258+
fn lt(&self, other: &char) -> bool { *self < *other }
259+
#[inline(always)]
260+
fn le(&self, other: &char) -> bool { *self <= *other }
261+
#[inline(always)]
262+
fn gt(&self, other: &char) -> bool { *self > *other }
263+
#[inline(always)]
264+
fn ge(&self, other: &char) -> bool { *self >= *other }
265+
}
266+
266267
#[test]
267268
fn test_is_lowercase() {
268269
assert!(is_lowercase('a'));

src/libcore/cmp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ totaleq_impl!(i64)
6464
totaleq_impl!(int)
6565
totaleq_impl!(uint)
6666

67+
totaleq_impl!(char)
68+
6769
#[deriving(Clone, Eq)]
6870
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
6971

@@ -116,6 +118,8 @@ totalord_impl!(i64)
116118
totalord_impl!(int)
117119
totalord_impl!(uint)
118120

121+
totalord_impl!(char)
122+
119123
pub fn cmp2<A:TotalOrd,B:TotalOrd>(
120124
a1: &A, b1: &B,
121125
a2: &A, b2: &B) -> Ordering

src/libstd/rope.rs

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
* * access to a character by index is logarithmic (linear in strings);
3434
*/
3535

36-
use core::option;
3736
use core::prelude::*;
38-
use core::str;
39-
use core::uint;
40-
use core::vec;
4137

4238
/// The type of ropes.
4339
pub type Rope = node::Root;
@@ -215,8 +211,8 @@ pub fn bal(rope:Rope) -> Rope {
215211
match (rope) {
216212
node::Empty => return rope,
217213
node::Content(x) => match (node::bal(x)) {
218-
option::None => rope,
219-
option::Some(y) => node::Content(y)
214+
None => rope,
215+
Some(y) => node::Content(y)
220216
}
221217
}
222218
}
@@ -558,13 +554,7 @@ pub fn char_at(rope: Rope, pos: uint) -> char {
558554
pub mod node {
559555
use rope::node;
560556

561-
use core::cast;
562-
use core::char;
563-
use core::option;
564557
use core::prelude::*;
565-
use core::str;
566-
use core::uint;
567-
use core::vec;
568558

569559
/// Implementation of type `rope`
570560
pub enum Root {
@@ -835,8 +825,8 @@ pub mod node {
835825
let mut it = leaf_iterator::start(node);
836826
loop {
837827
match leaf_iterator::next(&mut it) {
838-
option::None => break,
839-
option::Some(x) => {
828+
None => break,
829+
Some(x) => {
840830
//FIXME (#2744): Replace with memcpy or something similar
841831
let local_buf: ~[u8] = cast::transmute(*x.content);
842832
let mut i = x.byte_offset;
@@ -885,24 +875,24 @@ pub mod node {
885875
*
886876
* # Return value
887877
*
888-
* * `option::None` if no transformation happened
889-
* * `option::Some(x)` otherwise, in which case `x` has the same contents
878+
* * `None` if no transformation happened
879+
* * `Some(x)` otherwise, in which case `x` has the same contents
890880
* as `node` bot lower height and/or fragmentation.
891881
*/
892882
pub fn bal(node: @Node) -> Option<@Node> {
893-
if height(node) < hint_max_node_height { return option::None; }
883+
if height(node) < hint_max_node_height { return None; }
894884
//1. Gather all leaves as a forest
895885
let mut forest = ~[];
896886
let mut it = leaf_iterator::start(node);
897887
loop {
898888
match leaf_iterator::next(&mut it) {
899-
option::None => break,
900-
option::Some(x) => forest.push(@Leaf(x))
889+
None => break,
890+
Some(x) => forest.push(@Leaf(x))
901891
}
902892
}
903893
//2. Rebuild tree from forest
904894
let root = @*tree_from_forest_destructive(forest);
905-
return option::Some(root);
895+
return Some(root);
906896

907897
}
908898

@@ -1061,14 +1051,14 @@ pub mod node {
10611051
while result == 0 {
10621052
match (char_iterator::next(&mut ita), char_iterator::next(&mut itb))
10631053
{
1064-
(option::None, option::None) => break,
1065-
(option::Some(chara), option::Some(charb)) => {
1066-
result = char::cmp(chara, charb);
1054+
(None, None) => break,
1055+
(Some(chara), Some(charb)) => {
1056+
result = chara.cmp(&charb) as int;
10671057
}
1068-
(option::Some(_), _) => {
1058+
(Some(_), _) => {
10691059
result = 1;
10701060
}
1071-
(_, option::Some(_)) => {
1061+
(_, Some(_)) => {
10721062
result = -1;
10731063
}
10741064
}
@@ -1145,9 +1135,7 @@ pub mod node {
11451135
pub mod leaf_iterator {
11461136
use rope::node::{Concat, Leaf, Node, height};
11471137

1148-
use core::option;
11491138
use core::prelude::*;
1150-
use core::vec;
11511139

11521140
pub struct T {
11531141
stack: ~[@Node],
@@ -1168,7 +1156,7 @@ pub mod node {
11681156
}
11691157

11701158
pub fn next(it: &mut T) -> Option<Leaf> {
1171-
if it.stackpos < 0 { return option::None; }
1159+
if it.stackpos < 0 { return None; }
11721160
loop {
11731161
let current = it.stack[it.stackpos];
11741162
it.stackpos -= 1;
@@ -1179,7 +1167,7 @@ pub mod node {
11791167
it.stackpos += 1;
11801168
it.stack[it.stackpos] = x.left;
11811169
}
1182-
Leaf(x) => return option::Some(x)
1170+
Leaf(x) => return Some(x)
11831171
}
11841172
};
11851173
}
@@ -1189,9 +1177,7 @@ pub mod node {
11891177
use rope::node::{Leaf, Node};
11901178
use rope::node::leaf_iterator;
11911179

1192-
use core::option;
11931180
use core::prelude::*;
1194-
use core::str;
11951181

11961182
pub struct T {
11971183
leaf_iterator: leaf_iterator::T,
@@ -1202,28 +1188,28 @@ pub mod node {
12021188
pub fn start(node: @Node) -> T {
12031189
T {
12041190
leaf_iterator: leaf_iterator::start(node),
1205-
leaf: option::None,
1191+
leaf: None,
12061192
leaf_byte_pos: 0u,
12071193
}
12081194
}
12091195

12101196
pub fn empty() -> T {
12111197
T {
12121198
leaf_iterator: leaf_iterator::empty(),
1213-
leaf: option::None,
1199+
leaf: None,
12141200
leaf_byte_pos: 0u,
12151201
}
12161202
}
12171203

12181204
pub fn next(it: &mut T) -> Option<char> {
12191205
loop {
12201206
match get_current_or_next_leaf(it) {
1221-
option::None => return option::None,
1222-
option::Some(_) => {
1207+
None => return None,
1208+
Some(_) => {
12231209
let next_char = get_next_char_in_leaf(it);
12241210
match next_char {
1225-
option::None => loop,
1226-
option::Some(_) => return next_char
1211+
None => loop,
1212+
Some(_) => return next_char
12271213
}
12281214
}
12291215
}
@@ -1232,12 +1218,12 @@ pub mod node {
12321218

12331219
pub fn get_current_or_next_leaf(it: &mut T) -> Option<Leaf> {
12341220
match it.leaf {
1235-
option::Some(_) => return it.leaf,
1236-
option::None => {
1221+
Some(_) => return it.leaf,
1222+
None => {
12371223
let next = leaf_iterator::next(&mut it.leaf_iterator);
12381224
match next {
1239-
option::None => return option::None,
1240-
option::Some(_) => {
1225+
None => return None,
1226+
Some(_) => {
12411227
it.leaf = next;
12421228
it.leaf_byte_pos = 0u;
12431229
return next;
@@ -1249,20 +1235,20 @@ pub mod node {
12491235

12501236
pub fn get_next_char_in_leaf(it: &mut T) -> Option<char> {
12511237
match copy it.leaf {
1252-
option::None => return option::None,
1253-
option::Some(aleaf) => {
1238+
None => return None,
1239+
Some(aleaf) => {
12541240
if it.leaf_byte_pos >= aleaf.byte_len {
12551241
//We are actually past the end of the leaf
1256-
it.leaf = option::None;
1257-
return option::None
1242+
it.leaf = None;
1243+
return None
12581244
} else {
12591245
let range =
12601246
str::char_range_at(*aleaf.content,
12611247
(*it).leaf_byte_pos + aleaf.byte_offset);
12621248
let ch = range.ch;
12631249
let next = range.next;
12641250
(*it).leaf_byte_pos = next - aleaf.byte_offset;
1265-
return option::Some(ch)
1251+
return Some(ch)
12661252
}
12671253
}
12681254
}
@@ -1273,11 +1259,7 @@ pub mod node {
12731259
#[cfg(test)]
12741260
mod tests {
12751261
use rope::*;
1276-
1277-
use core::option;
1278-
use core::str;
1279-
use core::uint;
1280-
use core::vec;
1262+
use core::prelude::*;
12811263

12821264
//Utility function, used for sanity check
12831265
fn rope_to_string(r: Rope) -> ~str {
@@ -1341,11 +1323,11 @@ mod tests {
13411323
let mut equal = true;
13421324
while equal {
13431325
match (node::char_iterator::next(&mut rope_iter)) {
1344-
option::None => {
1326+
None => {
13451327
if string_iter < string_len {
13461328
equal = false;
13471329
} break; }
1348-
option::Some(c) => {
1330+
Some(c) => {
13491331
let range = str::char_range_at(*sample, string_iter);
13501332
string_iter = range.next;
13511333
if range.ch != c { equal = false; break; }
@@ -1373,8 +1355,8 @@ mod tests {
13731355
let mut it = iterator::char::start(r);
13741356
loop {
13751357
match (node::char_iterator::next(&mut it)) {
1376-
option::None => break,
1377-
option::Some(_) => len += 1u
1358+
None => break,
1359+
Some(_) => len += 1u
13781360
}
13791361
}
13801362

0 commit comments

Comments
 (0)