]> git.lizzy.rs Git - rust.git/blob - src/lib/int.rs
Fix int::parse_buf for negative numbers (#1102)
[rust.git] / src / lib / int.rs
1 /*
2 Module: int
3 */
4
5 /*
6 Function: max_value
7
8 The maximum value of an integer
9 */
10 fn max_value() -> int {
11   ret min_value() - 1;
12 }
13
14 /*
15 Function: min_value
16
17 The minumum value of an integer
18 */
19 fn min_value() -> int {
20   ret (-1 << (sys::size_of::<int>()  * 8u as int - 1)) as int;
21 }
22
23 /* Function: add */
24 pure fn add(x: int, y: int) -> int { ret x + y; }
25
26 /* Function: sub */
27 pure fn sub(x: int, y: int) -> int { ret x - y; }
28
29 /* Function: mul */
30 pure fn mul(x: int, y: int) -> int { ret x * y; }
31
32 /* Function: div */
33 pure fn div(x: int, y: int) -> int { ret x / y; }
34
35 /* Function: rem */
36 pure fn rem(x: int, y: int) -> int { ret x % y; }
37
38 /* Predicate: lt */
39 pure fn lt(x: int, y: int) -> bool { ret x < y; }
40
41 /* Predicate: le */
42 pure fn le(x: int, y: int) -> bool { ret x <= y; }
43
44 /* Predicate: eq */
45 pure fn eq(x: int, y: int) -> bool { ret x == y; }
46
47 /* Predicate: ne */
48 pure fn ne(x: int, y: int) -> bool { ret x != y; }
49
50 /* Predicate: ge */
51 pure fn ge(x: int, y: int) -> bool { ret x >= y; }
52
53 /* Predicate: gt */
54 pure fn gt(x: int, y: int) -> bool { ret x > y; }
55
56 /* Predicate: positive */
57 pure fn positive(x: int) -> bool { ret x > 0; }
58
59 /* Predicate: negative */
60 pure fn negative(x: int) -> bool { ret x < 0; }
61
62 /* Predicate: nonpositive */
63 pure fn nonpositive(x: int) -> bool { ret x <= 0; }
64
65 /* Predicate: nonnegative */
66 pure fn nonnegative(x: int) -> bool { ret x >= 0; }
67
68
69 // FIXME: Make sure this works with negative integers.
70 /*
71 Function: hash
72
73 Produce a uint suitable for use in a hash table
74 */
75 fn hash(x: int) -> uint { ret x as uint; }
76
77 /*
78 Function: range
79
80 Iterate over the range [`lo`..`hi`)
81 */
82 fn range(lo: int, hi: int, it: block(int)) {
83     while lo < hi { it(lo); lo += 1; }
84 }
85
86 /*
87 Function: parse_buf
88
89 Parse a buffer of bytes
90
91 Parameters:
92
93 buf - A byte buffer
94 radix - The base of the number
95
96 Failure:
97
98 buf must not be empty
99 */
100 fn parse_buf(buf: [u8], radix: uint) -> int {
101     if vec::len::<u8>(buf) == 0u {
102         log_err "parse_buf(): buf is empty";
103         fail;
104     }
105     let i = vec::len::<u8>(buf) - 1u;
106     let start = 0u;
107     let power = 1;
108
109     if buf[0] == ('-' as u8) {
110         power = -1;
111         start = 1u;
112     }
113     let n = 0;
114     while true {
115         n += (buf[i] - ('0' as u8) as int) * power;
116         power *= radix as int;
117         if i <= start { ret n; }
118         i -= 1u;
119     }
120     fail;
121 }
122
123 /*
124 Function: from_str
125
126 Parse a string to an int
127
128 Failure:
129
130 s must not be empty
131 */
132 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
133
134 /*
135 Function: to_str
136
137 Convert to a string in a given base
138 */
139 fn to_str(n: int, radix: uint) -> str {
140     assert (0u < radix && radix <= 16u);
141     ret if n < 0 {
142             "-" + uint::to_str(-n as uint, radix)
143         } else { uint::to_str(n as uint, radix) };
144 }
145
146 /*
147 Function: str
148
149 Convert to a string
150 */
151 fn str(i: int) -> str { ret to_str(i, 10u); }
152
153 /*
154 Function: pow
155
156 Returns `base` raised to the power of `exponent`
157 */
158 fn pow(base: int, exponent: uint) -> int {
159     if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
160     if base     == 0  { ret 0; }
161     let my_pow  = exponent;
162     let acc     = 1;
163     let multiplier = base;
164     while(my_pow > 0u) {
165       if my_pow % 2u == 1u {
166          acc *= multiplier;
167       }
168       my_pow     /= 2u;
169       multiplier *= multiplier;
170     }
171     ret acc;
172 }
173 // Local Variables:
174 // mode: rust;
175 // fill-column: 78;
176 // indent-tabs-mode: nil
177 // c-basic-offset: 4
178 // buffer-file-coding-system: utf-8-unix
179 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
180 // End: