1
0
Fork 0
forked from len0rd/rockbox

Magnus Holmgren's improved atoi()

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2446 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Daniel Stenberg 2002-09-30 08:50:00 +00:00
parent 2c6e06185d
commit 07557e5612

View file

@ -17,15 +17,38 @@
* *
****************************************************************************/ ****************************************************************************/
#include "string.h" #include "ctype.h"
int atoi (const char *str) int atoi (const char *str)
{ {
int val = 0, mlt = 1; int value = 0;
char *p; int sign = 1;
p = (char *) (str + strlen(str) - 1);
for (; p >= str; --p, mlt *=10) while (isspace(*str))
val += (mlt * ((int)*p - '0')); {
return val; str++;
} }
if ('-' == *str)
{
sign = -1;
str++;
}
else if ('+' == *str)
{
str++;
}
while ('0' == *str)
{
str++;
}
while (isdigit(*str))
{
value = (value * 10) + (*str - '0');
str++;
}
return value * sign;
}