diff --git a/CHANGELOG b/CHANGELOG index af4aac039..63823e87f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ Release 1.5.2 (2013-03-??) ========================== - fixed GH #57: poco-1.5.1: Doesn't compile for Android - added VoidEvent (by Arturo Castro) +- fixed GH #80: NumberFormatter::append broken Release 1.5.1 (2013-01-11) ========================== diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a6436f7fd..523a3cab1 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -30,6 +30,7 @@ Patrick White Mike Naquin Roger Meier Mathaus Mendel +Arturo Castro -- $Id$ diff --git a/Foundation/src/NumericString.cpp b/Foundation/src/NumericString.cpp index f8806be5d..6609835a1 100644 --- a/Foundation/src/NumericString.cpp +++ b/Foundation/src/NumericString.cpp @@ -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. /// Used only internally. { - std::string::size_type frac = str.length() - str.find(decSep) - 1; - if (precision && (frac < precision)) str.append(precision - frac, '0'); + std::string::size_type decSepPos = str.find(decSep); + 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); } diff --git a/Foundation/testsuite/src/NumberFormatterTest.cpp b/Foundation/testsuite/src/NumberFormatterTest.cpp index 16e69b8fa..1e87673f1 100644 --- a/Foundation/testsuite/src/NumberFormatterTest.cpp +++ b/Foundation/testsuite/src/NumberFormatterTest.cpp @@ -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(1234567), 2); + assert (s == "1234567.00"); + s.erase(); + NumberFormatter::append(s, 1234567, 10, 1); + assert (s == " 1234567.0"); +} + + void NumberFormatterTest::setUp() { } @@ -254,6 +303,7 @@ CppUnit::Test* NumberFormatterTest::suite() CppUnit_addTest(pSuite, NumberFormatterTest, testFormatBool); CppUnit_addTest(pSuite, NumberFormatterTest, testFormatHex); CppUnit_addTest(pSuite, NumberFormatterTest, testFormatFloat); + CppUnit_addTest(pSuite, NumberFormatterTest, testAppend); return pSuite; } diff --git a/Foundation/testsuite/src/NumberFormatterTest.h b/Foundation/testsuite/src/NumberFormatterTest.h index eb58cd6a8..d9cabedf6 100644 --- a/Foundation/testsuite/src/NumberFormatterTest.h +++ b/Foundation/testsuite/src/NumberFormatterTest.h @@ -51,7 +51,8 @@ public: void testFormatBool(); void testFormatHex(); void testFormatFloat(); - + void testAppend(); + void setUp(); void tearDown();