From c7edcb3a72f3991962acce0874c2ee24a4f38cf5 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 17 Apr 2011 13:10:02 -0400 Subject: Support #fmt precision for string types --- src/lib/ExtFmt.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/lib/ExtFmt.rs') diff --git a/src/lib/ExtFmt.rs b/src/lib/ExtFmt.rs index 432e936f..af18a0d6 100644 --- a/src/lib/ExtFmt.rs +++ b/src/lib/ExtFmt.rs @@ -246,7 +246,17 @@ mod CT { } if (s.(i) == '.' as u8) { - ret parse_count(s, i + 1u, lim); + auto count = parse_count(s, i + 1u, lim); + // If there were no digits specified, i.e. the precision + // was ".", then the precision is 0 + alt (count._0) { + case (count_implied) { + ret tup(count_is(0), count._1); + } + case (_) { + ret count; + } + } } else { ret tup(count_implied, i); } @@ -318,6 +328,7 @@ mod RT { // instead just use a bool per flag type conv = rec(vec[flag] flags, count width, + count precision, ty ty); fn conv_int(&conv cv, int i) -> str { @@ -356,7 +367,19 @@ mod RT { } fn conv_str(&conv cv, str s) -> str { - ret pad(cv, s); + auto unpadded = s; + alt (cv.precision) { + case (count_implied) { + } + case (count_is(?max)) { + // For strings, precision is the maximum characters displayed + if (max as uint < _str.char_len(s)) { + // FIXME: substr works on bytes, not chars! + unpadded = _str.substr(s, 0u, max as uint); + } + } + } + ret pad(cv, unpadded); } fn pad(&conv cv, str s) -> str { -- cgit v1.2.3