dovecot-2.2: lib: strnum - add permissive partial-string integer...
dovecot at dovecot.org
dovecot at dovecot.org
Wed Jul 2 15:23:20 UTC 2014
details: http://hg.dovecot.org/dovecot-2.2/rev/27006dc092d2
changeset: 17554:27006dc092d2
user: Phil Carmody <phil at dovecot.fi>
date: Wed Jul 02 18:21:24 2014 +0300
description:
lib: strnum - add permissive partial-string integer parser
Not all strings we want to parse are already strtok'ed into separate pieces.
Therefore add helpers which will read the integer, and return a pointer
past the parsed integer.
The previous helpers can be considered a special case which just follows up
with a check that the '\0' has been reached.
Showing a preference for const pointers generally, this does not try to
mimic the non-const interface of strto{l,ul,ll,ull}().
Signed-off-by: Phil Carmody <phil at dovecot.fi>
diffstat:
src/lib/strnum.c | 14 +++++++++++---
src/lib/strnum.h | 10 ++++++++--
2 files changed, 19 insertions(+), 5 deletions(-)
diffs (61 lines):
diff -r 59b6893fbf65 -r 27006dc092d2 src/lib/strnum.c
--- a/src/lib/strnum.c Wed Jul 02 18:21:24 2014 +0300
+++ b/src/lib/strnum.c Wed Jul 02 18:21:24 2014 +0300
@@ -82,7 +82,7 @@
return 0;
}
-int str_to_uintmax(const char *str, uintmax_t *num_r)
+int str_parse_uintmax(const char *str, uintmax_t *num_r, const char **endp_r)
{
uintmax_t n = 0;
@@ -98,11 +98,19 @@
}
n = n * 10 + (*str - '0');
}
- if (*str != '\0')
- return -1;
+ if (endp_r != NULL)
+ *endp_r = str;
*num_r = n;
return 0;
}
+int str_to_uintmax(const char *str, uintmax_t *num_r)
+{
+ const char *endp;
+ int ret = str_parse_uintmax(str, num_r, &endp);
+ if ((ret == 0) && (*endp != '\0'))
+ ret = -1;
+ return ret;
+}
int str_to_int(const char *str, int *num_r)
{
diff -r 59b6893fbf65 -r 27006dc092d2 src/lib/strnum.h
--- a/src/lib/strnum.h Wed Jul 02 18:21:24 2014 +0300
+++ b/src/lib/strnum.h Wed Jul 02 18:21:24 2014 +0300
@@ -5,8 +5,13 @@
Stop when `end_char' is found from string. */
bool str_is_numeric(const char *str, char end_char) ATTR_PURE;
-/* str_to_*() functions return 0 if string is valid number in valid range.
- Otherwise -1 is returned and num_r is left untouched */
+/* str_to_*() functions return 0 if string is nothing more than a valid number
+ in valid range. Otherwise -1 is returned and num_r is left untouched
+
+ str_parse_*() helpers do not require the number to be the entire string
+ and pass back the pointer just past a valid parsed integer in endp_r if
+ it is non-NULL. What is written to endp_r in error cases is undefined.
+*/
int str_to_uint(const char *str, unsigned int *num_r) ATTR_WARN_UNUSED_RESULT;
int str_to_ulong(const char *str, unsigned long *num_r) ATTR_WARN_UNUSED_RESULT;
@@ -14,6 +19,7 @@
int str_to_uint32(const char *str, uint32_t *num_r) ATTR_WARN_UNUSED_RESULT;
int str_to_uint64(const char *str, uint64_t *num_r) ATTR_WARN_UNUSED_RESULT;
int str_to_uintmax(const char *str, uintmax_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_parse_uintmax(const char *str, uintmax_t *num_r, const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);
int str_to_int(const char *str, int *num_r) ATTR_WARN_UNUSED_RESULT;
int str_to_long(const char *str, long *num_r) ATTR_WARN_UNUSED_RESULT;
More information about the dovecot-cvs
mailing list