mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
added methods parseBool() and tryParseBool() to Poco::NumberParser + test unit
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "Poco/NumberParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/String.h"
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
|
||||
@@ -177,4 +178,59 @@ bool NumberParser::tryParseFloat(const std::string& s, double& value)
|
||||
}
|
||||
|
||||
|
||||
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 NumberParser::tryParseBool(const std::string& s, bool& value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
Reference in New Issue
Block a user