mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-07 07:14:56 -06:00
9.34
This commit is contained in:
committed by
Kornel Lesiński
parent
83f8ddcc5b
commit
f08f4dcc3c
148
CPP/Common/StringToInt.cpp
Executable file → Normal file
148
CPP/Common/StringToInt.cpp
Executable file → Normal file
@@ -4,94 +4,140 @@
|
||||
|
||||
#include "StringToInt.h"
|
||||
|
||||
UInt64 ConvertStringToUInt64(const char *s, const char **end)
|
||||
static const UInt32 k_UInt32_max = 0xFFFFFFFF;
|
||||
static const UInt64 k_UInt64_max = 0xFFFFFFFFFFFFFFFF;
|
||||
|
||||
#define CONVERT_STRING_TO_UINT_FUNC(uintType, charType) \
|
||||
uintType ConvertStringTo ## uintType(const charType *s, const charType **end) { \
|
||||
if (end) *end = s; \
|
||||
uintType res = 0; \
|
||||
for (;; s++) { \
|
||||
charType c = *s; \
|
||||
if (c < '0' || c > '9') { if (end) *end = s; return res; } \
|
||||
if (res > (k_ ## uintType ## _max) / 10) return 0; \
|
||||
res *= 10; \
|
||||
unsigned v = (c - '0'); \
|
||||
if (res > (k_ ## uintType ## _max) - v) return 0; \
|
||||
res += v; }}
|
||||
|
||||
CONVERT_STRING_TO_UINT_FUNC(UInt32, char)
|
||||
CONVERT_STRING_TO_UINT_FUNC(UInt32, wchar_t)
|
||||
CONVERT_STRING_TO_UINT_FUNC(UInt64, char)
|
||||
CONVERT_STRING_TO_UINT_FUNC(UInt64, wchar_t)
|
||||
|
||||
Int32 ConvertStringToInt32(const wchar_t *s, const wchar_t **end)
|
||||
{
|
||||
UInt64 result = 0;
|
||||
for (;;)
|
||||
if (end)
|
||||
*end = s;
|
||||
const wchar_t *s2 = s;
|
||||
if (*s == '-')
|
||||
s2++;
|
||||
if (*s2 == 0)
|
||||
return 0;
|
||||
const wchar_t *end2;
|
||||
UInt32 res = ConvertStringToUInt32(s2, &end2);
|
||||
if (*s == '-')
|
||||
{
|
||||
if (res > ((UInt32)1 << (32 - 1)))
|
||||
return 0;
|
||||
}
|
||||
else if ((res & ((UInt32)1 << (32 - 1))) != 0)
|
||||
return 0;
|
||||
if (end)
|
||||
*end = end2;
|
||||
if (*s == '-')
|
||||
return -(Int32)res;
|
||||
return (Int32)res;
|
||||
}
|
||||
|
||||
UInt32 ConvertOctStringToUInt32(const char *s, const char **end)
|
||||
{
|
||||
if (end)
|
||||
*end = s;
|
||||
UInt32 res = 0;
|
||||
for (;; s++)
|
||||
{
|
||||
char c = *s;
|
||||
if (c < '0' || c > '9')
|
||||
if (c < '0' || c > '7')
|
||||
{
|
||||
if (end != NULL)
|
||||
if (end)
|
||||
*end = s;
|
||||
return result;
|
||||
return res;
|
||||
}
|
||||
result *= 10;
|
||||
result += (c - '0');
|
||||
s++;
|
||||
if ((res & (UInt32)7 << (32 - 3)) != 0)
|
||||
return 0;
|
||||
res <<= 3;
|
||||
res |= (unsigned)(c - '0');
|
||||
}
|
||||
}
|
||||
|
||||
UInt64 ConvertOctStringToUInt64(const char *s, const char **end)
|
||||
{
|
||||
UInt64 result = 0;
|
||||
for (;;)
|
||||
if (end)
|
||||
*end = s;
|
||||
UInt64 res = 0;
|
||||
for (;; s++)
|
||||
{
|
||||
char c = *s;
|
||||
if (c < '0' || c > '7')
|
||||
{
|
||||
if (end != NULL)
|
||||
if (end)
|
||||
*end = s;
|
||||
return result;
|
||||
return res;
|
||||
}
|
||||
result <<= 3;
|
||||
result += (c - '0');
|
||||
s++;
|
||||
if ((res & (UInt64)7 << (64 - 3)) != 0)
|
||||
return 0;
|
||||
res <<= 3;
|
||||
res |= (unsigned)(c - '0');
|
||||
}
|
||||
}
|
||||
|
||||
UInt64 ConvertHexStringToUInt64(const char *s, const char **end)
|
||||
UInt32 ConvertHexStringToUInt32(const char *s, const char **end)
|
||||
{
|
||||
UInt64 result = 0;
|
||||
for (;;)
|
||||
if (end)
|
||||
*end = s;
|
||||
UInt32 res = 0;
|
||||
for (;; s++)
|
||||
{
|
||||
char c = *s;
|
||||
UInt32 v;
|
||||
unsigned v;
|
||||
if (c >= '0' && c <= '9') v = (c - '0');
|
||||
else if (c >= 'A' && c <= 'F') v = 10 + (c - 'A');
|
||||
else if (c >= 'a' && c <= 'f') v = 10 + (c - 'a');
|
||||
else
|
||||
{
|
||||
if (end != NULL)
|
||||
if (end)
|
||||
*end = s;
|
||||
return result;
|
||||
return res;
|
||||
}
|
||||
result <<= 4;
|
||||
result |= v;
|
||||
s++;
|
||||
if ((res & (UInt32)0xF << (32 - 4)) != 0)
|
||||
return 0;
|
||||
res <<= 4;
|
||||
res |= v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end)
|
||||
UInt64 ConvertHexStringToUInt64(const char *s, const char **end)
|
||||
{
|
||||
UInt64 result = 0;
|
||||
for (;;)
|
||||
if (end)
|
||||
*end = s;
|
||||
UInt64 res = 0;
|
||||
for (;; s++)
|
||||
{
|
||||
wchar_t c = *s;
|
||||
if (c < '0' || c > '9')
|
||||
char c = *s;
|
||||
unsigned v;
|
||||
if (c >= '0' && c <= '9') v = (c - '0');
|
||||
else if (c >= 'A' && c <= 'F') v = 10 + (c - 'A');
|
||||
else if (c >= 'a' && c <= 'f') v = 10 + (c - 'a');
|
||||
else
|
||||
{
|
||||
if (end != NULL)
|
||||
if (end)
|
||||
*end = s;
|
||||
return result;
|
||||
return res;
|
||||
}
|
||||
result *= 10;
|
||||
result += (c - '0');
|
||||
s++;
|
||||
if ((res & (UInt64)0xF << (64 - 4)) != 0)
|
||||
return 0;
|
||||
res <<= 4;
|
||||
res |= v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Int64 ConvertStringToInt64(const char *s, const char **end)
|
||||
{
|
||||
if (*s == '-')
|
||||
return -(Int64)ConvertStringToUInt64(s + 1, end);
|
||||
return ConvertStringToUInt64(s, end);
|
||||
}
|
||||
|
||||
Int64 ConvertStringToInt64(const wchar_t *s, const wchar_t **end)
|
||||
{
|
||||
if (*s == L'-')
|
||||
return -(Int64)ConvertStringToUInt64(s + 1, end);
|
||||
return ConvertStringToUInt64(s, end);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user