Bug in doubleToStr #557

This commit is contained in:
Alex Fabijanic
2014-10-07 23:40:36 -05:00
parent 5aa996aa64
commit c8563c86bf
3 changed files with 28 additions and 22 deletions

View File

@@ -263,14 +263,10 @@ public:
///
/// Returns the length of data written.
{
std::size_t len = length;
if (length == 0 || length > buffer.size())
length = buffer.size();
if (len == 0)
len = buffer.size();
else if (len > buffer.size())
len = buffer.size();
return write(buffer.begin(), len);
return write(buffer.begin(), length);
}
std::size_t size() const

View File

@@ -86,20 +86,16 @@ void insertThousandSep(std::string& str, char thSep, char decSep = '.')
/// Used only internally.
{
poco_assert (decSep != thSep);
if (str.size() == 0) return;
std::string::size_type exPos = str.find('e');
if (exPos == std::string::npos) exPos = str.find('E');
std::string::size_type decPos = str.find(decSep);
// there's no rinsert, using forward iterator to go backwards
std::string::iterator it = str.end();
std::string::iterator begin = str.begin();
if (exPos != std::string::npos)
{
while (it != begin)
{
--it;
if ((*it == 'e') || (*it == 'E')) break;
}
}
if (exPos != std::string::npos) it -= str.size() - exPos;
if (decPos != std::string::npos)
{
while (it != begin)
@@ -109,14 +105,19 @@ void insertThousandSep(std::string& str, char thSep, char decSep = '.')
}
}
int thCount = 0;
for (; it != begin; --it)
if (it == str.end()) --it;
for (; it != begin;)
{
if (!std::isdigit(*it)) continue;
if (++thCount == 3)
{
it = str.insert(it, thSep);
thCount = 0;
}
std::string::iterator pos = it;
std::string::value_type chr = *it;
std::string::value_type prevChr = *--it;
if (!std::isdigit(chr)) continue;
if (++thCount == 3 && std::isdigit(prevChr))
it = str.insert(pos, thSep);
if (thCount == 3) thCount = 0;
}
}

View File

@@ -594,6 +594,10 @@ void StringTest::testStringToDouble()
assert(strToDouble(format("12%c3456789012345678901234567890", ds), result, ds, ts));
assertEqualDelta(12.34, result, 0.01);
assert(strToDouble("1234345", result, ds, ts));
assertEqualDelta(1234345, result, 0.00000001);
assert(strToDouble(format("1%c234%c345", ts, ts), result, ds, ts));
assertEqualDelta(1234345, result, 0.00000001);
assert(strToDouble(format("1%c234%c3456789012345678901234567890", ts, ds), result, ds, ts));
assertEqualDelta(1234.3456789, result, 0.00000001);
assert(strToDouble(format("12%c345%c3456789012345678901234567890", ts, ds), result, ds, ts));
@@ -992,6 +996,11 @@ void StringTest::testFloatToString()
assert (doubleToStr(str, val, 2, 22, ',') == "-10,372,157,551,632.90");
assert (doubleToStr(str, val, 2, 22, '.', ',') == "-10.372.157.551.632,90");
assert (doubleToStr(str, val, 2, 22, ' ', ',') == "-10 372 157 551 632,90");
int ival = 1234567890;
assert(doubleToStr(str, ival, 1, 15, ',') == "1,234,567,890.0");
ival = -123456789;
assert(doubleToStr(str, ival, 1, 14, ',') == "-123,456,789.0");
}