From 4ec83cd9398df1dae37f0ba47094f7ec0c84bf46 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 15 Oct 2010 23:19:55 +0200 Subject: [PATCH] [MINOR] standard: add read_uint() to parse a delimited unsigned integer This function parses an integer and returns it along with the pointer to the next char not part of the number. --- include/common/standard.h | 23 +++++++++++++++++++++++ src/standard.c | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index cb5a3ec56b..71c5b76812 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 8aaa594dad..017f87fd9c 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. -- 2.47.2