changeset 671:f3c75c3d7a04

int: allow str2uXX_full consumers to specify the expected terminator char In general, we care about nul-termination but we can make it more flexible for those willing to use the full API. Note that since these are changes in macros and static inlines, this should have zero overhead for the common case, and either negligible or zero overhead for the advanced cases where other terminators are required. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 21 Feb 2019 18:44:13 -0500
parents e114cae635b0
children 0dd0932ec440
files include/jeffpc/int.h
diffstat 1 files changed, 12 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/include/jeffpc/int.h	Thu Feb 21 18:39:07 2019 -0500
+++ b/include/jeffpc/int.h	Thu Feb 21 18:44:13 2019 -0500
@@ -38,7 +38,8 @@
 #define STR_TO_INT(size, imax)						\
 static inline int str2u##size##_full(const char *restrict s,		\
 				     uint##size##_t *i,			\
-				     int base)				\
+				     int base,				\
+				     char terminator)			\
 {									\
 	char *endptr;							\
 	uint64_t tmp;							\
@@ -55,8 +56,8 @@
 	if (endptr == s)						\
 		return -EINVAL;						\
 									\
-	/* nul-terminated? */						\
-	if (*endptr != '\0')						\
+	/* first unparsed char must be the expected terminator */	\
+	if (*endptr != (terminator))					\
 		return -EINVAL;						\
 									\
 	if (tmp > imax)							\
@@ -72,15 +73,15 @@
 
 #undef STR_TO_INT
 
-/* base [2, 36] */
-#define str2u64_base(s, i, b)	str2u64_full((s), (i), (b))
-#define str2u32_base(s, i, b)	str2u32_full((s), (i), (b))
-#define str2u16_base(s, i, b)	str2u16_full((s), (i), (b))
+/* base [2, 36], nul-terminated */
+#define str2u64_base(s, i, b)	str2u64_full((s), (i), (b), '\0')
+#define str2u32_base(s, i, b)	str2u32_full((s), (i), (b), '\0')
+#define str2u16_base(s, i, b)	str2u16_full((s), (i), (b), '\0')
 
-/* base 10 */
-#define str2u64(s, i)	str2u64_full((s), (i), 10)
-#define str2u32(s, i)	str2u32_full((s), (i), 10)
-#define str2u16(s, i)	str2u16_full((s), (i), 10)
+/* base 10, nul-terminated */
+#define str2u64(s, i)	str2u64_full((s), (i), 10, '\0')
+#define str2u32(s, i)	str2u32_full((s), (i), 10, '\0')
+#define str2u16(s, i)	str2u16_full((s), (i), 10, '\0')
 
 /*
  * These prototypes exist to catch bugs in the code generating macros below.