From: Markus Armbruster <armbru@redhat.com>
To: Tao Xu <tao3.xu@intel.com>
Cc: lvivier@redhat.com, thuth@redhat.com, ehabkost@redhat.com,
mst@redhat.com, jonathan.cameron@huawei.com, sw@weilnetz.de,
fan.du@intel.com, mdroth@linux.vnet.ibm.com,
qemu-devel@nongnu.org, jingqi.liu@intel.com, imammedo@redhat.com
Subject: Re: [PATCH v17 03/14] util/cutils: refactor do_strtosz() to support suffixes list
Date: Tue, 26 Nov 2019 11:27:49 +0100 [thread overview]
Message-ID: <87imn77x0a.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20191122074826.1373-4-tao3.xu@intel.com> (Tao Xu's message of "Fri, 22 Nov 2019 15:48:15 +0800")
Tao Xu <tao3.xu@intel.com> writes:
> Add do_strtomul() to convert string according to different suffixes.
>
> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
> Signed-off-by: Tao Xu <tao3.xu@intel.com>
> ---
>
> No changes in v17.
>
> Changes in v15:
> - Add a new patch to refactor do_strtosz() (Eduardo)
> ---
> util/cutils.c | 72 ++++++++++++++++++++++++++++++---------------------
> 1 file changed, 42 insertions(+), 30 deletions(-)
>
> diff --git a/util/cutils.c b/util/cutils.c
> index d94a468954..ffef92338a 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -181,41 +181,37 @@ int fcntl_setfl(int fd, int flag)
> }
> #endif
>
> -static int64_t suffix_mul(char suffix, int64_t unit)
> +static int64_t suffix_mul(const char *suffixes[], int num_suffix,
> + const char *endptr, int *offset, int64_t unit)
This function could really use a contract comment now.
> {
> - switch (qemu_toupper(suffix)) {
> - case 'B':
> - return 1;
> - case 'K':
> - return unit;
> - case 'M':
> - return unit * unit;
> - case 'G':
> - return unit * unit * unit;
> - case 'T':
> - return unit * unit * unit * unit;
> - case 'P':
> - return unit * unit * unit * unit * unit;
> - case 'E':
> - return unit * unit * unit * unit * unit * unit;
> + int i, suffix_len;
> + int64_t mul = 1;
> +
> + for (i = 0; i < num_suffix; i++) {
Aha: @num_suffix is the number of elements in suffixes[].
I'd put a terminating NULL into suffixes[] and dispense with
@num_suffix:
for (i = 0; suffix[i]; i++) {
> + suffix_len = strlen(suffixes[i]);
@suffix_len should be size_t, because that's what strlen() returns.
> + if (g_ascii_strncasecmp(suffixes[i], endptr, suffix_len) == 0) {
@endptr is a terrible name: it points to the *beginning* of the suffix.
> + *offset = suffix_len;
@offset is a terrible name: it provides no clue whatsoever on its
meaning. It's actually for receiving the length of the suffix.
Please replace it by char **end, because that's how the related
functions work.
> + return mul;
> + }
> + mul *= unit;
> }
> +
> return -1;
> }
>
> /*
> - * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
> - * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
> - * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on
> - * other error.
> + * Convert string according to different suffixes. End pointer will be returned
> + * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on other error.
> */
> -static int do_strtosz(const char *nptr, const char **end,
> - const char default_suffix, int64_t unit,
> +static int do_strtomul(const char *nptr, const char **end,
> + const char *suffixes[], int num_suffix,
> + const char *default_suffix, int64_t unit,
> uint64_t *result)
The function comment is barely adequate before your patch: it doesn't
explain the arguments. Your patch adds more arguments, thus more
guesswork for the reader.
Again, I'd put a terminating NULL into suffixes[] and dispense with
@num_suffix.
> {
> int retval;
> const char *endptr;
> - unsigned char c;
> int mul_required = 0;
> + int offset = 0;
> long double val, mul, integral, fraction;
>
> retval = qemu_strtold_finite(nptr, &endptr, &val);
> @@ -226,12 +222,12 @@ static int do_strtosz(const char *nptr, const char **end,
> if (fraction != 0) {
> mul_required = 1;
> }
> - c = *endptr;
> - mul = suffix_mul(c, unit);
> +
> + mul = suffix_mul(suffixes, num_suffix, endptr, &offset, unit);
@endptr points behind the number, i.e. to the suffix, and @offset is the
length of the suffix. Suggest to rename @endptr to @suffix, and replace
@offset by @endptr.
> if (mul >= 0) {
> - endptr++;
> + endptr += offset;
> } else {
> - mul = suffix_mul(default_suffix, unit);
> + mul = suffix_mul(suffixes, num_suffix, default_suffix, &offset, unit);
> assert(mul >= 0);
Please assert "no trailing crap in @default_suffix".
> }
> if (mul == 1 && mul_required) {
> @@ -256,19 +252,35 @@ out:
> return retval;
> }
>
> +/*
> + * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
> + * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
> + * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on
> + * other error.
> + */
> +static int do_strtosz(const char *nptr, const char **end,
> + const char *default_suffix, int64_t unit,
> + uint64_t *result)
> +{
> + static const char *suffixes[] = { "B", "K", "M", "G", "T", "P", "E" };
> +
> + return do_strtomul(nptr, end, suffixes, ARRAY_SIZE(suffixes),
> + default_suffix, unit, result);
> +}
> +
> int qemu_strtosz(const char *nptr, const char **end, uint64_t *result)
> {
> - return do_strtosz(nptr, end, 'B', 1024, result);
> + return do_strtosz(nptr, end, "B", 1024, result);
> }
>
> int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result)
> {
> - return do_strtosz(nptr, end, 'M', 1024, result);
> + return do_strtosz(nptr, end, "M", 1024, result);
> }
>
> int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result)
> {
> - return do_strtosz(nptr, end, 'B', 1000, result);
> + return do_strtosz(nptr, end, "B", 1000, result);
> }
>
> /**
next prev parent reply other threads:[~2019-11-26 10:37 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-22 7:48 [PATCH v17 00/14] Build ACPI Heterogeneous Memory Attribute Table (HMAT) Tao Xu
2019-11-22 7:48 ` [PATCH v17 01/14] util/cutils: Add Add qemu_strtold and qemu_strtold_finite Tao Xu
2019-11-25 1:05 ` Tao Xu
2019-11-26 13:54 ` Markus Armbruster
2019-11-27 4:37 ` Tao Xu
2019-11-27 6:44 ` Markus Armbruster
2019-11-27 7:22 ` Tao Xu
2019-11-25 6:45 ` Markus Armbruster
2019-11-22 7:48 ` [PATCH v17 02/14] util/cutils: Use qemu_strtold_finite to parse size Tao Xu
2019-11-25 6:56 ` Markus Armbruster
2019-11-26 8:31 ` Tao Xu
2019-11-26 9:33 ` Markus Armbruster
2019-11-22 7:48 ` [PATCH v17 03/14] util/cutils: refactor do_strtosz() to support suffixes list Tao Xu
2019-11-25 7:20 ` Markus Armbruster
2019-11-25 12:15 ` Eduardo Habkost
2019-11-26 10:04 ` Markus Armbruster
2019-11-26 10:33 ` Daniel P. Berrangé
2019-11-26 12:37 ` Markus Armbruster
2019-11-26 15:46 ` Eduardo Habkost
2019-11-26 10:27 ` Markus Armbruster [this message]
2019-11-22 7:48 ` [PATCH v17 04/14] util/cutils: Add qemu_strtotime_ns() Tao Xu
2019-11-22 7:48 ` [PATCH v17 05/14] qapi: Add builtin type time Tao Xu
2019-11-25 8:04 ` Markus Armbruster
2019-11-22 7:48 ` [PATCH v17 06/14] tests: Add test for QAPI " Tao Xu
2019-11-25 9:08 ` Markus Armbruster
2019-11-22 7:48 ` [PATCH v17 07/14] numa: Extend CLI to provide initiator information for numa nodes Tao Xu
2019-11-22 7:48 ` [PATCH v17 08/14] numa: Extend CLI to provide memory latency and bandwidth information Tao Xu
2019-11-22 7:48 ` [PATCH v17 09/14] numa: Extend CLI to provide memory side cache information Tao Xu
2019-11-22 12:21 ` Igor Mammedov
2019-11-22 7:48 ` [PATCH v17 10/14] hmat acpi: Build Memory Proximity Domain Attributes Structure(s) Tao Xu
2019-11-22 7:48 ` [PATCH v17 11/14] hmat acpi: Build System Locality Latency and Bandwidth Information Structure(s) Tao Xu
2019-11-22 7:48 ` [PATCH v17 12/14] hmat acpi: Build Memory Side Cache " Tao Xu
2019-11-22 12:32 ` Igor Mammedov
2019-11-25 1:10 ` Tao Xu
2019-11-22 7:48 ` [PATCH v17 13/14] tests/numa: Add case for QMP build HMAT Tao Xu
2019-11-22 12:45 ` Igor Mammedov
2019-11-22 7:48 ` [PATCH v17 14/14] tests/bios-tables-test: add test cases for ACPI HMAT Tao Xu
2019-11-22 8:41 ` [PATCH v17 00/14] Build ACPI Heterogeneous Memory Attribute Table (HMAT) no-reply
2019-11-22 9:17 ` no-reply
2019-11-22 12:38 ` Igor Mammedov
2019-11-25 1:08 ` Tao Xu
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=87imn77x0a.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=ehabkost@redhat.com \
--cc=fan.du@intel.com \
--cc=imammedo@redhat.com \
--cc=jingqi.liu@intel.com \
--cc=jonathan.cameron@huawei.com \
--cc=lvivier@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=tao3.xu@intel.com \
--cc=thuth@redhat.com \
/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 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.