[m-rev.] for review: format uints directly

Zoltan Somogyi zoltan.somogyi at runbox.com
Fri Nov 20 09:15:37 AEDT 2020


2020-11-19 22:17 GMT+11:00 "Julien Fischer" <jfischer at opturion.com>:
> +        (
> +            Base = base_octal,
> +            UIntStr = uint_to_octal_string(UInt)
> +        ;
> +            Base = base_decimal,
> +            UIntStr = uint_to_string(UInt)
> +        ;
> +            ( Base = base_hex_lc
> +            ; Base = base_hex_p
> +            ),
> +            UIntStr = uint_to_hex_string(UInt)
> +        ;
> +            Base = base_hex_uc,
> +            UIntStr = string.to_upper(uint_to_hex_string(UInt))
>          )

This is not a new issue, but this diff is probably the right time to address
this. Instead of constructing a hex string with lower case a-f and then
upcasing to A-F, we should just call a variant of uint_to_hex_string
that generates A-F in the first place. Call it something like uint_to_uc_hex_string.

> @@ -5556,6 +5565,56 @@ int_to_base_string_group_2(NegN, Base, Curr, GroupLength, Sep, Str) :-
>      Str = java.lang.Long.toString(U & 0xffffffffL);
>  ").
> 
> +:- pragma foreign_proc("C",
> +    uint_to_hex_string(U::in) = (Str::uo),
> +    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
> +        does_not_affect_liveness, no_sharing],
> +"
> +    char buffer[21];
> +    sprintf(buffer, ""%"" MR_INTEGER_LENGTH_MODIFIER ""x"", U);
> +    MR_allocate_aligned_string_msg(Str, strlen(buffer), MR_ALLOC_ID);
> +    strcpy(Str, buffer);
> +").

Here, and the octal version below, please add a comment about why 21 chars
will always be enough space. In fact, for octal, I don't think it will be enough :-(

> diff --git a/tests/hard_coded/uint_string_conv.exp b/tests/hard_coded/uint_string_conv.exp
> index e69de29..d29865e 100644
> --- a/tests/hard_coded/uint_string_conv.exp
> +++ b/tests/hard_coded/uint_string_conv.exp
> @@ -0,0 +1,28 @@
> +Decimal                Octal                    Hex +0                      0                        0 +1                      1                        1 +2                      2                        2 +3                      3                        3 +4                      4                        4 +7                      7                        7 +8                      10                       8 +9                      11                       9 +10                     12                       a +11                     13                       b +12                     14                       c +13                     15                       d +14                     16                       e +15                     17                       f +16                     20                       10 +32                     40                       20 +64                     100                      40 +127                    177                      7f +128                    200                      80 +255                    377                      ff +256                    400                      100 +32767                  77777                    7fff +65535                  177777                   ffff +2147483647             17777777777              7fffffff +4294967295             37777777777              ffffffff +4294967295             37777777777              ffffffff +
> diff --git a/tests/hard_coded/uint_string_conv.exp2 b/tests/hard_coded/uint_string_conv.exp2
> index e69de29..1f7e451 100644
> --- a/tests/hard_coded/uint_string_conv.exp2
> +++ b/tests/hard_coded/uint_string_conv.exp2
> @@ -0,0 +1,28 @@
> +Decimal                Octal                    Hex +0                      0                        0 +1                      1                        1 +2                      2                        2 +3                      3                        3 +4                      4                        4 +7                      7                        7 +8                      10                       8 +9                      11                       9 +10                     12                       a +11                     13                       b +12                     14                       c +13                     15                       d +14                     16                       e +15                     17                       f +16                     20                       10 +32                     40                       20 +64                     100                      40 +127                    177                      7f +128                    200                      80 +255                    377                      ff +256                    400                      100 +32767                  77777                    7fff +65535                  177777                   ffff +2147483647             17777777777              7fffffff +4294967295             37777777777              ffffffff +18446744073709551615   1777777777777777777777   ffffffffffffffff +

Both the email readers I use make a hash of these files, so I will check them
post-commit.

Apart from the issues above, the diff is fine.

Zoltan.


More information about the reviews mailing list