Skip to content

Commit

Permalink
lib: pull base-guessing logic to helper function
Browse files Browse the repository at this point in the history
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: Harvey Harrison <harvey.harrison@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
hharrison authored and torvalds committed Oct 16, 2008
1 parent 73b4a24 commit aa46a63
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions lib/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
/* Works only for digits and letters, but small and fast */
#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
* @cp: The start of the string
Expand All @@ -40,32 +52,28 @@
*/
unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
{
unsigned long result = 0,value;
unsigned long result = 0;

if (!base) {
base = 10;
if (*cp == '0') {
base = 8;
cp++;
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
cp++;
base = 16;
}
}
} else if (base == 16) {
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp += 2;
}
while (isxdigit(*cp) &&
(value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
result = result*base + value;
if (!base)
base = simple_guess_base(cp);

if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp += 2;

while (isxdigit(*cp)) {
unsigned int value;

value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
if (value >= base)
break;
result = result * base + value;
cp++;
}

if (endp)
*endp = (char *)cp;
return result;
}

EXPORT_SYMBOL(simple_strtoul);

/**
Expand All @@ -91,32 +99,28 @@ EXPORT_SYMBOL(simple_strtol);
*/
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) {
base = 10;
if (*cp == '0') {
base = 8;
cp++;
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
cp++;
base = 16;
}
}
} else if (base == 16) {
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp += 2;
}
while (isxdigit(*cp)
&& (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
result = result*base + value;
if (!base)
base = simple_guess_base(cp);

if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp += 2;

while (isxdigit(*cp)) {
unsigned int value;

value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
if (value >= base)
break;
result = result * base + value;
cp++;
}

if (endp)
*endp = (char *)cp;
return result;
}

EXPORT_SYMBOL(simple_strtoull);

/**
Expand Down

0 comments on commit aa46a63

Please sign in to comment.