All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] add u64 number parser
@ 2016-07-23  0:23 ` James Smart
  0 siblings, 0 replies; 10+ messages in thread
From: James Smart @ 2016-07-23  0:23 UTC (permalink / raw)
  To: linux-kernel, linux-nvme, linux-scsi


add u64 number parser

Will be used by the nvme-fabrics FC transport in parsing options

Signed-off-by: James Smart <james.smart@broadcom.com>
---
 include/linux/parser.h |  1 +
 lib/parser.c           | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/include/linux/parser.h b/include/linux/parser.h
index 39d5b79..884c1e6 100644
--- a/include/linux/parser.h
+++ b/include/linux/parser.h
@@ -27,6 +27,7 @@ typedef struct {
 
 int match_token(char *, const match_table_t table, substring_t args[]);
 int match_int(substring_t *, int *result);
+int match_u64(substring_t *, u64 *result);
 int match_octal(substring_t *, int *result);
 int match_hex(substring_t *, int *result);
 bool match_wildcard(const char *pattern, const char *str);
diff --git a/lib/parser.c b/lib/parser.c
index b6d1163..3278958 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -152,6 +152,36 @@ static int match_number(substring_t *s, int *result, int base)
 }
 
 /**
+ * match_u64int: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting u64 on success
+ * @base: base to use when converting string
+ *
+ * Description: Given a &substring_t and a base, attempts to parse the substring
+ * as a number in that base. On success, sets @result to the integer represented
+ * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
+ */
+static int match_u64int(substring_t *s, u64 *result, int base)
+{
+	char *buf;
+	int ret;
+	u64 val;
+	size_t len = s->to - s->from;
+
+	buf = kmalloc(len + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, len);
+	buf[len] = '\0';
+
+	ret = kstrtoull(buf, base, &val);
+	if (!ret)
+		*result = val;
+	kfree(buf);
+	return ret;
+}
+
+/**
  * match_int: - scan a decimal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
@@ -167,6 +197,23 @@ int match_int(substring_t *s, int *result)
 EXPORT_SYMBOL(match_int);
 
 /**
+ * match_u64: - scan a decimal representation of a u64 from
+ *                  a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting unsigned long long on success
+ *
+ * Description: Attempts to parse the &substring_t @s as a long decimal
+ * integer. On success, sets @result to the integer represented by the
+ * string and returns 0.
+ * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
+ */
+int match_u64(substring_t *s, u64 *result)
+{
+	return match_u64int(s, result, 0);
+}
+EXPORT_SYMBOL(match_u64);
+
+/**
  * match_octal: - scan an octal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH] add u64 number parser
@ 2016-07-23  0:23 ` James Smart
  0 siblings, 0 replies; 10+ messages in thread
From: James Smart @ 2016-07-23  0:23 UTC (permalink / raw)



add u64 number parser

Will be used by the nvme-fabrics FC transport in parsing options

Signed-off-by: James Smart <james.smart at broadcom.com>
---
 include/linux/parser.h |  1 +
 lib/parser.c           | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/include/linux/parser.h b/include/linux/parser.h
index 39d5b79..884c1e6 100644
--- a/include/linux/parser.h
+++ b/include/linux/parser.h
@@ -27,6 +27,7 @@ typedef struct {
 
 int match_token(char *, const match_table_t table, substring_t args[]);
 int match_int(substring_t *, int *result);
+int match_u64(substring_t *, u64 *result);
 int match_octal(substring_t *, int *result);
 int match_hex(substring_t *, int *result);
 bool match_wildcard(const char *pattern, const char *str);
diff --git a/lib/parser.c b/lib/parser.c
index b6d1163..3278958 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -152,6 +152,36 @@ static int match_number(substring_t *s, int *result, int base)
 }
 
 /**
+ * match_u64int: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting u64 on success
+ * @base: base to use when converting string
+ *
+ * Description: Given a &substring_t and a base, attempts to parse the substring
+ * as a number in that base. On success, sets @result to the integer represented
+ * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
+ */
+static int match_u64int(substring_t *s, u64 *result, int base)
+{
+	char *buf;
+	int ret;
+	u64 val;
+	size_t len = s->to - s->from;
+
+	buf = kmalloc(len + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, len);
+	buf[len] = '\0';
+
+	ret = kstrtoull(buf, base, &val);
+	if (!ret)
+		*result = val;
+	kfree(buf);
+	return ret;
+}
+
+/**
  * match_int: - scan a decimal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
@@ -167,6 +197,23 @@ int match_int(substring_t *s, int *result)
 EXPORT_SYMBOL(match_int);
 
 /**
+ * match_u64: - scan a decimal representation of a u64 from
+ *                  a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting unsigned long long on success
+ *
+ * Description: Attempts to parse the &substring_t @s as a long decimal
+ * integer. On success, sets @result to the integer represented by the
+ * string and returns 0.
+ * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
+ */
+int match_u64(substring_t *s, u64 *result)
+{
+	return match_u64int(s, result, 0);
+}
+EXPORT_SYMBOL(match_u64);
+
+/**
  * match_octal: - scan an octal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] add u64 number parser
  2016-07-23  0:23 ` James Smart
  (?)
@ 2016-07-23  1:32   ` Bart Van Assche
  -1 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2016-07-23  1:32 UTC (permalink / raw)
  To: James Smart, linux-kernel, linux-nvme, linux-scsi

On 07/22/16 17:23, James Smart wrote:
> +	buf = kmalloc(len + 1, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +	memcpy(buf, s->from, len);
> +	buf[len] = '\0';

Hello James,

Have you considered to combine the above kmalloc() and memcpy() calls 
into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?

Bart.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] add u64 number parser
@ 2016-07-23  1:32   ` Bart Van Assche
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2016-07-23  1:32 UTC (permalink / raw)
  To: James Smart, linux-kernel, linux-nvme, linux-scsi

On 07/22/16 17:23, James Smart wrote:
> +	buf = kmalloc(len + 1, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +	memcpy(buf, s->from, len);
> +	buf[len] = '\0';

Hello James,

Have you considered to combine the above kmalloc() and memcpy() calls 
into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?

Bart.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] add u64 number parser
@ 2016-07-23  1:32   ` Bart Van Assche
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2016-07-23  1:32 UTC (permalink / raw)


On 07/22/16 17:23, James Smart wrote:
> +	buf = kmalloc(len + 1, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +	memcpy(buf, s->from, len);
> +	buf[len] = '\0';

Hello James,

Have you considered to combine the above kmalloc() and memcpy() calls 
into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?

Bart.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] add u64 number parser
  2016-07-23  1:32   ` Bart Van Assche
@ 2016-07-23 15:52     ` James Smart
  -1 siblings, 0 replies; 10+ messages in thread
From: James Smart @ 2016-07-23 15:52 UTC (permalink / raw)
  To: Bart Van Assche, linux-kernel, linux-nvme, linux-scsi


On 7/22/2016 6:32 PM, Bart Van Assche wrote:
> On 07/22/16 17:23, James Smart wrote:
>> +    buf = kmalloc(len + 1, GFP_KERNEL);
>> +    if (!buf)
>> +        return -ENOMEM;
>> +    memcpy(buf, s->from, len);
>> +    buf[len] = '\0';
>
> Hello James,
>
> Have you considered to combine the above kmalloc() and memcpy() calls 
> into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?
>
> Bart.
>

No, I followed the example of existing parse functions in the file.

-- james

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] add u64 number parser
@ 2016-07-23 15:52     ` James Smart
  0 siblings, 0 replies; 10+ messages in thread
From: James Smart @ 2016-07-23 15:52 UTC (permalink / raw)



On 7/22/2016 6:32 PM, Bart Van Assche wrote:
> On 07/22/16 17:23, James Smart wrote:
>> +    buf = kmalloc(len + 1, GFP_KERNEL);
>> +    if (!buf)
>> +        return -ENOMEM;
>> +    memcpy(buf, s->from, len);
>> +    buf[len] = '\0';
>
> Hello James,
>
> Have you considered to combine the above kmalloc() and memcpy() calls 
> into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?
>
> Bart.
>

No, I followed the example of existing parse functions in the file.

-- james

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] add u64 number parser
  2016-07-23 15:52     ` James Smart
@ 2016-08-11 21:02       ` Christoph Hellwig
  -1 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2016-08-11 21:02 UTC (permalink / raw)
  To: James Smart; +Cc: Bart Van Assche, linux-kernel, linux-nvme, linux-scsi

On Sat, Jul 23, 2016 at 08:52:18AM -0700, James Smart wrote:
> 
> On 7/22/2016 6:32 PM, Bart Van Assche wrote:
> > On 07/22/16 17:23, James Smart wrote:
> > > +    buf = kmalloc(len + 1, GFP_KERNEL);
> > > +    if (!buf)
> > > +        return -ENOMEM;
> > > +    memcpy(buf, s->from, len);
> > > +    buf[len] = '\0';
> > 
> > Hello James,
> > 
> > Have you considered to combine the above kmalloc() and memcpy() calls
> > into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?
> > 
> > Bart.
> > 
> 
> No, I followed the example of existing parse functions in the file.

The kasprintf would indeed be nicer, but I'm fine with keeping the
existing style for this patch.  Bonus points for sending a follow on
to convert all of them over.

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] add u64 number parser
@ 2016-08-11 21:02       ` Christoph Hellwig
  0 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2016-08-11 21:02 UTC (permalink / raw)


On Sat, Jul 23, 2016@08:52:18AM -0700, James Smart wrote:
> 
> On 7/22/2016 6:32 PM, Bart Van Assche wrote:
> > On 07/22/16 17:23, James Smart wrote:
> > > +    buf = kmalloc(len + 1, GFP_KERNEL);
> > > +    if (!buf)
> > > +        return -ENOMEM;
> > > +    memcpy(buf, s->from, len);
> > > +    buf[len] = '\0';
> > 
> > Hello James,
> > 
> > Have you considered to combine the above kmalloc() and memcpy() calls
> > into a single kasprintf(GFP_KERNEL, "%.*s", len, s->from) call?
> > 
> > Bart.
> > 
> 
> No, I followed the example of existing parse functions in the file.

The kasprintf would indeed be nicer, but I'm fine with keeping the
existing style for this patch.  Bonus points for sending a follow on
to convert all of them over.

Reviewed-by: Christoph Hellwig <hch at lst.de>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] add u64 number parser
@ 2016-07-23 13:12 Alexey Dobriyan
  0 siblings, 0 replies; 10+ messages in thread
From: Alexey Dobriyan @ 2016-07-23 13:12 UTC (permalink / raw)
  To: james.smart; +Cc: linux-kernel

> +	ret = kstrtoull(buf, base, &val);
> +	if (!ret)
> +		*result = val;

Simply pass "result" directly to kstrtoull(). It will not write the result
in case of error.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-08-11 21:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-23  0:23 [PATCH] add u64 number parser James Smart
2016-07-23  0:23 ` James Smart
2016-07-23  1:32 ` Bart Van Assche
2016-07-23  1:32   ` Bart Van Assche
2016-07-23  1:32   ` Bart Van Assche
2016-07-23 15:52   ` James Smart
2016-07-23 15:52     ` James Smart
2016-08-11 21:02     ` Christoph Hellwig
2016-08-11 21:02       ` Christoph Hellwig
2016-07-23 13:12 Alexey Dobriyan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.