diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strencodings.cpp | 12 | ||||
-rw-r--r-- | src/util/strencodings.h | 7 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index d10f92ffe6..44f9c708ba 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -318,6 +318,18 @@ bool ParseInt64(const std::string& str, int64_t *out) n <= std::numeric_limits<int64_t>::max(); } +bool ParseUInt8(const std::string& str, uint8_t *out) +{ + uint32_t u32; + if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) { + return false; + } + if (out != nullptr) { + *out = static_cast<uint8_t>(u32); + } + return true; +} + bool ParseUInt32(const std::string& str, uint32_t *out) { if (!ParsePrechecks(str)) diff --git a/src/util/strencodings.h b/src/util/strencodings.h index eaa0fa9992..8415e4bd6a 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -100,6 +100,13 @@ NODISCARD bool ParseInt32(const std::string& str, int32_t *out); NODISCARD bool ParseInt64(const std::string& str, int64_t *out); /** + * Convert decimal string to unsigned 8-bit integer with strict parse error feedback. + * @returns true if the entire string could be parsed as valid integer, + * false if not the entire string could be parsed or when overflow or underflow occurred. + */ +NODISCARD bool ParseUInt8(const std::string& str, uint8_t *out); + +/** * Convert decimal string to unsigned 32-bit integer with strict parse error feedback. * @returns true if the entire string could be parsed as valid integer, * false if not the entire string could be parsed or when overflow or underflow occurred. |