diff --git a/include/common/standard.h b/include/common/standard.h index cb5a3ec56..71c5b7681 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -271,6 +271,28 @@ static inline unsigned int __strl2uic(const char *s, int len) return i; } +/* This function reads an unsigned integer from the string pointed to by + * and returns it. The pointer is adjusted to point to the first unread + * char. The function automatically stops at . + */ +static inline unsigned int __read_uint(const char **s, const char *end) +{ + const char *ptr = *s; + unsigned int i = 0; + unsigned int j, k; + + while (ptr < end) { + j = *ptr - '0'; + k = i * 10; + if (j > 9) + break; + i = k + j; + ptr++; + } + *s = ptr; + return i; +} + extern unsigned int str2ui(const char *s); extern unsigned int str2uic(const char *s); extern unsigned int strl2ui(const char *s, int len); @@ -278,6 +300,7 @@ extern unsigned int strl2uic(const char *s, int len); extern int strl2ic(const char *s, int len); extern int strl2irc(const char *s, int len, int *ret); extern int strl2llrc(const char *s, int len, long long *ret); +extern unsigned int read_uint(const char **s, const char *end); unsigned int inetaddr_host(const char *text); unsigned int inetaddr_host_lim(const char *text, const char *stop); unsigned int inetaddr_host_lim_ret(const char *text, char *stop, const char **ret); diff --git a/src/standard.c b/src/standard.c index 8aaa594da..017f87fd9 100644 --- a/src/standard.c +++ b/src/standard.c @@ -535,6 +535,11 @@ unsigned int strl2uic(const char *s, int len) return __strl2uic(s, len); } +unsigned int read_uint(const char **s, const char *end) +{ + return __read_uint(s, end); +} + /* This one is 7 times faster than strtol() on athlon with checks. * It returns the value of the number composed of all valid digits read, * and can process negative numbers too.