Skip to content

Commit

Permalink
formatHex with lower case (#3657)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackyWoo committed Jul 4, 2022
1 parent 3e4a286 commit b1823b6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 56 deletions.
96 changes: 48 additions & 48 deletions Foundation/include/Poco/NumberFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field
/// having at least the specified width.

static std::string formatHex(int value, bool prefix = false);
static std::string formatHex(int value, bool prefix = false, bool upper = true);
/// Formats an int value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.
/// The value is treated as unsigned.

static std::string formatHex(int value, int width, bool prefix = false);
static std::string formatHex(int value, int width, bool prefix = false, bool upper = true);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
Expand All @@ -86,12 +86,12 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field having at
/// least the specified width.

static std::string formatHex(unsigned value, bool prefix = false);
static std::string formatHex(unsigned value, bool prefix = false, bool upper = true);
/// Formats an unsigned int value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.

static std::string formatHex(unsigned value, int width, bool prefix = false);
static std::string formatHex(unsigned value, int width, bool prefix = false, bool upper = true);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
Expand All @@ -111,13 +111,13 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field
/// having at least the specified width.

static std::string formatHex(long value, bool prefix = false);
static std::string formatHex(long value, bool prefix = false, bool upper = true);
/// Formats an unsigned long value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.
/// The value is treated as unsigned.

static std::string formatHex(long value, int width, bool prefix = false);
static std::string formatHex(long value, int width, bool prefix = false, bool upper = true);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
Expand All @@ -138,12 +138,12 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded
/// in a field having at least the specified width.

static std::string formatHex(unsigned long value, bool prefix = false);
static std::string formatHex(unsigned long value, bool prefix = false, bool upper = true);
/// Formats an unsigned long value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.

static std::string formatHex(unsigned long value, int width, bool prefix = false);
static std::string formatHex(unsigned long value, int width, bool prefix = false, bool upper = true);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
Expand All @@ -165,13 +165,13 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field having at least
/// the specified width.

static std::string formatHex(long long value, bool prefix = false);
static std::string formatHex(long long value, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.
/// The value is treated as unsigned.

static std::string formatHex(long long value, int width, bool prefix = false);
static std::string formatHex(long long value, int width, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
Expand All @@ -190,12 +190,12 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field having at least the
/// specified width.

static std::string formatHex(unsigned long long value, bool prefix = false);
static std::string formatHex(unsigned long long value, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.

static std::string formatHex(unsigned long long value, int width, bool prefix = false);
static std::string formatHex(unsigned long long value, int width, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width. If prefix is true, "0x" prefix is
Expand All @@ -215,13 +215,13 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field having at least
/// the specified width.

static std::string formatHex(Int64 value, bool prefix = false);
static std::string formatHex(Int64 value, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.
/// The value is treated as unsigned.

static std::string formatHex(Int64 value, int width, bool prefix = false);
static std::string formatHex(Int64 value, int width, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
Expand All @@ -240,12 +240,12 @@ class Foundation_API NumberFormatter
/// right justified and zero-padded in a field having at least the
/// specified width.

static std::string formatHex(UInt64 value, bool prefix = false);
static std::string formatHex(UInt64 value, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation.
/// If prefix is true, "0x" prefix is prepended to the
/// resulting string.

static std::string formatHex(UInt64 value, int width, bool prefix = false);
static std::string formatHex(UInt64 value, int width, bool prefix = false, bool upper = true);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width. If prefix is true, "0x" prefix is
Expand Down Expand Up @@ -532,18 +532,18 @@ inline std::string NumberFormatter::format0(int value, int width)
}


inline std::string NumberFormatter::formatHex(int value, bool prefix)
inline std::string NumberFormatter::formatHex(int value, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix);
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(int value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0');
uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -572,18 +572,18 @@ inline std::string NumberFormatter::format0(unsigned int value, int width)
}


inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned value, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -612,18 +612,18 @@ inline std::string NumberFormatter::format0(long value, int width)
}


inline std::string NumberFormatter::formatHex(long value, bool prefix)
inline std::string NumberFormatter::formatHex(long value, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix);
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(long value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(long value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0');
uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -652,18 +652,18 @@ inline std::string NumberFormatter::format0(unsigned long value, int width)
}


inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -696,18 +696,18 @@ inline std::string NumberFormatter::format0(long long value, int width)
}


inline std::string NumberFormatter::formatHex(long long value, bool prefix)
inline std::string NumberFormatter::formatHex(long long value, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix);
uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(long long value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(long long value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix, width, '0');
uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -736,18 +736,18 @@ inline std::string NumberFormatter::format0(unsigned long long value, int width)
}


inline std::string NumberFormatter::formatHex(unsigned long long value, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned long long value, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(unsigned long long value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(unsigned long long value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -779,18 +779,18 @@ inline std::string NumberFormatter::format0(Int64 value, int width)
}


inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
inline std::string NumberFormatter::formatHex(Int64 value, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix);
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0');
uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down Expand Up @@ -819,18 +819,18 @@ inline std::string NumberFormatter::format0(UInt64 value, int width)
}


inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix);
uIntToStr(value, 0x10, result, prefix, -1, ' ', 0, upper);
return result;
}


inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix, bool upper)
{
std::string result;
uIntToStr(value, 0x10, result, prefix, width, '0');
uIntToStr(value, 0x10, result, prefix, width, '0', 0, upper);
return result;
}

Expand Down
24 changes: 16 additions & 8 deletions Foundation/include/Poco/NumericString.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ bool intToStr(T value,
bool prefix = false,
int width = -1,
char fill = ' ',
char thSep = 0)
char thSep = 0,
bool upper = true)
/// Converts 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
Expand All @@ -396,7 +397,10 @@ bool intToStr(T value,
{
tmpVal = value;
value /= base;
*ptr++ = "FEDCBA9876543210123456789ABCDEF"[15 + (tmpVal - value * base)];
if (upper)
*ptr++ = "FEDCBA9876543210123456789ABCDEF"[15 + (tmpVal - value * base)];
else
*ptr++ = "fedcba9876543210123456789abcdef"[15 + (tmpVal - value * base)];
if (thSep && (base == 10) && (++thCount == 3))
{
*ptr++ = thSep;
Expand Down Expand Up @@ -452,7 +456,8 @@ bool uIntToStr(T value,
bool prefix = false,
int width = -1,
char fill = ' ',
char thSep = 0)
char thSep = 0,
bool upper = true)
/// 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
Expand All @@ -474,7 +479,10 @@ bool uIntToStr(T value,
{
tmpVal = value;
value /= base;
*ptr++ = "FEDCBA9876543210123456789ABCDEF"[15 + (tmpVal - value * base)];
if (upper)
*ptr++ = "FEDCBA9876543210123456789ABCDEF"[15 + (tmpVal - value * base)];
else
*ptr++ = "fedcba9876543210123456789abcdef"[15 + (tmpVal - value * base)];
if (thSep && (base == 10) && (++thCount == 3))
{
*ptr++ = thSep;
Expand Down Expand Up @@ -520,26 +528,26 @@ bool uIntToStr(T value,


template <typename T>
bool intToStr (T number, unsigned short base, std::string& result, bool prefix = false, int width = -1, char fill = ' ', char thSep = 0)
bool intToStr (T number, unsigned short base, std::string& result, bool prefix = false, int width = -1, char fill = ' ', char thSep = 0, bool upper = true)
/// Converts integer to string; This is a wrapper function, for details see see the
/// bool intToStr(T, unsigned short, char*, int, int, char, char) implementation.
{
char res[POCO_MAX_INT_STRING_LEN] = {0};
std::size_t size = POCO_MAX_INT_STRING_LEN;
bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep);
bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep, upper);
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)
bool uIntToStr (T number, unsigned short base, std::string& result, bool prefix = false, int width = -1, char fill = ' ', char thSep = 0, bool upper = true)
/// 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};
std::size_t size = POCO_MAX_INT_STRING_LEN;
bool ret = uIntToStr(number, base, res, size, prefix, width, fill, thSep);
bool ret = uIntToStr(number, base, res, size, prefix, width, fill, thSep, upper);
result.assign(res, size);
return ret;
}
Expand Down
Loading

0 comments on commit b1823b6

Please sign in to comment.