diff --git a/golden/data.qbe b/golden/data.qbe index 30dff5e..8c63208 100644 --- a/golden/data.qbe +++ b/golden/data.qbe @@ -1,3 +1,3 @@ export data $d = align 8 -{z 16, b $g + 32 "foo\nbar\NULbaz" -1} \ No newline at end of file +{z 16, b $g + 32 "foo\012bar\000baz" -1} \ No newline at end of file diff --git a/src/Language/QBE.hs b/src/Language/QBE.hs index 13b1c44..e6b8d98 100644 --- a/src/Language/QBE.hs +++ b/src/Language/QBE.hs @@ -85,17 +85,21 @@ import Data.Text (Text) import Data.Text.Short (ShortText) import qualified Data.Text.Short as TS import Data.ByteString (ByteString) +import qualified Data.ByteString as BS +import Data.ByteString.Internal (w2c) import Data.Word (Word64) import Data.List.NonEmpty (NonEmpty, toList) import Data.Maybe (maybeToList) import Prettyprinter ( Pretty(pretty), Doc, (<+>), vsep, hsep, hang, punctuate, group, flatAlt - , space, encloseSep, tupled, comma, equals, braces, lbrace, rbrace ) + , space, encloseSep, tupled, comma, equals, braces, lbrace, rbrace, enclose ) -- Instances import Data.Hashable (Hashable) import Control.DeepSeq (NFData) import Data.String (IsString) import Data.Data (Data) +import Numeric (showOct) +import Data.Char (isPrint, isAscii) -- * Identifiers ---------------- @@ -262,7 +266,17 @@ data DataItem instance Pretty DataItem where pretty (Symbol ident alignment) = hsep $ pretty ident : maybeToList ((pretty '+' <+>) . pretty <$> alignment) - pretty (String bs) = pretty $ show bs -- HACK: hoping that the escape sequences are the same... + -- ~~HACK: hoping that the escape sequences are the same...~~ + -- ↑ wrong bitch (it's undocumented; i don't blame you babe.) + pretty (String bs) = + enclose "\"" "\"" . pretty . showHexSequences . BS.unpack $ bs + where + showHexSequences = + foldr (\a acc -> char a <> acc) "" + char c + | isAscii (w2c c) && isPrint (w2c c) = [w2c c] + | otherwise = "\\" <> pad 3 (showOct c "") + pad n s = replicate (max 0 (n - length s)) '0' <> s pretty (Const c) = pretty c data Field diff --git a/test/Main.hs b/test/Main.hs index 444460d..096abc0 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -6,8 +6,9 @@ module Main (main) where import Language.QBE -import Test.Tasty (TestTree, defaultMain, testGroup) +import Test.Tasty (TestTree, testGroup) import Test.Tasty.Silver (goldenVsAction) +import Test.Tasty.Silver.Interactive (defaultMain) import System.FilePath ((), (<.>)) import Prettyprinter (Pretty(pretty), layoutPretty, defaultLayoutOptions) import Prettyprinter.Render.Text (renderStrict)