1 This module deals with printing C string literals
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 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
28 stringToC [c] = charToC c
30 -- if we have something "octifiable" in "c", we'd better "octify"
31 -- the rest of the string, too.
32 = if (c < ' ' || c > '~')
33 then (charToC c) ++ (concat (map char_to_C cs))
34 else (charToC c) ++ (stringToC cs)
36 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
38 | c == '\b' = "\\b" -- ToDo: chk some of these...
43 | otherwise = '\\' : (octify (ord c))
45 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
58 else '\\' : (octify (ord c))
60 -- really: charToSimpleHaskell
63 = if (c >= 'a' && c <= 'z')
64 || (c >= 'A' && c <= 'Z')
65 || (c >= '0' && c <= '9')
68 _ -> '\\' : show (ord c)
70 octify :: Int -> String
75 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]