qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v17 01/14] util/cutils: Add Add qemu_strtold and qemu_strtold


From: Markus Armbruster
Subject: Re: [PATCH v17 01/14] util/cutils: Add Add qemu_strtold and qemu_strtold_finite
Date: Mon, 25 Nov 2019 07:45:38 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux)

Tao Xu <address@hidden> writes:

> Work like qemu_strtod() and qemu_strtold_finite, except store long
> double.
>
> Signed-off-by: Tao Xu <address@hidden>
> ---
>
> No changes in v17.
> ---
>  include/qemu/cutils.h |  3 +++
>  util/cutils.c         | 48 ++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 50 insertions(+), 1 deletion(-)
>
> diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
> index b54c847e0f..48cf9bf776 100644
> --- a/include/qemu/cutils.h
> +++ b/include/qemu/cutils.h
> @@ -146,6 +146,9 @@ int qemu_strtou64(const char *nptr, const char **endptr, 
> int base,
>                    uint64_t *result);
>  int qemu_strtod(const char *nptr, const char **endptr, double *result);
>  int qemu_strtod_finite(const char *nptr, const char **endptr, double 
> *result);
> +int qemu_strtold(const char *nptr, const char **endptr, long double *result);
> +int qemu_strtold_finite(const char *nptr, const char **endptr,
> +                        long double *result);
>  
>  int parse_uint(const char *s, unsigned long long *value, char **endptr,
>                 int base);
> diff --git a/util/cutils.c b/util/cutils.c
> index fd591cadf0..5db3b2add5 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -553,7 +553,7 @@ int qemu_strtou64(const char *nptr, const char **endptr, 
> int base,
>  
>  /**
>   * Convert string @nptr to a double.
> -  *
> + *
>   * This is a wrapper around strtod() that is harder to misuse.
>   * Semantics of @nptr and @endptr match strtod() with differences
>   * noted below.
> @@ -616,6 +616,52 @@ int qemu_strtod_finite(const char *nptr, const char 
> **endptr, double *result)
>      return ret;
>  }
>  
> +/*
> + * Convert string @nptr to a long double.
> + *
> + * Works like qemu_strtod(), except it stores long double.

"except it stores long double" feels redundant.

In similar comments elsewhere in this file, we spell out the different
overflow behavior.  Let's do the same here, and replace the redundant
part by "except it stores +/-HUGE_VALL on overflow".

> + */
> +int qemu_strtold(const char *nptr, const char **endptr, long double *result)
> +{
> +    char *ep;
> +
> +    if (!nptr) {
> +        if (endptr) {
> +            *endptr = nptr;
> +        }
> +        return -EINVAL;
> +    }
> +
> +    errno = 0;
> +    *result = strtold(nptr, &ep);
> +    return check_strtox_error(nptr, ep, endptr, errno);
> +}
> +
> +/*
> + * Convert string @nptr to a finite long double.
> + *
> + * Works like qemu_strtod_finite(), except it stores long double.
> + */

Likewise.

> +int qemu_strtold_finite(const char *nptr, const char **endptr,
> +                        long double *result)
> +{
> +    long double tmp;
> +    int ret;
> +
> +    ret = qemu_strtold(nptr, endptr, &tmp);
> +    if (!ret && !isfinite(tmp)) {
> +        if (endptr) {
> +            *endptr = nptr;
> +        }
> +        ret = -EINVAL;
> +    }
> +
> +    if (ret != -EINVAL) {
> +        *result = tmp;
> +    }
> +    return ret;
> +}
> +
>  /**
>   * Searches for the first occurrence of 'c' in 's', and returns a pointer
>   * to the trailing null byte if none was found.

Preferably with the comments tweaked:
Reviewed-by: Markus Armbruster <address@hidden>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]