mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 03:03:23 +02:00
backport bool support to the NumberParser/NumberFormatter
This commit is contained in:
@@ -57,11 +57,18 @@ class Foundation_API NumberFormatter
|
||||
/// an existing string.
|
||||
///
|
||||
/// Internally, std::sprintf() is used to do the actual
|
||||
/// formatting.
|
||||
/// formatting.
|
||||
{
|
||||
public:
|
||||
static std::string format(int value);
|
||||
/// Formats an integer value in decimal notation.
|
||||
enum BoolFormat
|
||||
{
|
||||
FMT_TRUE_FALSE,
|
||||
FMT_YES_NO,
|
||||
FMT_ON_OFF
|
||||
};
|
||||
|
||||
static std::string format(int value);
|
||||
/// Formats an integer value in decimal notation.
|
||||
|
||||
static std::string format(int value, int width);
|
||||
/// Formats an integer value in decimal notation,
|
||||
@@ -213,11 +220,15 @@ public:
|
||||
|
||||
static std::string format(const void* ptr);
|
||||
/// Formats a pointer in an eight (32-bit architectures) or
|
||||
/// sixteen (64-bit architectures) characters wide
|
||||
/// field in hexadecimal notation.
|
||||
/// sixteen (64-bit architectures) characters wide
|
||||
/// field in hexadecimal notation.
|
||||
|
||||
static void append(std::string& str, int value);
|
||||
/// Formats an integer value in decimal 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.
|
||||
|
||||
static void append(std::string& str, int value, int width);
|
||||
/// Formats an integer value in decimal notation,
|
||||
|
@@ -112,9 +112,24 @@ public:
|
||||
|
||||
static bool tryParseFloat(const std::string& s, double& value);
|
||||
/// Parses a double value in decimal floating point notation
|
||||
/// from the given string.
|
||||
/// Returns true if a valid floating point number has been found,
|
||||
/// false otherwise.
|
||||
/// 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,9 +55,30 @@
|
||||
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];
|
||||
char buffer[64];
|
||||
std::sprintf(buffer, "%d", value);
|
||||
str.append(buffer);
|
||||
}
|
||||
|
@@ -37,18 +37,20 @@
|
||||
#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)
|
||||
#define I64_FMT "l"
|
||||
#define I64_FMT "l"
|
||||
#elif defined(_MSC_VER)
|
||||
#define I64_FMT "I64"
|
||||
#define I64_FMT "I64"
|
||||
#elif defined(__APPLE__)
|
||||
#define I64_FMT "q"
|
||||
#define I64_FMT "q"
|
||||
#else
|
||||
#define I64_FMT "ll"
|
||||
#endif
|
||||
@@ -177,12 +179,67 @@ double NumberParser::parseFloat(const std::string& 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 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;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user