diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-09-30 08:50:00 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-09-30 08:50:00 +0000 |
commit | 07557e5612723897fee21e62795dabc0ad0925f8 (patch) | |
tree | db740ad336cc177bc4853c8af4c98191563ef40e | |
parent | 2c6e06185d8a97c155f133e62e6d0008196bd8b6 (diff) |
Magnus Holmgren's improved atoi()
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2446 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | firmware/common/atoi.c | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/firmware/common/atoi.c b/firmware/common/atoi.c index 4911d93e92..59887ec9fa 100644 --- a/firmware/common/atoi.c +++ b/firmware/common/atoi.c @@ -17,15 +17,38 @@ * ****************************************************************************/ -#include "string.h" +#include "ctype.h" int atoi (const char *str) { - int val = 0, mlt = 1; - char *p; - p = (char *) (str + strlen(str) - 1); - for (; p >= str; --p, mlt *=10) - val += (mlt * ((int)*p - '0')); - return val; -} + int value = 0; + int sign = 1; + + 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; +} |