[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2] ntoab: Handle LONG_MIN case.
From: |
Jan Nieuwenhuizen |
Subject: |
Re: [PATCH v2] ntoab: Handle LONG_MIN case. |
Date: |
Mon, 15 Jun 2020 06:51:54 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) |
Danny Milosavljevic writes:
> * lib/mes/ntoab.c (ntoab): Handle LONG_MIN case.
> ---
> lib/mes/ntoab.c | 25 ++++++++++++++++++++++++-
> 1 file changed, 24 insertions(+), 1 deletion(-)
Oh, great!
> diff --git a/lib/mes/ntoab.c b/lib/mes/ntoab.c
> index a83b4d31..25780652 100644
> --- a/lib/mes/ntoab.c
> +++ b/lib/mes/ntoab.c
> @@ -20,6 +20,10 @@
>
> #include <assert.h>
> #include <mes/lib.h>
> +#include <stdint.h>
> +
> +#define STR(x) #x
> +#define XSTR(s) STR(s)
>
> char *
> ntoab (long x, int base, int signed_p)
> @@ -34,7 +38,26 @@ ntoab (long x, int base, int signed_p)
> if (signed_p && x < 0)
> {
> sign_p = 1;
> - u = -x;
> + if (x == LONG_MIN)
> + {
> + /* Cannot do u = (-x), so avoid it. */
> + long i;
> + if (base == 10)
> + return XSTR(LONG_MIN);
> + /* We cause the same result as
> + i = (-x) % base
> + u = (-x) / base
> + would in mathematics if x and base were integers.
> + It will be the case that -base < x <= 0 after the loop.
> + Because base > 1, the quotient will definitely fit into u.
> + i will contain the last digit to print. */
> + for (u = 0; x <= -base; x += base, ++u)
> + ;
> + i = -x;
> + *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
> + }
> + else
> + u = -x;
> }
> else
> u = x;
I like the comments, that's very helpful. Do you think we should have
add a test?
LGTM!
Thanks!
Janneke
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | AvatarĀ® http://AvatarAcademy.com