From 890454a30f7ac7eb7f0acbebfe526c5ff5ce18a1 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Fri, 10 Jul 2015 19:27:53 -0700 Subject: [PATCH] Support '+' format for floating-point types as well --- Sources/Plasma/CoreLib/plFormat.cpp | 17 ++++++++--------- Sources/Plasma/CoreLib/plFormat.h | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Sources/Plasma/CoreLib/plFormat.cpp b/Sources/Plasma/CoreLib/plFormat.cpp index 53312dcb..cf5ebda2 100644 --- a/Sources/Plasma/CoreLib/plFormat.cpp +++ b/Sources/Plasma/CoreLib/plFormat.cpp @@ -121,11 +121,10 @@ namespace plFormat_Private spec.fDigitClass = kDigitHexUpper; break; case '+': - spec.fDigitClass = kDigitDecAlwaysSigned; + spec.fAlwaysSigned = true; break; case 'd': - if (spec.fDigitClass != kDigitDecAlwaysSigned) - spec.fDigitClass = kDigitDec; + spec.fDigitClass = kDigitDec; break; case 'o': spec.fDigitClass = kDigitOct; @@ -265,7 +264,7 @@ static void _formatDecimal(const plFormat_Private::FormatSpec &format, if (format_size == 0) format_size = 1; - if (value < 0 || format.fDigitClass == plFormat_Private::kDigitDecAlwaysSigned) + if (value < 0 || format.fAlwaysSigned) ++format_size; hsAssert(format_size < 24, "Format length too long"); @@ -275,7 +274,7 @@ static void _formatDecimal(const plFormat_Private::FormatSpec &format, if (value < 0) buffer[0] = '-'; - else if (format.fDigitClass == plFormat_Private::kDigitDecAlwaysSigned) + else if (format.fAlwaysSigned) buffer[0] = '+'; _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); \ break; \ case plFormat_Private::kDigitDec: \ - case plFormat_Private::kDigitDecAlwaysSigned: \ case plFormat_Private::kDigitDefault: \ _formatDecimal<_stype>(format, output, value); \ break; \ @@ -369,7 +367,6 @@ static void _formatChar(const plFormat_Private::FormatSpec &format, _formatNumeric<_utype>(format, output, value, 16, true); \ break; \ case plFormat_Private::kDigitDec: \ - case plFormat_Private::kDigitDecAlwaysSigned: \ case plFormat_Private::kDigitDefault: \ _formatDecimal<_utype>(format, output, value); \ break; \ @@ -402,6 +399,10 @@ PL_FORMAT_IMPL(double) size_t end = 0; format_buffer[end++] = '%'; + + if (format.fAlwaysSigned) + format_buffer[end++] = '+'; + if (format.fPrecision >= 0) { int count = snprintf(format_buffer + end, arrsize(format_buffer) - end, ".%d", format.fPrecision); @@ -459,7 +460,6 @@ PL_FORMAT_IMPL(char) _formatNumeric(format, output, value, 16, true); break; case plFormat_Private::kDigitDec: - case plFormat_Private::kDigitDecAlwaysSigned: _formatDecimal(format, output, value); break; case plFormat_Private::kDigitChar: @@ -488,7 +488,6 @@ PL_FORMAT_IMPL(wchar_t) _formatNumeric(format, output, value, 16, true); break; case plFormat_Private::kDigitDec: - case plFormat_Private::kDigitDecAlwaysSigned: _formatDecimal(format, output, value); break; case plFormat_Private::kDigitChar: diff --git a/Sources/Plasma/CoreLib/plFormat.h b/Sources/Plasma/CoreLib/plFormat.h index ad3cb594..7f69cdfd 100644 --- a/Sources/Plasma/CoreLib/plFormat.h +++ b/Sources/Plasma/CoreLib/plFormat.h @@ -65,7 +65,7 @@ Mead, WA 99021 * `<` | Align left * `>` | Align right * `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) * `x` | Hex (lower-case) * `X` | Hex (upper-case) @@ -95,7 +95,6 @@ namespace plFormat_Private { kDigitDefault, /**< Default digit formatting */ kDigitDec, /**< Format as decimal integer */ - kDigitDecAlwaysSigned, /**< Same as `kDigitDec`, but include a '+' for positive numbers too */ kDigitHex, /**< Hex integer (assume unsigned) */ kDigitHexUpper, /**< Hex integer with upper-case digits */ kDigitOct, /**< Octal integer (assume unsigned) */ @@ -118,6 +117,7 @@ namespace plFormat_Private int fPrecision = -1; /**< Requested precision for floating-point */ char fPadChar = 0; /**< Explicit padding char (default is space) */ + bool fAlwaysSigned = false; /**< Show + for positive numbers (dec/float) */ Alignment fAlignment = kAlignDefault; /**< Requested pad alignment */ DigitClass fDigitClass = kDigitDefault; /**< Requested int formatting */ FloatClass fFloatClass = kFloatDefault; /**< Requested float formatting */