aboutsummaryrefslogtreecommitdiff
path: root/src/lib/_int.rs
diff options
context:
space:
mode:
authorRoy Frostig <[email protected]>2010-08-06 15:48:23 -0700
committerRoy Frostig <[email protected]>2010-08-06 15:48:23 -0700
commit80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad (patch)
treec7da38fa419c3eaa17d10750fedb7da2e40e1aa4 /src/lib/_int.rs
parentAccept uint literals as literal patterns. (diff)
downloadrust-80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad.tar.xz
rust-80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad.zip
Redo yesterday's buf_writer-wrapper in a less silly and convoluted way. Add integer stringifying functions to _int module.
Diffstat (limited to 'src/lib/_int.rs')
-rw-r--r--src/lib/_int.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lib/_int.rs b/src/lib/_int.rs
index 9b756675..03017259 100644
--- a/src/lib/_int.rs
+++ b/src/lib/_int.rs
@@ -44,3 +44,47 @@ fn next_power_of_two(uint n) -> uint {
}
ret tmp + 1u;
}
+
+fn uto_string(mutable uint n, uint radix) -> str
+{
+ check (0u < radix && radix <= 16u);
+ fn digit(uint n) -> str {
+ 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"; }
+ }
+ }
+
+ if (n == 0u) { ret "0"; }
+
+ let str s = "";
+ while (n > 0u) {
+ s = digit(n % radix) + s;
+ n /= radix;
+ }
+ ret s;
+}
+
+fn to_string(mutable int n, uint radix) -> str
+{
+ check (0u < radix && radix <= 16u);
+ if (n < 0) {
+ ret "-" + uto_string((-n) as uint, radix);
+ } else {
+ ret uto_string(n as uint, radix);
+ }
+}