util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zane Van Iperen <z.vaniperen@uq.edu.au>
To: "util-linux@vger.kernel.org" <util-linux@vger.kernel.org>
Cc: Zane Van Iperen <z.vaniperen@uq.edu.au>
Subject: [PATCH] libuuid: add uuid_parse_range()
Date: Thu, 13 Feb 2020 12:46:47 +0000	[thread overview]
Message-ID: <20200213124615.31804-1-z.vaniperen@uq.edu.au> (raw)

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


                 reply	other threads:[~2020-02-13 12:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200213124615.31804-1-z.vaniperen@uq.edu.au \
    --to=z.vaniperen@uq.edu.au \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).