diff options
author | Richard Walters <rwalters@digitalstirling.com> | 2018-08-09 13:58:45 -0700 |
---|---|---|
committer | Richard Walters <rwalters@digitalstirling.com> | 2018-08-09 13:58:45 -0700 |
commit | ba59bf4fe2216dd86ee2083b28f85beda1e1b845 (patch) | |
tree | 640939b5fe542eb5bd414fc2f3ccc4f3b4bd006a | |
parent | 65669919341ab2940f00dc32ff622f00c6c2f6da (diff) |
use SystemAbstractions::ToInteger to convert strings to integers
-rw-r--r-- | src/Uri.cpp | 54 |
1 files changed, 15 insertions, 39 deletions
diff --git a/src/Uri.cpp b/src/Uri.cpp index 7ad1d68..143b06b 100644 --- a/src/Uri.cpp +++ b/src/Uri.cpp @@ -12,6 +12,7 @@ #include <algorithm> #include <functional> #include <inttypes.h> +#include <limits> #include <memory> #include <sstream> #include <string> @@ -130,44 +131,6 @@ namespace { }; /** - * This function parses the given string as an unsigned 16-bit - * integer, detecting invalid characters, overflow, etc. - * - * @param[in] numberString - * This is the string containing the number to parse. - * - * @param[out] number - * This is where to store the number parsed. - * - * @return - * An indication of whether or not the number was parsed - * successfully is returned. - */ - bool ParseUint16( - const std::string& numberString, - uint16_t& number - ) { - uint32_t numberIn32Bits = 0; - for (auto c: numberString) { - if ( - (c < '0') - || (c > '9') - ) { - return false; - } - numberIn32Bits *= 10; - numberIn32Bits += (uint16_t)(c - '0'); - if ( - (numberIn32Bits & ~((1 << 16) - 1)) != 0 - ) { - return false; - } - } - number = (uint16_t)numberIn32Bits; - return true; - } - - /** * This function checks to make sure the given string * is a valid rendering of an octet as a decimal number. * @@ -874,9 +837,22 @@ namespace Uri { if (portString.empty()) { hasPort = false; } else { - if (!ParseUint16(portString, port)) { + intmax_t portAsInt; + if ( + SystemAbstractions::ToInteger( + portString, + portAsInt + ) != SystemAbstractions::ToIntegerResult::Success + ) { + return false; + } + if ( + (portAsInt < 0) + || (portAsInt > (decltype(portAsInt))std::numeric_limits< decltype(port) >::max()) + ) { return false; } + port = (decltype(port))portAsInt; hasPort = true; } return true; |