1 This module deals with printing C string literals
5 CLabelString, isCLabelString, pprCLabelString,
9 pprFSInCStyle, pprStringInCStyle
12 #include "HsVersions.h"
14 import Char ( ord, chr, isAlphaNum )
20 type CLabelString = FAST_STRING -- A C label, completely unencoded
22 pprCLabelString lbl = ptext lbl
24 isCLabelString :: CLabelString -> Bool -- Checks to see if this is a valid C label
28 ok c = isAlphaNum c || c == '_' || c == '.'
29 -- The '.' appears in e.g. "foo.so" in the
30 -- module part of a ExtName. Maybe it should be separate
32 cSEP = SLIT("_") -- official C separator
37 pprFSInCStyle :: FAST_STRING -> SDoc
38 -- Assumes it contains only characters '\0'..'\xFF'!
39 pprFSInCStyle fs = pprStringInCStyle (_UNPK_ fs)
41 pprStringInCStyle :: String -> SDoc
42 pprStringInCStyle s = doubleQuotes (text (concatMap charToC s))
44 charToC :: Char -> String
48 charToC c | c >= ' ' && c <= '~' = [c]
49 | c > '\xFF' = panic ("charToC "++show c)
51 chr (ord '0' + ord c `div` 64),
52 chr (ord '0' + ord c `div` 8 `mod` 8),
53 chr (ord '0' + ord c `mod` 8)]