poke-devel
[Top][All Lists]
Advanced

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

[PATCH] pkl: Add special cases for "\t\n\\\"" chars in formater/printer


From: Mohammad-Reza Nabipoor
Subject: [PATCH] pkl: Add special cases for "\t\n\\\"" chars in formater/printer
Date: Thu, 2 Dec 2021 19:19:02 +0330

2021-12-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/pkl-rt-1.pk (_pkl_escape_string): Add special cases for
        tab (\t), new-line ('\n'), double-quote ('"') and backslash ('\\').
        * testsuite/poke.pkl/format-30.pk: New test.
        * testsuite/poke.pkl/format-31.pk: Likewise.
        * testsuite/poke.pkl/format-32.pk: Likewise.
        * testsuite/poke.pkl/format-33.pk: Likewise.
        * testsuite/poke.pkl/format-34.pk: Likewise.
        * testsuite/poke.pkl/format-35.pk: Likewise.
---
 ChangeLog             | 11 +++++++++++
 libpoke/pkl-rt-1.pk   | 27 +++++++++++++++++++++++++--
 testsuite/Makefile.am |  6 ++++++
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 05fa68a8..7e9172f4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2021-12-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/pkl-rt-1.pk (_pkl_escape_string): Add special cases for
+       tab (\t), new-line ('\n'), double-quote ('"') and backslash ('\\').
+       * testsuite/poke.pkl/format-30.pk: New test.
+       * testsuite/poke.pkl/format-31.pk: Likewise.
+       * testsuite/poke.pkl/format-32.pk: Likewise.
+       * testsuite/poke.pkl/format-33.pk: Likewise.
+       * testsuite/poke.pkl/format-34.pk: Likewise.
+       * testsuite/poke.pkl/format-35.pk: Likewise.
+
 2021-12-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * libpoke/pvm.jitter (strace): Re-implement using
diff --git a/libpoke/pkl-rt-1.pk b/libpoke/pkl-rt-1.pk
index 6dd8f34d..dcf90307 100644
--- a/libpoke/pkl-rt-1.pk
+++ b/libpoke/pkl-rt-1.pk
@@ -310,13 +310,36 @@ fun _pkl_escape_string = (string str) string:
 
     /* Calculate the length of escaped string.  */
     for (var i = 0UL; i < len; ++i)
-      len_esc += str[i] - 0x20UB < 0x5fUB /* is printable */ ? 1 : 4 /* \xXX 
*/;
+      len_esc +=
+        str[i] == '\t' || str[i] == '\n' || str[i] == '"' || str[i] == '\\' ? 2
+          : 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)
+        if (str[i] == '\t')
+          {
+            __pkl_unsafe_string_set (esc, j, "\\t");
+            j += 2;
+          }
+        else if (str[i] == '\n')
+          {
+            __pkl_unsafe_string_set (esc, j, "\\n");
+            j += 2;
+          }
+        else if (str[i] == '"')
+          {
+            __pkl_unsafe_string_set (esc, j, "\\\"");
+            j += 2;
+          }
+        else if (str[i] == '\\')
+          {
+            __pkl_unsafe_string_set (esc, j, "\\\\");
+            j += 2;
+          }
+        else if (str[i] - 0x20UB < 0x5fUB)
           {
             __pkl_unsafe_string_set (esc, j, str[i] as string);
             ++j;
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index bffe6ab0..4a890233 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -1058,6 +1058,12 @@ EXTRA_DIST = \
   poke.pkl/format-27.pk \
   poke.pkl/format-28.pk \
   poke.pkl/format-29.pk \
+  poke.pkl/format-30.pk \
+  poke.pkl/format-31.pk \
+  poke.pkl/format-32.pk \
+  poke.pkl/format-33.pk \
+  poke.pkl/format-34.pk \
+  poke.pkl/format-35.pk \
   poke.pkl/format-diag-1.pk \
   poke.pkl/formfeedchar.pk \
   poke.pkl/fun-types-1.pk \
-- 
2.34.1




reply via email to

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