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.
@@ -217,6 +210,13 @@ bool strToInt(const std::string& str, I& result, short base, char thSep = ',')
{
return strToInt(str.c_str(), result, base, thSep);
}
namespace Impl {
static char DUMMY_EXP_UNDERFLOW; // dummy default val
}
template <typename F>
@@ -451,7 +451,7 @@ namespace Impl {
const char* _beg;
char* _cur;
const char* _end;
};
};
} // namespace Impl
@@ -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
@@ -545,6 +620,19 @@ bool intToStr (T number, unsigned short base, std::string& result, bool prefix =
result.assign(res, size);
return ret;
}
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;
}
//