]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] standard: add read_uint() to parse a delimited unsigned integer
authorWilly Tarreau <w@1wt.eu>
Fri, 15 Oct 2010 21:19:55 +0000 (23:19 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 30 Oct 2010 17:04:37 +0000 (19:04 +0200)
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
src/standard.c

index cb5a3ec56bcf37d84d1bccbc550a63353a795339..71c5b768129e39c08c5f44ebca53fbde118dfcef 100644 (file)
@@ -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 <s>
+ * and returns it. The <s> pointer is adjusted to point to the first unread
+ * char. The function automatically stops at <end>.
+ */
+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);
index 8aaa594dad6e8878d9e2063dd4e56865b4a6a135..017f87fd9ce206b75fd5097e015b440ef156ca2e 100644 (file)
@@ -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.