GH #80: NumberFormatter::append broken

fixed GH #80: NumberFormatter::append broken
This commit is contained in:
aleks-f 2013-02-07 21:43:40 -06:00
parent 33f502c119
commit 72b5b7acae
5 changed files with 67 additions and 3 deletions

View File

@ -4,6 +4,7 @@ Release 1.5.2 (2013-03-??)
========================== ==========================
- fixed GH #57: poco-1.5.1: Doesn't compile for Android - fixed GH #57: poco-1.5.1: Doesn't compile for Android
- added VoidEvent (by Arturo Castro) - added VoidEvent (by Arturo Castro)
- fixed GH #80: NumberFormatter::append broken
Release 1.5.1 (2013-01-11) Release 1.5.1 (2013-01-11)
========================== ==========================

View File

@ -30,6 +30,7 @@ Patrick White
Mike Naquin Mike Naquin
Roger Meier Roger Meier
Mathaus Mendel Mathaus Mendel
Arturo Castro
-- --
$Id$ $Id$

View File

@ -57,8 +57,19 @@ void pad(std::string& str, int precision, int width, char prefix = ' ', char dec
/// Alternative prefix (e.g. zero instead of space) can be supplied by caller. /// Alternative prefix (e.g. zero instead of space) can be supplied by caller.
/// Used only internally. /// Used only internally.
{ {
std::string::size_type frac = str.length() - str.find(decSep) - 1; std::string::size_type decSepPos = str.find(decSep);
if (precision && (frac < precision)) str.append(precision - frac, '0'); if (decSepPos == std::string::npos)
{
str.append(1, '.');
decSepPos = str.size() - 1;
}
std::string::size_type frac = str.length() - decSepPos - 1;
if (precision)
{
if (frac < precision) str.append(precision - frac, '0');
else if (frac > precision) str = str.substr(0, frac + precision);
}
if (width && (str.length() < width)) str.insert(str.begin(), width - str.length(), prefix); if (width && (str.length() < width)) str.insert(str.begin(), width - str.length(), prefix);
} }

View File

@ -235,6 +235,55 @@ void NumberFormatterTest::testFormatFloat()
} }
void NumberFormatterTest::testAppend()
{
std::string s;
NumberFormatter::append(s, 123);
assert (s == "123");
s.erase();
NumberFormatter::append(s, 123, 4);
assert (s == " 123");
s.erase();
NumberFormatter::append0(s, 123, 5);
assert (s == "00123");
s.erase();
NumberFormatter::appendHex(s, 0xDEAD);
assert (s == "DEAD");
s.erase();
NumberFormatter::appendHex(s, 0xDEAD, 6);
assert (s == "00DEAD");
s.erase();
NumberFormatter::append(s, 123u);
assert (s == "123");
s.erase();
NumberFormatter::append(s, 123u, 4);
assert (s == " 123");
s.erase();
NumberFormatter::append0(s, 123u, 5);
assert (s == "00123");
s.erase();
NumberFormatter::append(s, 123.4);
assert (s == "123.4");
s.erase();
NumberFormatter::append(s, 123.4567, 2);
assert (s == "123.45");
s.erase();
NumberFormatter::append(s, 123.4567, 10, 5);
assert (s == " 123.45670");
s.erase();
NumberFormatter::append(s, 123., 2);
assert (s == "123.00");
s.erase();
NumberFormatter::append(s, static_cast<double>(1234567), 2);
assert (s == "1234567.00");
s.erase();
NumberFormatter::append(s, 1234567, 10, 1);
assert (s == " 1234567.0");
}
void NumberFormatterTest::setUp() void NumberFormatterTest::setUp()
{ {
} }
@ -254,6 +303,7 @@ CppUnit::Test* NumberFormatterTest::suite()
CppUnit_addTest(pSuite, NumberFormatterTest, testFormatBool); CppUnit_addTest(pSuite, NumberFormatterTest, testFormatBool);
CppUnit_addTest(pSuite, NumberFormatterTest, testFormatHex); CppUnit_addTest(pSuite, NumberFormatterTest, testFormatHex);
CppUnit_addTest(pSuite, NumberFormatterTest, testFormatFloat); CppUnit_addTest(pSuite, NumberFormatterTest, testFormatFloat);
CppUnit_addTest(pSuite, NumberFormatterTest, testAppend);
return pSuite; return pSuite;
} }

View File

@ -51,7 +51,8 @@ public:
void testFormatBool(); void testFormatBool();
void testFormatHex(); void testFormatHex();
void testFormatFloat(); void testFormatFloat();
void testAppend();
void setUp(); void setUp();
void tearDown(); void tearDown();