Skip to content

Commit 4f3171e

Browse files
committed
Merge pull request #1450 from erickt/master
adding some misc functions and some functions just for [u8]
2 parents 7b00bac + 6cfc196 commit 4f3171e

File tree

7 files changed

+214
-20
lines changed

7 files changed

+214
-20
lines changed

src/libcore/u32.rs

+43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,49 @@ Return the maximal value for a u32
1616
*/
1717
const max_value: u32 = 0xffff_ffffu32;
1818

19+
/* Function: add */
20+
pure fn add(x: u32, y: u32) -> u32 { ret x + y; }
21+
22+
/* Function: sub */
23+
pure fn sub(x: u32, y: u32) -> u32 { ret x - y; }
24+
25+
/* Function: mul */
26+
pure fn mul(x: u32, y: u32) -> u32 { ret x * y; }
27+
28+
/* Function: div */
29+
pure fn div(x: u32, y: u32) -> u32 { ret x / y; }
30+
31+
/* Function: rem */
32+
pure fn rem(x: u32, y: u32) -> u32 { ret x % y; }
33+
34+
/* Predicate: lt */
35+
pure fn lt(x: u32, y: u32) -> bool { ret x < y; }
36+
37+
/* Predicate: le */
38+
pure fn le(x: u32, y: u32) -> bool { ret x <= y; }
39+
40+
/* Predicate: eq */
41+
pure fn eq(x: u32, y: u32) -> bool { ret x == y; }
42+
43+
/* Predicate: ne */
44+
pure fn ne(x: u32, y: u32) -> bool { ret x != y; }
45+
46+
/* Predicate: ge */
47+
pure fn ge(x: u32, y: u32) -> bool { ret x >= y; }
48+
49+
/* Predicate: gt */
50+
pure fn gt(x: u32, y: u32) -> bool { ret x > y; }
51+
52+
/*
53+
Function: range
54+
55+
Iterate over the range [`lo`..`hi`)
56+
*/
57+
fn range(lo: u32, hi: u32, it: block(u32)) {
58+
let i = lo;
59+
while i < hi { it(i); i += 1u32; }
60+
}
61+
1962
//
2063
// Local Variables:
2164
// mode: rust

src/libcore/u64.rs

+43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,49 @@ Return the maximal value for a u64
1616
*/
1717
const max_value: u64 = 18446744073709551615u64;
1818

19+
/* Function: add */
20+
pure fn add(x: u64, y: u64) -> u64 { ret x + y; }
21+
22+
/* Function: sub */
23+
pure fn sub(x: u64, y: u64) -> u64 { ret x - y; }
24+
25+
/* Function: mul */
26+
pure fn mul(x: u64, y: u64) -> u64 { ret x * y; }
27+
28+
/* Function: div */
29+
pure fn div(x: u64, y: u64) -> u64 { ret x / y; }
30+
31+
/* Function: rem */
32+
pure fn rem(x: u64, y: u64) -> u64 { ret x % y; }
33+
34+
/* Predicate: lt */
35+
pure fn lt(x: u64, y: u64) -> bool { ret x < y; }
36+
37+
/* Predicate: le */
38+
pure fn le(x: u64, y: u64) -> bool { ret x <= y; }
39+
40+
/* Predicate: eq */
41+
pure fn eq(x: u64, y: u64) -> bool { ret x == y; }
42+
43+
/* Predicate: ne */
44+
pure fn ne(x: u64, y: u64) -> bool { ret x != y; }
45+
46+
/* Predicate: ge */
47+
pure fn ge(x: u64, y: u64) -> bool { ret x >= y; }
48+
49+
/* Predicate: gt */
50+
pure fn gt(x: u64, y: u64) -> bool { ret x > y; }
51+
52+
/*
53+
Function: range
54+
55+
Iterate over the range [`lo`..`hi`)
56+
*/
57+
fn range(lo: u64, hi: u64, it: block(u64)) {
58+
let i = lo;
59+
while i < hi { it(i); i += 1u64; }
60+
}
61+
1962
/*
2063
Function: to_str
2164

src/libcore/uint.rs

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ pure fn ge(x: uint, y: uint) -> bool { ret x >= y; }
103103
/* Predicate: gt */
104104
pure fn gt(x: uint, y: uint) -> bool { ret x > y; }
105105

106+
/*
107+
Function: hash
108+
109+
Produce a uint suitable for use in a hash table
110+
*/
111+
fn hash(x: uint) -> uint { ret x; }
112+
106113
/*
107114
Function: range
108115

src/libcore/vec.rs

+94-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Function: enum_chars
719719
720720
Returns a vector containing a range of chars
721721
*/
722-
fn enum_chars(start: u8, end: u8) : u8::le(start, end) -> [char] {
722+
fn enum_chars(start: u8, end: u8) : ::u8::le(start, end) -> [char] {
723723
let i = start;
724724
let r = [];
725725
while i <= end { r += [i as char]; i += 1u as u8; }
@@ -889,6 +889,99 @@ mod unsafe {
889889
}
890890
}
891891

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

src/libstd/c_vec.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ taken to ensure that a reference to the c_vec::t is still held if needed.
3131
export t;
3232
export create, create_with_dtor;
3333
export get, set;
34-
export size;
34+
export len;
3535
export ptr;
3636

3737
/*
@@ -43,7 +43,7 @@ export ptr;
4343
*/
4444

4545
tag t<T> {
46-
t({ base: *mutable T, size: uint, rsrc: @dtor_res});
46+
t({ base: *mutable T, len: uint, rsrc: @dtor_res});
4747
}
4848

4949
resource dtor_res(dtor: option::t<fn@()>) {
@@ -60,37 +60,37 @@ resource dtor_res(dtor: option::t<fn@()>) {
6060
/*
6161
Function: create
6262
63-
Create a c_vec::t from a native buffer with a given size.
63+
Create a c_vec::t from a native buffer with a given length.
6464
6565
Parameters:
6666
6767
base - A native pointer to a buffer
68-
size - The number of elements in the buffer
68+
len - The number of elements in the buffer
6969
*/
70-
unsafe fn create<T>(base: *mutable T, size: uint) -> t<T> {
70+
unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
7171
ret t({base: base,
72-
size: size,
72+
len: len,
7373
rsrc: @dtor_res(option::none)
7474
});
7575
}
7676

7777
/*
7878
Function: create_with_dtor
7979
80-
Create a c_vec::t from a native buffer, with a given size,
80+
Create a c_vec::t from a native buffer, with a given length,
8181
and a function to run upon destruction.
8282
8383
Parameters:
8484
8585
base - A native pointer to a buffer
86-
size - The number of elements in the buffer
86+
len - The number of elements in the buffer
8787
dtor - A function to run when the value is destructed, useful
8888
for freeing the buffer, etc.
8989
*/
90-
unsafe fn create_with_dtor<T>(base: *mutable T, size: uint, dtor: fn@())
90+
unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
9191
-> t<T> {
9292
ret t({base: base,
93-
size: size,
93+
len: len,
9494
rsrc: @dtor_res(option::some(dtor))
9595
});
9696
}
@@ -109,7 +109,7 @@ Failure:
109109
If `ofs` is greater or equal to the length of the vector
110110
*/
111111
fn get<T: copy>(t: t<T>, ofs: uint) -> T {
112-
assert ofs < (*t).size;
112+
assert ofs < len(t);
113113
ret unsafe { *ptr::mut_offset((*t).base, ofs) };
114114
}
115115

@@ -123,22 +123,21 @@ Failure:
123123
If `ofs` is greater or equal to the length of the vector
124124
*/
125125
fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
126-
assert ofs < (*t).size;
126+
assert ofs < len(t);
127127
unsafe { *ptr::mut_offset((*t).base, ofs) = v };
128128
}
129129

130130
/*
131131
Section: Elimination forms
132132
*/
133133

134-
// FIXME: Rename to len
135134
/*
136-
Function: size
135+
Function: len
137136
138137
Returns the length of the vector
139138
*/
140-
fn size<T>(t: t<T>) -> uint {
141-
ret (*t).size;
139+
fn len<T>(t: t<T>) -> uint {
140+
ret (*t).len;
142141
}
143142

144143
/*

src/libstd/map.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,22 @@ 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
384393
Construct a hashmap for int keys
385394
*/
386395
fn new_int_hash<V: copy>() -> hashmap<int, V> {
387-
fn hash_int(&&x: int) -> uint { ret x as uint; }
396+
fn hash_int(&&x: int) -> uint { int::hash(x) }
388397
fn eq_int(&&a: int, &&b: int) -> bool { ret a == b; }
389398
ret mk_hashmap(hash_int, eq_int);
390399
}
@@ -395,7 +404,7 @@ Function: new_uint_hash
395404
Construct a hashmap for uint keys
396405
*/
397406
fn new_uint_hash<V: copy>() -> hashmap<uint, V> {
398-
fn hash_uint(&&x: uint) -> uint { ret x; }
407+
fn hash_uint(&&x: uint) -> uint { uint::hash(x) }
399408
fn eq_uint(&&a: uint, &&b: uint) -> bool { ret a == b; }
400409
ret mk_hashmap(hash_uint, eq_uint);
401410
}

src/test/stdtest/c_vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn test_basic() {
2828
set(cv, 4u, 9u8);
2929
assert get(cv, 3u) == 8u8;
3030
assert get(cv, 4u) == 9u8;
31-
assert size(cv) == 16u;
31+
assert len(cv) == 16u;
3232
}
3333

3434
#[test]

0 commit comments

Comments
 (0)