extract locale dependency from numeric string parsing

This commit is contained in:
Aleksandar Fabijanic 2012-10-13 03:51:32 +00:00
parent e7b0913a7a
commit ada0ec20e7
5 changed files with 277 additions and 222 deletions

View File

@ -59,19 +59,19 @@ public:
static const unsigned short NUM_BASE_DEC = 10;
static const unsigned short NUM_BASE_HEX = 0x10;
static int parse(const std::string& s);
static int parse(const std::string& s, char thousandSeparator = ',');
/// 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.
static bool tryParse(const std::string& s, int& value);
static bool tryParse(const std::string& s, int& value, char thousandSeparator = ',');
/// Parses an integer value in decimal notation from the given string.
/// 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, char thousandSeparator = ',');
/// 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.
static bool tryParseUnsigned(const std::string& s, unsigned& value);
static bool tryParseUnsigned(const std::string& s, unsigned& value, char thousandSeparator = ',');
/// Parses an unsigned integer value in decimal notation from the given string.
/// Returns true if a valid integer has been found, false otherwise.
@ -95,19 +95,19 @@ public:
#if defined(POCO_HAVE_INT64)
static Int64 parse64(const std::string& s);
static Int64 parse64(const std::string& s, char thousandSeparator = ',');
/// 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.
static bool tryParse64(const std::string& s, Int64& value);
static bool tryParse64(const std::string& s, Int64& value, char thousandSeparator = ',');
/// Parses a 64-bit integer value in decimal notation from the given string.
/// 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, char thousandSeparator = ',');
/// 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.
static bool tryParseUnsigned64(const std::string& s, UInt64& value);
static bool tryParseUnsigned64(const std::string& s, UInt64& value, char thousandSeparator = ',');
/// 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.
@ -129,13 +129,13 @@ public:
#endif // defined(POCO_HAVE_INT64)
static double parseFloat(const std::string& s);
static double parseFloat(const std::string& s, char decimalSeparator = '.', char thousandSeparator = ',');
/// Parses a double value in decimal floating point notation
/// from the given string.
/// Throws a SyntaxException if the string does not hold a floating-point
/// number in decimal notation.
static bool tryParseFloat(const std::string& s, double& value);
static bool tryParseFloat(const std::string& s, double& value, char decimalSeparator = '.', char thousandSeparator = ',');
/// Parses a double value in decimal floating point notation
/// from the given string.
/// Returns true if a valid floating point number has been found,

View File

@ -81,7 +81,7 @@ inline char thousandSeparator()
template <typename I>
bool strToInt(const char* pStr, I& result, short base)
bool strToInt(const char* pStr, I& result, short base, char thSep = ',')
/// Converts zero-terminated character array to integer number;
/// Thousand separators are recognized for base10 and current locale;
/// it is silently skipped but not verified for correct positioning.
@ -89,7 +89,7 @@ bool strToInt(const char* pStr, I& result, short base)
/// the return value is false with the result value undetermined.
{
if (!pStr) return false;
while (*pStr == ' ') ++pStr;
while (isspace(*pStr)) ++pStr;
if (*pStr == '\0') return false;
I sign = 1;
if ((base == 10) && (*pStr == '-'))
@ -102,7 +102,6 @@ bool strToInt(const char* pStr, I& result, short base)
// parser states:
const char STATE_SIGNIFICANT_DIGITS = 1;
char state = 0;
const char thSep = thousandSeparator();
result = 0;
I limitCheck = std::numeric_limits<I>::max() / base;
@ -168,6 +167,11 @@ bool strToInt(const char* pStr, I& result, short base)
case ' ':
if ((base == 10) && (thSep == ' ')) break;
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
goto done;
default:
@ -183,12 +187,12 @@ done:
template <typename I>
bool strToInt(const std::string& str, I& result, short base)
bool strToInt(const std::string& str, I& result, short base, char thSep = ',')
/// Converts string to integer number;
/// This is a wrapper function, for details see see the
/// bool strToInt(const char*, I&, short&) implementation.
/// bool strToInt(const char*, I&, short, char) implementation.
{
return strToInt(str.c_str(), result, base);
return strToInt(str.c_str(), result, base, thSep);
}
@ -201,7 +205,7 @@ static char DUMMY_EXP_UNDERFLOW = 0; // dummy default val
}
template <typename F>
bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW, char decSep = '.', char thSep = ',')
/// Converts zero-terminated array to floating-point number;
/// Returns true if succesful. Exponent underflow (i.e. loss of precision)
/// is signalled in eu. Thousand separators are recognized for the locale
@ -210,7 +214,9 @@ bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
/// If parsing was unsuccesful, the return value is false with
/// result and eu values undetermined.
{
if (!pStr || (pStr && *pStr == '\0')) return false;
poco_assert (decSep != thSep);
if (pStr == 0 || *pStr == '\0') return false;
// parser states:
const char STATE_LEADING_SPACES = 0;
@ -220,7 +226,6 @@ bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
const char STATE_EXP_DIGITS = 4;
const char STATE_SUFFIX = 5; // 'f' suffix
const char decSep = decimalSeparator();
char numSign = 1, expSign = 1;
char state = STATE_LEADING_SPACES;
F mantissa = 0.0, exponent = 0.0;
@ -234,21 +239,37 @@ bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
case '.':
if (decSep == '.')
{
if (state > STATE_DIGITS_BEFORE_DEC_POINT) return false;
if (state >= STATE_DIGITS_AFTER_DEC_POINT) return false;
state = STATE_DIGITS_AFTER_DEC_POINT;
break;
}
break;
else if ((thSep == '.') && (state == STATE_DIGITS_BEFORE_DEC_POINT))
break;
else
return false;
case ',':
if (decSep == ',')
{
if (state > STATE_DIGITS_BEFORE_DEC_POINT) return false;
if (state >= STATE_DIGITS_AFTER_DEC_POINT) return false;
state = STATE_DIGITS_AFTER_DEC_POINT;
break;
}
break;
else if ((thSep == ',') && (state == STATE_DIGITS_BEFORE_DEC_POINT))
break;
else
return false;
case ' ':
if ((state > STATE_LEADING_SPACES) && (state < STATE_DIGITS_AFTER_DEC_POINT))
case ' ': // space (SPC)
if ((thSep == ' ') && (state == STATE_DIGITS_BEFORE_DEC_POINT)) break;
case '\t': // horizontal tab (TAB)
case '\n': // line feed (LF)
case '\v': // vertical tab (VT)
case '\f': // form feed (FF)
case '\r': // carriage return (CR)
if ((state >= STATE_DIGITS_AFTER_DEC_POINT) || (state >= STATE_EXP_DIGITS))
break;
else if ((state > STATE_LEADING_SPACES) && (state < STATE_DIGITS_AFTER_DEC_POINT))
return false;
break;
@ -327,12 +348,12 @@ bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
template <typename F>
bool strToFloat (const std::string& s, F& result, char& eu = DUMMY_EXP_UNDERFLOW)
bool strToFloat (const std::string& s, F& result, char& eu = DUMMY_EXP_UNDERFLOW, char decSep = '.', char thSep = ',')
/// Converts string to floating-point number;
/// This is a wrapper function, for details see see the
/// bool strToFloat(const char*, F&, char&) implementation.
/// bool strToFloat(const char*, F&, char&, char, char) implementation.
{
return strToFloat(s.c_str(), result, eu);
return strToFloat(s.c_str(), result, eu, decSep, thSep);
}

View File

@ -60,35 +60,35 @@
namespace Poco {
int NumberParser::parse(const std::string& s)
int NumberParser::parse(const std::string& s, char thSep)
{
int result;
if (tryParse(s, result))
if (tryParse(s, result, thSep))
return result;
else
throw SyntaxException("Not a valid integer", s);
}
bool NumberParser::tryParse(const std::string& s, int& value)
bool NumberParser::tryParse(const std::string& s, int& value, char thSep)
{
return strToInt(s.c_str(), value, NUM_BASE_DEC);
return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
}
unsigned NumberParser::parseUnsigned(const std::string& s)
unsigned NumberParser::parseUnsigned(const std::string& s, char thSep)
{
unsigned result;
if (tryParseUnsigned(s, result))
if (tryParseUnsigned(s, result, thSep))
return result;
else
throw SyntaxException("Not a valid unsigned integer", s);
}
bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value)
bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value, char thSep)
{
return strToInt(s.c_str(), value, NUM_BASE_DEC);
return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
}
@ -127,35 +127,35 @@ bool NumberParser::tryParseOct(const std::string& s, unsigned& value)
#if defined(POCO_HAVE_INT64)
Int64 NumberParser::parse64(const std::string& s)
Int64 NumberParser::parse64(const std::string& s, char thSep)
{
Int64 result;
if (tryParse64(s, result))
if (tryParse64(s, result, thSep))
return result;
else
throw SyntaxException("Not a valid integer", s);
}
bool NumberParser::tryParse64(const std::string& s, Int64& value)
bool NumberParser::tryParse64(const std::string& s, Int64& value, char thSep)
{
return strToInt(s.c_str(), value, NUM_BASE_DEC);
return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
}
UInt64 NumberParser::parseUnsigned64(const std::string& s)
UInt64 NumberParser::parseUnsigned64(const std::string& s, char thSep)
{
UInt64 result;
if (tryParseUnsigned64(s, result))
if (tryParseUnsigned64(s, result, thSep))
return result;
else
throw SyntaxException("Not a valid unsigned integer", s);
}
bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value)
bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value, char thSep)
{
return strToInt(s.c_str(), value, NUM_BASE_DEC);
return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
}
@ -194,19 +194,20 @@ bool NumberParser::tryParseOct64(const std::string& s, UInt64& value)
#endif // defined(POCO_HAVE_INT64)
double NumberParser::parseFloat(const std::string& s)
double NumberParser::parseFloat(const std::string& s, char decSep, char thSep)
{
double result;
if (tryParseFloat(s, result))
if (tryParseFloat(s, result, decSep, thSep))
return result;
else
throw SyntaxException("Not a valid floating-point number", s);
}
bool NumberParser::tryParseFloat(const std::string& s, double& value)
bool NumberParser::tryParseFloat(const std::string& s, double& value, char decSep, char thSep)
{
return strToFloat(s.c_str(), value);
char eu;
return strToFloat(s.c_str(), value, eu, decSep, thSep);
}

View File

@ -74,11 +74,17 @@ NumberParserTest::~NumberParserTest()
void NumberParserTest::testParse()
{
const char ts = thousandSeparator();
std::string sep(".,");
for (int i = 0; i < 2; ++i)
{
char ts = sep[i];
assert(NumberParser::parse("123") == 123);
assert(NumberParser::parse(format("123%c456", ts), ts) == 123456);
assert(NumberParser::parse(format("1%c234%c567", ts, ts), ts) == 1234567);
}
assert(NumberParser::parse("123") == 123);
assert(NumberParser::parse(format("123%c456", ts)) == 123456);
assert(NumberParser::parse(format("1%c234%c567", ts, ts)) == 1234567);
assert(NumberParser::parse("+123") == 123);
assert(NumberParser::parse("-123") == -123);
assert(NumberParser::parse("0") == 0);
@ -123,73 +129,81 @@ void NumberParserTest::testParse()
#endif
#ifndef POCO_NO_FPENVIRONMENT
const char dp = decimalSeparator();
assertEqualDelta(1.0, NumberParser::parseFloat(format("1", dp)), 0.01);
assertEqualDelta(0.0, NumberParser::parseFloat(format("0%c0", dp)), 0.01);
assertEqualDelta(0., NumberParser::parseFloat(format("0%c0", dp)), 0.01);
assertEqualDelta(.0, NumberParser::parseFloat(format("0%c0", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34f", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34", dp)), 0.01);
assertEqualDelta(-12.34, NumberParser::parseFloat(format("-12%c34", dp)), 0.01);
assertEqualDelta(.34, NumberParser::parseFloat(format("%c34", dp)), 0.01);
assertEqualDelta(-.34, NumberParser::parseFloat(format("-%c34", dp)), 0.01);
assertEqualDelta(12., NumberParser::parseFloat(format("12%c", dp)), 0.01);
assertEqualDelta(-12., NumberParser::parseFloat(format("-12%c", dp)), 0.01);
assertEqualDelta(12, NumberParser::parseFloat("12"), 0.01);
assertEqualDelta(-12, NumberParser::parseFloat("-12"), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c3456789012345678901234567890", dp)), 0.01);
assertEqualDelta(1234.3456789, NumberParser::parseFloat(format("1%c234%c3456789012345678901234567890", ts, dp)), 0.00000001);
assertEqualDelta(12345.3456789, NumberParser::parseFloat(format("12%c345%c3456789012345678901234567890", ts, dp)), 0.00000001);
assertEqualDelta(123456.3456789, NumberParser::parseFloat(format("123%c456%c3456789012345678901234567890", ts, dp)), 0.00000001);
assertEqualDelta(1234567.3456789, NumberParser::parseFloat(format("1%c234%c567%c3456789012345678901234567890", ts, ts, dp)), 0.00000001);
assertEqualDelta(12345678.3456789, NumberParser::parseFloat(format("12%c345%c678%c3456789012345678901234567890", ts, ts, dp)), 0.00000001);
assertEqualDelta(123456789.3456789, NumberParser::parseFloat(format("123%c456%c789%c3456789012345678901234567890", ts, ts, dp)), 0.00000001);
if ((std::numeric_limits<double>::max() / 10) < 1.23456e10)
fail ("test value larger than max value for this platform");
else
for (int i = 0; i < 2; ++i)
{
double d = 1.234e100;
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234e100", dp)), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234E+100", dp)), 0.01);
char ts = sep[i];
for (int j = 0; j < 2; ++j)
{
char dp = sep[j];
if (ts == dp) continue;
d = 1.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234E-100", dp)), 0.01);
assertEqualDelta(1.0, NumberParser::parseFloat(format("1", dp), dp, ts), 0.01);
assertEqualDelta(0.0, NumberParser::parseFloat(format("0%c0", dp), dp, ts), 0.01);
assertEqualDelta(0., NumberParser::parseFloat(format("0%c0", dp), dp, ts), 0.01);
assertEqualDelta(.0, NumberParser::parseFloat(format("0%c0", dp), dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34", dp), dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34f", dp), dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34", dp), dp, ts), 0.01);
assertEqualDelta(-12.34, NumberParser::parseFloat(format("-12%c34", dp), dp, ts), 0.01);
assertEqualDelta(.34, NumberParser::parseFloat(format("%c34", dp), dp, ts), 0.01);
assertEqualDelta(-.34, NumberParser::parseFloat(format("-%c34", dp), dp, ts), 0.01);
assertEqualDelta(12., NumberParser::parseFloat(format("12%c", dp), dp, ts), 0.01);
assertEqualDelta(-12., NumberParser::parseFloat(format("-12%c", dp), dp, ts), 0.01);
assertEqualDelta(12, NumberParser::parseFloat("12", dp, ts), 0.01);
assertEqualDelta(-12, NumberParser::parseFloat("-12", dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c3456789012345678901234567890", dp), dp, ts), 0.01);
d = -1.234e100;
assertEqualDelta(d, NumberParser::parseFloat(format("-1%c234e+100", dp)), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format("-1%c234E100", dp)), 0.01);
assertEqualDelta(1234.3456789, NumberParser::parseFloat(format("1%c234%c3456789012345678901234567890", ts, dp), dp, ts), 0.00000001);
assertEqualDelta(12345.3456789, NumberParser::parseFloat(format("12%c345%c3456789012345678901234567890", ts, dp), dp, ts), 0.00000001);
assertEqualDelta(123456.3456789, NumberParser::parseFloat(format("123%c456%c3456789012345678901234567890", ts, dp), dp, ts), 0.00000001);
assertEqualDelta(1234567.3456789, NumberParser::parseFloat(format("1%c234%c567%c3456789012345678901234567890", ts, ts, dp), dp, ts), 0.00000001);
assertEqualDelta(12345678.3456789, NumberParser::parseFloat(format("12%c345%c678%c3456789012345678901234567890", ts, ts, dp), dp, ts), 0.00000001);
assertEqualDelta(123456789.3456789, NumberParser::parseFloat(format("123%c456%c789%c3456789012345678901234567890", ts, ts, dp), dp, ts), 0.00000001);
d = 1.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp)), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp)), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp)), 0.01);
if ((std::numeric_limits<double>::max() / 10) < 1.23456e10)
fail ("test value larger than max value for this platform");
else
{
double d = 1.234e100;
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234e100", dp), dp, ts), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234E+100", dp), dp, ts), 0.01);
d = 1234.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234%c234e-100 ", ts, dp)), 0.01);
d = 12345.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 12%c345%c234e-100 ", ts, dp)), 0.01);
d = 123456.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 123%c456%c234e-100 ", ts, dp)), 0.01);
d = 1.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format("1%c234E-100", dp), dp, ts), 0.01);
d = -1234.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -1%c234%c234e-100 ", ts, dp)), 0.01);
d = -12345.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -12%c345%c234e-100 ", ts, dp)), 0.01);
d = -123456.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -123%c456%c234e-100 ", ts, dp)), 0.01);
d = -1.234e100;
assertEqualDelta(d, NumberParser::parseFloat(format("-1%c234e+100", dp), dp, ts), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format("-1%c234E100", dp), dp, ts), 0.01);
d = 1.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp), dp, ts), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp), dp, ts), 0.01);
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234e-100 ", dp), dp, ts), 0.01);
d = 1234.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 1%c234%c234e-100 ", ts, dp), dp, ts), 0.01);
d = 12345.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 12%c345%c234e-100 ", ts, dp), dp, ts), 0.01);
d = 123456.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" 123%c456%c234e-100 ", ts, dp), dp, ts), 0.01);
d = -1234.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -1%c234%c234e-100 ", ts, dp), dp, ts), 0.01);
d = -12345.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -12%c345%c234e-100 ", ts, dp), dp, ts), 0.01);
d = -123456.234e-100;
assertEqualDelta(d, NumberParser::parseFloat(format(" -123%c456%c234e-100 ", ts, dp), dp, ts), 0.01);
}
double d = 12.34e-10;
assertEqualDelta(d, NumberParser::parseFloat(format("12%c34e-10", dp), dp, ts), 0.01);
assertEqualDelta(-12.34, NumberParser::parseFloat(format("-12%c34", dp), dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format(" 12%c34", dp),dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34 ", dp), dp, ts), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format(" 12%c34 ", dp), dp, ts), 0.01);
}
}
double d = 12.34e-10;
assertEqualDelta(d, NumberParser::parseFloat(format("12%c34e-10", dp)), 0.01);
assertEqualDelta(-12.34, NumberParser::parseFloat(format("-12%c34", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format(" 12%c34", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format("12%c34 ", dp)), 0.01);
assertEqualDelta(12.34, NumberParser::parseFloat(format(" 12%c34 ", dp)), 0.01);
#endif // POCO_NO_FPENVIRONMENT
}

View File

@ -39,7 +39,6 @@
#include "Poco/Stopwatch.h"
#include <iostream>
#include <iomanip>
#include <cstdio>
using Poco::trimLeft;
@ -379,130 +378,148 @@ void StringTest::testStringToFloat()
{
#ifndef POCO_NO_FPENVIRONMENT
const char ds = decimalSeparator();
const char ts = thousandSeparator();
double result;
assert(strToFloat(format("1", ds), result));
assertEqualDelta(1.0, result, 0.01);
assert(strToFloat(format("0", ds), result));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("0%c0", ds), result));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("0%c0", ds), result));
assertEqualDelta(0., result, 0.01);
assert(strToFloat(format("0%c0", ds), result));
assertEqualDelta(.0, result, 0.01);
assert(strToFloat(format("12%c34", ds), result));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34f", ds), result));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34", ds), result));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("-12%c34", ds), result));
assertEqualDelta(-12.34, result, 0.01);
assert(strToFloat(format("%c34", ds), result));
assertEqualDelta(.34, result, 0.01);
assert(strToFloat(format("-%c34", ds), result));
assertEqualDelta(-.34, result, 0.01);
assert(strToFloat(format("12%c", ds), result));
assertEqualDelta(12., result, 0.01);
assert(strToFloat(format("-12%c", ds), result));
assertEqualDelta(-12., result, 0.01);
assert(strToFloat("12", result));
assertEqualDelta(12, result, 0.01);
assert(strToFloat("-12", result));
assertEqualDelta(-12, result, 0.01);
assert(strToFloat(format("12%c3456789012345678901234567890", ds), result));
assertEqualDelta(12.34, result, 0.01);
char eu;
std::string sep(".,");
assert(strToFloat(format("1%c234%c3456789012345678901234567890", ts, ds), result));
assertEqualDelta(1234.3456789, result, 0.00000001);
assert(strToFloat(format("12%c345%c3456789012345678901234567890", ts, ds), result));
assertEqualDelta(12345.3456789, result, 0.00000001);
assert(strToFloat(format("123%c456%c3456789012345678901234567890", ts, ds), result));
assertEqualDelta(123456.3456789, result, 0.00000001);
assert(strToFloat(format("1%c234%c567%c3456789012345678901234567890", ts, ts, ds), result));
assertEqualDelta(1234567.3456789, result, 0.00000001);
assert(strToFloat(format("12%c345%c678%c3456789012345678901234567890", ts, ts, ds), result));
assertEqualDelta(12345678.3456789, result, 0.00000001);
assert(strToFloat(format("123%c456%c789%c3456789012345678901234567890", ts, ts, ds), result));
assertEqualDelta(123456789.3456789, result, 0.00000001);
if ((std::numeric_limits<double>::max() / 10) < 1.23456e10)
fail ("test value larger than max value for this platform");
else
for (int i = 0; i < 2; ++i)
{
double d = 12e34;
assert(strToFloat(format("12e34", ds), result));
assertEqualDelta(d, result, 0.01e34);
char ds = sep[i];
for (int j = 0; j < 2; ++j)
{
char ts = sep[j];
if (ts == ds) continue;
d = 1.234e100;
assert(strToFloat(format("1%c234e100", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("1%c234E+100", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat("1", result, eu, ds, ts));
assertEqualDelta(1.0, result, 0.01);
assert(strToFloat(format("%c1", ds), result, eu, ds, ts));
assertEqualDelta(.1, result, 0.01);
assert(strToFloat(format("1%c", ds), result, eu, ds, ts));
assertEqualDelta(1., result, 0.01);
assert(strToFloat("0", result, eu, ds, ts));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("0%c", ds), result, eu, ds, ts));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("%c0", ds), result, eu, ds, ts));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("0%c0", ds), result, eu, ds, ts));
assertEqualDelta(0.0, result, 0.01);
assert(strToFloat(format("0%c0", ds), result, eu, ds, ts));
assertEqualDelta(0., result, 0.01);
assert(strToFloat(format("0%c0", ds), result, eu, ds, ts));
assertEqualDelta(.0, result, 0.01);
assert(strToFloat(format("12%c34", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34f", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("-12%c34", ds), result, eu, ds, ts));
assertEqualDelta(-12.34, result, 0.01);
assert(strToFloat(format("%c34", ds), result, eu, ds, ts));
assertEqualDelta(.34, result, 0.01);
assert(strToFloat(format("-%c34", ds), result, eu, ds, ts));
assertEqualDelta(-.34, result, 0.01);
assert(strToFloat(format("12%c", ds), result, eu, ds, ts));
assertEqualDelta(12., result, 0.01);
assert(strToFloat(format("-12%c", ds), result, eu, ds, ts));
assertEqualDelta(-12., result, 0.01);
assert(strToFloat("12", result, eu, ds, ts));
assertEqualDelta(12, result, 0.01);
assert(strToFloat("-12", result, eu, ds, ts));
assertEqualDelta(-12, result, 0.01);
assert(strToFloat(format("12%c3456789012345678901234567890", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
d = 1.234e-100;
assert(strToFloat(format("1%c234E-100", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("1%c234%c3456789012345678901234567890", ts, ds), result, eu, ds, ts));
assertEqualDelta(1234.3456789, result, 0.00000001);
assert(strToFloat(format("12%c345%c3456789012345678901234567890", ts, ds), result, eu, ds, ts));
assertEqualDelta(12345.3456789, result, 0.00000001);
assert(strToFloat(format("123%c456%c3456789012345678901234567890", ts, ds), result, eu, ds, ts));
assertEqualDelta(123456.3456789, result, 0.00000001);
assert(strToFloat(format("1%c234%c567%c3456789012345678901234567890", ts, ts, ds), result, eu, ds, ts));
assertEqualDelta(1234567.3456789, result, 0.00000001);
assert(strToFloat(format("12%c345%c678%c3456789012345678901234567890", ts, ts, ds), result, eu, ds, ts));
assertEqualDelta(12345678.3456789, result, 0.00000001);
assert(strToFloat(format("123%c456%c789%c3456789012345678901234567890", ts, ts, ds), result, eu, ds, ts));
assertEqualDelta(123456789.3456789, result, 0.00000001);
d = -1.234e100;
assert(strToFloat(format("-1%c234e+100", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("-1%c234E100", ds), result));
assertEqualDelta(d, result, 0.01);
if ((std::numeric_limits<double>::max() / 10) < 1.23456e10)
fail ("test value larger than max value for this platform");
else
{
double d = 12e34;
assert(strToFloat(format("12e34", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01e34);
d = 1.234e-100;
assert(strToFloat(format(" 1%c234e-100 ", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format(" 1%c234e-100 ", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format(" 1%c234e-100 ", ds), result));
assertEqualDelta(d, result, 0.01);
d = 1.234e100;
assert(strToFloat(format("1%c234e100", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("1%c234E+100", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = 1234.234e-100;
assert(strToFloat(format(" 1%c234%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
d = 12345.234e-100;
assert(strToFloat(format(" 12%c345%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
d = 123456.234e-100;
assert(strToFloat(format(" 123%c456%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
d = 1.234e-100;
assert(strToFloat(format("1%c234E-100", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = -1234.234e-100;
assert(strToFloat(format(" -1%c234%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
d = -12345.234e-100;
assert(strToFloat(format(" -12%c345%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
d = -123456.234e-100;
char ou = 0;
assert(strToFloat(format(" -123%c456%c234e-100 ", ts, ds), result));
assertEqualDelta(d, result, 0.01);
assert (ou == 0);
d = -1.234e100;
assert(strToFloat(format("-1%c234e+100", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("-1%c234E100", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = 1.234e-100;
assert(strToFloat(format(" 1%c234e-100 ", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format(" 1%c234e-100 ", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format(" 1%c234e-100 ", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = 1234.234e-100;
assert(strToFloat(format(" 1%c234%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = 12345.234e-100;
assert(strToFloat(format(" 12%c345%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = 123456.234e-100;
assert(strToFloat(format(" 123%c456%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = -1234.234e-100;
assert(strToFloat(format(" -1%c234%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = -12345.234e-100;
assert(strToFloat(format(" -12%c345%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
d = -123456.234e-100;
char ou = 0;
assert(strToFloat(format(" -123%c456%c234e-100 ", ts, ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert (ou == 0);
}
double d = 12.34e-10;
assert(strToFloat(format("12%c34e-10", ds), result, eu, ds, ts));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("-12%c34", ds), result, eu, ds, ts));
assertEqualDelta(-12.34, result, 0.01);
assert(strToFloat(format(" 12%c34", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34 ", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format(" 12%c34 ", ds), result, eu, ds, ts));
assertEqualDelta(12.34, result, 0.01);
}
}
double d = 12.34e-10;
assert(strToFloat(format("12%c34e-10", ds), result));
assertEqualDelta(d, result, 0.01);
assert(strToFloat(format("-12%c34", ds), result));
assertEqualDelta(-12.34, result, 0.01);
assert(strToFloat(format(" 12%c34", ds), result));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format("12%c34 ", ds), result));
assertEqualDelta(12.34, result, 0.01);
assert(strToFloat(format(" 12%c34 ", ds), result));
assertEqualDelta(12.34, result, 0.01);
#endif // POCO_NO_FPENVIRONMENT
}
void StringTest::testStringToFloatError()
{
#ifndef POCO_NO_FPENVIRONMENT
#if !defined(POCO_NO_FPENVIRONMENT) && !defined(POCO_NO_LOCALE)
const char ds = decimalSeparator();
const char ts = thousandSeparator();
@ -659,9 +676,11 @@ void StringTest::benchmarkStrToFloat()
std::cout << std::setw(14) << "std::strtod:\t" << std::setw(10) << std::setfill(' ') << timeStrtod << "[ms]" <<
std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStrtod) << '\t' ;
graph = (int) (timeStream / timeStrtod); for (int i = 0; i < graph; ++i) std::cout << '#';
std::cout << std::endl << std::setw(14) << "strToFloat:\t" << std::setw(10) << std::setfill(' ') << timeStrToFloat << "[ms]" <<
std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStrToFloat) << '\t' ;
graph = (int) (timeStream / timeStrToFloat); for (int i = 0; i < graph; ++i) std::cout << '#';
std::cout << std::endl << std::setw(14) << "std::sscanf:\t" << std::setw(10) << std::setfill(' ') << timeScanf << "[ms]" <<
std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeScanf) << '\t' ;
graph = (int) (timeStream / timeScanf); for (int i = 0; i < graph; ++i) std::cout << '#';