ref: a0c0a97c3cb0a12e7b0d3bfc47b89506e9eb78cb
parent: 6ae6f05f761a36086f80e78c018a77c0e8ee0fbb
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Dec 5 15:48:09 EST 2017
[lib/c] Fix number conversions with prefix When we want to prefix with 0 a number we have to worry about the prefix of the number, because if it begins with a sign or 0x then we have to append the 0 after the prefix.
--- a/lib/c/src/vfprintf.c
+++ b/lib/c/src/vfprintf.c
@@ -80,6 +80,7 @@
{
char *buf0 = buf;
int len, base = conv->base, prec = conv->prec;
+ uintmax_t oval = val;
if (prec == -1)
prec = 1;
@@ -89,14 +90,10 @@
while (buf0 - buf < prec)
*--buf = '0';
- /*
- * TODO: It cannot be done here because the combination
- * %#04x produces 00x1
- */
if (flags & ALTFORM) {
if (base == 8 && *buf != '0') {
*--buf = '0';
- } else if (base == 16 && val != 0) {
+ } else if (base == 16 && oval != 0) {
*--buf = conv->digs[16];
*--buf = '0';
}
@@ -161,7 +158,7 @@
static size_t
strout(char *s, size_t len, int width, int fill, FILE * restrict fp)
{
- int left = 0, adjust, ch;
+ int left = 0, adjust, ch, prefix;
size_t cnt = 0;
if (width < 0) {
@@ -175,6 +172,19 @@
cnt = adjust + len;
if (left)
adjust = -adjust;
+
+ if (fill == '0') {
+ if (*s == '-' || *s == '+')
+ prefix = 1;
+ else if (*s == '0' && toupper(s[1]) == 'X')
+ prefix = 2;
+ else
+ prefix = 0;
+ while (prefix--) {
+ putc(*s++, fp);
+ --len;
+ }
+ }
for ( ; adjust > 0; adjust--)
putc(fill, fp);