shithub: MicroHs

Download patch

ref: 86275df67d7a36f429d28fa9f12ae7fb7f52f755
parent: 3ba552fd05c696349c6e43bfcc59953395400d40
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Mon Feb 5 15:50:16 EST 2024

Fix Show Char quotation bug.

--- a/lib/Data/Char.hs
+++ b/lib/Data/Char.hs
@@ -75,6 +75,9 @@
 isSpace :: Char -> Bool
 isSpace c = c == ' ' || c == '\t' || c == '\n'
 
+isAscii :: Char -> Bool
+isAscii c = c <= '\127'
+
 digitToInt :: Char -> Int
 digitToInt c | (primCharLE '0' c) && (primCharLE c '9') = ord c - ord '0'
              | (primCharLE 'a' c) && (primCharLE c 'f') = ord c - (ord 'a' - 10)
@@ -95,19 +98,23 @@
 
 instance Show Char where
   showsPrec _ '\'' = showString "'\\''"
-  showsPrec _ c = showChar '\'' . showString (encodeChar c) . showChar '\''
+  showsPrec _ c = showChar '\'' . showString (encodeChar c "") . showChar '\''
   showList    s = showChar '"'  . f s
     where f [] = showChar '"'
           f (c:cs) =
             if c == '"' then showString "\\\"" . f cs
-            else showString (encodeChar c) . f cs
+            else showString (encodeChar c cs) . f cs
 
 -- XXX should not export this
-encodeChar :: Char -> String
-encodeChar c =
+encodeChar :: Char -> String -> String
+encodeChar c rest =
   let
-    spec = [('\n', "\\n"), ('\r', "\\r"), ('\t', "\\t"), ('\b', "\\b"),
-            ('\\', "\\\\")]
-    look [] = if isPrint c then [c] else "\\" ++ show (ord c)
+    needProtect =
+      case rest of
+        [] -> False
+        c : _ -> isDigit c
+    spec = [('\a',"\\a"), ('\b', "\\b"), ('\f', "\\f"), ('\n', "\\n"),
+            ('\r', "\\r"), ('\t', "\\t"), ('\v', "\\v"), ('\\', "\\\\")]
+    look [] = if isPrint c then [c] else "\\" ++ show (ord c) ++ if needProtect then "\\&" else ""
     look ((d,s):xs) = if d == c then s else look xs
   in look spec
--- a/tests/Read.hs
+++ b/tests/Read.hs
@@ -55,4 +55,4 @@
   print (read "'\x4'" :: Char)
   print (read "\"abc\"" :: String)
   print (read "\"a\nc\"" :: String)
-  print (read "\"a\1\&c\"" :: String)
+  print (read "\"a\1\&1c\"" :: String)
--