mirror of
https://github.com/libsdl-org/SDL.git
synced 2024-11-27 13:53:37 +08:00
stdlib: Rewrite SDL_strto(ll?|ul) impl
This commit is contained in:
parent
e326540a45
commit
e109aa09aa
@ -393,7 +393,8 @@ static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int
|
|||||||
++text;
|
++text;
|
||||||
} while (count == 0 || (text - text_start) != count);
|
} while (count == 0 || (text - text_start) != count);
|
||||||
}
|
}
|
||||||
if (text == number_start) { // no number was parsed, so no characters were consumed
|
if (text == number_start) {
|
||||||
|
// no number was parsed, and thus no characters were consumed
|
||||||
text = text_start;
|
text = text_start;
|
||||||
}
|
}
|
||||||
if (overflow) {
|
if (overflow) {
|
||||||
@ -411,47 +412,25 @@ static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL)
|
||||||
static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
|
static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
|
||||||
{
|
{
|
||||||
const char *textstart = text;
|
const unsigned long long_max = (~0UL) >> 1;
|
||||||
long value = 0;
|
unsigned long long value;
|
||||||
bool negative = false;
|
bool negative;
|
||||||
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
||||||
if (*text == '-') {
|
if (negative) {
|
||||||
negative = true;
|
const unsigned long abs_long_min = long_max + 1;
|
||||||
++text;
|
if (value == 0 || value > abs_long_min) {
|
||||||
}
|
value = 0ULL - abs_long_min;
|
||||||
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
|
||||||
text += 2;
|
|
||||||
}
|
|
||||||
for (;;) {
|
|
||||||
int v;
|
|
||||||
if (SDL_isdigit((unsigned char)*text)) {
|
|
||||||
v = *text - '0';
|
|
||||||
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
|
||||||
v = 10 + (*text - 'A');
|
|
||||||
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
|
||||||
v = 10 + (*text - 'a');
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
value = 0ULL - value;
|
||||||
}
|
|
||||||
value *= radix;
|
|
||||||
value += v;
|
|
||||||
++text;
|
|
||||||
|
|
||||||
if (count > 0 && (text - textstart) == count) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else if (value > long_max) {
|
||||||
|
value = long_max;
|
||||||
}
|
}
|
||||||
if (valuep && text > textstart) {
|
*valuep = value;
|
||||||
if (negative && value) {
|
return len;
|
||||||
*valuep = -value;
|
|
||||||
} else {
|
|
||||||
*valuep = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return text - textstart;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -534,39 +513,23 @@ static size_t SDL_ScanLongW(const wchar_t *text, int count, int radix, long *val
|
|||||||
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
|
||||||
static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
|
static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
|
||||||
{
|
{
|
||||||
const char *textstart = text;
|
const unsigned long ulong_max = ~0UL;
|
||||||
unsigned long value = 0;
|
unsigned long long value;
|
||||||
|
bool negative;
|
||||||
if (*text == '-') {
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
||||||
return SDL_ScanLong(text, count, radix, (long *)valuep);
|
if (negative) {
|
||||||
}
|
if (value == 0 || value > ulong_max) {
|
||||||
|
value = ulong_max;
|
||||||
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
} else if (value == ulong_max) {
|
||||||
text += 2;
|
value = 1;
|
||||||
}
|
|
||||||
for (;;) {
|
|
||||||
int v;
|
|
||||||
if (SDL_isdigit((unsigned char)*text)) {
|
|
||||||
v = *text - '0';
|
|
||||||
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
|
||||||
v = 10 + (*text - 'A');
|
|
||||||
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
|
||||||
v = 10 + (*text - 'a');
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
value = 0ULL - value;
|
||||||
}
|
|
||||||
value *= radix;
|
|
||||||
value += v;
|
|
||||||
++text;
|
|
||||||
|
|
||||||
if (count > 0 && (text - textstart) == count) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else if (value > ulong_max) {
|
||||||
|
value = ulong_max;
|
||||||
}
|
}
|
||||||
if (valuep && text > textstart) {
|
*valuep = value;
|
||||||
*valuep = value;
|
return len;
|
||||||
}
|
|
||||||
return text - textstart;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -601,58 +564,37 @@ static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL)
|
||||||
static size_t SDL_ScanLongLong(const char *text, int count, int radix, long long *valuep)
|
static size_t SDL_ScanLongLong(const char *text, int count, int radix, long long *valuep)
|
||||||
{
|
{
|
||||||
const char *textstart = text;
|
const unsigned long long llong_max = (~0ULL) >> 1;
|
||||||
long long value = 0;
|
unsigned long long value;
|
||||||
bool negative = false;
|
bool negative;
|
||||||
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
||||||
if (*text == '-') {
|
if (negative) {
|
||||||
negative = true;
|
const unsigned long long abs_llong_min = llong_max + 1;
|
||||||
++text;
|
if (value == 0 || value > abs_llong_min) {
|
||||||
}
|
value = 0ULL - abs_llong_min;
|
||||||
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
|
||||||
text += 2;
|
|
||||||
}
|
|
||||||
for (;;) {
|
|
||||||
int v;
|
|
||||||
if (SDL_isdigit((unsigned char)*text)) {
|
|
||||||
v = *text - '0';
|
|
||||||
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
|
||||||
v = 10 + (*text - 'A');
|
|
||||||
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
|
||||||
v = 10 + (*text - 'a');
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
value = 0ULL - value;
|
||||||
}
|
|
||||||
value *= radix;
|
|
||||||
value += v;
|
|
||||||
++text;
|
|
||||||
|
|
||||||
if (count > 0 && (text - textstart) == count) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else if (value > llong_max) {
|
||||||
|
value = llong_max;
|
||||||
}
|
}
|
||||||
if (valuep && text > textstart) {
|
*valuep = value;
|
||||||
if (negative && value) {
|
return len;
|
||||||
*valuep = -value;
|
|
||||||
} else {
|
|
||||||
*valuep = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return text - textstart;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
|
||||||
static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, unsigned long long *valuep)
|
static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, unsigned long long *valuep)
|
||||||
{
|
{
|
||||||
|
const unsigned long long ullong_max = ~0ULL;
|
||||||
bool negative;
|
bool negative;
|
||||||
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, valuep, &negative);
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, valuep, &negative);
|
||||||
if (negative) {
|
if (negative) {
|
||||||
if (*valuep == 0) {
|
if (*valuep == 0) {
|
||||||
*valuep = ~0ULL; // ULLONG_MAX
|
*valuep = ullong_max;
|
||||||
} else {
|
} else {
|
||||||
*valuep = 0ULL - *valuep;
|
*valuep = 0ULL - *valuep;
|
||||||
}
|
}
|
||||||
@ -1282,18 +1224,8 @@ long SDL_strtol(const char *string, char **endp, int base)
|
|||||||
#ifdef HAVE_STRTOL
|
#ifdef HAVE_STRTOL
|
||||||
return strtol(string, endp, base);
|
return strtol(string, endp, base);
|
||||||
#else
|
#else
|
||||||
size_t len;
|
|
||||||
long value = 0;
|
long value = 0;
|
||||||
|
size_t len = SDL_ScanLong(string, 0, base, &value);
|
||||||
if (!base) {
|
|
||||||
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
|
||||||
base = 16;
|
|
||||||
} else {
|
|
||||||
base = 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
len = SDL_ScanLong(string, 0, base, &value);
|
|
||||||
if (endp) {
|
if (endp) {
|
||||||
*endp = (char *)string + len;
|
*endp = (char *)string + len;
|
||||||
}
|
}
|
||||||
@ -1306,18 +1238,8 @@ unsigned long SDL_strtoul(const char *string, char **endp, int base)
|
|||||||
#ifdef HAVE_STRTOUL
|
#ifdef HAVE_STRTOUL
|
||||||
return strtoul(string, endp, base);
|
return strtoul(string, endp, base);
|
||||||
#else
|
#else
|
||||||
size_t len;
|
|
||||||
unsigned long value = 0;
|
unsigned long value = 0;
|
||||||
|
size_t len = SDL_ScanUnsignedLong(string, 0, base, &value);
|
||||||
if (!base) {
|
|
||||||
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
|
||||||
base = 16;
|
|
||||||
} else {
|
|
||||||
base = 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
len = SDL_ScanUnsignedLong(string, 0, base, &value);
|
|
||||||
if (endp) {
|
if (endp) {
|
||||||
*endp = (char *)string + len;
|
*endp = (char *)string + len;
|
||||||
}
|
}
|
||||||
@ -1330,18 +1252,8 @@ long long SDL_strtoll(const char *string, char **endp, int base)
|
|||||||
#ifdef HAVE_STRTOLL
|
#ifdef HAVE_STRTOLL
|
||||||
return strtoll(string, endp, base);
|
return strtoll(string, endp, base);
|
||||||
#else
|
#else
|
||||||
size_t len;
|
|
||||||
long long value = 0;
|
long long value = 0;
|
||||||
|
size_t len = SDL_ScanLongLong(string, 0, base, &value);
|
||||||
if (!base) {
|
|
||||||
if ((SDL_strlen(string) > 2) && (SDL_strncasecmp(string, "0x", 2) == 0)) {
|
|
||||||
base = 16;
|
|
||||||
} else {
|
|
||||||
base = 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
len = SDL_ScanLongLong(string, 0, base, &value);
|
|
||||||
if (endp) {
|
if (endp) {
|
||||||
*endp = (char *)string + len;
|
*endp = (char *)string + len;
|
||||||
}
|
}
|
||||||
|
@ -1509,6 +1509,14 @@ static int SDLCALL stdlib_strtoull(void *arg)
|
|||||||
SDLTest_AssertPass("Call to SDL_strtol(\"-2147483649\", &endp, 0)");
|
SDLTest_AssertPass("Call to SDL_strtol(\"-2147483649\", &endp, 0)");
|
||||||
SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
|
SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
|
||||||
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
|
|
||||||
|
text = "-9999999999999999999999999999999999999999";
|
||||||
|
expected_lresult = -2147483648;
|
||||||
|
expected_endp = (char *)text + 41;
|
||||||
|
lresult = SDL_strtol(text, &endp, 0);
|
||||||
|
SDLTest_AssertPass("Call to SDL_strtol(\"-9999999999999999999999999999999999999999\", &endp, 0)");
|
||||||
|
SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
|
||||||
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SDL_strtoul (32-bit)
|
// SDL_strtoul (32-bit)
|
||||||
@ -1547,6 +1555,14 @@ static int SDLCALL stdlib_strtoull(void *arg)
|
|||||||
SDLTest_AssertPass("Call to SDL_strtoul(\"-4294967296\", &endp, 0)");
|
SDLTest_AssertPass("Call to SDL_strtoul(\"-4294967296\", &endp, 0)");
|
||||||
SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
|
SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
|
||||||
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
|
|
||||||
|
text = "-9999999999999999999999999999999999999999";
|
||||||
|
expected_ulresult = 4294967295;
|
||||||
|
expected_endp = (char *)text + 41;
|
||||||
|
ulresult = SDL_strtoul(text, &endp, 0);
|
||||||
|
SDLTest_AssertPass("Call to SDL_strtoul(\"-9999999999999999999999999999999999999999\", &endp, 0)");
|
||||||
|
SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
|
||||||
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SDL_strtoll (64-bit)
|
// SDL_strtoll (64-bit)
|
||||||
@ -1585,6 +1601,14 @@ static int SDLCALL stdlib_strtoull(void *arg)
|
|||||||
SDLTest_AssertPass("Call to SDL_strtoll(\"-9223372036854775809\", &endp, 0)");
|
SDLTest_AssertPass("Call to SDL_strtoll(\"-9223372036854775809\", &endp, 0)");
|
||||||
SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
|
SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
|
||||||
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
|
|
||||||
|
text = "-9999999999999999999999999999999999999999";
|
||||||
|
expected_llresult = -9223372036854775807 - 1;
|
||||||
|
expected_endp = (char *)text + 41;
|
||||||
|
llresult = SDL_strtoll(text, &endp, 0);
|
||||||
|
SDLTest_AssertPass("Call to SDL_strtoll(\"-9999999999999999999999999999999999999999\", &endp, 0)");
|
||||||
|
SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
|
||||||
|
SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TEST_COMPLETED;
|
return TEST_COMPLETED;
|
||||||
|
Loading…
Reference in New Issue
Block a user