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 )
21 type CLabelString = FastString -- A C label, completely unencoded
23 pprCLabelString :: CLabelString -> SDoc
24 pprCLabelString lbl = ftext lbl
26 isCLabelString :: CLabelString -> Bool -- Checks to see if this is a valid C label
28 = all ok (unpackFS lbl)
30 ok c = isAlphaNum c || c == '_' || c == '.'
31 -- The '.' appears in e.g. "foo.so" in the
32 -- module part of a ExtName. Maybe it should be separate
38 pprFSInCStyle :: FastString -> SDoc
39 -- Assumes it contains only characters '\0'..'\xFF'!
40 pprFSInCStyle fs = pprStringInCStyle (unpackFS fs)
42 pprStringInCStyle :: String -> SDoc
43 pprStringInCStyle s = doubleQuotes (text (concatMap charToC s))
45 charToC :: Char -> String
49 charToC c | c >= ' ' && c <= '~' = [c]
50 | c > '\xFF' = panic ("charToC "++show c)
52 chr (ord '0' + ord c `div` 64),
53 chr (ord '0' + ord c `div` 8 `mod` 8),
54 chr (ord '0' + ord c `mod` 8)]