mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 02:53:10 +01:00
NumberParser::parseHex[64](): allow 0x/0X prefix
This commit is contained in:
@@ -84,7 +84,9 @@ unsigned NumberParser::parseHex(const std::string& s)
|
|||||||
|
|
||||||
bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
|
bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
|
||||||
{
|
{
|
||||||
return strToInt(s.c_str(), value, NUM_BASE_HEX);
|
int offset = 0;
|
||||||
|
if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) offset = 2;
|
||||||
|
return strToInt(s.c_str() + offset, value, NUM_BASE_HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -151,7 +153,9 @@ UInt64 NumberParser::parseHex64(const std::string& s)
|
|||||||
|
|
||||||
bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
|
bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
|
||||||
{
|
{
|
||||||
return strToInt(s.c_str(), value, NUM_BASE_HEX);
|
int offset = 0;
|
||||||
|
if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) offset = 2;
|
||||||
|
return strToInt(s.c_str() + offset, value, NUM_BASE_HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ void NumberParserTest::testParse()
|
|||||||
assert(NumberParser::parse("-0123") == -123);
|
assert(NumberParser::parse("-0123") == -123);
|
||||||
assert(NumberParser::parseUnsigned("123") == 123);
|
assert(NumberParser::parseUnsigned("123") == 123);
|
||||||
assert(NumberParser::parseHex("12AB") == 0x12ab);
|
assert(NumberParser::parseHex("12AB") == 0x12ab);
|
||||||
|
assert(NumberParser::parseHex("0x12AB") == 0x12ab);
|
||||||
|
assert(NumberParser::parseHex("0X12AB") == 0x12ab);
|
||||||
assert(NumberParser::parseHex("00") == 0);
|
assert(NumberParser::parseHex("00") == 0);
|
||||||
assert(NumberParser::parseOct("123") == 0123);
|
assert(NumberParser::parseOct("123") == 0123);
|
||||||
assert(NumberParser::parseOct("0123") == 0123);
|
assert(NumberParser::parseOct("0123") == 0123);
|
||||||
|
|||||||
Reference in New Issue
Block a user