rust/src/lib/u64.rs

37 lines
811 B
Rust
Raw Normal View History

2011-09-02 17:34:58 -05:00
fn to_str(n: u64, radix: uint) -> str {
2011-07-27 07:19:39 -05:00
assert (0u < radix && radix <= 16u);
2011-07-27 07:19:39 -05:00
let r64 = radix as u64;
2011-09-02 17:34:58 -05:00
fn digit(n: u64) -> str {
2011-07-27 07:19:39 -05:00
ret alt n {
2011-09-02 17:34:58 -05:00
0u64 { "0" }
1u64 { "1" }
2u64 { "2" }
3u64 { "3" }
4u64 { "4" }
5u64 { "5" }
6u64 { "6" }
7u64 { "7" }
8u64 { "8" }
9u64 { "9" }
10u64 { "a" }
11u64 { "b" }
12u64 { "c" }
13u64 { "d" }
14u64 { "e" }
15u64 { "f" }
2011-07-27 07:19:39 -05:00
_ { fail }
};
}
2011-09-02 17:34:58 -05:00
if n == 0u64 { ret "0"; }
2011-09-02 17:34:58 -05:00
let s = "";
2011-07-27 07:19:39 -05:00
while n > 0u64 { s = digit(n % r64) + s; n /= r64; }
ret s;
}
2011-09-02 17:34:58 -05:00
fn str(n: u64) -> str { ret to_str(n, 10u); }