From 80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Fri, 6 Aug 2010 15:48:23 -0700 Subject: Redo yesterday's buf_writer-wrapper in a less silly and convoluted way. Add integer stringifying functions to _int module. --- src/lib/_int.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/lib/_int.rs') 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); + } +} -- cgit v1.2.3 From 581a95a804f77259153c030d39f861282b468612 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Sat, 24 Jul 2010 16:01:34 -0700 Subject: Add an int->str conversion function. The test currently fails because string equality isn't implemented. --- src/lib/_int.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/lib/_int.rs') diff --git a/src/lib/_int.rs b/src/lib/_int.rs index 03017259..e76c2bf5 100644 --- a/src/lib/_int.rs +++ b/src/lib/_int.rs @@ -45,7 +45,7 @@ fn next_power_of_two(uint n) -> uint { ret tmp + 1u; } -fn uto_string(mutable uint n, uint radix) -> str +fn uto_str(mutable uint n, uint radix) -> str { check (0u < radix && radix <= 16u); fn digit(uint n) -> str { @@ -60,12 +60,12 @@ fn uto_string(mutable uint n, uint radix) -> str 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 (10u) { ret "a"; } + case (11u) { ret "b"; } + case (12u) { ret "c"; } + case (13u) { ret "d"; } + case (14u) { ret "e"; } + case (15u) { ret "f"; } } } @@ -79,12 +79,12 @@ fn uto_string(mutable uint n, uint radix) -> str ret s; } -fn to_string(mutable int n, uint radix) -> str +fn to_str(mutable int n, uint radix) -> str { check (0u < radix && radix <= 16u); if (n < 0) { - ret "-" + uto_string((-n) as uint, radix); + ret "-" + uto_str((-n) as uint, radix); } else { - ret uto_string(n as uint, radix); + ret uto_str(n as uint, radix); } } -- cgit v1.2.3