Skip to content

Commit

Permalink
[#334] escaping unicode character as well as regular characters
Browse files Browse the repository at this point in the history
  • Loading branch information
dariodsa committed Dec 8, 2020
1 parent 0e81ac8 commit 7587a21
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Toml/Type/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,23 @@ prettyKeyValue options i = mapOrdered (\kv -> [kvText kv]) options . HashMap.toL
showText = Text.pack . show

showTextUnicode :: Text -> Text
showTextUnicode text = Text.pack $ show $ foldl (\acc (ch, asciiCh) -> acc ++ getCh ch asciiCh) "" asciiArr
showTextUnicode text = Text.pack quotedText
where
xs = Text.unpack text
asciiArr = zip xs $ asciiStatus xs
quotedText = finalText
finalText = foldl (\acc (ch, asciiCh) -> acc ++ getCh ch asciiCh) "" asciiArr
xss = unicodeEscape $ show $ Text.unpack text
asciiArr = zip xss $ asciiStatus xss
getCh ch True = [ch]
getCh ch False = printf "\\U%08x" ordChr :: String
getCh ch False = printf "\\\\U%08x" ordChr :: String
where
ordChr = ord ch
asciiStatus = map isAscii

unicodeEscape "" = ""
unicodeEscape ('\\':'\\':'u':xs) = '\\':'\\':'u': unicodeEscape xs
unicodeEscape ('\\':'\\':'U':xs) = '\\':'\\':'U': unicodeEscape xs
unicodeEscape (x:xs) = x : unicodeEscape xs

showDouble :: Double -> Text
showDouble d | isInfinite d && d < 0 = "-inf"
| isInfinite d = "inf"
Expand Down

0 comments on commit 7587a21

Please sign in to comment.