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. /// Returns the length of data written.
{ {
std::size_t len = length; if (length == 0 || length > buffer.size())
length = buffer.size();
if (len == 0) return write(buffer.begin(), length);
len = buffer.size();
else if (len > buffer.size())
len = buffer.size();
return write(buffer.begin(), len);
} }
std::size_t size() const std::size_t size() const

View File

@@ -86,20 +86,16 @@ void insertThousandSep(std::string& str, char thSep, char decSep = '.')
/// Used only internally. /// Used only internally.
{ {
poco_assert (decSep != thSep); poco_assert (decSep != thSep);
if (str.size() == 0) return;
std::string::size_type exPos = str.find('e'); 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); std::string::size_type decPos = str.find(decSep);
// there's no rinsert, using forward iterator to go backwards // there's no rinsert, using forward iterator to go backwards
std::string::iterator it = str.end(); std::string::iterator it = str.end();
std::string::iterator begin = str.begin(); std::string::iterator begin = str.begin();
if (exPos != std::string::npos) if (exPos != std::string::npos) it -= str.size() - exPos;
{
while (it != begin)
{
--it;
if ((*it == 'e') || (*it == 'E')) break;
}
}
if (decPos != std::string::npos) if (decPos != std::string::npos)
{ {
while (it != begin) while (it != begin)
@@ -109,14 +105,19 @@ void insertThousandSep(std::string& str, char thSep, char decSep = '.')
} }
} }
int thCount = 0; int thCount = 0;
for (; it != begin; --it) if (it == str.end()) --it;
for (; it != begin;)
{ {
if (!std::isdigit(*it)) continue; std::string::iterator pos = it;
if (++thCount == 3) std::string::value_type chr = *it;
{ std::string::value_type prevChr = *--it;
it = str.insert(it, thSep);
thCount = 0; 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)); assert(strToDouble(format("12%c3456789012345678901234567890", ds), result, ds, ts));
assertEqualDelta(12.34, result, 0.01); 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)); assert(strToDouble(format("1%c234%c3456789012345678901234567890", ts, ds), result, ds, ts));
assertEqualDelta(1234.3456789, result, 0.00000001); assertEqualDelta(1234.3456789, result, 0.00000001);
assert(strToDouble(format("12%c345%c3456789012345678901234567890", ts, ds), result, ds, ts)); 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"); 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");
} }