mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
util: add ParseUInt16(), use it in SplitHostPort()
This commit is contained in:
parent
6423c8175f
commit
2875a764f7
2 changed files with 21 additions and 2 deletions
|
@ -115,8 +115,8 @@ void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
|
||||||
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
|
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
|
||||||
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
|
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
|
||||||
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
|
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
|
||||||
int32_t n;
|
uint16_t n;
|
||||||
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
|
if (ParseUInt16(in.substr(colon + 1), &n)) {
|
||||||
in = in.substr(0, colon);
|
in = in.substr(0, colon);
|
||||||
portOut = n;
|
portOut = n;
|
||||||
}
|
}
|
||||||
|
@ -335,6 +335,18 @@ bool ParseUInt8(const std::string& str, uint8_t *out)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ParseUInt16(const std::string& str, uint16_t* out)
|
||||||
|
{
|
||||||
|
uint32_t u32;
|
||||||
|
if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (out != nullptr) {
|
||||||
|
*out = static_cast<uint16_t>(u32);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool ParseUInt32(const std::string& str, uint32_t *out)
|
bool ParseUInt32(const std::string& str, uint32_t *out)
|
||||||
{
|
{
|
||||||
if (!ParsePrechecks(str))
|
if (!ParsePrechecks(str))
|
||||||
|
|
|
@ -115,6 +115,13 @@ constexpr inline bool IsSpace(char c) noexcept {
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
|
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
|
||||||
|
* @returns true if the entire string could be parsed as valid integer,
|
||||||
|
* false if the entire string could not be parsed or if overflow or underflow occurred.
|
||||||
|
*/
|
||||||
|
[[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
|
* 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,
|
* @returns true if the entire string could be parsed as valid integer,
|
||||||
|
|
Loading…
Add table
Reference in a new issue