Skip to content

Commit 2976a51

Browse files
committed
---
yaml --- r: 7078 b: refs/heads/master c: 3e68803 h: refs/heads/master v: v3
1 parent d5baa5c commit 2976a51

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: eade7844a339557e57b2ba0503d8edf19ff60f43
2+
refs/heads/master: 3e68803891dc384e8c3e5a24bb17700187d86cf7

trunk/src/libcore/vec.rs

+94-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ Function: enum_chars
709709
710710
Returns a vector containing a range of chars
711711
*/
712-
fn enum_chars(start: u8, end: u8) : u8::le(start, end) -> [char] {
712+
fn enum_chars(start: u8, end: u8) : ::u8::le(start, end) -> [char] {
713713
let i = start;
714714
let r = [];
715715
while i <= end { r += [i as char]; i += 1u as u8; }
@@ -879,6 +879,99 @@ mod unsafe {
879879
}
880880
}
881881

882+
/*
883+
Module: u8
884+
*/
885+
mod u8 {
886+
export cmp;
887+
export lt, le, eq, ne, ge, gt;
888+
export hash;
889+
890+
#[nolink]
891+
#[abi = "cdecl"]
892+
native mod libc {
893+
fn memcmp(s1: *u8, s2: *u8, n: ctypes::size_t) -> ctypes::c_int;
894+
}
895+
896+
/*
897+
Function cmp
898+
899+
Bytewise string comparison
900+
*/
901+
pure fn cmp(&&a: [u8], &&b: [u8]) -> int unsafe {
902+
let a_len = len(a);
903+
let b_len = len(b);
904+
let n = math::min(a_len, b_len) as ctypes::size_t;
905+
let r = libc::memcmp(to_ptr(a), to_ptr(b), n) as int;
906+
907+
if r != 0 { r } else {
908+
if a_len == b_len {
909+
0
910+
} else if a_len < b_len {
911+
-1
912+
} else {
913+
1
914+
}
915+
}
916+
}
917+
918+
/*
919+
Function: lt
920+
921+
Bytewise less than or equal
922+
*/
923+
pure fn lt(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) < 0 }
924+
925+
/*
926+
Function: le
927+
928+
Bytewise less than or equal
929+
*/
930+
pure fn le(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) <= 0 }
931+
932+
/*
933+
Function: eq
934+
935+
Bytewise equality
936+
*/
937+
pure fn eq(&&a: [u8], &&b: [u8]) -> bool unsafe { cmp(a, b) == 0 }
938+
939+
/*
940+
Function: ne
941+
942+
Bytewise inequality
943+
*/
944+
pure fn ne(&&a: [u8], &&b: [u8]) -> bool unsafe { cmp(a, b) != 0 }
945+
946+
/*
947+
Function: ge
948+
949+
Bytewise greater than or equal
950+
*/
951+
pure fn ge(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) >= 0 }
952+
953+
/*
954+
Function: gt
955+
956+
Bytewise greater than
957+
*/
958+
pure fn gt(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) > 0 }
959+
960+
/*
961+
Function: hash
962+
963+
String hash function
964+
*/
965+
fn hash(&&s: [u8]) -> uint {
966+
// djb hash.
967+
// FIXME: replace with murmur.
968+
969+
let u: uint = 5381u;
970+
vec::iter(s, { |c| u *= 33u; u += c as uint; });
971+
ret u;
972+
}
973+
}
974+
882975
// Local Variables:
883976
// mode: rust;
884977
// fill-column: 78;

trunk/src/libstd/map.rs

+9
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ fn new_str_hash<V: copy>() -> hashmap<str, V> {
378378
ret mk_hashmap(str::hash, str::eq);
379379
}
380380

381+
/*
382+
Function: new_bytes_hash
383+
384+
Construct a hashmap for byte string keys
385+
*/
386+
fn new_bytes_hash<V: copy>() -> hashmap<[u8], V> {
387+
ret mk_hashmap(vec::u8::hash, vec::u8::eq);
388+
}
389+
381390
/*
382391
Function: new_int_hash
383392

0 commit comments

Comments
 (0)