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:
parent
2c6e06185d
commit
07557e5612
1 changed files with 31 additions and 8 deletions
|
|
@ -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)
|
|
||||||
val += (mlt * ((int)*p - '0'));
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
while (isspace(*str))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue