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/uint-template.rs | |
| parent | 4019d3a86b907c80de8b26c5b5c1f5bb52636fb1 (diff) | |
Cleanups in the int and uint templates.
Diffstat (limited to 'src/libcore/uint-template.rs')
| -rw-r--r-- | src/libcore/uint-template.rs | 60 |
1 files changed, 32 insertions, 28 deletions
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) } |
