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

File diff suppressed because it is too large Load Diff

View File

@@ -1,124 +1,139 @@
// //
// NumberParser.h // NumberParser.h
// //
// $Id: //poco/1.4/Foundation/include/Poco/NumberParser.h#1 $ // $Id: //poco/1.4/Foundation/include/Poco/NumberParser.h#1 $
// //
// Library: Foundation // Library: Foundation
// Package: Core // Package: Core
// Module: NumberParser // Module: NumberParser
// //
// Definition of the NumberParser class. // Definition of the NumberParser class.
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and // must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#ifndef Foundation_NumberParser_INCLUDED #ifndef Foundation_NumberParser_INCLUDED
#define Foundation_NumberParser_INCLUDED #define Foundation_NumberParser_INCLUDED
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
namespace Poco { namespace Poco {
class Foundation_API NumberParser class Foundation_API NumberParser
/// The NumberParser class provides static methods /// The NumberParser class provides static methods
/// for parsing numbers out of strings. /// for parsing numbers out of strings.
{ {
public: public:
static int parse(const std::string& s); static int parse(const std::string& s);
/// Parses an integer value in decimal notation from the given string. /// Parses an integer value in decimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in decimal notation. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
static bool tryParse(const std::string& s, int& value); static bool tryParse(const std::string& s, int& value);
/// Parses an integer value in decimal notation from the given string. /// Parses an integer value in decimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
static unsigned parseUnsigned(const std::string& s); static unsigned parseUnsigned(const std::string& s);
/// Parses an unsigned integer value in decimal notation from the given string. /// Parses an unsigned integer value in decimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in decimal notation. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
static bool tryParseUnsigned(const std::string& s, unsigned& value); static bool tryParseUnsigned(const std::string& s, unsigned& value);
/// Parses an unsigned integer value in decimal notation from the given string. /// Parses an unsigned integer value in decimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
static unsigned parseHex(const std::string& s); static unsigned parseHex(const std::string& s);
/// Parses an integer value in hexadecimal notation from the given string. /// Parses an integer value in hexadecimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in /// Throws a SyntaxException if the string does not hold a number in
/// hexadecimal notation. /// hexadecimal notation.
static bool tryParseHex(const std::string& s, unsigned& value); static bool tryParseHex(const std::string& s, unsigned& value);
/// Parses an unsigned integer value in hexadecimal notation from the given string. /// Parses an unsigned integer value in hexadecimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
static Int64 parse64(const std::string& s); static Int64 parse64(const std::string& s);
/// Parses a 64-bit integer value in decimal notation from the given string. /// Parses a 64-bit integer value in decimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in decimal notation. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
static bool tryParse64(const std::string& s, Int64& value); static bool tryParse64(const std::string& s, Int64& value);
/// Parses a 64-bit integer value in decimal notation from the given string. /// Parses a 64-bit integer value in decimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
static UInt64 parseUnsigned64(const std::string& s); static UInt64 parseUnsigned64(const std::string& s);
/// Parses an unsigned 64-bit integer value in decimal notation from the given string. /// Parses an unsigned 64-bit integer value in decimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in decimal notation. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
static bool tryParseUnsigned64(const std::string& s, UInt64& value); static bool tryParseUnsigned64(const std::string& s, UInt64& value);
/// Parses an unsigned 64-bit integer value in decimal notation from the given string. /// Parses an unsigned 64-bit integer value in decimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
static UInt64 parseHex64(const std::string& s); static UInt64 parseHex64(const std::string& s);
/// Parses a 64 bit-integer value in hexadecimal notation from the given string. /// Parses a 64 bit-integer value in hexadecimal notation from the given string.
/// Throws a SyntaxException if the string does not hold a number in hexadecimal notation. /// Throws a SyntaxException if the string does not hold a number in hexadecimal notation.
static bool tryParseHex64(const std::string& s, UInt64& value); static bool tryParseHex64(const std::string& s, UInt64& value);
/// Parses an unsigned 64-bit integer value in hexadecimal notation from the given string. /// Parses an unsigned 64-bit integer value in hexadecimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise. /// Returns true if a valid integer has been found, false otherwise.
#endif // defined(POCO_HAVE_INT64) #endif // defined(POCO_HAVE_INT64)
static double parseFloat(const std::string& s); static double parseFloat(const std::string& s);
/// 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.
/// Throws a SyntaxException if the string does not hold a floating-point /// Throws a SyntaxException if the string does not hold a floating-point
/// number in decimal notation. /// number in decimal notation.
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
} // namespace Poco /// from the given string.
/// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off".
/// String forms are NOT case sensitive.
#endif // Foundation_NumberParser_INCLUDED /// 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.
};
} // namespace Poco
#endif // Foundation_NumberParser_INCLUDED

View File

@@ -1,408 +1,429 @@
// //
// NumberFormatter.cpp // NumberFormatter.cpp
// //
// $Id: //poco/1.4/Foundation/src/NumberFormatter.cpp#4 $ // $Id: //poco/1.4/Foundation/src/NumberFormatter.cpp#4 $
// //
// Library: Foundation // Library: Foundation
// Package: Core // Package: Core
// Module: NumberFormatter // Module: NumberFormatter
// //
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and // must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "Poco/NumberFormatter.h" #include "Poco/NumberFormatter.h"
#include "Poco/MemoryStream.h" #include "Poco/MemoryStream.h"
#include <iomanip> #include <iomanip>
#if !defined(POCO_NO_LOCALE) #if !defined(POCO_NO_LOCALE)
#include <locale> #include <locale>
#endif #endif
#include <cstdio> #include <cstdio>
#if defined(_MSC_VER) #if 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
namespace Poco { namespace Poco {
void NumberFormatter::append(std::string& str, int value) std::string NumberFormatter::format(bool value, BoolFormat format)
{ {
char buffer[64]; switch(format)
std::sprintf(buffer, "%d", value); {
str.append(buffer); default:
} case FMT_TRUE_FALSE:
if (value == true)
return "true";
void NumberFormatter::append(std::string& str, int value, int width) return "false";
{ case FMT_YES_NO:
poco_assert (width > 0 && width < 64); if (value == true)
return "yes";
char buffer[64]; return "no";
std::sprintf(buffer, "%*d", width, value); case FMT_ON_OFF:
str.append(buffer); if (value == true)
} return "on";
return "off";
}
void NumberFormatter::append0(std::string& str, int value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, int value)
char buffer[64]; {
std::sprintf(buffer, "%0*d", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%d", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, int value)
{ void NumberFormatter::append(std::string& str, int value, int width)
char buffer[64]; {
std::sprintf(buffer, "%X", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*d", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, int value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, int value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*X", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*d", width, value);
str.append(buffer);
void NumberFormatter::append(std::string& str, unsigned value) }
{
char buffer[64];
std::sprintf(buffer, "%u", value); void NumberFormatter::appendHex(std::string& str, int value)
str.append(buffer); {
} char buffer[64];
std::sprintf(buffer, "%X", value);
str.append(buffer);
void NumberFormatter::append(std::string& str, unsigned value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::appendHex(std::string& str, int value, int width)
char buffer[64]; {
std::sprintf(buffer, "%*u", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
str.append(buffer);
void NumberFormatter::append0(std::string& str, unsigned int value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, unsigned value)
char buffer[64]; {
std::sprintf(buffer, "%0*u", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%u", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, unsigned value)
{ void NumberFormatter::append(std::string& str, unsigned value, int width)
char buffer[64]; {
std::sprintf(buffer, "%X", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*u", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, unsigned value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, unsigned int value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*X", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*u", width, value);
str.append(buffer);
void NumberFormatter::append(std::string& str, long value) }
{
char buffer[64];
std::sprintf(buffer, "%ld", value); void NumberFormatter::appendHex(std::string& str, unsigned value)
str.append(buffer); {
} char buffer[64];
std::sprintf(buffer, "%X", value);
str.append(buffer);
void NumberFormatter::append(std::string& str, long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
char buffer[64]; {
std::sprintf(buffer, "%*ld", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
str.append(buffer);
void NumberFormatter::append0(std::string& str, long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, long value)
char buffer[64]; {
std::sprintf(buffer, "%0*ld", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%ld", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, long value)
{ void NumberFormatter::append(std::string& str, long value, int width)
char buffer[64]; {
std::sprintf(buffer, "%lX", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*ld", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, long value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*lX", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*ld", width, value);
str.append(buffer);
void NumberFormatter::append(std::string& str, unsigned long value) }
{
char buffer[64];
std::sprintf(buffer, "%lu", value); void NumberFormatter::appendHex(std::string& str, long value)
str.append(buffer); {
} char buffer[64];
std::sprintf(buffer, "%lX", value);
str.append(buffer);
void NumberFormatter::append(std::string& str, unsigned long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::appendHex(std::string& str, long value, int width)
char buffer[64]; {
std::sprintf(buffer, "%*lu", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*lX", width, value);
str.append(buffer);
void NumberFormatter::append0(std::string& str, unsigned long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, unsigned long value)
char buffer[64]; {
std::sprintf(buffer, "%0*lu", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%lu", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, unsigned long value)
{ void NumberFormatter::append(std::string& str, unsigned long value, int width)
char buffer[64]; {
std::sprintf(buffer, "%lX", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*lu", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, unsigned long value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, unsigned long value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*lX", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*lu", width, value);
str.append(buffer);
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) }
void NumberFormatter::append(std::string& str, Int64 value) void NumberFormatter::appendHex(std::string& str, unsigned long value)
{ {
char buffer[64]; char buffer[64];
std::sprintf(buffer, "%"I64_FMT"d", value); std::sprintf(buffer, "%lX", value);
str.append(buffer); str.append(buffer);
} }
void NumberFormatter::append(std::string& str, Int64 value, int width) void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
{ {
poco_assert (width > 0 && width < 64); poco_assert (width > 0 && width < 64);
char buffer[64]; char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"d", width, value); std::sprintf(buffer, "%0*lX", width, value);
str.append(buffer); str.append(buffer);
} }
void NumberFormatter::append0(std::string& str, Int64 value, int width) #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, Int64 value)
char buffer[64]; {
std::sprintf(buffer, "%0*"I64_FMT"d", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%"I64_FMT"d", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, Int64 value)
{ void NumberFormatter::append(std::string& str, Int64 value, int width)
char buffer[64]; {
std::sprintf(buffer, "%"I64_FMT"X", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"d", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, Int64 value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, Int64 value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*"I64_FMT"X", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"d", width, value);
str.append(buffer);
void NumberFormatter::append(std::string& str, UInt64 value) }
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"u", value); void NumberFormatter::appendHex(std::string& str, Int64 value)
str.append(buffer); {
} char buffer[64];
std::sprintf(buffer, "%"I64_FMT"X", value);
str.append(buffer);
void NumberFormatter::append(std::string& str, UInt64 value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
char buffer[64]; {
std::sprintf(buffer, "%*"I64_FMT"u", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
str.append(buffer);
void NumberFormatter::append0(std::string& str, UInt64 value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, UInt64 value)
char buffer[64]; {
std::sprintf(buffer, "%0*"I64_FMT"u", width, value); char buffer[64];
str.append(buffer); std::sprintf(buffer, "%"I64_FMT"u", value);
} str.append(buffer);
}
void NumberFormatter::appendHex(std::string& str, UInt64 value)
{ void NumberFormatter::append(std::string& str, UInt64 value, int width)
char buffer[64]; {
std::sprintf(buffer, "%"I64_FMT"X", value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"u", width, value);
str.append(buffer);
void NumberFormatter::appendHex(std::string& str, UInt64 value, int width) }
{
poco_assert (width > 0 && width < 64);
void NumberFormatter::append0(std::string& str, UInt64 value, int width)
char buffer[64]; {
std::sprintf(buffer, "%0*"I64_FMT"X", width, value); poco_assert (width > 0 && width < 64);
str.append(buffer);
} char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"u", width, value);
str.append(buffer);
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) }
void NumberFormatter::append(std::string& str, float value) void NumberFormatter::appendHex(std::string& str, UInt64 value)
{ {
char buffer[64]; char buffer[64];
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer)); std::sprintf(buffer, "%"I64_FMT"X", value);
#if !defined(POCO_NO_LOCALE) str.append(buffer);
ostr.imbue(std::locale::classic()); }
#endif
ostr << std::setprecision(8) << value;
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten())); void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
} {
poco_assert (width > 0 && width < 64);
void NumberFormatter::append(std::string& str, double value) char buffer[64];
{ std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
char buffer[64]; str.append(buffer);
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer)); }
#if !defined(POCO_NO_LOCALE)
ostr.imbue(std::locale::classic());
#endif #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
ostr << std::setprecision(16) << value;
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
} void NumberFormatter::append(std::string& str, float value)
{
char buffer[64];
void NumberFormatter::append(std::string& str, double value, int precision) Poco::MemoryOutputStream ostr(buffer, sizeof(buffer));
{ #if !defined(POCO_NO_LOCALE)
poco_assert (precision >= 0 && precision < 32); ostr.imbue(std::locale::classic());
#endif
char buffer[64]; ostr << std::setprecision(8) << value;
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer)); str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
#if !defined(POCO_NO_LOCALE) }
ostr.imbue(std::locale::classic());
#endif
ostr << std::fixed << std::showpoint << std::setprecision(precision) << value; void NumberFormatter::append(std::string& str, double value)
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten())); {
} char buffer[64];
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer));
#if !defined(POCO_NO_LOCALE)
void NumberFormatter::append(std::string& str, double value, int width, int precision) ostr.imbue(std::locale::classic());
{ #endif
poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width); ostr << std::setprecision(16) << value;
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
char buffer[64]; }
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer));
#if !defined(POCO_NO_LOCALE)
ostr.imbue(std::locale::classic()); void NumberFormatter::append(std::string& str, double value, int precision)
#endif {
ostr << std::fixed << std::showpoint << std::setw(width) << std::setprecision(precision) << value; poco_assert (precision >= 0 && precision < 32);
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
} char buffer[64];
Poco::MemoryOutputStream ostr(buffer, sizeof(buffer));
#if !defined(POCO_NO_LOCALE)
void NumberFormatter::append(std::string& str, const void* ptr) ostr.imbue(std::locale::classic());
{ #endif
char buffer[24]; ostr << std::fixed << std::showpoint << std::setprecision(precision) << value;
#if defined(POCO_PTR_IS_64_BIT) str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
#if defined(POCO_LONG_IS_64_BIT) }
std::sprintf(buffer, "%016lX", (UIntPtr) ptr);
#else
std::sprintf(buffer, "%016"I64_FMT"X", (UIntPtr) ptr); void NumberFormatter::append(std::string& str, double value, int width, int precision)
#endif {
#else poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width);
std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
#endif char buffer[64];
str.append(buffer); Poco::MemoryOutputStream ostr(buffer, sizeof(buffer));
} #if !defined(POCO_NO_LOCALE)
ostr.imbue(std::locale::classic());
#endif
} // namespace Poco ostr << std::fixed << std::showpoint << std::setw(width) << std::setprecision(precision) << value;
str.append(buffer, static_cast<std::string::size_type>(ostr.charsWritten()));
}
void NumberFormatter::append(std::string& str, const void* ptr)
{
char buffer[24];
#if defined(POCO_PTR_IS_64_BIT)
#if defined(POCO_LONG_IS_64_BIT)
std::sprintf(buffer, "%016lX", (UIntPtr) ptr);
#else
std::sprintf(buffer, "%016"I64_FMT"X", (UIntPtr) ptr);
#endif
#else
std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
#endif
str.append(buffer);
}
} // namespace Poco

View File

@@ -1,189 +1,246 @@
// //
// NumberParser.cpp // NumberParser.cpp
// //
// $Id: //poco/1.4/Foundation/src/NumberParser.cpp#4 $ // $Id: //poco/1.4/Foundation/src/NumberParser.cpp#4 $
// //
// Library: Foundation // Library: Foundation
// Package: Core // Package: Core
// Module: NumberParser // Module: NumberParser
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and // must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "Poco/NumberParser.h" #include "Poco/NumberParser.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/MemoryStream.h" #include "Poco/MemoryStream.h"
#if !defined(POCO_NO_LOCALE) #include "Poco/String.h"
#include <locale> #if !defined(POCO_NO_LOCALE)
#endif #include <locale>
#include <cstdio> #endif
#include <cstdio>
#include <cctype>
#if defined(POCO_LONG_IS_64_BIT)
#define I64_FMT "l"
#elif defined(_MSC_VER) #if defined(POCO_LONG_IS_64_BIT)
#define I64_FMT "I64" #define I64_FMT "l"
#elif defined(__APPLE__) #elif defined(_MSC_VER)
#define I64_FMT "q" #define I64_FMT "I64"
#else #elif defined(__APPLE__)
#define I64_FMT "ll" #define I64_FMT "q"
#endif #else
#define I64_FMT "ll"
#endif
namespace Poco {
namespace Poco {
int NumberParser::parse(const std::string& s)
{
int result; int NumberParser::parse(const std::string& s)
if (tryParse(s, result)) {
return result; int result;
else if (tryParse(s, result))
throw SyntaxException("Not a valid integer", s); return result;
} else
throw SyntaxException("Not a valid integer", s);
}
bool NumberParser::tryParse(const std::string& s, int& value)
{
char temp; bool NumberParser::tryParse(const std::string& s, int& value)
return std::sscanf(s.c_str(), "%d%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%d%c", &value, &temp) == 1;
}
unsigned NumberParser::parseUnsigned(const std::string& s)
{
unsigned result; unsigned NumberParser::parseUnsigned(const std::string& s)
if (tryParseUnsigned(s, result)) {
return result; unsigned result;
else if (tryParseUnsigned(s, result))
throw SyntaxException("Not a valid unsigned integer", s); return result;
} else
throw SyntaxException("Not a valid unsigned integer", s);
}
bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value)
{
char temp; bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value)
return std::sscanf(s.c_str(), "%u%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%u%c", &value, &temp) == 1;
}
unsigned NumberParser::parseHex(const std::string& s)
{
unsigned result; unsigned NumberParser::parseHex(const std::string& s)
if (tryParseHex(s, result)) {
return result; unsigned result;
else if (tryParseHex(s, result))
throw SyntaxException("Not a valid hexadecimal integer", s); return result;
} else
throw SyntaxException("Not a valid hexadecimal integer", s);
}
bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
{
char temp; bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
return std::sscanf(s.c_str(), "%x%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%x%c", &value, &temp) == 1;
}
#if defined(POCO_HAVE_INT64)
#if defined(POCO_HAVE_INT64)
Int64 NumberParser::parse64(const std::string& s)
{
Int64 result; Int64 NumberParser::parse64(const std::string& s)
if (tryParse64(s, result)) {
return result; Int64 result;
else if (tryParse64(s, result))
throw SyntaxException("Not a valid integer", s); return result;
} else
throw SyntaxException("Not a valid integer", s);
}
bool NumberParser::tryParse64(const std::string& s, Int64& value)
{
char temp; bool NumberParser::tryParse64(const std::string& s, Int64& value)
return std::sscanf(s.c_str(), "%"I64_FMT"d%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%"I64_FMT"d%c", &value, &temp) == 1;
}
UInt64 NumberParser::parseUnsigned64(const std::string& s)
{
UInt64 result; UInt64 NumberParser::parseUnsigned64(const std::string& s)
if (tryParseUnsigned64(s, result)) {
return result; UInt64 result;
else if (tryParseUnsigned64(s, result))
throw SyntaxException("Not a valid unsigned integer", s); return result;
} else
throw SyntaxException("Not a valid unsigned integer", s);
}
bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value)
{
char temp; bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value)
return std::sscanf(s.c_str(), "%"I64_FMT"u%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%"I64_FMT"u%c", &value, &temp) == 1;
}
UInt64 NumberParser::parseHex64(const std::string& s)
{
UInt64 result; UInt64 NumberParser::parseHex64(const std::string& s)
if (tryParseHex64(s, result)) {
return result; UInt64 result;
else if (tryParseHex64(s, result))
throw SyntaxException("Not a valid hexadecimal integer", s); return result;
} else
throw SyntaxException("Not a valid hexadecimal integer", s);
}
bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
{
char temp; bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
return std::sscanf(s.c_str(), "%"I64_FMT"x%c", &value, &temp) == 1; {
} char temp;
return std::sscanf(s.c_str(), "%"I64_FMT"x%c", &value, &temp) == 1;
}
#endif // defined(POCO_HAVE_INT64)
#endif // defined(POCO_HAVE_INT64)
double NumberParser::parseFloat(const std::string& s)
{
double result; double NumberParser::parseFloat(const std::string& s)
if (tryParseFloat(s, result)) {
return result; double result;
else if (tryParseFloat(s, result))
throw SyntaxException("Not a valid floating-point number", s); return result;
} else
throw SyntaxException("Not a valid floating-point number", s);
}
bool NumberParser::tryParseFloat(const std::string& s, double& value)
{
Poco::MemoryInputStream istr(s.data(), s.size()); bool NumberParser::tryParseFloat(const std::string& s, double& value)
#if !defined(POCO_NO_LOCALE) {
istr.imbue(std::locale::classic()); Poco::MemoryInputStream istr(s.data(), s.size());
#endif #if !defined(POCO_NO_LOCALE)
istr >> value; istr.imbue(std::locale::classic());
return istr.eof() && !istr.fail(); #endif
} istr >> value;
return istr.eof() && !istr.fail();
}
} // namespace Poco
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