Skip to content

Commit 967d4ad

Browse files
committed
---
yaml --- r: 31594 b: refs/heads/dist-snap c: b14a6ac h: refs/heads/master v: v3
1 parent 5832ee2 commit 967d4ad

File tree

13 files changed

+115
-99
lines changed

13 files changed

+115
-99
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 4019d3a86b907c80de8b26c5b5c1f5bb52636fb1
10+
refs/heads/dist-snap: b14a6aca9f5f62df2ac12ee92d0ca37514908305
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/src/libcore/int-template.rs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ export compl;
1212
export abs;
1313
export parse_buf, from_str, to_str, to_str_bytes, str;
1414
export num, ord, eq, times, timesi;
15+
export bits, bytes;
1516

16-
const min_value: T = -1 as T << (inst::bits - 1 as T);
17+
const bits : uint = inst::bits;
18+
const bytes : uint = (inst::bits / 8);
19+
20+
const min_value: T = (-1 as T) << (bits - 1);
1721
const max_value: T = min_value - 1 as T;
1822

1923
pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } }
@@ -58,59 +62,6 @@ pure fn abs(i: T) -> T {
5862
if is_negative(i) { -i } else { i }
5963
}
6064

61-
/**
62-
* Parse a buffer of bytes
63-
*
64-
* # Arguments
65-
*
66-
* * buf - A byte buffer
67-
* * radix - The base of the number
68-
*/
69-
fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
70-
if vec::len(buf) == 0u { return none; }
71-
let mut i = vec::len(buf) - 1u;
72-
let mut start = 0u;
73-
let mut power = 1 as T;
74-
75-
if buf[0] == ('-' as u8) {
76-
power = -1 as T;
77-
start = 1u;
78-
}
79-
let mut n = 0 as T;
80-
loop {
81-
alt char::to_digit(buf[i] as char, radix) {
82-
some(d) { n += (d as T) * power; }
83-
none { return none; }
84-
}
85-
power *= radix as T;
86-
if i <= start { return some(n); }
87-
i -= 1u;
88-
};
89-
}
90-
91-
/// Parse a string to an int
92-
fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) }
93-
94-
/// Convert to a string in a given base
95-
fn to_str(n: T, radix: uint) -> ~str {
96-
do to_str_bytes(n, radix) |slice| {
97-
do vec::as_buf(slice) |p, len| {
98-
unsafe { str::unsafe::from_buf_len(p, len) }
99-
}
100-
}
101-
}
102-
103-
fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
104-
if n < 0 as T {
105-
uint::to_str_bytes(true, -n as uint, radix, f)
106-
} else {
107-
uint::to_str_bytes(false, n as uint, radix, f)
108-
}
109-
}
110-
111-
/// Convert to a string
112-
fn str(i: T) -> ~str { return to_str(i, 10u); }
113-
11465
impl ord of ord for T {
11566
pure fn lt(&&other: T) -> bool {
11667
return self < other;
@@ -123,6 +74,7 @@ impl eq of eq for T {
12374
}
12475
}
12576

77+
12678
impl num of num::num for T {
12779
pure fn add(&&other: T) -> T { return self + other; }
12880
pure fn sub(&&other: T) -> T { return self - other; }
@@ -172,6 +124,59 @@ impl timesi of iter::timesi for T {
172124
}
173125
}
174126

127+
/**
128+
* Parse a buffer of bytes
129+
*
130+
* # Arguments
131+
*
132+
* * buf - A byte buffer
133+
* * radix - The base of the number
134+
*/
135+
fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
136+
if vec::len(buf) == 0u { return none; }
137+
let mut i = vec::len(buf) - 1u;
138+
let mut start = 0u;
139+
let mut power = 1 as T;
140+
141+
if buf[0] == ('-' as u8) {
142+
power = -1 as T;
143+
start = 1u;
144+
}
145+
let mut n = 0 as T;
146+
loop {
147+
alt char::to_digit(buf[i] as char, radix) {
148+
some(d) { n += (d as T) * power; }
149+
none { return none; }
150+
}
151+
power *= radix as T;
152+
if i <= start { return some(n); }
153+
i -= 1u;
154+
};
155+
}
156+
157+
/// Parse a string to an int
158+
fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) }
159+
160+
/// Convert to a string in a given base
161+
fn to_str(n: T, radix: uint) -> ~str {
162+
do to_str_bytes(n, radix) |slice| {
163+
do vec::as_buf(slice) |p, len| {
164+
unsafe { str::unsafe::from_buf_len(p, len) }
165+
}
166+
}
167+
}
168+
169+
fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
170+
if n < 0 as T {
171+
uint::to_str_bytes(true, -n as uint, radix, f)
172+
} else {
173+
uint::to_str_bytes(false, n as uint, radix, f)
174+
}
175+
}
176+
177+
/// Convert to a string
178+
fn str(i: T) -> ~str { return to_str(i, 10u); }
179+
175180
// FIXME: Has alignment issues on windows and 32-bit linux (#2609)
176181
#[test]
177182
#[ignore]
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
type T = i16;
2-
3-
const bits: T = 16 as T;
2+
const bits: uint = u16::bits;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
type T = i32;
2-
3-
const bits: T = 32 as T;
2+
const bits: uint = u32::bits;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
type T = i64;
2-
3-
const bits: T = 64 as T;
2+
const bits: uint = u64::bits;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
type T = i8;
2-
3-
const bits: T = 8 as T;
2+
const bits: uint = u8::bits;

branches/dist-snap/src/libcore/int-template/int.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
type T = int;
2-
3-
#[cfg(target_arch = "x86")]
4-
const bits: T = 32 as T;
5-
6-
#[cfg(target_arch = "x86_64")]
7-
const bits: T = 64 as T;
2+
const bits: uint = uint::bits;
83

94
/// Produce a uint suitable for use in a hash table
10-
pure fn hash(x: &int) -> uint { *x as uint }
5+
pure fn hash(x: &int) -> uint {
6+
let u : uint = *x as uint;
7+
uint::hash(&u)
8+
}
119

1210
/// Returns `base` raised to the power of `exponent`
1311
fn pow(base: int, exponent: uint) -> int {

branches/dist-snap/src/libcore/uint-template.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export compl;
1212
export to_str, to_str_bytes;
1313
export from_str, from_str_radix, str, parse_buf;
1414
export num, ord, eq, times, timesi;
15+
export bits, bytes;
16+
17+
const bits : uint = inst::bits;
18+
const bytes : uint = (inst::bits / 8);
1519

1620
const min_value: T = 0 as T;
1721
const max_value: T = 0 as T - 1 as T;
@@ -76,34 +80,6 @@ impl num of num::num for T {
7680
pure fn from_int(n: int) -> T { return n as T; }
7781
}
7882

79-
/**
80-
* Parse a buffer of bytes
81-
*
82-
* # Arguments
83-
*
84-
* * buf - A byte buffer
85-
* * radix - The base of the number
86-
*
87-
* # Failure
88-
*
89-
* `buf` must not be empty
90-
*/
91-
fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
92-
if vec::len(buf) == 0u { return none; }
93-
let mut i = vec::len(buf) - 1u;
94-
let mut power = 1u as T;
95-
let mut n = 0u as T;
96-
loop {
97-
alt char::to_digit(buf[i] as char, radix) {
98-
some(d) { n += d as T * power; }
99-
none { return none; }
100-
}
101-
power *= radix as T;
102-
if i == 0u { return some(n); }
103-
i -= 1u;
104-
};
105-
}
106-
10783
impl times of iter::times for T {
10884
#[inline(always)]
10985
#[doc = "A convenience form for basic iteration. Given a variable `x` \
@@ -133,6 +109,34 @@ impl timesi of iter::timesi for T {
133109
}
134110
}
135111

112+
/**
113+
* Parse a buffer of bytes
114+
*
115+
* # Arguments
116+
*
117+
* * buf - A byte buffer
118+
* * radix - The base of the number
119+
*
120+
* # Failure
121+
*
122+
* `buf` must not be empty
123+
*/
124+
fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
125+
if vec::len(buf) == 0u { return none; }
126+
let mut i = vec::len(buf) - 1u;
127+
let mut power = 1u as T;
128+
let mut n = 0u as T;
129+
loop {
130+
alt char::to_digit(buf[i] as char, radix) {
131+
some(d) { n += d as T * power; }
132+
none { return none; }
133+
}
134+
power *= radix as T;
135+
if i == 0u { return some(n); }
136+
i -= 1u;
137+
};
138+
}
139+
136140
/// Parse a string to an int
137141
fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) }
138142

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
type T = u16;
2+
const bits: uint = 16;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
type T = u32;
2+
const bits: uint = 32;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
type T = u64;
2+
const bits: uint = 64;

branches/dist-snap/src/libcore/uint-template/u8.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type T = u8;
2+
const bits: uint = 8;
23

34
// Type-specific functions here. These must be reexported by the
45
// parent module so that they appear in core::u8 and not core::u8::u8;

branches/dist-snap/src/libcore/uint-template/uint.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
type T = uint;
22

3+
#[cfg(target_arch = "x86")]
4+
#[cfg(target_arch = "arm")]
5+
const bits: uint = 32;
6+
7+
#[cfg(target_arch = "x86_64")]
8+
const bits: uint = 64;
9+
310
/**
411
* Divide two numbers, return the result, rounded up.
512
*
@@ -54,7 +61,9 @@ pure fn div_round(x: uint, y: uint) -> uint {
5461
pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }
5562

5663
/// Produce a uint suitable for use in a hash table
57-
pure fn hash(x: &uint) -> uint { *x }
64+
pure fn hash(x: &uint) -> uint {
65+
hash::hash_uint(*x) as uint
66+
}
5867

5968
/**
6069
* Iterate over the range [`lo`..`hi`), or stop when requested

0 commit comments

Comments
 (0)