strtol(3) now returns 0 if the string was + or -.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-12-16 21:44:32 +01:00
parent f1a49dbad3
commit 73bcafc004
1 changed files with 4 additions and 0 deletions

View File

@ -54,6 +54,7 @@ namespace Maxsi
template <class INT, bool UNSIGNED> INT ParseInteger(const char* str, char** endptr, int base) template <class INT, bool UNSIGNED> INT ParseInteger(const char* str, char** endptr, int base)
{ {
const char* origstr = str;
int origbase = base; int origbase = base;
while ( IsSpace(*str) ) { str++; } while ( IsSpace(*str) ) { str++; }
if ( base < 0 || 36 < base ) { if ( endptr ) { *endptr = (char*) str; } return 0; } if ( base < 0 || 36 < base ) { if ( endptr ) { *endptr = (char*) str; } return 0; }
@ -69,6 +70,7 @@ namespace Maxsi
} }
if ( !base ) { base = 10; } if ( !base ) { base = 10; }
if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; } if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; }
size_t numconvertedchars = 0;
while ( (c = *str ) ) while ( (c = *str ) )
{ {
int val = Debase(c); int val = Debase(c);
@ -78,7 +80,9 @@ namespace Maxsi
// TODO: Detect overflow! // TODO: Detect overflow!
result = result * (INT) base + (INT) val; result = result * (INT) base + (INT) val;
str++; str++;
numconvertedchars++;
} }
if ( !numconvertedchars ) { str = origstr; result = 0; }
if ( endptr ) { *endptr = (char*) str; } if ( endptr ) { *endptr = (char*) str; }
return result; return result;
} }