Support octal #fmt conversions

This commit is contained in:
Brian Anderson 2011-04-26 20:13:23 -04:00
parent 66b59e4b05
commit 2e12fbfc06
3 changed files with 25 additions and 0 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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 ' '