From fc662341e6f85da78ada0e443f6116b978f79f22 Mon Sep 17 00:00:00 2001 From: Igor Pavlov <87184205+ip7z@users.noreply.github.com> Date: Tue, 14 May 2024 00:00:00 +0000 Subject: 24.05 --- CPP/Common/IntToString.cpp | 231 +++++++++++++++++++++++++-------------------- 1 file changed, 127 insertions(+), 104 deletions(-) (limited to 'CPP/Common/IntToString.cpp') diff --git a/CPP/Common/IntToString.cpp b/CPP/Common/IntToString.cpp index 21b0680..0a7cb3b 100644 --- a/CPP/Common/IntToString.cpp +++ b/CPP/Common/IntToString.cpp @@ -7,10 +7,19 @@ #include "IntToString.h" #define CONVERT_INT_TO_STR(charType, tempSize) \ - unsigned char temp[tempSize]; unsigned i = 0; \ - while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \ - *s++ = (charType)('0' + (unsigned)val); \ - while (i != 0) { i--; *s++ = (charType)temp[i]; } \ + if (val < 10) \ + *s++ = (charType)('0' + (unsigned)val); \ + else { \ + Byte temp[tempSize]; \ + size_t i = 0; \ + do { \ + temp[++i] = (Byte)('0' + (unsigned)(val % 10)); \ + val /= 10; } \ + while (val >= 10); \ + *s++ = (charType)('0' + (unsigned)val); \ + do { *s++ = (charType)temp[i]; } \ + while (--i); \ + } \ *s = 0; \ return s; @@ -22,88 +31,109 @@ char * ConvertUInt32ToString(UInt32 val, char *s) throw() char * ConvertUInt64ToString(UInt64 val, char *s) throw() { if (val <= (UInt32)0xFFFFFFFF) - { return ConvertUInt32ToString((UInt32)val, s); - } CONVERT_INT_TO_STR(char, 24) } -void ConvertUInt64ToOct(UInt64 val, char *s) throw() +wchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw() { - UInt64 v = val; - unsigned i; - for (i = 1;; i++) + CONVERT_INT_TO_STR(wchar_t, 16) +} + +wchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw() +{ + if (val <= (UInt32)0xFFFFFFFF) + return ConvertUInt32ToString((UInt32)val, s); + CONVERT_INT_TO_STR(wchar_t, 24) +} + +void ConvertInt64ToString(Int64 val, char *s) throw() +{ + if (val < 0) { - v >>= 3; - if (v == 0) - break; + *s++ = '-'; + val = -val; } - s[i] = 0; - do + ConvertUInt64ToString((UInt64)val, s); +} + +void ConvertInt64ToString(Int64 val, wchar_t *s) throw() +{ + if (val < 0) { - unsigned t = (unsigned)(val & 0x7); - val >>= 3; - s[--i] = (char)('0' + t); + *s++ = L'-'; + val = -val; } - while (i); + ConvertUInt64ToString((UInt64)val, s); } -#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))))) - -static inline char GetHexChar(unsigned t) { return GET_HEX_CHAR(t); } +void ConvertUInt64ToOct(UInt64 val, char *s) throw() +{ + { + UInt64 v = val; + do + s++; + while (v >>= 3); + } + *s = 0; + do + { + const unsigned t = (unsigned)val & 7; + *--s = (char)('0' + t); + } + while (val >>= 3); +} +MY_ALIGN(16) const char k_Hex_Upper[16] = + { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; +MY_ALIGN(16) const char k_Hex_Lower[16] = + { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; void ConvertUInt32ToHex(UInt32 val, char *s) throw() { - UInt32 v = val; - unsigned i; - for (i = 1;; i++) { - v >>= 4; - if (v == 0) - break; + UInt32 v = val; + do + s++; + while (v >>= 4); } - s[i] = 0; + *s = 0; do { - unsigned t = (unsigned)(val & 0xF); - val >>= 4; - s[--i] = GET_HEX_CHAR(t); + const unsigned t = (unsigned)val & 0xF; + *--s = GET_HEX_CHAR_UPPER(t); } - while (i); + while (val >>= 4); } - void ConvertUInt64ToHex(UInt64 val, char *s) throw() { - UInt64 v = val; - unsigned i; - for (i = 1;; i++) { - v >>= 4; - if (v == 0) - break; + UInt64 v = val; + do + s++; + while (v >>= 4); } - s[i] = 0; + *s = 0; do { - unsigned t = (unsigned)(val & 0xF); - val >>= 4; - s[--i] = GET_HEX_CHAR(t); + const unsigned t = (unsigned)val & 0xF; + *--s = GET_HEX_CHAR_UPPER(t); } - while (i); + while (val >>= 4); } void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw() { s[8] = 0; - for (int i = 7; i >= 0; i--) + int i = 7; + do { - unsigned t = val & 0xF; - val >>= 4; - s[i] = GET_HEX_CHAR(t); + { const unsigned t = (unsigned)val & 0xF; s[i--] = GET_HEX_CHAR_UPPER(t); } + { const unsigned t = (Byte)val >> 4; val >>= 8; s[i--] = GET_HEX_CHAR_UPPER(t); } } + while (i >= 0); } /* @@ -112,81 +142,74 @@ void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s) s[8] = 0; for (int i = 7; i >= 0; i--) { - unsigned t = val & 0xF; + const unsigned t = (unsigned)val & 0xF; val >>= 4; - s[i] = (wchar_t)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))); + s[i] = GET_HEX_CHAR(t); } } */ -wchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw() -{ - CONVERT_INT_TO_STR(wchar_t, 16) -} - -wchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw() -{ - if (val <= (UInt32)0xFFFFFFFF) - { - return ConvertUInt32ToString((UInt32)val, s); - } - CONVERT_INT_TO_STR(wchar_t, 24) -} -void ConvertInt64ToString(Int64 val, char *s) throw() -{ - if (val < 0) - { - *s++ = '-'; - val = -val; - } - ConvertUInt64ToString((UInt64)val, s); -} +MY_ALIGN(16) static const Byte k_Guid_Pos[] = + { 6,4,2,0, 11,9, 16,14, 19,21, 24,26,28,30,32,34 }; -void ConvertInt64ToString(Int64 val, wchar_t *s) throw() +char *RawLeGuidToString(const Byte *g, char *s) throw() { - if (val < 0) + s[ 8] = '-'; + s[13] = '-'; + s[18] = '-'; + s[23] = '-'; + s[36] = 0; + for (unsigned i = 0; i < 16; i++) { - *s++ = L'-'; - val = -val; + char *s2 = s + k_Guid_Pos[i]; + const unsigned v = g[i]; + s2[0] = GET_HEX_CHAR_UPPER(v >> 4); + s2[1] = GET_HEX_CHAR_UPPER(v & 0xF); } - ConvertUInt64ToString((UInt64)val, s); + return s + 36; } - -static void ConvertByteToHex2Digits(unsigned v, char *s) throw() +char *RawLeGuidToString_Braced(const Byte *g, char *s) throw() { - s[0] = GetHexChar(v >> 4); - s[1] = GetHexChar(v & 0xF); + *s++ = '{'; + s = RawLeGuidToString(g, s); + *s++ = '}'; + *s = 0; + return s; } -static void ConvertUInt16ToHex4Digits(UInt32 val, char *s) throw() -{ - ConvertByteToHex2Digits(val >> 8, s); - ConvertByteToHex2Digits(val & 0xFF, s + 2); -} -char *RawLeGuidToString(const Byte *g, char *s) throw() +void ConvertDataToHex_Lower(char *dest, const Byte *src, size_t size) throw() { - ConvertUInt32ToHex8Digits(GetUi32(g ), s); s += 8; *s++ = '-'; - ConvertUInt16ToHex4Digits(GetUi16(g + 4), s); s += 4; *s++ = '-'; - ConvertUInt16ToHex4Digits(GetUi16(g + 6), s); s += 4; *s++ = '-'; - for (unsigned i = 0; i < 8; i++) + if (size) { - if (i == 2) - *s++ = '-'; - ConvertByteToHex2Digits(g[8 + i], s); - s += 2; + const Byte *lim = src + size; + do + { + const unsigned b = *src++; + dest[0] = GET_HEX_CHAR_LOWER(b >> 4); + dest[1] = GET_HEX_CHAR_LOWER(b & 0xF); + dest += 2; + } + while (src != lim); } - *s = 0; - return s; + *dest = 0; } -char *RawLeGuidToString_Braced(const Byte *g, char *s) throw() +void ConvertDataToHex_Upper(char *dest, const Byte *src, size_t size) throw() { - *s++ = '{'; - s = RawLeGuidToString(g, s); - *s++ = '}'; - *s = 0; - return s; + if (size) + { + const Byte *lim = src + size; + do + { + const unsigned b = *src++; + dest[0] = GET_HEX_CHAR_UPPER(b >> 4); + dest[1] = GET_HEX_CHAR_UPPER(b & 0xF); + dest += 2; + } + while (src != lim); + } + *dest = 0; } -- cgit v1.2.3-55-g6feb