Browse Source

Support '+' format for floating-point types as well

Michael Hansen 10 years ago
parent
commit
890454a30f
  1. 17
      Sources/Plasma/CoreLib/plFormat.cpp
  2. 4
      Sources/Plasma/CoreLib/plFormat.h

17
Sources/Plasma/CoreLib/plFormat.cpp

@ -121,11 +121,10 @@ namespace plFormat_Private
spec.fDigitClass = kDigitHexUpper; spec.fDigitClass = kDigitHexUpper;
break; break;
case '+': case '+':
spec.fDigitClass = kDigitDecAlwaysSigned; spec.fAlwaysSigned = true;
break; break;
case 'd': case 'd':
if (spec.fDigitClass != kDigitDecAlwaysSigned) spec.fDigitClass = kDigitDec;
spec.fDigitClass = kDigitDec;
break; break;
case 'o': case 'o':
spec.fDigitClass = kDigitOct; spec.fDigitClass = kDigitOct;
@ -265,7 +264,7 @@ static void _formatDecimal(const plFormat_Private::FormatSpec &format,
if (format_size == 0) if (format_size == 0)
format_size = 1; format_size = 1;
if (value < 0 || format.fDigitClass == plFormat_Private::kDigitDecAlwaysSigned) if (value < 0 || format.fAlwaysSigned)
++format_size; ++format_size;
hsAssert(format_size < 24, "Format length too long"); hsAssert(format_size < 24, "Format length too long");
@ -275,7 +274,7 @@ static void _formatDecimal(const plFormat_Private::FormatSpec &format,
if (value < 0) if (value < 0)
buffer[0] = '-'; buffer[0] = '-';
else if (format.fDigitClass == plFormat_Private::kDigitDecAlwaysSigned) else if (format.fAlwaysSigned)
buffer[0] = '+'; buffer[0] = '+';
_formatString(format, output, buffer, format_size, plFormat_Private::kAlignRight); _formatString(format, output, buffer, format_size, plFormat_Private::kAlignRight);
@ -340,7 +339,6 @@ static void _formatChar(const plFormat_Private::FormatSpec &format,
_formatNumeric<_utype>(format, output, value, 16, true); \ _formatNumeric<_utype>(format, output, value, 16, true); \
break; \ break; \
case plFormat_Private::kDigitDec: \ case plFormat_Private::kDigitDec: \
case plFormat_Private::kDigitDecAlwaysSigned: \
case plFormat_Private::kDigitDefault: \ case plFormat_Private::kDigitDefault: \
_formatDecimal<_stype>(format, output, value); \ _formatDecimal<_stype>(format, output, value); \
break; \ break; \
@ -369,7 +367,6 @@ static void _formatChar(const plFormat_Private::FormatSpec &format,
_formatNumeric<_utype>(format, output, value, 16, true); \ _formatNumeric<_utype>(format, output, value, 16, true); \
break; \ break; \
case plFormat_Private::kDigitDec: \ case plFormat_Private::kDigitDec: \
case plFormat_Private::kDigitDecAlwaysSigned: \
case plFormat_Private::kDigitDefault: \ case plFormat_Private::kDigitDefault: \
_formatDecimal<_utype>(format, output, value); \ _formatDecimal<_utype>(format, output, value); \
break; \ break; \
@ -402,6 +399,10 @@ PL_FORMAT_IMPL(double)
size_t end = 0; size_t end = 0;
format_buffer[end++] = '%'; format_buffer[end++] = '%';
if (format.fAlwaysSigned)
format_buffer[end++] = '+';
if (format.fPrecision >= 0) { if (format.fPrecision >= 0) {
int count = snprintf(format_buffer + end, arrsize(format_buffer) - end, int count = snprintf(format_buffer + end, arrsize(format_buffer) - end,
".%d", format.fPrecision); ".%d", format.fPrecision);
@ -459,7 +460,6 @@ PL_FORMAT_IMPL(char)
_formatNumeric<unsigned char>(format, output, value, 16, true); _formatNumeric<unsigned char>(format, output, value, 16, true);
break; break;
case plFormat_Private::kDigitDec: case plFormat_Private::kDigitDec:
case plFormat_Private::kDigitDecAlwaysSigned:
_formatDecimal<signed char>(format, output, value); _formatDecimal<signed char>(format, output, value);
break; break;
case plFormat_Private::kDigitChar: case plFormat_Private::kDigitChar:
@ -488,7 +488,6 @@ PL_FORMAT_IMPL(wchar_t)
_formatNumeric<uint32_t>(format, output, value, 16, true); _formatNumeric<uint32_t>(format, output, value, 16, true);
break; break;
case plFormat_Private::kDigitDec: case plFormat_Private::kDigitDec:
case plFormat_Private::kDigitDecAlwaysSigned:
_formatDecimal<uint32_t>(format, output, value); _formatDecimal<uint32_t>(format, output, value);
break; break;
case plFormat_Private::kDigitChar: case plFormat_Private::kDigitChar:

4
Sources/Plasma/CoreLib/plFormat.h

@ -65,7 +65,7 @@ Mead, WA 99021
* `<` | Align left * `<` | Align left
* `>` | Align right * `>` | Align right
* `NNN` | Pad to NNN characters (minimum - can be more) * `NNN` | Pad to NNN characters (minimum - can be more)
* `+` | Show a '+' char for positive signed values (decimal only) * `+` | Show a '+' char for positive signed values (decimal / float only)
* `_C` | Use C as the pad character (only '\001'..'\177' supported for now) * `_C` | Use C as the pad character (only '\001'..'\177' supported for now)
* `x` | Hex (lower-case) * `x` | Hex (lower-case)
* `X` | Hex (upper-case) * `X` | Hex (upper-case)
@ -95,7 +95,6 @@ namespace plFormat_Private
{ {
kDigitDefault, /**< Default digit formatting */ kDigitDefault, /**< Default digit formatting */
kDigitDec, /**< Format as decimal integer */ kDigitDec, /**< Format as decimal integer */
kDigitDecAlwaysSigned, /**< Same as `kDigitDec`, but include a '+' for positive numbers too */
kDigitHex, /**< Hex integer (assume unsigned) */ kDigitHex, /**< Hex integer (assume unsigned) */
kDigitHexUpper, /**< Hex integer with upper-case digits */ kDigitHexUpper, /**< Hex integer with upper-case digits */
kDigitOct, /**< Octal integer (assume unsigned) */ kDigitOct, /**< Octal integer (assume unsigned) */
@ -118,6 +117,7 @@ namespace plFormat_Private
int fPrecision = -1; /**< Requested precision for floating-point */ int fPrecision = -1; /**< Requested precision for floating-point */
char fPadChar = 0; /**< Explicit padding char (default is space) */ char fPadChar = 0; /**< Explicit padding char (default is space) */
bool fAlwaysSigned = false; /**< Show + for positive numbers (dec/float) */
Alignment fAlignment = kAlignDefault; /**< Requested pad alignment */ Alignment fAlignment = kAlignDefault; /**< Requested pad alignment */
DigitClass fDigitClass = kDigitDefault; /**< Requested int formatting */ DigitClass fDigitClass = kDigitDefault; /**< Requested int formatting */
FloatClass fFloatClass = kFloatDefault; /**< Requested float formatting */ FloatClass fFloatClass = kFloatDefault; /**< Requested float formatting */

Loading…
Cancel
Save