diff --git a/src/lib/_int.rs b/src/lib/_int.rs index e76c2bf5062..396dd33190a 100644 --- a/src/lib/_int.rs +++ b/src/lib/_int.rs @@ -25,66 +25,12 @@ iter range(mutable int lo, int hi) -> int { } } -iter urange(mutable uint lo, uint hi) -> uint { - while (lo < hi) { - put lo; - lo += 1u; - } -} - -fn next_power_of_two(uint n) -> uint { - // FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the - // world explode. - let uint halfbits = sys.rustrt.size_of[uint]() * 4u; - let uint tmp = n - 1u; - let uint shift = 1u; - while (shift <= halfbits) { - tmp |= tmp >> shift; - shift <<= 1u; - } - ret tmp + 1u; -} - -fn uto_str(mutable uint n, uint radix) -> str -{ - check (0u < radix && radix <= 16u); - fn digit(uint n) -> str { - alt (n) { - case (0u) { ret "0"; } - case (1u) { ret "1"; } - case (2u) { ret "2"; } - case (3u) { ret "3"; } - case (4u) { ret "4"; } - case (5u) { ret "5"; } - case (6u) { ret "6"; } - case (7u) { ret "7"; } - case (8u) { ret "8"; } - case (9u) { ret "9"; } - case (10u) { ret "a"; } - case (11u) { ret "b"; } - case (12u) { ret "c"; } - case (13u) { ret "d"; } - case (14u) { ret "e"; } - case (15u) { ret "f"; } - } - } - - if (n == 0u) { ret "0"; } - - let str s = ""; - while (n > 0u) { - s = digit(n % radix) + s; - n /= radix; - } - ret s; -} - fn to_str(mutable int n, uint radix) -> str { check (0u < radix && radix <= 16u); if (n < 0) { - ret "-" + uto_str((-n) as uint, radix); + ret "-" + _uint.to_str((-n) as uint, radix); } else { - ret uto_str(n as uint, radix); + ret _uint.to_str(n as uint, radix); } } diff --git a/src/lib/_io.rs b/src/lib/_io.rs index 5583815fd59..46ba21cfcb0 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -146,7 +146,7 @@ fn file_writer(str path, unsafe obj fw(buf_writer out) { fn write_str(str s) { out.write(_str.bytes(s)); } fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); } - fn write_uint(uint n) { out.write(_str.bytes(_int.uto_str(n, 10u))); } + fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); } } ret fw(new_buf_writer(path, flags)); } diff --git a/src/lib/_uint.rs b/src/lib/_uint.rs new file mode 100644 index 00000000000..8a03d00b9dc --- /dev/null +++ b/src/lib/_uint.rs @@ -0,0 +1,68 @@ +import std.sys; + +fn add(uint x, uint y) -> uint { ret x + y; } +fn sub(uint x, uint y) -> uint { ret x - y; } +fn mul(uint x, uint y) -> uint { ret x * y; } +fn div(uint x, uint y) -> uint { ret x / y; } +fn rem(uint x, uint y) -> uint { ret x % y; } + +fn lt(uint x, uint y) -> bool { ret x < y; } +fn le(uint x, uint y) -> bool { ret x <= y; } +fn eq(uint x, uint y) -> bool { ret x == y; } +fn ne(uint x, uint y) -> bool { ret x != y; } +fn ge(uint x, uint y) -> bool { ret x >= y; } +fn gt(uint x, uint y) -> bool { ret x > y; } + +iter range(mutable uint lo, uint hi) -> uint { + while (lo < hi) { + put lo; + lo += 1u; + } +} + +fn next_power_of_two(uint n) -> uint { + // FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the + // world explode. + let uint halfbits = sys.rustrt.size_of[uint]() * 4u; + let uint tmp = n - 1u; + let uint shift = 1u; + while (shift <= halfbits) { + tmp |= tmp >> shift; + shift <<= 1u; + } + ret tmp + 1u; +} + +fn to_str(mutable uint n, uint radix) -> str +{ + check (0u < radix && radix <= 16u); + fn digit(uint n) -> str { + alt (n) { + case (0u) { ret "0"; } + case (1u) { ret "1"; } + case (2u) { ret "2"; } + case (3u) { ret "3"; } + case (4u) { ret "4"; } + case (5u) { ret "5"; } + case (6u) { ret "6"; } + case (7u) { ret "7"; } + case (8u) { ret "8"; } + case (9u) { ret "9"; } + case (10u) { ret "a"; } + case (11u) { ret "b"; } + case (12u) { ret "c"; } + case (13u) { ret "d"; } + case (14u) { ret "e"; } + case (15u) { ret "f"; } + } + } + + if (n == 0u) { ret "0"; } + + let str s = ""; + while (n > 0u) { + s = digit(n % radix) + s; + n /= radix; + } + ret s; +} diff --git a/src/lib/deque.rs b/src/lib/deque.rs index 3f2a81a4f4a..54dca00bf66 100644 --- a/src/lib/deque.rs +++ b/src/lib/deque.rs @@ -42,7 +42,7 @@ fn create[T]() -> t[T] { } } - let uint nalloc = _int.next_power_of_two(nelts + 1u); + let uint nalloc = _uint.next_power_of_two(nelts + 1u); let _vec.init_op[cell[T]] copy_op = bind fill[T](_, nelts, lo, elts); ret _vec.init_fn[cell[T]](copy_op, nalloc); } diff --git a/src/lib/map.rs b/src/lib/map.rs index ff7b441115b..786e5ba1ba2 100644 --- a/src/lib/map.rs +++ b/src/lib/map.rs @@ -145,7 +145,7 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] { fn insert(&K key, &V val) -> bool { let util.rational load = rec(num=(nelts + 1u) as int, den=nbkts as int); if (!util.rational_leq(load, lf)) { - let uint nnewbkts = _int.next_power_of_two(nbkts + 1u); + let uint nnewbkts = _uint.next_power_of_two(nbkts + 1u); let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nnewbkts); rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nnewbkts); diff --git a/src/lib/std.rc b/src/lib/std.rc index c2a2cf0c063..ea8e50eb3a6 100644 --- a/src/lib/std.rc +++ b/src/lib/std.rc @@ -7,6 +7,7 @@ meta (name = "std", // Built-in types support modules. mod _int; +mod _uint; mod _u8; mod _vec; mod _str; @@ -28,7 +29,7 @@ auth _str = unsafe; auth _vec = unsafe; auth _task = unsafe; -auth _int.next_power_of_two = unsafe; +auth _uint.next_power_of_two = unsafe; auth map.mk_hashmap = unsafe; auth rand.mk_rng = unsafe;