poke-devel
[Top][All Lists]
Advanced

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

Re: [PATCH 08/12] libpoke: Fix printf %v for strings to give valid liter


From: Jose E. Marchesi
Subject: Re: [PATCH 08/12] libpoke: Fix printf %v for strings to give valid literals
Date: Thu, 27 May 2021 14:04:46 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi Mohammad.

> diff --git a/libpoke/pkl-gen.pks b/libpoke/pkl-gen.pks
> index 431b0ef7..70f69485 100644
> --- a/libpoke/pkl-gen.pks
> +++ b/libpoke/pkl-gen.pks
> @@ -1893,6 +1893,7 @@
>          begsc
>          push "\""
>          prints
> +        .call _pkl_escape_string; VAL
>          prints
>          push "\""
>          prints
> diff --git a/libpoke/pkl-rt.pk b/libpoke/pkl-rt.pk
> index 033636e0..f8aa32b8 100644
> --- a/libpoke/pkl-rt.pk
> +++ b/libpoke/pkl-rt.pk
> @@ -251,6 +251,40 @@ fun _pkl_base_prefix = (int<32> base) string:
>     return "";
>   }
>  
> +/* Return a new string in which all the non-printable characters are replaced
> +   with a escape sequence (\xXX).  */
> +
> +fun _pkl_escape_string = (string str) string:
> +  {
> +    var chars = "0123456789abcdef",
> +        len = str'length,
> +        len_esc = 0;
> +
> +    /* Calculate the length of escaped string.  */
> +    for (var i = 0UL; i < len; ++i)
> +      len_esc += str[i] - 0x20UB < 0x5fUB /* is printable */ ? 1 : 4 /* \xXX 
> */;
> +
> +    var esc = "?" * len_esc;
> +
> +    for (var i = 0UL, j = 0UL; i < len; ++i)
> +      {
> +        if (str[i] - 0x20UB < 0x5fUB)
> +          {
> +            __pkl_unsafe_string_set (esc, j, str[i] as string);
> +            ++j;
> +          }
> +        else
> +          {
> +            var nib_lo = chars[str[i] & 0xfUB] as string,
> +                nib_hi = chars[(str[i] .>> 4) & 0xfUB] as string;
> +
> +            __pkl_unsafe_string_set (esc, j, "\\x" + nib_hi + nib_lo);
> +            j += 4;
> +          }
> +      }
> +    return esc;
> +  }

Very nice :)

OK for both master and maint/poke-1 (I think we really should backport
the strset instruction and __pkl_unsafe_string_set to poke 1.x).



reply via email to

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