From: Ben Pfaff Date: Sun, 29 Aug 2004 06:53:30 +0000 (+0000) Subject: (printf_integer) Fix handling of precision. Improve comments. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d95c0e6f3626d4db58d227c91ae2f1ce14062d9;p=pintos-anon (printf_integer) Fix handling of precision. Improve comments. --- diff --git a/src/lib/lib.c b/src/lib/lib.c index be4397e..32c44e2 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -322,8 +322,9 @@ printf_integer (uintmax_t value, bool negative, const char *digits, base = strlen (digits); /* Accumulate digits into buffer. - This algorithm produces digits in reverse order, so we count - backward from the end of the array. */ + This algorithm produces digits in reverse order, so later we + will output the buffer's content in reverse. This is also + the reason that later we append zeros and the sign. */ cp = buf; while (value > 0) { @@ -331,15 +332,15 @@ printf_integer (uintmax_t value, bool negative, const char *digits, value /= base; } - /* Prepend enough zeros to match precision. + /* Append enough zeros to match precision. If precision is 0, then a value of zero is rendered as a null string. Otherwise at least one digit is presented. */ if (c->precision < 0) c->precision = 1; - while (cp - buf < c->precision && cp - buf > (int) sizeof buf - 8) + while (cp - buf < c->precision && cp - buf < (int) sizeof buf - 8) *cp++ = '0'; - /* Prepend sign. */ + /* Append sign. */ if (c->flags & PLUS) *cp++ = negative ? '-' : '+'; else if (c->flags & SPACE)