Add an int->str conversion function.

The test currently fails because string equality isn't implemented.
This commit is contained in:
Jeffrey Yasskin 2010-07-24 16:01:34 -07:00 committed by Graydon Hoare
parent 987589e946
commit 581a95a804
4 changed files with 29 additions and 12 deletions

View File

@ -395,6 +395,7 @@ TEST_XFAILS_X86 := $(TASK_XFAILS) \
test/run-pass/fn-lval.rs \ test/run-pass/fn-lval.rs \
test/run-pass/generic-fn-infer.rs \ test/run-pass/generic-fn-infer.rs \
test/run-pass/generic-recursive-tag.rs \ test/run-pass/generic-recursive-tag.rs \
test/run-pass/int-lib.rs \
test/run-pass/iter-ret.rs \ test/run-pass/iter-ret.rs \
test/run-pass/lib-deque.rs \ test/run-pass/lib-deque.rs \
test/run-pass/lib-map.rs \ test/run-pass/lib-map.rs \
@ -464,6 +465,7 @@ TEST_XFAILS_LLVM := $(TASK_XFAILS) \
import.rs \ import.rs \
inner-module.rs \ inner-module.rs \
integral-indexing.rs \ integral-indexing.rs \
int-lib.rs \
iter-range.rs \ iter-range.rs \
iter-ret.rs \ iter-ret.rs \
large-records.rs \ large-records.rs \

View File

@ -45,7 +45,7 @@ fn next_power_of_two(uint n) -> uint {
ret tmp + 1u; 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); check (0u < radix && radix <= 16u);
fn digit(uint n) -> str { fn digit(uint n) -> str {
@ -60,12 +60,12 @@ fn uto_string(mutable uint n, uint radix) -> str
case (7u) { ret "7"; } case (7u) { ret "7"; }
case (8u) { ret "8"; } case (8u) { ret "8"; }
case (9u) { ret "9"; } case (9u) { ret "9"; }
case (10u) { ret "A"; } case (10u) { ret "a"; }
case (11u) { ret "B"; } case (11u) { ret "b"; }
case (12u) { ret "C"; } case (12u) { ret "c"; }
case (13u) { ret "D"; } case (13u) { ret "d"; }
case (14u) { ret "E"; } case (14u) { ret "e"; }
case (15u) { ret "F"; } case (15u) { ret "f"; }
} }
} }
@ -79,12 +79,12 @@ fn uto_string(mutable uint n, uint radix) -> str
ret s; 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); check (0u < radix && radix <= 16u);
if (n < 0) { if (n < 0) {
ret "-" + uto_string((-n) as uint, radix); ret "-" + uto_str((-n) as uint, radix);
} else { } else {
ret uto_string(n as uint, radix); ret uto_str(n as uint, radix);
} }
} }

View File

@ -125,8 +125,8 @@ fn file_writer(str path,
{ {
unsafe obj fw(buf_writer out) { unsafe obj fw(buf_writer out) {
fn write_str(str s) { out.write(_str.bytes(s)); } fn write_str(str s) { out.write(_str.bytes(s)); }
fn write_int(int n) { out.write(_str.bytes(_int.to_string(n, 10u))); } fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
fn write_uint(uint n) { out.write(_str.bytes(_int.uto_string(n, 10u))); } fn write_uint(uint n) { out.write(_str.bytes(_int.uto_str(n, 10u))); }
} }
ret fw(new_buf_writer(path, flags)); ret fw(new_buf_writer(path, flags));
} }

View File

@ -0,0 +1,15 @@
use std;
import std._int;
fn test_to_str() {
check (_int.to_str(0, 10u) == "0");
check (_int.to_str(1, 10u) == "1");
check (_int.to_str(-1, 10u) == "-1");
check (_int.to_str(255, 16u) == "ff");
check (_int.to_str(-71, 36u) == "-1z");
}
fn main() {
test_to_str();
}