mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 11:05:03 +02:00
backport bool support to the NumberParser/NumberFormatter
This commit is contained in:
@@ -60,6 +60,13 @@ class Foundation_API NumberFormatter
|
|||||||
/// formatting.
|
/// formatting.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum BoolFormat
|
||||||
|
{
|
||||||
|
FMT_TRUE_FALSE,
|
||||||
|
FMT_YES_NO,
|
||||||
|
FMT_ON_OFF
|
||||||
|
};
|
||||||
|
|
||||||
static std::string format(int value);
|
static std::string format(int value);
|
||||||
/// Formats an integer value in decimal notation.
|
/// Formats an integer value in decimal notation.
|
||||||
|
|
||||||
@@ -216,6 +223,10 @@ public:
|
|||||||
/// sixteen (64-bit architectures) characters wide
|
/// sixteen (64-bit architectures) characters wide
|
||||||
/// field in hexadecimal notation.
|
/// 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);
|
static void append(std::string& str, int value);
|
||||||
/// Formats an integer value in decimal notation.
|
/// Formats an integer value in decimal notation.
|
||||||
|
|
||||||
|
@@ -115,6 +115,21 @@ public:
|
|||||||
/// from the given string.
|
/// from the given string.
|
||||||
/// Returns true if a valid floating point number has been found,
|
/// Returns true if a valid floating point number has been found,
|
||||||
/// false otherwise.
|
/// 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 {
|
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)
|
void NumberFormatter::append(std::string& str, int value)
|
||||||
{
|
{
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
|
@@ -37,10 +37,12 @@
|
|||||||
#include "Poco/NumberParser.h"
|
#include "Poco/NumberParser.h"
|
||||||
#include "Poco/Exception.h"
|
#include "Poco/Exception.h"
|
||||||
#include "Poco/MemoryStream.h"
|
#include "Poco/MemoryStream.h"
|
||||||
|
#include "Poco/String.h"
|
||||||
#if !defined(POCO_NO_LOCALE)
|
#if !defined(POCO_NO_LOCALE)
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#endif
|
#endif
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
|
||||||
#if defined(POCO_LONG_IS_64_BIT)
|
#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
|
} // namespace Poco
|
||||||
|
Reference in New Issue
Block a user