diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2020-10-09 20:50:38 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-13 19:30:17 +0000 |
commit | ff37b70553dbfd0b2aaeabb8a29c4d492507a9d3 (patch) | |
tree | 728b9266de70d79901eed31b0e39de0abc78cf24 /src/mongo/platform/decimal128.cpp | |
parent | bbe604a5b3f94f78a30a76941ea89c0ca5db14d2 (diff) | |
download | mongo-ff37b70553dbfd0b2aaeabb8a29c4d492507a9d3.tar.gz |
SERVER-50917 util/ctype.h to replace <cctype> & <ctype.h> funcs
Diffstat (limited to 'src/mongo/platform/decimal128.cpp')
-rw-r--r-- | src/mongo/platform/decimal128.cpp | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/src/mongo/platform/decimal128.cpp b/src/mongo/platform/decimal128.cpp index 3d0e06f42d5..65eaf7eaef4 100644 --- a/src/mongo/platform/decimal128.cpp +++ b/src/mongo/platform/decimal128.cpp @@ -32,7 +32,6 @@ #include "mongo/platform/basic.h" #include <algorithm> -#include <cctype> #include <cmath> #include <cstdlib> #include <iostream> @@ -51,21 +50,16 @@ #include "mongo/config.h" #include "mongo/platform/endian.h" #include "mongo/util/assert_util.h" +#include "mongo/util/ctype.h" #include "mongo/util/str.h" -namespace { +namespace mongo { -std::string toAsciiLowerCase(mongo::StringData input) { - std::string res = input.toString(); - for (char& c : res) { - c = tolower(c); - } - return res; -} +namespace { // Returns the number of characters consumed from input string. If unable to parse, // it returns 0. -size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlags) { +size_t validateInputString(StringData input, std::uint32_t* signalingFlags) { // Input must be of these forms: // * Valid decimal (standard or scientific notation): // /[-+]?\d*(.\d+)?([e][+\-]?\d+)?/ @@ -77,18 +71,18 @@ size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlag // Check for NaN and Infinity size_t start = (isSigned) ? 1 : 0; size_t charsConsumed = start; - mongo::StringData noSign = input.substr(start); + StringData noSign = input.substr(start); bool isNanOrInf = noSign == "nan" || noSign == "inf" || noSign == "infinity"; if (isNanOrInf) return start + noSign.size(); // Input starting with non digit - if (!std::isdigit(noSign[0])) { + if (!ctype::isDigit(noSign[0])) { if (noSign[0] != '.') { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } else if (noSign.size() == 1) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } } @@ -102,11 +96,11 @@ size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlag char c = noSign[i]; if (c == '.') { if (parsedDot) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } parsedDot = true; - } else if (!std::isdigit(c)) { + } else if (!ctype::isDigit(c)) { break; } else { hasCoefficient = true; @@ -119,7 +113,7 @@ size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlag if (isZero) { // Override inexact/overflow flag set by the intel library - *signalingFlags = mongo::Decimal128::SignalingFlag::kNoFlag; + *signalingFlags = Decimal128::SignalingFlag::kNoFlag; } // Input is valid if we've parsed the entire string @@ -129,21 +123,21 @@ size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlag // String with empty coefficient and non-empty exponent if (!hasCoefficient) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } // Check exponent - mongo::StringData exponent = noSign.substr(i); + StringData exponent = noSign.substr(i); if (exponent[0] != 'e' || exponent.size() < 2) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } if (exponent[1] == '-' || exponent[1] == '+') { exponent = exponent.substr(2); if (exponent.size() == 0) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; + *signalingFlags = Decimal128::SignalingFlag::kInvalid; return 0; } charsConsumed += 2; @@ -152,21 +146,13 @@ size_t validateInputString(mongo::StringData input, std::uint32_t* signalingFlag ++charsConsumed; } - for (size_t j = 0; j < exponent.size(); j++) { - char c = exponent[j]; - if (!std::isdigit(c)) { - *signalingFlags = mongo::Decimal128::SignalingFlag::kInvalid; - return 0; - } - ++charsConsumed; + if (!std::all_of(exponent.begin(), exponent.end(), [](char c) { return ctype::isDigit(c); })) { + *signalingFlags = Decimal128::SignalingFlag::kInvalid; + return 0; } + charsConsumed += exponent.size(); return charsConsumed; } -} // namespace - -namespace mongo { - -namespace { // Determine system's endian ordering in order to construct decimal 128 values directly constexpr bool kNativeLittle = (endian::Order::kNative == endian::Order::kLittle); @@ -313,7 +299,7 @@ Decimal128::Decimal128(std::string stringValue, std::uint32_t* signalingFlags, RoundingMode roundMode, size_t* charsConsumed) { - std::string lower = toAsciiLowerCase(stringValue); + std::string lower = str::toLower(stringValue); BID_UINT128 dec128; // The intel library function requires a char * while c_str() returns a const char*. // We're using const_cast here since the library function should not modify the input. |