diff options
author | Ori Bernstein <ori@eigenstate.org> | 2014-06-21 13:45:27 -0400 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2014-06-21 13:45:27 -0400 |
commit | 373ca257592c99f1fc300e8b2a97e5aa6a13eae2 (patch) | |
tree | 8b6b62ea655a77ebe06115ca869f53c15e4e5410 | |
parent | 13c80d5f597c5c69ed312bb881826e819bf84d30 (diff) | |
download | mc-373ca257592c99f1fc300e8b2a97e5aa6a13eae2.tar.gz |
Correct support for zero/space padded negative int
We would either print '- 123' or 000000-123'
in the past. Fixed.
-rw-r--r-- | libstd/fmt.myr | 14 | ||||
-rw-r--r-- | test/stdfmtpad.myr | 3 |
2 files changed, 14 insertions, 3 deletions
diff --git a/libstd/fmt.myr b/libstd/fmt.myr index 7f2c88b..8677573 100644 --- a/libstd/fmt.myr +++ b/libstd/fmt.myr @@ -115,13 +115,19 @@ generic intfmt = {buf : byte[:], bits : @a::(integral,numeric), base, signed, pa npad = clamp(padto - i, 0, padto) n = 0 + if isneg + npad-- + ;; + if padfill == '0' && isneg && n < buf.len + n += encode(buf[n:], '-') + ;; for j = 0; j < min(npad, buf.len); j++ if n >= buf.len break ;; n += encode(buf[n:], padfill) ;; - if isneg && n < buf.len + if padfill != '0' && isneg && n < buf.len n += encode(buf[n:], '-') ;; for j = i; j != 0; j-- @@ -162,7 +168,7 @@ const bfmtv = {buf, fmt, ap padfill = ' ' (c, fmt) = striter(fmt) /* modifiers */ - if fmt.len > 0 + while fmt.len > 0 match c | 'x': (c, fmt) = striter(fmt) @@ -176,7 +182,7 @@ const bfmtv = {buf, fmt, ap (c, fmt) = striter(fmt) padfill = '0' ;; - if isdigit(c) + if isdigit(c) && padto == 0 /* We can't get a 0 on the first iteration, since that was matched above. So, no special checks @@ -187,6 +193,8 @@ const bfmtv = {buf, fmt, ap padto = padto*10 + charval(c, 10) (c, fmt) = striter(fmt) ;; + else + break ;; ;; /* format specifiers */ diff --git a/test/stdfmtpad.myr b/test/stdfmtpad.myr index b566017..f836611 100644 --- a/test/stdfmtpad.myr +++ b/test/stdfmtpad.myr @@ -7,5 +7,8 @@ const main = { std.put("%01s\n", "a") std.put("%10i\n", 10) std.put("%010i\n", 10) + std.put("%010ui\n", -1) + std.put("%010i\n", -1) + std.put("%10i\n", -1) std.put("%3i\n", 100000) } |