mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-16 18:56:52 +02:00
backport bool support to the NumberParser/NumberFormatter
This commit is contained in:
@@ -60,6 +60,13 @@ class Foundation_API NumberFormatter
|
||||
/// formatting.
|
||||
{
|
||||
public:
|
||||
enum BoolFormat
|
||||
{
|
||||
FMT_TRUE_FALSE,
|
||||
FMT_YES_NO,
|
||||
FMT_ON_OFF
|
||||
};
|
||||
|
||||
static std::string format(int value);
|
||||
/// Formats an integer value in decimal notation.
|
||||
|
||||
@@ -216,6 +223,10 @@ public:
|
||||
/// sixteen (64-bit architectures) characters wide
|
||||
/// field in hexadecimal notation.
|
||||
|
||||
static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE);
|
||||
/// Formats a bool value in decimal/text notation,
|
||||
/// according to format parameter.
|
||||
|
||||
static void append(std::string& str, int value);
|
||||
/// Formats an integer value in decimal notation.
|
||||
|
||||
|
@@ -115,6 +115,21 @@ public:
|
||||
/// from the given string.
|
||||
/// Returns true if a valid floating point number has been found,
|
||||
/// false otherwise.
|
||||
|
||||
static bool parseBool(const std::string& s);
|
||||
/// Parses a bool value in decimal or string notation
|
||||
/// from the given string.
|
||||
/// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off".
|
||||
/// String forms are NOT case sensitive.
|
||||
/// Throws a SyntaxException if the string does not hold a valid bool number
|
||||
|
||||
static bool tryParseBool(const std::string& s, bool& value);
|
||||
/// Parses a bool value in decimal or string notation
|
||||
/// from the given string.
|
||||
/// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off".
|
||||
/// String forms are NOT case sensitive.
|
||||
/// Returns true if a valid bool number has been found,
|
||||
/// false otherwise.
|
||||
};
|
||||
|
||||
|
||||
|
@@ -55,6 +55,27 @@
|
||||
namespace Poco {
|
||||
|
||||
|
||||
std::string NumberFormatter::format(bool value, BoolFormat format)
|
||||
{
|
||||
switch(format)
|
||||
{
|
||||
default:
|
||||
case FMT_TRUE_FALSE:
|
||||
if (value == true)
|
||||
return "true";
|
||||
return "false";
|
||||
case FMT_YES_NO:
|
||||
if (value == true)
|
||||
return "yes";
|
||||
return "no";
|
||||
case FMT_ON_OFF:
|
||||
if (value == true)
|
||||
return "on";
|
||||
return "off";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void NumberFormatter::append(std::string& str, int value)
|
||||
{
|
||||
char buffer[64];
|
||||
|
@@ -37,10 +37,12 @@
|
||||
#include "Poco/NumberParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/MemoryStream.h"
|
||||
#include "Poco/String.h"
|
||||
#if !defined(POCO_NO_LOCALE)
|
||||
#include <locale>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
|
||||
|
||||
#if defined(POCO_LONG_IS_64_BIT)
|
||||
@@ -186,4 +188,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