aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-04-17 16:48:03 -0400
committerBrian Anderson <[email protected]>2011-04-17 16:51:17 -0400
commit8f6603f58ec7247c585f795413ff5b4ddf409c49 (patch)
tree401f1de8cd40ce027f37130b7560adbcd38fd58a
parentSupport #fmt precision for unsigned types (diff)
downloadrust-8f6603f58ec7247c585f795413ff5b4ddf409c49.tar.xz
rust-8f6603f58ec7247c585f795413ff5b4ddf409c49.zip
Support #fmt precision for signed types
-rw-r--r--src/lib/ExtFmt.rs83
-rw-r--r--src/test/run-pass/syntax-extension-fmt.rs32
2 files changed, 66 insertions, 49 deletions
diff --git a/src/lib/ExtFmt.rs b/src/lib/ExtFmt.rs
index f8c542c6..23290384 100644
--- a/src/lib/ExtFmt.rs
+++ b/src/lib/ExtFmt.rs
@@ -332,46 +332,13 @@ mod RT {
ty ty);
fn conv_int(&conv cv, int i) -> str {
- ret pad(cv, _int.to_str(i, 10u));
+ auto radix = 10u;
+ auto prec = get_int_precision(cv);
+ ret pad(cv, int_to_str_prec(i, radix, prec));
}
fn conv_uint(&conv cv, uint u) -> str {
-
- // Convert a uint to string with a minimum number of digits. If
- // precision is 0 and num is 0 then the result is the empty
- // string. Could move this to _str, but it doesn't seem all that
- // useful.
- fn uint_to_str_prec(uint num, uint radix, uint prec) -> str {
- auto s;
-
- if (prec == 0u && num == 0u) {
- s = "";
- } else {
- s = _uint.to_str(num, radix);
- auto len = _str.char_len(s);
- if (len < prec) {
- auto diff = prec - len;
- auto pad = str_init_elt('0', diff);
- s = pad + s;
- }
- }
-
- ret s;
- }
-
- fn get_precision(&conv cv) -> uint {
- alt (cv.precision) {
- case (count_is(?c)) {
- ret c as uint;
- }
- case (count_implied) {
- ret 1u;
- }
- }
- }
-
- auto prec = get_precision(cv);
-
+ auto prec = get_int_precision(cv);
auto res;
alt (cv.ty) {
case (ty_default) {
@@ -418,6 +385,48 @@ mod RT {
ret pad(cv, unpadded);
}
+ // Convert an int to string with minimum number of digits. If precision is
+ // 0 and num is 0 then the result is the empty string.
+ fn int_to_str_prec(int num, uint radix, uint prec) -> str {
+ if (num < 0) {
+ ret "-" + uint_to_str_prec((-num) as uint, radix, prec);
+ } else {
+ ret uint_to_str_prec(num as uint, radix, prec);
+ }
+ }
+
+ // Convert a uint to string with a minimum number of digits. If precision
+ // is 0 and num is 0 then the result is the empty string. Could move this
+ // to _uint, but it doesn't seem all that useful.
+ fn uint_to_str_prec(uint num, uint radix, uint prec) -> str {
+ auto s;
+
+ if (prec == 0u && num == 0u) {
+ s = "";
+ } else {
+ s = _uint.to_str(num, radix);
+ auto len = _str.char_len(s);
+ if (len < prec) {
+ auto diff = prec - len;
+ auto pad = str_init_elt('0', diff);
+ s = pad + s;
+ }
+ }
+
+ ret s;
+ }
+
+ fn get_int_precision(&conv cv) -> uint {
+ alt (cv.precision) {
+ case (count_is(?c)) {
+ ret c as uint;
+ }
+ case (count_implied) {
+ ret 1u;
+ }
+ }
+ }
+
// FIXME: This might be useful in _str, but needs to be utf8 safe first
fn str_init_elt(char c, uint n_elts) -> str {
auto svec = _vec.init_elt[u8](c as u8, n_elts);
diff --git a/src/test/run-pass/syntax-extension-fmt.rs b/src/test/run-pass/syntax-extension-fmt.rs
index 5c71a27b..b75529fa 100644
--- a/src/test/run-pass/syntax-extension-fmt.rs
+++ b/src/test/run-pass/syntax-extension-fmt.rs
@@ -55,39 +55,47 @@ fn main() {
test(#fmt("%-10t", 0xff_u), "11111111 ");
// Precision
-// test(#fmt("%.d", 0), "");
+ test(#fmt("%.d", 0), "");
test(#fmt("%.u", 0u), "");
test(#fmt("%.x", 0u), "");
-// test(#fmt("%.d", 10), "10");
-// test(#fmt("%.d", -10), "-10");
+ test(#fmt("%.t", 0u), "");
+ test(#fmt("%.d", 10), "10");
+ test(#fmt("%.d", -10), "-10");
test(#fmt("%.u", 10u), "10");
test(#fmt("%.s", "test"), "");
test(#fmt("%.x", 127u), "7f");
+ test(#fmt("%.t", 3u), "11");
-// test(#fmt("%.0d", 0), "");
+ test(#fmt("%.0d", 0), "");
test(#fmt("%.0u", 0u), "");
test(#fmt("%.0x", 0u), "");
-// test(#fmt("%.0d", 10), "10");
-// test(#fmt("%.0d", -10), "-10");
+ test(#fmt("%.0t", 0u), "");
+ test(#fmt("%.0d", 10), "10");
+ test(#fmt("%.0d", -10), "-10");
test(#fmt("%.0u", 10u), "10");
test(#fmt("%.0s", "test"), "");
test(#fmt("%.0x", 127u), "7f");
+ test(#fmt("%.0t", 3u), "11");
-// test(#fmt("%.1d", 0), "0");
+ test(#fmt("%.1d", 0), "0");
test(#fmt("%.1u", 0u), "0");
test(#fmt("%.1x", 0u), "0");
-// test(#fmt("%.1d", 10), "10");
-// test(#fmt("%.1d", -10), "-10");
+ test(#fmt("%.1t", 0u), "0");
+ test(#fmt("%.1d", 10), "10");
+ test(#fmt("%.1d", -10), "-10");
test(#fmt("%.1u", 10u), "10");
test(#fmt("%.1s", "test"), "t");
test(#fmt("%.1x", 127u), "7f");
+ test(#fmt("%.1t", 3u), "11");
-// test(#fmt("%.5d", 0), "00000");
+ test(#fmt("%.5d", 0), "00000");
test(#fmt("%.5u", 0u), "00000");
test(#fmt("%.5x", 0u), "00000");
-// test(#fmt("%.5d", 10), "00010");
-// test(#fmt("%.5d", -10), "-00010");
+ test(#fmt("%.5t", 0u), "00000");
+ test(#fmt("%.5d", 10), "00010");
+ test(#fmt("%.5d", -10), "-00010");
test(#fmt("%.5u", 10u), "00010");
test(#fmt("%.5s", "test"), "test");
test(#fmt("%.5x", 127u), "0007f");
+ test(#fmt("%.5t", 3u), "00011");
}