From 99a697b56abba8e4ab94fc14b5b4769bee9702f0 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 13 Apr 2011 21:36:32 -0400 Subject: Add support for upper-case hex and binary output to #fmt. Only works for uints at present. Necessitated the addition of _str.to_upper. --- src/comp/front/extfmt.rs | 3 +++ src/lib/ExtFmt.rs | 6 ++++++ src/lib/_str.rs | 18 ++++++++++++++++++ src/test/run-pass/lib-str.rs | 11 +++++++++++ src/test/run-pass/syntax-extension-fmt.rs | 2 ++ 5 files changed, 40 insertions(+) (limited to 'src') diff --git a/src/comp/front/extfmt.rs b/src/comp/front/extfmt.rs index 54174dd3..bd5fc433 100644 --- a/src/comp/front/extfmt.rs +++ b/src/comp/front/extfmt.rs @@ -246,6 +246,9 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr { case (ty_hex(_)) { ret make_conv_call(arg.span, "uint", cnv, arg); } + case (ty_bits) { + ret make_conv_call(arg.span, "uint", cnv, arg); + } case (_) { log unsupported; fail; diff --git a/src/lib/ExtFmt.rs b/src/lib/ExtFmt.rs index da32568a..e15fa462 100644 --- a/src/lib/ExtFmt.rs +++ b/src/lib/ExtFmt.rs @@ -315,6 +315,12 @@ mod RT { case (ty_hex_lower) { ret _uint.to_str(u, 16u); } + case (ty_hex_upper) { + ret _str.to_upper(_uint.to_str(u, 16u)); + } + case (ty_bits) { + ret _uint.to_str(u, 2u); + } } } diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 31d0790d..7690fe44 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -470,6 +470,24 @@ fn connect(vec[str] v, str sep) -> str { ret s; } +// FIXME: This only handles ASCII +fn to_upper(str s) -> str { + auto outstr = ""; + auto ascii_a = 'a' as u8; + auto ascii_z = 'z' as u8; + auto diff = 32u8; + for (u8 byte in s) { + auto next; + if (ascii_a <= byte && byte <= ascii_z) { + next = byte - diff; + } else { + next = byte; + } + push_byte(outstr, next); + } + ret outstr; +} + // Local Variables: // mode: rust; diff --git a/src/test/run-pass/lib-str.rs b/src/test/run-pass/lib-str.rs index 52f0f51b..e1e4abb1 100644 --- a/src/test/run-pass/lib-str.rs +++ b/src/test/run-pass/lib-str.rs @@ -89,6 +89,16 @@ fn test_connect() { t(vec("hi"), " ", "hi"); } +fn test_to_upper() { + // to_upper doesn't understand unicode yet, + // but we need to at least preserve it + auto unicode = "\u65e5\u672c"; + auto input = "abcDEF" + unicode + "xyz:.;"; + auto expected = "ABCDEF" + unicode + "XYZ:.;"; + auto actual = _str.to_upper(input); + check (_str.eq(expected, actual)); +} + fn main() { test_bytes_len(); @@ -98,4 +108,5 @@ fn main() { test_substr(); test_concat(); test_connect(); + test_to_upper(); } diff --git a/src/test/run-pass/syntax-extension-fmt.rs b/src/test/run-pass/syntax-extension-fmt.rs index 78a414b3..fcb0df18 100644 --- a/src/test/run-pass/syntax-extension-fmt.rs +++ b/src/test/run-pass/syntax-extension-fmt.rs @@ -23,4 +23,6 @@ fn main() { test(#fmt("%b", false), "false"); test(#fmt("%c", 'A'), "A"); test(#fmt("%x", 0xff_u), "ff"); + test(#fmt("%X", 0x12ab_u), "12AB"); + test(#fmt("%t", 0b11010101_u), "11010101"); } -- cgit v1.2.3