diff --git a/Foundation/include/Poco/NumberFormatter.h b/Foundation/include/Poco/NumberFormatter.h index 2dfd2103c..9e459bf38 100644 --- a/Foundation/include/Poco/NumberFormatter.h +++ b/Foundation/include/Poco/NumberFormatter.h @@ -18,14 +18,14 @@ // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: -// +// // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT @@ -60,6 +60,13 @@ class Foundation_API NumberFormatter /// formatting. { public: + enum BoolFormat + { + FMT_TRUE_FALSE, + FMT_YES_NO, + FMT_ON_OFF + }; + static std::string format(int value); /// Formats an integer value in decimal notation. @@ -69,7 +76,7 @@ public: /// the specified width. static std::string format0(int value, int width); - /// Formats an integer value in decimal notation, + /// Formats an integer value in decimal notation, /// right justified and zero-padded in a field /// having at least the specified width. @@ -92,8 +99,8 @@ public: /// specified width. static std::string format0(unsigned int value, int width); - /// Formats an unsigned int value in decimal notation, - /// right justified and zero-padded in a field having at + /// Formats an unsigned int value in decimal notation, + /// right justified and zero-padded in a field having at /// least the specified width. static std::string formatHex(unsigned value); @@ -109,11 +116,11 @@ public: static std::string format(long value, int width); /// Formats a long value in decimal notation, - /// right justified in a field having at least the + /// right justified in a field having at least the /// specified width. static std::string format0(long value, int width); - /// Formats a long value in decimal notation, + /// Formats a long value in decimal notation, /// right justified and zero-padded in a field /// having at least the specified width. @@ -123,7 +130,7 @@ public: static std::string formatHex(long value, int width); /// Formats an unsigned long value in hexadecimal notation, - /// right justified and zero-padded in a field having at least the + /// right justified and zero-padded in a field having at least the /// specified width. /// The value is treated as unsigned. @@ -132,11 +139,11 @@ public: static std::string format(unsigned long value, int width); /// Formats an unsigned long value in decimal notation, - /// right justified in a field having at least the specified + /// right justified in a field having at least the specified /// width. static std::string format0(unsigned long value, int width); - /// Formats an unsigned long value in decimal notation, + /// Formats an unsigned long value in decimal notation, /// right justified and zero-padded /// in a field having at least the specified width. @@ -145,7 +152,7 @@ public: static std::string formatHex(unsigned long value, int width); /// Formats an unsigned long value in hexadecimal notation, - /// right justified and zero-padded in a field having at least the + /// right justified and zero-padded in a field having at least the /// specified width. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) @@ -158,8 +165,8 @@ public: /// right justified in a field having at least the specified width. static std::string format0(Int64 value, int width); - /// Formats a 64-bit integer value in decimal notation, - /// right justified and zero-padded in a field having at least + /// Formats a 64-bit integer value in decimal notation, + /// right justified and zero-padded in a field having at least /// the specified width. static std::string formatHex(Int64 value); @@ -168,7 +175,7 @@ public: static std::string formatHex(Int64 value, int width); /// Formats a 64-bit integer value in hexadecimal notation, - /// right justified and zero-padded in a field having at least + /// right justified and zero-padded in a field having at least /// the specified width. /// The value is treated as unsigned. @@ -180,8 +187,8 @@ public: /// right justified in a field having at least the specified width. static std::string format0(UInt64 value, int width); - /// Formats an unsigned 64-bit integer value in decimal notation, - /// right justified and zero-padded in a field having at least the + /// Formats an unsigned 64-bit integer value in decimal notation, + /// right justified and zero-padded in a field having at least the /// specified width. static std::string formatHex(UInt64 value); @@ -189,7 +196,7 @@ public: static std::string formatHex(UInt64 value, int width); /// Formats a 64-bit integer value in hexadecimal notation, - /// right justified and zero-padded in a field having at least + /// right justified and zero-padded in a field having at least /// the specified width. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) @@ -216,6 +223,10 @@ public: /// sixteen (64-bit architectures) characters wide /// field in hexadecimal notation. + static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE); + /// Formats a bool value in decimal/text notation, + /// according to format parameter. + static void append(std::string& str, int value); /// Formats an integer value in decimal notation. @@ -225,7 +236,7 @@ public: /// the specified width. static void append0(std::string& str, int value, int width); - /// Formats an integer value in decimal notation, + /// Formats an integer value in decimal notation, /// right justified and zero-padded in a field /// having at least the specified width. @@ -248,8 +259,8 @@ public: /// specified width. static void append0(std::string& str, unsigned int value, int width); - /// Formats an unsigned int value in decimal notation, - /// right justified and zero-padded in a field having at + /// Formats an unsigned int value in decimal notation, + /// right justified and zero-padded in a field having at /// least the specified width. static void appendHex(std::string& str, unsigned value); @@ -265,11 +276,11 @@ public: static void append(std::string& str, long value, int width); /// Formats a long value in decimal notation, - /// right justified in a field having at least the + /// right justified in a field having at least the /// specified width. static void append0(std::string& str, long value, int width); - /// Formats a long value in decimal notation, + /// Formats a long value in decimal notation, /// right justified and zero-padded in a field /// having at least the specified width. @@ -279,7 +290,7 @@ public: static void appendHex(std::string& str, long value, int width); /// Formats an unsigned long value in hexadecimal notation, - /// right justified and zero-padded in a field having at least the + /// right justified and zero-padded in a field having at least the /// specified width. /// The value is treated as unsigned. @@ -288,11 +299,11 @@ public: static void append(std::string& str, unsigned long value, int width); /// Formats an unsigned long value in decimal notation, - /// right justified in a field having at least the specified + /// right justified in a field having at least the specified /// width. static void append0(std::string& str, unsigned long value, int width); - /// Formats an unsigned long value in decimal notation, + /// Formats an unsigned long value in decimal notation, /// right justified and zero-padded /// in a field having at least the specified width. @@ -301,7 +312,7 @@ public: static void appendHex(std::string& str, unsigned long value, int width); /// Formats an unsigned long value in hexadecimal notation, - /// right justified and zero-padded in a field having at least the + /// right justified and zero-padded in a field having at least the /// specified width. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) @@ -314,8 +325,8 @@ public: /// right justified in a field having at least the specified width. static void append0(std::string& str, Int64 value, int width); - /// Formats a 64-bit integer value in decimal notation, - /// right justified and zero-padded in a field having at least + /// Formats a 64-bit integer value in decimal notation, + /// right justified and zero-padded in a field having at least /// the specified width. static void appendHex(std::string& str, Int64 value); @@ -324,7 +335,7 @@ public: static void appendHex(std::string& str, Int64 value, int width); /// Formats a 64-bit integer value in hexadecimal notation, - /// right justified and zero-padded in a field having at least + /// right justified and zero-padded in a field having at least /// the specified width. /// The value is treated as unsigned. @@ -336,8 +347,8 @@ public: /// right justified in a field having at least the specified width. static void append0(std::string& str, UInt64 value, int width); - /// Formats an unsigned 64-bit integer value in decimal notation, - /// right justified and zero-padded in a field having at least the + /// Formats an unsigned 64-bit integer value in decimal notation, + /// right justified and zero-padded in a field having at least the /// specified width. static void appendHex(std::string& str, UInt64 value); @@ -345,7 +356,7 @@ public: static void appendHex(std::string& str, UInt64 value, int width); /// Formats a 64-bit integer value in hexadecimal notation, - /// right justified and zero-padded in a field having at least + /// right justified and zero-padded in a field having at least /// the specified width. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) diff --git a/Foundation/src/NumberFormatter.cpp b/Foundation/src/NumberFormatter.cpp index 3465f8046..ac8b66c56 100644 --- a/Foundation/src/NumberFormatter.cpp +++ b/Foundation/src/NumberFormatter.cpp @@ -16,14 +16,14 @@ // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: -// +// // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT @@ -41,7 +41,7 @@ #if defined(_MSC_VER) #define I64_FMT "I64" -#elif defined(__APPLE__) +#elif defined(__APPLE__) #define I64_FMT "q" #else #define I64_FMT "ll" @@ -51,6 +51,27 @@ namespace Poco { +std::string NumberFormatter::format(bool value, BoolFormat format) +{ + switch(format) + { + default: + case FMT_TRUE_FALSE: + if (value == true) + return "true"; + return "false"; + case FMT_YES_NO: + if (value == true) + return "yes"; + return "no"; + case FMT_ON_OFF: + if (value == true) + return "on"; + return "off"; + } +} + + void NumberFormatter::append(std::string& str, int value) { char buffer[64]; diff --git a/Foundation/testsuite/src/NumberFormatterTest.cpp b/Foundation/testsuite/src/NumberFormatterTest.cpp index 3e58c6acb..f288a0c0a 100644 --- a/Foundation/testsuite/src/NumberFormatterTest.cpp +++ b/Foundation/testsuite/src/NumberFormatterTest.cpp @@ -12,14 +12,14 @@ // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: -// +// // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT @@ -60,13 +60,13 @@ void NumberFormatterTest::testFormat() assert (NumberFormatter::format((unsigned) 123) == "123"); assert (NumberFormatter::format((unsigned) 123, 5) == " 123"); assert (NumberFormatter::format0((unsigned) 123, 5) == "00123"); - + assert (NumberFormatter::format((long) 123) == "123"); assert (NumberFormatter::format((long) -123) == "-123"); assert (NumberFormatter::format((long) -123, 5) == " -123"); assert (NumberFormatter::format((unsigned long) 123) == "123"); - assert (NumberFormatter::format((unsigned long) 123, 5) == " 123"); + assert (NumberFormatter::format((unsigned long) 123, 5) == " 123"); #if defined(POCO_HAVE_INT64) assert (NumberFormatter::format((Int64) 123) == "123"); @@ -74,7 +74,7 @@ void NumberFormatterTest::testFormat() assert (NumberFormatter::format((Int64) -123, 5) == " -123"); assert (NumberFormatter::format((UInt64) 123) == "123"); - assert (NumberFormatter::format((UInt64) 123, 5) == " 123"); + assert (NumberFormatter::format((UInt64) 123, 5) == " 123"); #endif if (sizeof(void*) == 4) @@ -85,10 +85,17 @@ void NumberFormatterTest::testFormat() { assert (NumberFormatter::format((void*) 0x12345678) == "0000000012345678"); } - + assert (NumberFormatter::format(12.25) == "12.25"); assert (NumberFormatter::format(12.25, 4) == "12.2500"); assert (NumberFormatter::format(12.25, 8, 4) == " 12.2500"); + + assert (NumberFormatter::format(true, NumberFormatter::FMT_TRUE_FALSE) == "true"); + assert (NumberFormatter::format(false, NumberFormatter::FMT_TRUE_FALSE) == "false"); + assert (NumberFormatter::format(true, NumberFormatter::FMT_YES_NO) == "yes"); + assert (NumberFormatter::format(false, NumberFormatter::FMT_YES_NO) == "no"); + assert (NumberFormatter::format(true, NumberFormatter::FMT_ON_OFF) == "on"); + assert (NumberFormatter::format(false, NumberFormatter::FMT_ON_OFF) == "off"); }