1
- /**
2
- * Return the minimal value for an uint.
3
- *
4
- * This is always 0
5
- */
1
+ /*
2
+ Module: uint
3
+ */
4
+
5
+ /*
6
+ Function: min_value
7
+
8
+ Return the minimal value for an uint.
9
+
10
+ This is always 0
11
+ */
6
12
pure fn min_value ( ) -> uint { ret 0 u; }
7
13
8
- /**
9
- * Return the maximal value for an uint.
10
- *
11
- * This is 2^wordsize - 1
12
- */
14
+ /*
15
+ Function: max_value
16
+
17
+ Return the maximal value for an uint.
18
+
19
+ This is 2^wordsize - 1
20
+ */
13
21
pure fn max_value ( ) -> uint {
14
22
ret 0 u - 1 u;
15
23
}
16
24
25
+ /* Function: add */
17
26
pure fn add ( x : uint , y : uint ) -> uint { ret x + y; }
18
27
28
+ /* Function: sub */
19
29
pure fn sub ( x : uint , y : uint ) -> uint { ret x - y; }
20
30
31
+ /* Function: mul */
21
32
pure fn mul ( x : uint , y : uint ) -> uint { ret x * y; }
22
33
34
+ /* Function: div */
23
35
pure fn div ( x : uint , y : uint ) -> uint { ret x / y; }
24
36
37
+ /* Function: rem */
25
38
pure fn rem ( x : uint , y : uint ) -> uint { ret x % y; }
26
39
40
+ /* Predicate: lt */
27
41
pure fn lt ( x : uint , y : uint ) -> bool { ret x < y; }
28
42
43
+ /* Predicate: le */
29
44
pure fn le ( x : uint , y : uint ) -> bool { ret x <= y; }
30
45
46
+ /* Predicate: eq */
31
47
pure fn eq ( x : uint , y : uint ) -> bool { ret x == y; }
32
48
49
+ /* Predicate: ne */
33
50
pure fn ne ( x : uint , y : uint ) -> bool { ret x != y; }
34
51
52
+ /* Predicate: ge */
35
53
pure fn ge ( x : uint , y : uint ) -> bool { ret x >= y; }
36
54
55
+ /* Predicate: gt */
37
56
pure fn gt ( x : uint , y : uint ) -> bool { ret x > y; }
38
57
39
58
fn max ( x : uint , y : uint ) -> uint { if x > y { ret x; } ret y; }
40
59
41
60
fn min ( x : uint , y : uint ) -> uint { if x > y { ret y; } ret x; }
42
61
62
+ /*
63
+ Function: range
64
+
65
+ Iterate over the range [`lo`..`hi`)
66
+ */
43
67
fn range ( lo : uint , hi : uint , it : block ( uint ) ) {
44
68
while lo < hi { it ( lo) ; lo += 1 u; }
45
69
}
46
70
71
+ /*
72
+ Function: next_power_of_two
73
+
74
+ Returns the smallest power of 2 greater than or equal to `n`
75
+ */
47
76
fn next_power_of_two ( n : uint ) -> uint {
48
77
let halfbits: uint = sys:: size_of :: < uint > ( ) * 4 u;
49
78
let tmp: uint = n - 1 u;
@@ -52,6 +81,20 @@ fn next_power_of_two(n: uint) -> uint {
52
81
ret tmp + 1 u;
53
82
}
54
83
84
+ /*
85
+ Function: parse_buf
86
+
87
+ Parse a buffer of bytes
88
+
89
+ Parameters:
90
+
91
+ buf - A byte buffer
92
+ radix - The base of the number
93
+
94
+ Failure:
95
+
96
+ buf must not be empty
97
+ */
55
98
fn parse_buf ( buf : [ u8 ] , radix : uint ) -> uint {
56
99
if vec:: len :: < u8 > ( buf) == 0 u {
57
100
log_err "parse_buf(): buf is empty" ;
@@ -69,8 +112,22 @@ fn parse_buf(buf: [u8], radix: uint) -> uint {
69
112
fail;
70
113
}
71
114
115
+ /*
116
+ Function: from_str
117
+
118
+ Parse a string to an int
119
+
120
+ Failure:
121
+
122
+ s must not be empty
123
+ */
72
124
fn from_str ( s : str ) -> uint { parse_buf ( str:: bytes ( s) , 10 u) }
73
125
126
+ /*
127
+ Function: to_str
128
+
129
+ Convert to a string in a given base
130
+ */
74
131
fn to_str ( num : uint , radix : uint ) -> str {
75
132
let n = num;
76
133
assert ( 0 u < radix && radix <= 16 u) ;
@@ -106,6 +163,12 @@ fn to_str(num: uint, radix: uint) -> str {
106
163
while len != 0 u { len -= 1 u; s1 += str:: unsafe_from_byte ( s[ len] ) ; }
107
164
ret s1;
108
165
}
166
+
167
+ /*
168
+ Function: str
169
+
170
+ Convert to a string
171
+ */
109
172
fn str ( i : uint ) -> str { ret to_str ( i, 10 u) ; }
110
173
111
174
// Local Variables:
0 commit comments