Add custom printf formatter flag to always print at least .0 on floats

This commit is contained in:
baldurk
2015-10-04 16:11:49 +02:00
parent e9ad2418b4
commit 7436e83f43
2 changed files with 39 additions and 3 deletions
@@ -370,12 +370,14 @@ struct SPVConstant
if(type->type == SPVTypeData::eFloat)
{
// @ is a custom printf flag that ensures we always print .0
// after a float, but without restricting precision or sigfigs
if(type->bitCount == 64)
return StringFormat::Fmt("%lgf", d);
return StringFormat::Fmt("%@lgf", d);
if(type->bitCount == 32)
return StringFormat::Fmt("%gf", f);
return StringFormat::Fmt("%@gf", f);
if(type->bitCount == 16)
return StringFormat::Fmt("%gf", ConvertFromHalf(u16));
return StringFormat::Fmt("%@gf", ConvertFromHalf(u16));
}
else if(type->type == SPVTypeData::eSInt)
{
+34
View File
@@ -79,6 +79,8 @@ enum FormatterFlags
PrependSpace = 0x4,
AlternateForm = 0x8,
PadZeroes = 0x10,
// non standard
AlwaysDecimal = 0x20,
};
enum LengthModifier
@@ -380,6 +382,11 @@ void PrintFloat0(bool e, bool f, FormatterParams formatter, char prepend,
if(((e || f) && formatter.Precision > 0) || (formatter.Flags & AlternateForm))
numwidth++; // .
if(!e && !f && (formatter.Flags & AlwaysDecimal))
{
numwidth += 2; // .0
}
// sign space
if(prepend) numwidth++;
@@ -417,6 +424,12 @@ void PrintFloat0(bool e, bool f, FormatterParams formatter, char prepend,
else
{
addchar(output, actualsize, end, '0');
if(!e && !f && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
if(padlen > 0 && (formatter.Flags & LeftJustify))
@@ -727,6 +740,9 @@ void PrintFloat(double argd, FormatterParams &formatter, bool e, bool f, bool g,
if(expon < ndigits-1 || !g || (formatter.Flags & AlternateForm))
numwidth++; // .
if(g && (formatter.Flags & AlwaysDecimal))
numwidth += 2; // .0
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
numwidth += padtrailing0s;
}
@@ -798,6 +814,11 @@ void PrintFloat(double argd, FormatterParams &formatter, bool e, bool f, bool g,
if(padtrailing0s > 0)
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
}
else if(g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
else if(expon > ndigits)
{
@@ -807,11 +828,23 @@ void PrintFloat(double argd, FormatterParams &formatter, bool e, bool f, bool g,
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
if(g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
else
{
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
if(ndigits-1 <= expon && g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
}
// if exponent is less than 0 it's much easier - just print the number as
@@ -1113,6 +1146,7 @@ int utf8printf(char *buf, size_t bufsize, const char *fmt, va_list args)
else if(*iter == '+') formatter.Flags |= PrependPos;
else if(*iter == ' ') formatter.Flags |= PrependSpace;
else if(*iter == '#') formatter.Flags |= AlternateForm;
else if(*iter == '@') formatter.Flags |= AlwaysDecimal;
else if(*iter == '0') formatter.Flags |= PadZeroes;
else break;