Commit aa46a63e authored by Harvey Harrison's avatar Harvey Harrison Committed by Linus Torvalds

lib: pull base-guessing logic to helper function

The default base is 10 unless there is a leading zero, in which
case the base will be guessed as 8.

The base will only be guesed as 16 when the string starts with '0x'
the third character is a valid hex digit.
Signed-off-by: default avatarHarvey Harrison <harvey.harrison@gmail.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 73b4a24f
...@@ -32,6 +32,18 @@ ...@@ -32,6 +32,18 @@
/* Works only for digits and letters, but small and fast */ /* Works only for digits and letters, but small and fast */
#define TOLOWER(x) ((x) | 0x20) #define TOLOWER(x) ((x) | 0x20)
static unsigned int simple_guess_base(const char *cp)
{
if (cp[0] == '0') {
if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
return 16;
else
return 8;
} else {
return 10;
}
}
/** /**
* simple_strtoul - convert a string to an unsigned long * simple_strtoul - convert a string to an unsigned long
* @cp: The start of the string * @cp: The start of the string
...@@ -40,32 +52,28 @@ ...@@ -40,32 +52,28 @@
*/ */
unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base) unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
{ {
unsigned long result = 0,value; unsigned long result = 0;
if (!base) { if (!base)
base = 10; base = simple_guess_base(cp);
if (*cp == '0') {
base = 8; if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp++; cp += 2;
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
cp++; while (isxdigit(*cp)) {
base = 16; unsigned int value;
}
} value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
} else if (base == 16) { if (value >= base)
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x') break;
cp += 2; result = result * base + value;
}
while (isxdigit(*cp) &&
(value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
result = result*base + value;
cp++; cp++;
} }
if (endp) if (endp)
*endp = (char *)cp; *endp = (char *)cp;
return result; return result;
} }
EXPORT_SYMBOL(simple_strtoul); EXPORT_SYMBOL(simple_strtoul);
/** /**
...@@ -91,32 +99,28 @@ EXPORT_SYMBOL(simple_strtol); ...@@ -91,32 +99,28 @@ EXPORT_SYMBOL(simple_strtol);
*/ */
unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base) unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
{ {
unsigned long long result = 0,value; unsigned long long result = 0;
if (!base) { if (!base)
base = 10; base = simple_guess_base(cp);
if (*cp == '0') {
base = 8; if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp++; cp += 2;
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
cp++; while (isxdigit(*cp)) {
base = 16; unsigned int value;
}
} value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
} else if (base == 16) { if (value >= base)
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x') break;
cp += 2; result = result * base + value;
}
while (isxdigit(*cp)
&& (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
result = result*base + value;
cp++; cp++;
} }
if (endp) if (endp)
*endp = (char *)cp; *endp = (char *)cp;
return result; return result;
} }
EXPORT_SYMBOL(simple_strtoull); EXPORT_SYMBOL(simple_strtoull);
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment