poke-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] std: Added signed number support to atoi().


From: Jose E. Marchesi
Subject: Re: [PATCH] std: Added signed number support to atoi().
Date: Sat, 04 Dec 2021 19:07:20 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi Jordan.

> For example, it's now possible to get the number -10 as a result of calling
> atoi("-10") or 239 as a result of calling atoi("+ef", 16).
>
> 2021-12-03  Jordan Yelloz  <jordan@yelloz.me>
>
>         * libpoke/std.pk (atoi): Added code that extracts any single leading 
> '+'
>         or '-', decides a polarity of the number to parse, and shifts over the
>         input string by 1 character if necessary before parsing the digits.
>         * testsuite/poke.std/std-test.pk: Added tests for new
>         behaviors.

Thank you very much for the patch.

I have applied it to both master and maint/poke-1 with some minor
formatting modifications, on your behalf.


> ---
>  libpoke/std.pk                 | 23 +++++++++++++++++++++--
>  testsuite/poke.std/std-test.pk | 12 ++++++++++++
>  2 files changed, 33 insertions(+), 2 deletions(-)
>
> diff --git a/libpoke/std.pk b/libpoke/std.pk
> index bd38b7e1b527..178968c9bee5 100644
> --- a/libpoke/std.pk
> +++ b/libpoke/std.pk
> @@ -90,18 +90,37 @@ fun atoi = (string s, int<32> b = 10) int<64>:
>           ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
>        }
>  
> +    fun signchar = (string str) int<8>:
> +      {
> +        if (str'length > 1) return str[0];
> +        return 0;
> +      }
> +
> +    fun signum = (uint<8> c) int<32>:
> +      {
> +        if (c == '-') return -1;
> +        if (c == '+') return +1;
> +        return 0;
> +      }
> +
>      if (!(b in [2, 8, 10, 16]))
>        raise E_inval;
>  
> +    var sign = signum(signchar(s));
> +    if (sign == 0)
> +      sign = +1;
> +    else
> +      s = s[1:];
> +
>      for (c in s)
>      {
>        if (!valid(c, b))
> -        return result;
> +        break;
>  
>        result = result * b + htoi(c);
>      }
>  
> -    return result;
> +    return sign * result;
>    }
>  
>  fun ltos = (int<64> i, uint<32> base = 10) string:
> diff --git a/testsuite/poke.std/std-test.pk b/testsuite/poke.std/std-test.pk
> index 5de5afb65589..8654c07ba496 100644
> --- a/testsuite/poke.std/std-test.pk
> +++ b/testsuite/poke.std/std-test.pk
> @@ -24,19 +24,31 @@ var tests = [
>      func = lambda (string name) void:
>        {
>          assert (atoi ("0", 2) == 0L);
> +        assert (atoi ("+0", 2) == 0L);
> +        assert (atoi ("-0", 2) == 0L);
>          assert (atoi ("1", 2) == 1L);
> +        assert (atoi ("+1", 2) == 1L);
> +        assert (atoi ("-1", 2) == -1L);
>          assert (atoi ("111111111", 2) == 511L);
>          assert (atoi ("x", 2) == 0L);
> +        assert (atoi ("+x", 2) == 0L);
> +        assert (atoi ("-x", 2) == 0L);
>          assert (atoi ("1111x234", 2) == 15L);
>  
>          assert (atoi ("1777", 8) == 1023L);
> +        assert (atoi ("+1777", 8) == 1023L);
> +        assert (atoi ("-1777", 8) == -1023L);
>  
>          assert (atoi ("1777", 10) == 1777L);
>          assert (atoi ("1777") == 1777L);
> +        assert (atoi ("+1777") == 1777L);
> +        assert (atoi ("-1777") == -1777L);
>  
>          assert (atoi ("fce2", 16) == 64738L);
>          assert (atoi ("bEeF", 16) == 48879L);
>          assert (atoi ("deadbeef", 16) == 0xdeadbeef);
> +        assert (atoi ("+deadbeef", 16) == 0xdeadbeef);
> +        assert (atoi ("-deadbeef", 16) == -0xdeadbeef);
>        },
>    },
>    PkTest {



reply via email to

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