separate function for unsigned to string conversion

Separated unsigned integer conversion to eliminate warnings (and
slightly improve performance). Added Darwin-clang warning suppression
for unused functions/variables
This commit is contained in:
Alex 2012-11-26 19:01:44 -06:00 committed by aleks-f
parent b0ae034a94
commit dd2e677f8a
5 changed files with 166 additions and 77 deletions

View File

@ -441,7 +441,7 @@ inline std::string NumberFormatter::format0(int value, int width)
inline std::string NumberFormatter::formatHex(int value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix);
return result;
}
@ -449,7 +449,7 @@ inline std::string NumberFormatter::formatHex(int value, bool prefix)
inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0');
return result;
}
@ -457,7 +457,7 @@ inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
inline std::string NumberFormatter::format(unsigned value)
{
std::string result;
intToStr(value, 10, result);
uIntToStr(value, 10, result);
return result;
}
@ -465,7 +465,7 @@ inline std::string NumberFormatter::format(unsigned value)
inline std::string NumberFormatter::format(unsigned value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, ' ');
uIntToStr(value, 10, result, false, width, ' ');
return result;
}
@ -473,7 +473,7 @@ inline std::string NumberFormatter::format(unsigned value, int width)
inline std::string NumberFormatter::format0(unsigned int value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, '0');
uIntToStr(value, 10, result, false, width, '0');
return result;
}
@ -481,7 +481,7 @@ inline std::string NumberFormatter::format0(unsigned int value, int width)
inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix);
return result;
}
@ -489,7 +489,7 @@ inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0');
return result;
}
@ -521,7 +521,7 @@ inline std::string NumberFormatter::format0(long value, int width)
inline std::string NumberFormatter::formatHex(long value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix);
return result;
}
@ -529,7 +529,7 @@ inline std::string NumberFormatter::formatHex(long value, bool prefix)
inline std::string NumberFormatter::formatHex(long value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0');
return result;
}
@ -537,7 +537,7 @@ inline std::string NumberFormatter::formatHex(long value, int width, bool prefix
inline std::string NumberFormatter::format(unsigned long value)
{
std::string result;
intToStr(value, 10, result);
uIntToStr(value, 10, result);
return result;
}
@ -545,7 +545,7 @@ inline std::string NumberFormatter::format(unsigned long value)
inline std::string NumberFormatter::format(unsigned long value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, ' ');
uIntToStr(value, 10, result, false, width, ' ');
return result;
}
@ -553,7 +553,7 @@ inline std::string NumberFormatter::format(unsigned long value, int width)
inline std::string NumberFormatter::format0(unsigned long value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, '0');
uIntToStr(value, 10, result, false, width, '0');
return result;
}
@ -561,7 +561,7 @@ inline std::string NumberFormatter::format0(unsigned long value, int width)
inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix);
return result;
}
@ -569,7 +569,7 @@ inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0');
return result;
}
@ -604,7 +604,7 @@ inline std::string NumberFormatter::format0(Int64 value, int width)
inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix);
return result;
}
@ -612,7 +612,7 @@ inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0');
return result;
}
@ -620,7 +620,7 @@ inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefi
inline std::string NumberFormatter::format(UInt64 value)
{
std::string result;
intToStr(value, 10, result);
uIntToStr(value, 10, result);
return result;
}
@ -628,7 +628,7 @@ inline std::string NumberFormatter::format(UInt64 value)
inline std::string NumberFormatter::format(UInt64 value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, ' ');
uIntToStr(value, 10, result, false, width, ' ');
return result;
}
@ -636,7 +636,7 @@ inline std::string NumberFormatter::format(UInt64 value, int width)
inline std::string NumberFormatter::format0(UInt64 value, int width)
{
std::string result;
intToStr(value, 10, result, false, width, '0');
uIntToStr(value, 10, result, false, width, '0');
return result;
}
@ -644,7 +644,7 @@ inline std::string NumberFormatter::format0(UInt64 value, int width)
inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix);
return result;
}
@ -652,7 +652,7 @@ inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
{
std::string result;
intToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0');
return result;
}

View File

@ -68,13 +68,6 @@
namespace Poco {
namespace Impl {
static char DUMMY_EXP_UNDERFLOW = 0; // dummy default val
}
inline char decimalSeparator()
/// Returns decimal separator from global locale or
/// default '.' for platforms where locale is unavailable.
@ -219,6 +212,13 @@ bool strToInt(const std::string& str, I& result, short base, char thSep = ',')
}
namespace Impl {
static char DUMMY_EXP_UNDERFLOW; // dummy default val
}
template <typename F>
bool strToFloat (const char* pStr, F& result, char& eu = Impl::DUMMY_EXP_UNDERFLOW, char decSep = '.', char thSep = ',')
/// Converts zero-terminated array to floating-point number;
@ -534,6 +534,81 @@ bool intToStr(T value,
}
template <typename T>
bool uIntToStr(T value,
unsigned short base,
char* result,
unsigned& size,
bool prefix = false,
int width = -1,
char fill = ' ',
char thSep = 0)
/// Converts unsigned integer to string. Numeric bases from binary to hexadecimal are supported.
/// If width is non-zero, it pads the return value with fill character to the specified width.
/// When padding is zero character ('0'), it is prepended to the number itself; all other
/// paddings are prepended to the formatted result with minus sign or base prefix included
/// If prefix is true and base is octal or hexadecimal, respective prefix ('0' for octal,
/// "0x" for hexadecimal) is prepended. For all other bases, prefix argument is ignored.
/// Formatted string has at least [width] total length.
{
if (base < 2 || base > 0x10)
{
*result = '\0';
return false;
}
Impl::Ptr ptr(result, size);
int thCount = 0;
T tmpVal;
do
{
tmpVal = value;
value /= base;
*ptr++ = "FEDCBA9876543210123456789ABCDEF"[15 + (tmpVal - value * base)];
if (thSep && (base == 10) && (++thCount == 3))
{
*ptr++ = thSep;
thCount = 0;
}
} while (value);
if ('0' == fill)
{
if (prefix && base == 010) --width;
if (prefix && base == 0x10) width -= 2;
while ((ptr - result) < width) *ptr++ = fill;
}
if (prefix && base == 010) *ptr++ = '0';
else if (prefix && base == 0x10)
{
*ptr++ = 'x';
*ptr++ = '0';
}
if ('0' != fill)
{
while ((ptr - result) < width) *ptr++ = fill;
}
size = ptr - result;
poco_assert_dbg (size <= ptr.span());
poco_assert_dbg ((-1 == width) || (size >= width));
*ptr-- = '\0';
char* ptrr = result;
char tmp;
while(ptrr < ptr)
{
tmp = *ptr;
*ptr-- = *ptrr;
*ptrr++ = tmp;
}
return true;
}
template <typename T>
bool intToStr (T number, unsigned short base, std::string& result, bool prefix = false, int width = -1, char fill = ' ', char thSep = 0)
/// Converts integer to string; This is a wrapper function, for details see see the
@ -547,6 +622,19 @@ bool intToStr (T number, unsigned short base, std::string& result, bool prefix =
}
template <typename T>
bool uIntToStr (T number, unsigned short base, std::string& result, bool prefix = false, int width = -1, char fill = ' ', char thSep = 0)
/// Converts unsigned integer to string; This is a wrapper function, for details see see the
/// bool uIntToStr(T, unsigned short, char*, int, int, char, char) implementation.
{
char res[POCO_MAX_INT_STRING_LEN] = {0};
unsigned size = POCO_MAX_INT_STRING_LEN;
bool ret = uIntToStr(number, base, res, size, prefix, width, fill, thSep);
result.assign(res, size);
return ret;
}
//
// Functions using double-conversion library (http://code.google.com/p/double-conversion/).
// Library is the implementation of the Grisu algorithm as described in Florian Loitsch's paper:

View File

@ -107,7 +107,7 @@ void NumberFormatter::appendHex(std::string& str, int value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz);
str.append(result, sz);
}
@ -116,7 +116,7 @@ void NumberFormatter::appendHex(std::string& str, int value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -125,7 +125,7 @@ void NumberFormatter::append(std::string& str, unsigned value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz);
uIntToStr(value, 10, result, sz);
str.append(result, sz);
}
@ -134,7 +134,7 @@ void NumberFormatter::append(std::string& str, unsigned value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width);
uIntToStr(value, 10, result, sz, false, width);
str.append(result, sz);
}
@ -143,7 +143,7 @@ void NumberFormatter::append0(std::string& str, unsigned int value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width, '0');
uIntToStr(value, 10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -152,7 +152,7 @@ void NumberFormatter::appendHex(std::string& str, unsigned value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(value, 0x10, result, sz);
str.append(result, sz);
}
@ -161,7 +161,7 @@ void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(value, 0x10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -197,7 +197,7 @@ void NumberFormatter::appendHex(std::string& str, long value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz);
str.append(result, sz);
}
@ -206,7 +206,7 @@ void NumberFormatter::appendHex(std::string& str, long value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -215,7 +215,7 @@ void NumberFormatter::append(std::string& str, unsigned long value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz);
uIntToStr(value, 10, result, sz);
str.append(result, sz);
}
@ -224,7 +224,7 @@ void NumberFormatter::append(std::string& str, unsigned long value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width, '0');
uIntToStr(value, 10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -233,7 +233,7 @@ void NumberFormatter::append0(std::string& str, unsigned long value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width, '0');
uIntToStr(value, 10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -242,7 +242,7 @@ void NumberFormatter::appendHex(std::string& str, unsigned long value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(value, 0x10, result, sz);
str.append(result, sz);
}
@ -251,7 +251,7 @@ void NumberFormatter::appendHex(std::string& str, unsigned long value, int width
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(value, 0x10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -290,7 +290,7 @@ void NumberFormatter::appendHex(std::string& str, Int64 value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(static_cast<UInt64>(value), 0x10, result, sz);
str.append(result, sz);
}
@ -299,7 +299,7 @@ void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(static_cast<UInt64>(value), 0x10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -308,7 +308,7 @@ void NumberFormatter::append(std::string& str, UInt64 value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz);
uIntToStr(value, 10, result, sz);
str.append(result, sz);
}
@ -317,7 +317,7 @@ void NumberFormatter::append(std::string& str, UInt64 value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width, '0');
uIntToStr(value, 10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -326,7 +326,7 @@ void NumberFormatter::append0(std::string& str, UInt64 value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 10, result, sz, false, width, '0');
uIntToStr(value, 10, result, sz, false, width, '0');
str.append(result, sz);
}
@ -335,7 +335,7 @@ void NumberFormatter::appendHex(std::string& str, UInt64 value)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz);
uIntToStr(value, 0x10, result, sz);
str.append(result, sz);
}
@ -344,7 +344,7 @@ void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
{
char result[NF_MAX_INT_STRING_LEN];
unsigned sz = NF_MAX_INT_STRING_LEN;
intToStr(value, 0x10, result, sz, false, width, '0');
uIntToStr(value, 0x10, result, sz, false, width, '0');
str.append(result, sz);
}

View File

@ -62,6 +62,7 @@ using Poco::cat;
using Poco::strToInt;
using Poco::strToFloat;
using Poco::intToStr;
using Poco::uIntToStr;
using Poco::strToDoubleDC;
using Poco::floatToStr;
using Poco::doubleToStr;
@ -751,65 +752,65 @@ void StringTest::testIntToString()
assert (result == "1001001100101100000001011010010");
assert (intToStr(1234567890, 2, result, true, 35, '0'));
assert (result == "00001001001100101100000001011010010");
assert (intToStr(0xFF, 2, result));
assert (uIntToStr(0xFF, 2, result));
assert (result == "11111111");
assert (intToStr(0x0F, 2, result, false, 8, '0'));
assert (uIntToStr(0x0F, 2, result, false, 8, '0'));
assert (result == "00001111");
assert (intToStr(0x0F, 2, result));
assert (uIntToStr(0x0F, 2, result));
assert (result == "1111");
assert (intToStr(0xF0, 2, result));
assert (uIntToStr(0xF0, 2, result));
assert (result == "11110000");
assert (intToStr(0xFFFF, 2, result));
assert (uIntToStr(0xFFFF, 2, result));
assert (result == "1111111111111111");
assert (intToStr(0xFF00, 2, result));
assert (uIntToStr(0xFF00, 2, result));
assert (result == "1111111100000000");
assert (intToStr(0xFFFFFFFF, 2, result));
assert (uIntToStr(0xFFFFFFFF, 2, result));
assert (result == "11111111111111111111111111111111");
assert (intToStr(0xFF00FF00, 2, result));
assert (uIntToStr(0xFF00FF00, 2, result));
assert (result == "11111111000000001111111100000000");
assert (intToStr(0xF0F0F0F0, 2, result));
assert (uIntToStr(0xF0F0F0F0, 2, result));
assert (result == "11110000111100001111000011110000");
#if defined(POCO_HAVE_INT64)
assert (intToStr(0xFFFFFFFFFFFFFFFF, 2, result));
assert (uIntToStr(0xFFFFFFFFFFFFFFFF, 2, result));
std::cout << 0xFFFFFFFFFFFFFFFF << std::endl;
assert (result == "1111111111111111111111111111111111111111111111111111111111111111");
assert (intToStr(0xFF00000FF00000FF, 2, result));
assert (uIntToStr(0xFF00000FF00000FF, 2, result));
assert (result == "1111111100000000000000000000111111110000000000000000000011111111");
#endif
// octal
assert (intToStr(1234567890, 010, result));
assert (uIntToStr(1234567890, 010, result));
assert (result == "11145401322");
assert (intToStr(1234567890, 010, result, true));
assert (uIntToStr(1234567890, 010, result, true));
assert (result == "011145401322");
assert (intToStr(1234567890, 010, result, true, 15, '0'));
assert (uIntToStr(1234567890, 010, result, true, 15, '0'));
assert (result == "000011145401322");
assert (intToStr(012345670, 010, result, true));
assert (uIntToStr(012345670, 010, result, true));
assert (result == "012345670");
assert (intToStr(012345670, 010, result));
assert (uIntToStr(012345670, 010, result));
assert (result == "12345670");
// hexadecimal
assert (intToStr(0, 0x10, result, true));
assert (uIntToStr(0, 0x10, result, true));
assert (result == "0x0");
assert (intToStr(0, 0x10, result, true, 4, '0'));
assert (uIntToStr(0, 0x10, result, true, 4, '0'));
assert (result == "0x00");
assert (intToStr(0, 0x10, result, false, 4, '0'));
assert (uIntToStr(0, 0x10, result, false, 4, '0'));
assert (result == "0000");
assert (intToStr(1234567890, 0x10, result));
assert (uIntToStr(1234567890, 0x10, result));
assert (result == "499602D2");
assert (intToStr(1234567890, 0x10, result, true));
assert (uIntToStr(1234567890, 0x10, result, true));
assert (result == "0x499602D2");
assert (intToStr(1234567890, 0x10, result, true, 15, '0'));
assert (uIntToStr(1234567890, 0x10, result, true, 15, '0'));
assert (result == "0x00000499602D2");
assert (intToStr(0x1234567890ABCDEF, 0x10, result, true));
assert (uIntToStr(0x1234567890ABCDEF, 0x10, result, true));
assert (result == "0x1234567890ABCDEF");
assert (intToStr(0xDEADBEEF, 0x10, result));
assert (uIntToStr(0xDEADBEEF, 0x10, result));
assert (result == "DEADBEEF");
#if defined(POCO_HAVE_INT64)
assert (intToStr(0xFFFFFFFFFFFFFFFF, 0x10, result));
assert (uIntToStr(0xFFFFFFFFFFFFFFFF, 0x10, result));
assert (result == "FFFFFFFFFFFFFFFF");
assert (intToStr(0xFFFFFFFFFFFFFFFF, 0x10, result, true));
assert (uIntToStr(0xFFFFFFFFFFFFFFFF, 0x10, result, true));
assert (result == "0xFFFFFFFFFFFFFFFF");
#endif

View File

@ -49,7 +49,7 @@ SHAREDLIBLINKEXT = .dylib
# Compiler and Linker Flags
#
CFLAGS = $(ARCHFLAGS)
CXXFLAGS = $(ARCHFLAGS) -Wall -Wno-sign-compare
CXXFLAGS = $(ARCHFLAGS) -Wall -Wno-sign-compare -Wno-unused-variable -Wno-unused-function -Wno-unneeded-internal-declaration
LINKFLAGS = $(ARCHFLAGS)
SHLIBFLAGS = $(ARCHFLAGS)
DYLIBFLAGS = $(ARCHFLAGS)