diff options
author | Andre Hartmann <aha_1980@gmx.de> | 2018-06-17 20:09:39 +0200 |
---|---|---|
committer | André Hartmann <aha_1980@gmx.de> | 2018-07-07 19:58:18 +0000 |
commit | f98ee77cd3e9964b995d063345a895e537bc1157 (patch) | |
tree | 25b4f05d5bf44a6a74ca2b7092d50df44c55e1f9 /src/corelib/tools/qlocale_tools.cpp | |
parent | 85472b6b02b42ea624e1c00a5fd38c0d2889a731 (diff) |
QByteArray: toInt() and toDouble() ignore surrounding whitespaces
[ChangeLog][QtCore][QByteArray] QByteArray::toInt(),
QByteArray::toDouble() and the other number conversion functions
now ignore leading and trailing whitespaces, as their QString
counterparts already did. For consistency reasons, the same
behavior was added to qEnvironmentVariableIntValue() also.
Task-number: QTBUG-66187
Change-Id: I8b5e478ea8577b811d969286ea9e269f539c1ea4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/tools/qlocale_tools.cpp')
-rw-r--r-- | src/corelib/tools/qlocale_tools.cpp | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/corelib/tools/qlocale_tools.cpp b/src/corelib/tools/qlocale_tools.cpp index 4d969a4723..baa4da37f4 100644 --- a/src/corelib/tools/qlocale_tools.cpp +++ b/src/corelib/tools/qlocale_tools.cpp @@ -278,7 +278,7 @@ void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char * } double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, - TrailingJunkMode trailingJunkMode) + StrayCharacterMode strayCharMode) { if (*num == '\0') { ok = false; @@ -315,9 +315,13 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, double d = 0.0; #if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) - int conv_flags = (trailingJunkMode == TrailingJunkAllowed) ? - double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK : - double_conversion::StringToDoubleConverter::NO_FLAGS; + int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS; + if (strayCharMode == TrailingJunkAllowed) { + conv_flags = double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK; + } else if (strayCharMode == WhitespacesAllowed) { + conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES + | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES; + } double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_snan(), 0, 0); d = conv.StringToDouble(num, numLen, &processed); @@ -336,7 +340,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, if (qDoubleSscanf(num, QT_CLOCALE, "%lf%n", &d, &processed) < 1) processed = 0; - if ((trailingJunkMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) { + if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) { // Implementation defined nan symbol or garbage found. We don't accept it. processed = 0; ok = false; @@ -361,7 +365,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, #endif // !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) // Otherwise we would have gotten NaN or sorted it out above. - Q_ASSERT(trailingJunkMode == TrailingJunkAllowed || processed == numLen); + Q_ASSERT(strayCharMode == TrailingJunkAllowed || processed == numLen); // Check if underflow has occurred. if (isZero(d)) { |