[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: |
Mohammad-Reza Nabipoor |
Subject: |
Re: [PATCH 08/12] libpoke: Fix printf %v for strings to give valid literals |
Date: |
Sun, 30 May 2021 02:15:19 +0430 |
On Thu, May 27, 2021 at 02:04:46PM +0200, Jose E. Marchesi wrote:
>
> 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).
>
I did backport it.
Pushed.
Thanks.
- Re: [PATCH 05/12] libpoke/pkl-rt.pk: Fix typo s/gigi/gibi/, (continued)
- [PATCH 06/12] libpoke: Fix trivial errors, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 07/12] libpoke: Add `__pkl_unsafe_string_set` builtin, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 08/12] libpoke: Fix printf %v for strings to give valid literals, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 09/12] libpoke: Fix a bug and add assertions to pvm_make_*, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 10/12] libpoke: Add skeleton for string properties instructions, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 11/12] libpoke: Add `format`, Mohammad-Reza Nabipoor, 2021/05/25
- Re: [PATCH 11/12] libpoke: Add `format`, Jose E. Marchesi, 2021/05/29