Add Printf extension %H

- This conversion has behavior similar to %g: It automatically behaves like
  %f or %e based on the number of output characters. However, unlike %g,
  this decision is also based on what will produce the smallest string
  without truncating the output. The precision field (the * in %.*f) is
  ignored. Converting a double to text with %H and then back to a double
  should be lossless.
This commit is contained in:
Randy Heit 2013-09-25 20:46:13 -05:00
parent 0fb9f98a96
commit 1948642758
1 changed files with 23 additions and 1 deletions

View File

@ -638,6 +638,12 @@ namespace StringFormat
}
goto fp_begin;
}
else if (type == 'H')
{ // %H is an extension that behaves similarly to %g, except it automatically
// selects precision based on whatever will produce the smallest string.
expchar = 'e';
goto fp_begin;
}
#if 0
// The hdtoa function provided with FreeBSD uses a hexadecimal FP constant.
// Microsoft's compiler does not support these, so I would need to hack it
@ -690,7 +696,7 @@ fp_begin:
precision = DEFPREC;
}
dblarg = va_arg(arglist, double);
obuff = dtoaresult = dtoa(dblarg, expchar ? 2 : 3, precision, &expt, &signflag, &dtoaend);
obuff = dtoaresult = dtoa(dblarg, type != 'H' ? (expchar ? 2 : 3) : 0, precision, &expt, &signflag, &dtoaend);
//fp_common:
decimal_point = localeconv()->decimal_point;
flags |= F_SIGNED;
@ -742,6 +748,22 @@ fp_begin:
}
}
}
else if (type == 'H')
{
if (expt > -(ndig + 2) && expt <= (ndig + 4))
{ // Make %H smell like %f
expchar = '\0';
precision = ndig - expt;
if (precision < 0)
{
precision = 0;
}
}
else
{// Make %H smell like %e
precision = ndig;
}
}
if (expchar)
{
expsize = exponent(expstr, expt - 1, expchar);