backport bool support to the NumberParser/NumberFormatter

This commit is contained in:
Marian Krivos
2012-04-28 09:30:38 +00:00
parent b82cb14dfc
commit bd59670069
4 changed files with 1494 additions and 1390 deletions

View File

@@ -57,11 +57,18 @@ class Foundation_API NumberFormatter
/// an existing string. /// an existing string.
/// ///
/// Internally, std::sprintf() is used to do the actual /// Internally, std::sprintf() is used to do the actual
/// formatting. /// formatting.
{ {
public: public:
static std::string format(int value); enum BoolFormat
/// Formats an integer value in decimal notation. {
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); static std::string format(int value, int width);
/// Formats an integer value in decimal notation, /// Formats an integer value in decimal notation,
@@ -213,11 +220,15 @@ public:
static std::string format(const void* ptr); static std::string format(const void* ptr);
/// Formats a pointer in an eight (32-bit architectures) or /// Formats a pointer in an eight (32-bit architectures) or
/// sixteen (64-bit architectures) characters wide /// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation. /// field in hexadecimal notation.
static void append(std::string& str, int value); static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE);
/// Formats an integer value in decimal notation. /// 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); static void append(std::string& str, int value, int width);
/// Formats an integer value in decimal notation, /// Formats an integer value in decimal notation,

View File

@@ -112,9 +112,24 @@ public:
static bool tryParseFloat(const std::string& s, double& value); static bool tryParseFloat(const std::string& s, double& value);
/// Parses a double value in decimal floating point notation /// Parses a double value in decimal floating point notation
/// 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.
}; };

View File

@@ -55,9 +55,30 @@
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];
std::sprintf(buffer, "%d", value); std::sprintf(buffer, "%d", value);
str.append(buffer); str.append(buffer);
} }

View File

@@ -37,18 +37,20 @@
#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)
#define I64_FMT "l" #define I64_FMT "l"
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#define I64_FMT "I64" #define I64_FMT "I64"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define I64_FMT "q" #define I64_FMT "q"
#else #else
#define I64_FMT "ll" #define I64_FMT "ll"
#endif #endif
@@ -177,12 +179,67 @@ double NumberParser::parseFloat(const std::string& s)
bool NumberParser::tryParseFloat(const std::string& s, double& value) 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) #if !defined(POCO_NO_LOCALE)
istr.imbue(std::locale::classic()); istr.imbue(std::locale::classic());
#endif #endif
istr >> value; istr >> value;
return istr.eof() && !istr.fail(); 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;
} }