1 This module deals with printing C string literals
7 stringToC, charToC, pprFSInCStyle,
11 #include "HsVersions.h"
13 import Char ( ord, chr )
19 cSEP = SLIT("_") -- official C separator
22 stringToC :: String -> String
23 charToC, charToEasyHaskell :: Char -> String
25 pprFSInCStyle :: FAST_STRING -> SDoc
26 pprFSInCStyle fs = doubleQuotes (text (stringToC (_UNPK_ fs)))
28 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
31 stringToC [c] = charToC c
33 -- if we have something "octifiable" in "c", we'd better "octify"
34 -- the rest of the string, too.
35 = if (c < ' ' || c > '~')
36 then (charToC c) ++ (concat (map char_to_C cs))
37 else (charToC c) ++ (stringToC cs)
39 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
41 | c == '\b' = "\\b" -- ToDo: chk some of these...
46 | otherwise = '\\' : (octify (ord c))
48 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
61 else '\\' : (octify (ord c))
63 -- really: charToSimpleHaskell
66 = if (c >= 'a' && c <= 'z')
67 || (c >= 'A' && c <= 'Z')
68 || (c >= '0' && c <= '9')
71 _ -> '\\' : show (ord c)
73 octify :: Int -> String
78 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]