removed duplicate code

This commit is contained in:
hyung-hwan 2022-06-13 13:57:07 +00:00
parent 37c50c7d06
commit e218e38592

View File

@ -641,243 +641,4 @@ HIO_EXPORT void hio_sip_hash_24 (
hio_uint8_t out[8]
);
#if defined(__cplusplus)
}
#endif
/* Some C++ utilities below */
#if defined(__cplusplus)
/*
static inline bool is_space (char c) { return isspace(c); }
static inline bool is_wspace (wchar_t c) { return iswspace(c); }
unsigned x = hio_chars_to_uint<char,unsigned,is_space>("0x12345", 7, 0, NULL, NULL);
unsigned y = hio_chars_to_uint<wchar_t,unsigned,is_wspace>(L"0x12345", 7, 0, NULL, NULL);
int a = hio_chars_to_int<char,int,is_space>("-0x12345", 8, 0, NULL, NULL);
int b = hio_chars_to_int<wchar_t,int,is_wspace>(L"-0x12345", 8, 0, NULL, NULL);
*/
template<typename CHAR_TYPE, typename INT_TYPE, bool(*IS_SPACE)(CHAR_TYPE)>INT_TYPE hio_chars_to_int (const CHAR_TYPE* str, hio_oow_t len, int option, const CHAR_TYPE** endptr, int* is_sober)
{
INT_TYPE n = 0;
const CHAR_TYPE* p, * pp;
const CHAR_TYPE* end;
hio_oow_t rem;
int digit, negative = 0;
int base = HIO_CHARS_TO_INT_GET_OPTION_BASE(option);
p = str;
end = str + len;
if (HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option))
{
/* strip off leading spaces */
while (p < end && IS_SPACE(*p)) p++;
}
/* check for a sign */
while (p < end)
{
if (*p == '-')
{
negative = ~negative;
p++;
}
else if (*p == '+') p++;
else break;
}
/* check for a binary/octal/hexadecimal notation */
rem = end - p;
if (base == 0)
{
if (rem >= 1 && *p == '0')
{
p++;
if (rem == 1) base = 8;
else if (*p == 'x' || *p == 'X')
{
p++; base = 16;
}
else if (*p == 'b' || *p == 'B')
{
p++; base = 2;
}
else base = 8;
}
else base = 10;
}
else if (rem >= 2 && base == 16)
{
if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2;
}
else if (rem >= 2 && base == 2)
{
if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2;
}
/* process the digits */
pp = p;
while (p < end)
{
digit = HIO_ZDIGIT_TO_NUM(*p, base);
if (digit >= base) break;
n = n * base + digit;
p++;
}
if (HIO_CHARS_TO_INT_GET_OPTION_E(option))
{
if (*p == 'E' || *p == 'e')
{
INT_TYPE e = 0, i;
int e_neg = 0;
p++;
if (*p == '+')
{
p++;
}
else if (*p == '-')
{
p++; e_neg = 1;
}
while (p < end)
{
digit = HIO_ZDIGIT_TO_NUM(*p, base);
if (digit >= base) break;
e = e * base + digit;
p++;
}
if (e_neg)
for (i = 0; i < e; i++) n /= 10;
else
for (i = 0; i < e; i++) n *= 10;
}
}
/* base 8: at least a zero digit has been seen.
* other case: p > pp to be able to have at least 1 meaningful digit. */
if (is_sober) *is_sober = (base == 8 || p > pp);
if (HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option))
{
/* consume trailing spaces */
while (p < end && IS_SPACE(*p)) p++;
}
if (endptr) *endptr = p;
return (negative)? -n: n;
}
template<typename CHAR_TYPE, typename UINT_TYPE, bool(*IS_SPACE)(CHAR_TYPE)>UINT_TYPE hio_chars_to_uint (const CHAR_TYPE* str, hio_oow_t len, int option, const CHAR_TYPE** endptr, int* is_sober)
{
UINT_TYPE n = 0;
const CHAR_TYPE* p, * pp;
const CHAR_TYPE* end;
hio_oow_t rem;
int digit;
int base = HIO_CHARS_TO_UINT_GET_OPTION_BASE(option);
p = str;
end = str + len;
if (HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option))
{
/* strip off leading spaces */
while (p < end && IS_SPACE(*p)) p++;
}
/* check for a sign */
while (p < end)
{
if (*p == '+') p++;
else break;
}
/* check for a binary/octal/hexadecimal notation */
rem = end - p;
if (base == 0)
{
if (rem >= 1 && *p == '0')
{
p++;
if (rem == 1) base = 8;
else if (*p == 'x' || *p == 'X')
{
p++; base = 16;
}
else if (*p == 'b' || *p == 'B')
{
p++; base = 2;
}
else base = 8;
}
else base = 10;
}
else if (rem >= 2 && base == 16)
{
if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2;
}
else if (rem >= 2 && base == 2)
{
if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2;
}
/* process the digits */
pp = p;
while (p < end)
{
digit = HIO_ZDIGIT_TO_NUM(*p, base);
if (digit >= base) break;
n = n * base + digit;
p++;
}
if (HIO_CHARS_TO_UINT_GET_OPTION_E(option))
{
if (*p == 'E' || *p == 'e')
{
UINT_TYPE e = 0, i;
int e_neg = 0;
p++;
if (*p == '+')
{
p++;
}
else if (*p == '-')
{
p++; e_neg = 1;
}
while (p < end)
{
digit = HIO_ZDIGIT_TO_NUM(*p, base);
if (digit >= base) break;
e = e * base + digit;
p++;
}
if (e_neg)
for (i = 0; i < e; i++) n /= 10;
else
for (i = 0; i < e; i++) n *= 10;
}
}
/* base 8: at least a zero digit has been seen.
* other case: p > pp to be able to have at least 1 meaningful digit. */
if (is_sober) *is_sober = (base == 8 || p > pp);
if (HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option))
{
/* consume trailing spaces */
while (p < end && IS_SPACE(*p)) p++;
}
if (endptr) *endptr = p;
return n;
}
#endif
#endif