fix: escape characters as octal sequences
All checks were successful
build / build (push) Successful in 44s

This commit is contained in:
2026-05-18 10:00:05 -06:00
parent ab7cc053a4
commit 64be009635
3 changed files with 19 additions and 4 deletions

View File

@@ -1,3 +1,3 @@
export
data $d = align 8
{z 16, b $g + 32 "foo\nbar\NULbaz" -1}
{z 16, b $g + 32 "foo\012bar\000baz" -1}

View File

@@ -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

View File

@@ -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)