diff options
| author | Graydon Hoare <graydon@mozilla.com> | 2012-08-02 17:14:26 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2012-08-02 18:06:33 -0700 |
| commit | b14a6aca9f5f62df2ac12ee92d0ca37514908305 (patch) | |
| tree | 22a2d20f663669c894cab75e309b028f8c335200 /src/libcore | |
| parent | 4019d3a86b907c80de8b26c5b5c1f5bb52636fb1 (diff) | |
| download | rust-b14a6aca9f5f62df2ac12ee92d0ca37514908305.tar.gz rust-b14a6aca9f5f62df2ac12ee92d0ca37514908305.zip | |
Cleanups in the int and uint templates.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/int-template.rs | 113 | ||||
| -rw-r--r-- | src/libcore/int-template/i16.rs | 3 | ||||
| -rw-r--r-- | src/libcore/int-template/i32.rs | 3 | ||||
| -rw-r--r-- | src/libcore/int-template/i64.rs | 3 | ||||
| -rw-r--r-- | src/libcore/int-template/i8.rs | 3 | ||||
| -rw-r--r-- | src/libcore/int-template/int.rs | 12 | ||||
| -rw-r--r-- | src/libcore/uint-template.rs | 60 | ||||
| -rw-r--r-- | src/libcore/uint-template/u16.rs | 1 | ||||
| -rw-r--r-- | src/libcore/uint-template/u32.rs | 1 | ||||
| -rw-r--r-- | src/libcore/uint-template/u64.rs | 1 | ||||
| -rw-r--r-- | src/libcore/uint-template/u8.rs | 1 | ||||
| -rw-r--r-- | src/libcore/uint-template/uint.rs | 11 |
12 files changed, 114 insertions, 98 deletions
diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index 82b6d00d909..1cd1480ec43 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -12,8 +12,12 @@ export compl; export abs; export parse_buf, from_str, to_str, to_str_bytes, str; export num, ord, eq, times, timesi; +export bits, bytes; -const min_value: T = -1 as T << (inst::bits - 1 as T); +const bits : uint = inst::bits; +const bytes : uint = (inst::bits / 8); + +const min_value: T = (-1 as T) << (bits - 1); const max_value: T = min_value - 1 as T; pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } } @@ -58,59 +62,6 @@ pure fn abs(i: T) -> T { if is_negative(i) { -i } else { i } } -/** - * Parse a buffer of bytes - * - * # Arguments - * - * * buf - A byte buffer - * * radix - The base of the number - */ -fn parse_buf(buf: ~[u8], radix: uint) -> option<T> { - if vec::len(buf) == 0u { return none; } - let mut i = vec::len(buf) - 1u; - let mut start = 0u; - let mut power = 1 as T; - - if buf[0] == ('-' as u8) { - power = -1 as T; - start = 1u; - } - let mut n = 0 as T; - loop { - alt char::to_digit(buf[i] as char, radix) { - some(d) { n += (d as T) * power; } - none { return none; } - } - power *= radix as T; - if i <= start { return some(n); } - i -= 1u; - }; -} - -/// Parse a string to an int -fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) } - -/// Convert to a string in a given base -fn to_str(n: T, radix: uint) -> ~str { - do to_str_bytes(n, radix) |slice| { - do vec::as_buf(slice) |p, len| { - unsafe { str::unsafe::from_buf_len(p, len) } - } - } -} - -fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { - if n < 0 as T { - uint::to_str_bytes(true, -n as uint, radix, f) - } else { - uint::to_str_bytes(false, n as uint, radix, f) - } -} - -/// Convert to a string -fn str(i: T) -> ~str { return to_str(i, 10u); } - impl ord of ord for T { pure fn lt(&&other: T) -> bool { return self < other; @@ -123,6 +74,7 @@ impl eq of eq for T { } } + impl num of num::num for T { pure fn add(&&other: T) -> T { return self + other; } pure fn sub(&&other: T) -> T { return self - other; } @@ -172,6 +124,59 @@ impl timesi of iter::timesi for T { } } +/** + * Parse a buffer of bytes + * + * # Arguments + * + * * buf - A byte buffer + * * radix - The base of the number + */ +fn parse_buf(buf: ~[u8], radix: uint) -> option<T> { + if vec::len(buf) == 0u { return none; } + let mut i = vec::len(buf) - 1u; + let mut start = 0u; + let mut power = 1 as T; + + if buf[0] == ('-' as u8) { + power = -1 as T; + start = 1u; + } + let mut n = 0 as T; + loop { + alt char::to_digit(buf[i] as char, radix) { + some(d) { n += (d as T) * power; } + none { return none; } + } + power *= radix as T; + if i <= start { return some(n); } + i -= 1u; + }; +} + +/// Parse a string to an int +fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) } + +/// Convert to a string in a given base +fn to_str(n: T, radix: uint) -> ~str { + do to_str_bytes(n, radix) |slice| { + do vec::as_buf(slice) |p, len| { + unsafe { str::unsafe::from_buf_len(p, len) } + } + } +} + +fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { + if n < 0 as T { + uint::to_str_bytes(true, -n as uint, radix, f) + } else { + uint::to_str_bytes(false, n as uint, radix, f) + } +} + +/// Convert to a string +fn str(i: T) -> ~str { return to_str(i, 10u); } + // FIXME: Has alignment issues on windows and 32-bit linux (#2609) #[test] #[ignore] diff --git a/src/libcore/int-template/i16.rs b/src/libcore/int-template/i16.rs index 06eb96ba59c..75a8409f6ed 100644 --- a/src/libcore/int-template/i16.rs +++ b/src/libcore/int-template/i16.rs @@ -1,3 +1,2 @@ type T = i16; - -const bits: T = 16 as T; +const bits: uint = u16::bits; \ No newline at end of file diff --git a/src/libcore/int-template/i32.rs b/src/libcore/int-template/i32.rs index 151f3582586..043ab95f579 100644 --- a/src/libcore/int-template/i32.rs +++ b/src/libcore/int-template/i32.rs @@ -1,3 +1,2 @@ type T = i32; - -const bits: T = 32 as T; +const bits: uint = u32::bits; diff --git a/src/libcore/int-template/i64.rs b/src/libcore/int-template/i64.rs index 1c8a181ab8e..cea3c77c7f5 100644 --- a/src/libcore/int-template/i64.rs +++ b/src/libcore/int-template/i64.rs @@ -1,3 +1,2 @@ type T = i64; - -const bits: T = 64 as T; +const bits: uint = u64::bits; \ No newline at end of file diff --git a/src/libcore/int-template/i8.rs b/src/libcore/int-template/i8.rs index c103f1cbca4..25614791ad6 100644 --- a/src/libcore/int-template/i8.rs +++ b/src/libcore/int-template/i8.rs @@ -1,3 +1,2 @@ type T = i8; - -const bits: T = 8 as T; +const bits: uint = u8::bits; \ No newline at end of file diff --git a/src/libcore/int-template/int.rs b/src/libcore/int-template/int.rs index 2ed6155edc0..96ca59335a5 100644 --- a/src/libcore/int-template/int.rs +++ b/src/libcore/int-template/int.rs @@ -1,13 +1,11 @@ type T = int; - -#[cfg(target_arch = "x86")] -const bits: T = 32 as T; - -#[cfg(target_arch = "x86_64")] -const bits: T = 64 as T; +const bits: uint = uint::bits; /// Produce a uint suitable for use in a hash table -pure fn hash(x: &int) -> uint { *x as uint } +pure fn hash(x: &int) -> uint { + let u : uint = *x as uint; + uint::hash(&u) +} /// Returns `base` raised to the power of `exponent` fn pow(base: int, exponent: uint) -> int { diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index 49ea87785ec..d3e1fb41ca2 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -12,6 +12,10 @@ export compl; export to_str, to_str_bytes; export from_str, from_str_radix, str, parse_buf; export num, ord, eq, times, timesi; +export bits, bytes; + +const bits : uint = inst::bits; +const bytes : uint = (inst::bits / 8); const min_value: T = 0 as T; const max_value: T = 0 as T - 1 as T; @@ -76,34 +80,6 @@ impl num of num::num for T { pure fn from_int(n: int) -> T { return n as T; } } -/** - * Parse a buffer of bytes - * - * # Arguments - * - * * buf - A byte buffer - * * radix - The base of the number - * - * # Failure - * - * `buf` must not be empty - */ -fn parse_buf(buf: ~[u8], radix: uint) -> option<T> { - if vec::len(buf) == 0u { return none; } - let mut i = vec::len(buf) - 1u; - let mut power = 1u as T; - let mut n = 0u as T; - loop { - alt char::to_digit(buf[i] as char, radix) { - some(d) { n += d as T * power; } - none { return none; } - } - power *= radix as T; - if i == 0u { return some(n); } - i -= 1u; - }; -} - impl times of iter::times for T { #[inline(always)] #[doc = "A convenience form for basic iteration. Given a variable `x` \ @@ -133,6 +109,34 @@ impl timesi of iter::timesi for T { } } +/** + * Parse a buffer of bytes + * + * # Arguments + * + * * buf - A byte buffer + * * radix - The base of the number + * + * # Failure + * + * `buf` must not be empty + */ +fn parse_buf(buf: ~[u8], radix: uint) -> option<T> { + if vec::len(buf) == 0u { return none; } + let mut i = vec::len(buf) - 1u; + let mut power = 1u as T; + let mut n = 0u as T; + loop { + alt char::to_digit(buf[i] as char, radix) { + some(d) { n += d as T * power; } + none { return none; } + } + power *= radix as T; + if i == 0u { return some(n); } + i -= 1u; + }; +} + /// Parse a string to an int fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) } diff --git a/src/libcore/uint-template/u16.rs b/src/libcore/uint-template/u16.rs index 8d03656d6fe..b84b975c685 100644 --- a/src/libcore/uint-template/u16.rs +++ b/src/libcore/uint-template/u16.rs @@ -1 +1,2 @@ type T = u16; +const bits: uint = 16; diff --git a/src/libcore/uint-template/u32.rs b/src/libcore/uint-template/u32.rs index df9cf28a16c..d5324e03a16 100644 --- a/src/libcore/uint-template/u32.rs +++ b/src/libcore/uint-template/u32.rs @@ -1 +1,2 @@ type T = u32; +const bits: uint = 32; \ No newline at end of file diff --git a/src/libcore/uint-template/u64.rs b/src/libcore/uint-template/u64.rs index 4e83ebd4976..ee7f35bf6e3 100644 --- a/src/libcore/uint-template/u64.rs +++ b/src/libcore/uint-template/u64.rs @@ -1 +1,2 @@ type T = u64; +const bits: uint = 64; \ No newline at end of file diff --git a/src/libcore/uint-template/u8.rs b/src/libcore/uint-template/u8.rs index 96b2dd6d9c3..b7df2605db4 100644 --- a/src/libcore/uint-template/u8.rs +++ b/src/libcore/uint-template/u8.rs @@ -1,4 +1,5 @@ type T = u8; +const bits: uint = 8; // Type-specific functions here. These must be reexported by the // parent module so that they appear in core::u8 and not core::u8::u8; diff --git a/src/libcore/uint-template/uint.rs b/src/libcore/uint-template/uint.rs index eb8b08a4fe5..fc65d9a63e4 100644 --- a/src/libcore/uint-template/uint.rs +++ b/src/libcore/uint-template/uint.rs @@ -1,5 +1,12 @@ type T = uint; +#[cfg(target_arch = "x86")] +#[cfg(target_arch = "arm")] +const bits: uint = 32; + +#[cfg(target_arch = "x86_64")] +const bits: uint = 64; + /** * Divide two numbers, return the result, rounded up. * @@ -54,7 +61,9 @@ pure fn div_round(x: uint, y: uint) -> uint { pure fn div_floor(x: uint, y: uint) -> uint { return x / y; } /// Produce a uint suitable for use in a hash table -pure fn hash(x: &uint) -> uint { *x } +pure fn hash(x: &uint) -> uint { + hash::hash_uint(*x) as uint +} /** * Iterate over the range [`lo`..`hi`), or stop when requested |
