about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2011-04-26 20:13:23 -0400
committerBrian Anderson <andersrb@gmail.com>2011-04-26 20:20:17 -0400
commit2e12fbfc06abdf1547528047b1438204ca4121a1 (patch)
tree449454de4d0605d25c292650a563f7d4933efe4d /src
parent66b59e4b056ade5f0a5e5a8fd64534aeea9b952c (diff)
downloadrust-2e12fbfc06abdf1547528047b1438204ca4121a1.tar.gz
rust-2e12fbfc06abdf1547528047b1438204ca4121a1.zip
Support octal #fmt conversions
Diffstat (limited to 'src')
-rw-r--r--src/comp/front/extfmt.rs7
-rw-r--r--src/lib/ExtFmt.rs7
-rw-r--r--src/test/run-pass/syntax-extension-fmt.rs11
3 files changed, 25 insertions, 0 deletions
diff --git a/src/comp/front/extfmt.rs b/src/comp/front/extfmt.rs
index 39c074c3ca1..aea3e7df9c4 100644
--- a/src/comp/front/extfmt.rs
+++ b/src/comp/front/extfmt.rs
@@ -25,6 +25,7 @@ import std.ExtFmt.CT.ty_char;
 import std.ExtFmt.CT.ty_int;
 import std.ExtFmt.CT.ty_bits;
 import std.ExtFmt.CT.ty_hex;
+import std.ExtFmt.CT.ty_octal;
 import std.ExtFmt.CT.flag;
 import std.ExtFmt.CT.flag_left_justify;
 import std.ExtFmt.CT.flag_left_zero_pad;
@@ -236,6 +237,9 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
                 case (ty_bits) {
                     rt_type = "ty_bits";
                 }
+                case (ty_octal) {
+                    rt_type = "ty_octal";
+                }
                 case (_) {
                     rt_type = "ty_default";
                 }
@@ -381,6 +385,9 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
             case (ty_bits) {
                 ret make_conv_call(arg.span, "uint", cnv, arg);
             }
+            case (ty_octal) {
+                ret make_conv_call(arg.span, "uint", cnv, arg);
+            }
             case (_) {
                 log_err unsupported;
                 fail;
diff --git a/src/lib/ExtFmt.rs b/src/lib/ExtFmt.rs
index a87fc87904d..a9747c6a748 100644
--- a/src/lib/ExtFmt.rs
+++ b/src/lib/ExtFmt.rs
@@ -46,6 +46,7 @@ mod CT {
         ty_int(signedness);
         ty_bits;
         ty_hex(caseness);
+        ty_octal;
         // FIXME: More types
     }
 
@@ -289,6 +290,8 @@ mod CT {
             t = ty_hex(case_upper);
         } else if (_str.eq(tstr, "t")) {
             t = ty_bits;
+        } else if (_str.eq(tstr, "o")) {
+            t = ty_octal;
         } else {
             log_err "unknown type in conversion";
             fail;
@@ -326,6 +329,7 @@ mod RT {
         ty_bits;
         ty_hex_upper;
         ty_hex_lower;
+        ty_octal;
     }
 
     // FIXME: May not want to use a vector here for flags;
@@ -365,6 +369,9 @@ mod RT {
             case (ty_bits) {
                 res = uint_to_str_prec(u, 2u, prec);
             }
+            case (ty_octal) {
+                res = uint_to_str_prec(u, 8u, prec);
+            }
         }
         ret pad(cv, res, pad_unsigned);
     }
diff --git a/src/test/run-pass/syntax-extension-fmt.rs b/src/test/run-pass/syntax-extension-fmt.rs
index b69533702fe..46b529d5847 100644
--- a/src/test/run-pass/syntax-extension-fmt.rs
+++ b/src/test/run-pass/syntax-extension-fmt.rs
@@ -24,6 +24,7 @@ fn main() {
   test(#fmt("%c", 'A'), "A");
   test(#fmt("%x", 0xff_u), "ff");
   test(#fmt("%X", 0x12ab_u), "12AB");
+  test(#fmt("%o", 10u), "12");
   test(#fmt("%t", 0b11010101_u), "11010101");
 
   // 32-bit limits
@@ -31,6 +32,7 @@ fn main() {
   test(#fmt("%i", 2147483647), "2147483647");
   test(#fmt("%u", 4294967295u), "4294967295");
   test(#fmt("%x", 0xffffffff_u), "ffffffff");
+  test(#fmt("%o", 0xffffffff_u), "37777777777");
   test(#fmt("%t", 0xffffffff_u), "11111111111111111111111111111111");
 
   // Widths
@@ -42,6 +44,7 @@ fn main() {
   test(#fmt("%10b", true), "      true");
   test(#fmt("%10x", 0xff_u), "        ff");
   test(#fmt("%10X", 0xff_u), "        FF");
+  test(#fmt("%10o", 10u), "        12");
   test(#fmt("%10t", 0xff_u), "  11111111");
   test(#fmt("%10c", 'A'), "         A");
 
@@ -53,6 +56,7 @@ fn main() {
   test(#fmt("%-10b", true), "true      ");
   test(#fmt("%-10x", 0xff_u), "ff        ");
   test(#fmt("%-10X", 0xff_u), "FF        ");
+  test(#fmt("%-10o", 10u), "12        ");
   test(#fmt("%-10t", 0xff_u), "11111111  ");
   test(#fmt("%-10c", 'A'), "A         ");
 
@@ -66,6 +70,7 @@ fn main() {
   test(#fmt("%.u", 10u), "10");
   test(#fmt("%.s", "test"), "");
   test(#fmt("%.x", 127u), "7f");
+  test(#fmt("%.o", 10u), "12");
   test(#fmt("%.t", 3u), "11");
   test(#fmt("%.c", 'A'), "A");
 
@@ -78,6 +83,7 @@ fn main() {
   test(#fmt("%.0u", 10u), "10");
   test(#fmt("%.0s", "test"), "");
   test(#fmt("%.0x", 127u), "7f");
+  test(#fmt("%.0o", 10u), "12");
   test(#fmt("%.0t", 3u), "11");
   test(#fmt("%.0c", 'A'), "A");
 
@@ -90,6 +96,7 @@ fn main() {
   test(#fmt("%.1u", 10u), "10");
   test(#fmt("%.1s", "test"), "t");
   test(#fmt("%.1x", 127u), "7f");
+  test(#fmt("%.1o", 10u), "12");
   test(#fmt("%.1t", 3u), "11");
   test(#fmt("%.1c", 'A'), "A");
 
@@ -102,6 +109,7 @@ fn main() {
   test(#fmt("%.5u", 10u), "00010");
   test(#fmt("%.5s", "test"), "test");
   test(#fmt("%.5x", 127u), "0007f");
+  test(#fmt("%.5o", 10u), "00012");
   test(#fmt("%.5t", 3u), "00011");
   test(#fmt("%.5c", 'A'), "A");
 
@@ -134,6 +142,7 @@ fn main() {
   test(#fmt("%05u", 1u), "00001");
   test(#fmt("%05x", 127u), "0007f");
   test(#fmt("%05X", 127u), "0007F");
+  test(#fmt("%05o", 10u), "00012");
   test(#fmt("%05t", 3u), "00011");
   // 0-padding a string is undefined but glibc does this:
   test(#fmt("%05s", "test"), " test");
@@ -147,6 +156,7 @@ fn main() {
   test(#fmt("%-05u", 1u), "1    ");
   test(#fmt("%-05x", 127u), "7f   ");
   test(#fmt("%-05X", 127u), "7F   ");
+  test(#fmt("%-05o", 10u), "12   ");
   test(#fmt("%-05t", 3u), "11   ");
   test(#fmt("%-05s", "test"), "test ");
   test(#fmt("%-05c", 'A'), "A    ");
@@ -163,6 +173,7 @@ fn main() {
   test(#fmt("%06.5c", 'A'), "     A");
   test(#fmt("%06.5x", 127u), " 0007f");
   test(#fmt("%06.5X", 127u), " 0007F");
+  test(#fmt("%06.5o", 10u), " 00012");
 
   // TODO: Padding and +
   // TODO: Padding and ' '