From: David Levine Date: Thu, 18 Dec 2014 02:43:37 +0000 (-0600) Subject: Fixed format engine output of negative number with 0 fill character. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/7ae9fd31204fa5873ceebf1fed5ab22588bd9bd7?hp=7a2ed982bc5ffc80747768c807828ba252bc8265 Fixed format engine output of negative number with 0 fill character. Improper output was reported by Bob Carragher. --- diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 5732af91..97a4a05a 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -93,13 +93,18 @@ cpnumber(charstring_t dest, int num, unsigned int wid, char fill, size_t max) { charstring_push_back (rev, '?'); } else if (num < 0 && wid > 0) { /* Shouldn't need the wid > 0 check, that's why the condition - at the top checks wid < max-1 when num < 0. */ - charstring_push_back (rev, '-'); + at the top checks wid < max-1 when num < 0. */ --wid; + if (fill == ' ') { + charstring_push_back (rev, '-'); + } } while (wid-- > 0 && fill != 0) { charstring_push_back (rev, fill); } + if (num < 0 && fill == '0') { + charstring_push_back (rev, '-'); + } { /* Output the string in reverse. */ diff --git a/test/format/test-functions b/test/format/test-functions index 9cfaeeeb..b757e8e0 100755 --- a/test/format/test-functions +++ b/test/format/test-functions @@ -12,18 +12,29 @@ fi . "$MH_OBJ_DIR/test/common.sh" setup_test -expected="$MH_TEST_DIR/$$.expected" -actual="$MH_TEST_DIR/$$.actual" +expected="$MH_TEST_DIR/test-functions$$.expected" +actual="$MH_TEST_DIR/test-functions$$.actual" # check sday when day of week is specified -echo 1 >"$expected" +printf '1\n' >"$expected" fmttest -raw -format '%(sday{text})' 'Fri Sep 12 20:02 2014' >"$actual" check "$expected" "$actual" # check sday when day of week is not specified -echo 0 >"$expected" +printf '0\n' >"$expected" fmttest -raw -format '%(sday{text})' 'Sep 12 20:02 2014' >"$actual" check "$expected" "$actual" +# check negative number, without padding +printf '%s\n' ' -42' >"$expected" +fmttest -raw -format '%4(minus -42)' 0 >"$actual" +check "$expected" "$actual" + +# check negative number, with padding +# Output was "0-42" with nmh 1.6 and earlier. +printf '%s\n' -042 >"$expected" +fmttest -raw -format '%04(minus -42)' 0 >"$actual" +check "$expected" "$actual" + exit $failed