aboutsummaryrefslogtreecommitdiff
path: root/src/lib/_uint.rs
diff options
context:
space:
mode:
authorGraydon Hoare <[email protected]>2010-08-20 15:37:20 -0700
committerGraydon Hoare <[email protected]>2010-08-20 15:37:20 -0700
commita2bd79a6acc9fc9f1dba6e718c9d9eda7791a277 (patch)
tree1a520b5c0047b03a5d090123db1443e0d664aa17 /src/lib/_uint.rs
parentAccumulate number tokens properly, handle newline, EQ and EQEQ in rustc lexer. (diff)
downloadrust-a2bd79a6acc9fc9f1dba6e718c9d9eda7791a277.tar.xz
rust-a2bd79a6acc9fc9f1dba6e718c9d9eda7791a277.zip
Redo _uint.to_str to work with chars and only one tmp str, built left-to-right.
Diffstat (limited to 'src/lib/_uint.rs')
-rw-r--r--src/lib/_uint.rs56
1 files changed, 37 insertions, 19 deletions
diff --git a/src/lib/_uint.rs b/src/lib/_uint.rs
index 8a03d00b..897f0da6 100644
--- a/src/lib/_uint.rs
+++ b/src/lib/_uint.rs
@@ -36,33 +36,51 @@ fn next_power_of_two(uint n) -> uint {
fn to_str(mutable uint n, uint radix) -> str
{
check (0u < radix && radix <= 16u);
- fn digit(uint n) -> str {
+ fn digit(uint n) -> char {
alt (n) {
- case (0u) { ret "0"; }
- case (1u) { ret "1"; }
- case (2u) { ret "2"; }
- case (3u) { ret "3"; }
- case (4u) { ret "4"; }
- case (5u) { ret "5"; }
- case (6u) { ret "6"; }
- case (7u) { ret "7"; }
- case (8u) { ret "8"; }
- case (9u) { ret "9"; }
- case (10u) { ret "a"; }
- case (11u) { ret "b"; }
- case (12u) { ret "c"; }
- case (13u) { ret "d"; }
- case (14u) { ret "e"; }
- case (15u) { ret "f"; }
+ case (0u) { ret '0'; }
+ case (1u) { ret '1'; }
+ case (2u) { ret '2'; }
+ case (3u) { ret '3'; }
+ case (4u) { ret '4'; }
+ case (5u) { ret '5'; }
+ case (6u) { ret '6'; }
+ case (7u) { ret '7'; }
+ case (8u) { ret '8'; }
+ case (9u) { ret '9'; }
+ case (10u) { ret 'a'; }
+ case (11u) { ret 'b'; }
+ case (12u) { ret 'c'; }
+ case (13u) { ret 'd'; }
+ case (14u) { ret 'e'; }
+ case (15u) { ret 'f'; }
}
}
if (n == 0u) { ret "0"; }
+ let uint r = 1u;
+ if (n > r) {
+ while ((r*radix) < n) {
+ r *= radix;
+ }
+ }
+
let str s = "";
while (n > 0u) {
- s = digit(n % radix) + s;
- n /= radix;
+
+ auto i = n/r;
+
+ n -= (i * r);
+ r /= radix;
+
+ s += digit(i) as u8;
+ }
+
+ while (r > 0u) {
+ s += '0' as u8;
+ r /= radix;
}
+
ret s;
}