mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01:00
integers parsing
This commit is contained in:
parent
5d93d2bba1
commit
e5d3d23c92
@ -41,6 +41,10 @@
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include <string>
|
||||
#undef min
|
||||
#undef max
|
||||
#include <limits>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -76,6 +80,15 @@ public:
|
||||
/// Parses an unsigned integer value in hexadecimal notation from the given string.
|
||||
/// Returns true if a valid integer has been found, false otherwise.
|
||||
|
||||
static unsigned parseOct(const std::string& s);
|
||||
/// Parses an integer value in octal notation from the given string.
|
||||
/// Throws a SyntaxException if the string does not hold a number in
|
||||
/// hexadecimal notation.
|
||||
|
||||
static bool tryParseOct(const std::string& s, unsigned& value);
|
||||
/// Parses an unsigned integer value in octal notation from the given string.
|
||||
/// Returns true if a valid integer has been found, false otherwise.
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
|
||||
static Int64 parse64(const std::string& s);
|
||||
@ -102,6 +115,14 @@ public:
|
||||
/// Parses an unsigned 64-bit integer value in hexadecimal notation from the given string.
|
||||
/// Returns true if a valid integer has been found, false otherwise.
|
||||
|
||||
static UInt64 parseOct64(const std::string& s);
|
||||
/// Parses a 64 bit-integer value in octal notation from the given string.
|
||||
/// Throws a SyntaxException if the string does not hold a number in hexadecimal notation.
|
||||
|
||||
static bool tryParseOct64(const std::string& s, UInt64& value);
|
||||
/// Parses an unsigned 64-bit integer value in octal notation from the given string.
|
||||
/// Returns true if a valid integer has been found, false otherwise.
|
||||
|
||||
#endif // defined(POCO_HAVE_INT64)
|
||||
|
||||
static double parseFloat(const std::string& s);
|
||||
@ -130,6 +151,124 @@ public:
|
||||
/// String forms are NOT case sensitive.
|
||||
/// Returns true if a valid bool number has been found,
|
||||
/// false otherwise.
|
||||
|
||||
private:
|
||||
|
||||
template <typename T>
|
||||
static bool strToIntOct(const std::string &s, T& result)
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
if (std::numeric_limits<T>::is_signed) return false;
|
||||
std::string::const_iterator it = s.begin();
|
||||
std::string::const_iterator end = s.end();
|
||||
while (std::isspace(*it)) ++it;
|
||||
while (*it == '0') ++it;
|
||||
|
||||
unsigned base = 010;
|
||||
T n = 0;
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if (*it >= '0' && *it <= '7')
|
||||
{
|
||||
if (n > (std::numeric_limits<T>::max() / base))
|
||||
return false;
|
||||
n = n * base + *it - '0';
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if (it != end)
|
||||
{
|
||||
while (std::isspace(*it)) ++it;
|
||||
if (it != end) return false;
|
||||
}
|
||||
|
||||
result = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool strToIntDec(const std::string &s, T& result)
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
int sign = 1;
|
||||
std::string::const_iterator it = s.begin();
|
||||
std::string::const_iterator end = s.end();
|
||||
while (std::isspace(*it)) ++it;
|
||||
if (std::numeric_limits<T>::is_signed)
|
||||
{
|
||||
if (*it == '-')
|
||||
{
|
||||
sign = -1;
|
||||
++it;
|
||||
}
|
||||
else if (*it == '+') ++it;
|
||||
}
|
||||
|
||||
unsigned base = 10;
|
||||
T n = 0;
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if (*it >= '0' && *it <= '9')
|
||||
{
|
||||
if (n > (std::numeric_limits<T>::max() / base))
|
||||
return false;
|
||||
n = n * base + *it - '0';
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if (it != end)
|
||||
{
|
||||
while (std::isspace(*it)) ++it;
|
||||
if (it != end) return false;
|
||||
}
|
||||
|
||||
result = sign * n;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool strToIntHex(const std::string &s, T& result)
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
if (std::numeric_limits<T>::is_signed) return false;
|
||||
std::string::const_iterator it = s.begin();
|
||||
std::string::const_iterator end = s.end();
|
||||
while (std::isspace(*it)) ++it;
|
||||
|
||||
bool beginWithZero = false;
|
||||
if (*it == '0')
|
||||
{
|
||||
beginWithZero = true;
|
||||
++it;
|
||||
}
|
||||
if (beginWithZero && (*it != 'x') && (*it != 'X')) return false;
|
||||
else if ((*it == 'x') || (*it == 'X')) ++it;
|
||||
|
||||
unsigned base = 0x10;
|
||||
T n = 0;
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if ((*it >= '0' && *it <= '9') || (*it >= 'A' && *it <= 'F'))
|
||||
{
|
||||
if (n > (std::numeric_limits<T>::max() / base))
|
||||
return false;
|
||||
|
||||
if (*it >= '0' && *it <= '9')
|
||||
n = n * base + *it - '0';
|
||||
else if (*it >= 'A' && *it <= 'F')
|
||||
n = n * base + *it - 'A' + 10;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if (it != end)
|
||||
{
|
||||
while (std::isspace(*it)) ++it;
|
||||
if (it != end) return false;
|
||||
}
|
||||
|
||||
result = n;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -71,8 +71,7 @@ int NumberParser::parse(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParse(const std::string& s, int& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%d%c", &value, &temp) == 1;
|
||||
return strToIntDec(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -88,8 +87,7 @@ unsigned NumberParser::parseUnsigned(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%u%c", &value, &temp) == 1;
|
||||
return strToIntDec(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -105,8 +103,23 @@ unsigned NumberParser::parseHex(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%x%c", &value, &temp) == 1;
|
||||
return strToIntHex(s, value);
|
||||
}
|
||||
|
||||
|
||||
unsigned NumberParser::parseOct(const std::string& s)
|
||||
{
|
||||
unsigned result;
|
||||
if (tryParseOct(s, result))
|
||||
return result;
|
||||
else
|
||||
throw SyntaxException("Not a valid hexadecimal integer", s);
|
||||
}
|
||||
|
||||
|
||||
bool NumberParser::tryParseOct(const std::string& s, unsigned& value)
|
||||
{
|
||||
return strToIntOct(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -125,8 +138,7 @@ Int64 NumberParser::parse64(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParse64(const std::string& s, Int64& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%"I64_FMT"d%c", &value, &temp) == 1;
|
||||
return strToIntDec(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -142,8 +154,7 @@ UInt64 NumberParser::parseUnsigned64(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%"I64_FMT"u%c", &value, &temp) == 1;
|
||||
return strToIntDec(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -159,8 +170,23 @@ UInt64 NumberParser::parseHex64(const std::string& s)
|
||||
|
||||
bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
|
||||
{
|
||||
char temp;
|
||||
return std::sscanf(s.c_str(), "%"I64_FMT"x%c", &value, &temp) == 1;
|
||||
return strToIntHex(s, value);
|
||||
}
|
||||
|
||||
|
||||
UInt64 NumberParser::parseOct64(const std::string& s)
|
||||
{
|
||||
UInt64 result;
|
||||
if (tryParseOct64(s, result))
|
||||
return result;
|
||||
else
|
||||
throw SyntaxException("Not a valid hexadecimal integer", s);
|
||||
}
|
||||
|
||||
|
||||
bool NumberParser::tryParseOct64(const std::string& s, UInt64& value)
|
||||
{
|
||||
return strToIntOct(s, value);
|
||||
}
|
||||
|
||||
|
||||
@ -176,70 +202,70 @@ double NumberParser::parseFloat(const std::string& s)
|
||||
throw SyntaxException("Not a valid floating-point number", s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool NumberParser::tryParseFloat(const std::string& s, double& value)
|
||||
{
|
||||
Poco::MemoryInputStream istr(s.data(), s.size());
|
||||
Poco::MemoryInputStream istr(s.data(), s.size());
|
||||
#if !defined(POCO_NO_LOCALE)
|
||||
istr.imbue(std::locale::classic());
|
||||
istr.imbue(std::locale::classic());
|
||||
#endif
|
||||
istr >> value;
|
||||
return istr.eof() && !istr.fail();
|
||||
istr >> value;
|
||||
return istr.eof() && !istr.fail();
|
||||
}
|
||||
|
||||
|
||||
bool NumberParser::parseBool(const std::string& s)
|
||||
{
|
||||
bool result;
|
||||
if (tryParseBool(s, result))
|
||||
return result;
|
||||
else
|
||||
throw SyntaxException("Not a valid bool number", s);
|
||||
bool result;
|
||||
if (tryParseBool(s, result))
|
||||
return result;
|
||||
else
|
||||
throw SyntaxException("Not a valid bool number", s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool NumberParser::tryParseBool(const std::string& s, bool& value)
|
||||
{
|
||||
int n;
|
||||
if (NumberParser::tryParse(s, n))
|
||||
{
|
||||
value = (n != 0);
|
||||
return true;
|
||||
}
|
||||
int n;
|
||||
if (NumberParser::tryParse(s, n))
|
||||
{
|
||||
value = (n != 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (icompare(s, "true") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "yes") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "on") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (icompare(s, "false") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "no") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "off") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (icompare(s, "true") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "yes") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "on") == 0)
|
||||
{
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (icompare(s, "false") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "no") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
else if (icompare(s, "off") == 0)
|
||||
{
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -68,6 +68,9 @@ void NumberParserTest::testParse()
|
||||
assert(NumberParser::parse("-123") == -123);
|
||||
assert(NumberParser::parseUnsigned("123") == 123);
|
||||
assert(NumberParser::parseHex("12AB") == 0x12ab);
|
||||
assert(NumberParser::parseHex("0x12AB") == 0x12ab);
|
||||
assert(NumberParser::parseOct("123") == 0123);
|
||||
assert(NumberParser::parseOct("0123") == 0123);
|
||||
|
||||
assert(NumberParser::parseBool("0") == false);
|
||||
assert(NumberParser::parseBool("FALSE") == false);
|
||||
@ -81,6 +84,9 @@ void NumberParserTest::testParse()
|
||||
assert(NumberParser::parse64("-123") == -123);
|
||||
assert(NumberParser::parseUnsigned64("123") == 123);
|
||||
assert(NumberParser::parseHex64("12AB") == 0x12ab);
|
||||
assert(NumberParser::parseHex64("0x12AB") == 0x12ab);
|
||||
assert(NumberParser::parseOct64("123") == 0123);
|
||||
assert(NumberParser::parseOct64("0123") == 0123);
|
||||
#endif
|
||||
|
||||
assertEqualDelta(12.34, NumberParser::parseFloat("12.34"), 0.01);
|
||||
|
Loading…
x
Reference in New Issue
Block a user