8 The maximum value of an integer
10 fn max_value() -> int {
17 The minumum value of an integer
19 fn min_value() -> int {
20 ret (-1 << (sys::size_of::<int>() * 8u as int - 1)) as int;
24 pure fn add(x: int, y: int) -> int { ret x + y; }
27 pure fn sub(x: int, y: int) -> int { ret x - y; }
30 pure fn mul(x: int, y: int) -> int { ret x * y; }
33 pure fn div(x: int, y: int) -> int { ret x / y; }
36 pure fn rem(x: int, y: int) -> int { ret x % y; }
39 pure fn lt(x: int, y: int) -> bool { ret x < y; }
42 pure fn le(x: int, y: int) -> bool { ret x <= y; }
45 pure fn eq(x: int, y: int) -> bool { ret x == y; }
48 pure fn ne(x: int, y: int) -> bool { ret x != y; }
51 pure fn ge(x: int, y: int) -> bool { ret x >= y; }
54 pure fn gt(x: int, y: int) -> bool { ret x > y; }
56 /* Predicate: positive */
57 pure fn positive(x: int) -> bool { ret x > 0; }
59 /* Predicate: negative */
60 pure fn negative(x: int) -> bool { ret x < 0; }
62 /* Predicate: nonpositive */
63 pure fn nonpositive(x: int) -> bool { ret x <= 0; }
65 /* Predicate: nonnegative */
66 pure fn nonnegative(x: int) -> bool { ret x >= 0; }
69 // FIXME: Make sure this works with negative integers.
73 Produce a uint suitable for use in a hash table
75 fn hash(x: int) -> uint { ret x as uint; }
80 Iterate over the range [`lo`..`hi`)
82 fn range(lo: int, hi: int, it: block(int)) {
83 while lo < hi { it(lo); lo += 1; }
89 Parse a buffer of bytes
94 radix - The base of the number
100 fn parse_buf(buf: [u8], radix: uint) -> int {
101 if vec::len::<u8>(buf) == 0u {
102 log_err "parse_buf(): buf is empty";
105 let i = vec::len::<u8>(buf) - 1u;
109 if buf[0] == ('-' as u8) {
115 let digit = alt buf[i] as char {
116 '0' to '9' { buf[i] - ('0' as u8) }
117 'a' to 'z' { 10u8 + buf[i] - ('a' as u8) }
118 'A' to 'Z' { 10u8 + buf[i] - ('A' as u8) }
120 if (digit as uint) >= radix {
123 n += (digit as int) * power;
124 power *= radix as int;
125 if i <= start { ret n; }
134 Parse a string to an int
140 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
145 Convert to a string in a given base
147 fn to_str(n: int, radix: uint) -> str {
148 assert (0u < radix && radix <= 16u);
150 "-" + uint::to_str(-n as uint, radix)
151 } else { uint::to_str(n as uint, radix) };
159 fn str(i: int) -> str { ret to_str(i, 10u); }
164 Returns `base` raised to the power of `exponent`
166 fn pow(base: int, exponent: uint) -> int {
167 if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
168 if base == 0 { ret 0; }
169 let my_pow = exponent;
171 let multiplier = base;
173 if my_pow % 2u == 1u {
177 multiplier *= multiplier;
184 // indent-tabs-mode: nil
186 // buffer-file-coding-system: utf-8-unix
187 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";