util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libuuid: add uuid_parse_range()
@ 2020-02-13 12:46 Zane Van Iperen
  0 siblings, 0 replies; only message in thread
From: Zane Van Iperen @ 2020-02-13 12:46 UTC (permalink / raw)
  To: util-linux; +Cc: Zane Van Iperen

For compatibility with C++'s std::string_view, et al.

Signed-off-by: Zane van Iperen <z.vaniperen@uq.edu.au>
---
 libuuid/src/parse.c | 23 ++++++++++++++++-------
 libuuid/src/uuid.h  |  1 +
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/libuuid/src/parse.c b/libuuid/src/parse.c
index 074383efa..e4ec44ab3 100644
--- a/libuuid/src/parse.c
+++ b/libuuid/src/parse.c
@@ -40,15 +40,24 @@
 #include "uuidP.h"
 
 int uuid_parse(const char *in, uuid_t uu)
+{
+	size_t len = strlen(in);
+	if (len != 36)
+		return -1;
+
+	return uuid_parse_range(in, in + len, uu);
+}
+
+int uuid_parse_range(const char *in_start, const char *in_end, uuid_t uu)
 {
 	struct uuid	uuid;
 	int 		i;
 	const char	*cp;
 	char		buf[3];
 
-	if (strlen(in) != 36)
+	if ((in_end - in_start) != 36)
 		return -1;
-	for (i=0, cp = in; i <= 36; i++,cp++) {
+	for (i=0, cp = in_start; i <= 36; i++,cp++) {
 		if ((i == 8) || (i == 13) || (i == 18) ||
 		    (i == 23)) {
 			if (*cp == '-')
@@ -62,11 +71,11 @@ int uuid_parse(const char *in, uuid_t uu)
 		if (!isxdigit(*cp))
 			return -1;
 	}
-	uuid.time_low = strtoul(in, NULL, 16);
-	uuid.time_mid = strtoul(in+9, NULL, 16);
-	uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
-	uuid.clock_seq = strtoul(in+19, NULL, 16);
-	cp = in+24;
+	uuid.time_low = strtoul(in_start, NULL, 16);
+	uuid.time_mid = strtoul(in_start+9, NULL, 16);
+	uuid.time_hi_and_version = strtoul(in_start+14, NULL, 16);
+	uuid.clock_seq = strtoul(in_start+19, NULL, 16);
+	cp = in_start+24;
 	buf[2] = 0;
 	for (i=0; i < 6; i++) {
 		buf[0] = *cp++;
diff --git a/libuuid/src/uuid.h b/libuuid/src/uuid.h
index 03c232caa..b90c88836 100644
--- a/libuuid/src/uuid.h
+++ b/libuuid/src/uuid.h
@@ -100,6 +100,7 @@ extern int uuid_is_null(const uuid_t uu);
 
 /* parse.c */
 extern int uuid_parse(const char *in, uuid_t uu);
+extern int uuid_parse_range(const char *in_start, const char *in_end, uuid_t uu);
 
 /* unparse.c */
 extern void uuid_unparse(const uuid_t uu, char *out);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-02-13 12:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-13 12:46 [PATCH] libuuid: add uuid_parse_range() Zane Van Iperen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).