X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FabsCSyn%2FCStrings.lhs;h=f25e6c204f04f70677fcee659e25b346a918744c;hb=f6c31c0fbf77b1c46c508fa56214361671ccfd05;hp=628b54056851850e09030360e219e73ee9515daa;hpb=111cee3f1ad93816cb828e38b38521d85c3bcebb;p=ghc-hetmet.git diff --git a/ghc/compiler/absCSyn/CStrings.lhs b/ghc/compiler/absCSyn/CStrings.lhs index 628b540..f25e6c2 100644 --- a/ghc/compiler/absCSyn/CStrings.lhs +++ b/ghc/compiler/absCSyn/CStrings.lhs @@ -2,92 +2,54 @@ This module deals with printing C string literals \begin{code} module CStrings( - CLabelString, isCLabelString, - cSEP, pp_cSEP, + CLabelString, isCLabelString, pprCLabelString, - stringToC, charToC, pprFSInCStyle, - charToEasyHaskell + pp_cSEP, + + pprFSInCStyle, pprStringInCStyle ) where #include "HsVersions.h" import Char ( ord, chr, isAlphaNum ) +import FastString import Outputable \end{code} \begin{code} -type CLabelString = FAST_STRING -- A C label, completely unencoded +type CLabelString = FastString -- A C label, completely unencoded + +pprCLabelString :: CLabelString -> SDoc +pprCLabelString lbl = ftext lbl isCLabelString :: CLabelString -> Bool -- Checks to see if this is a valid C label isCLabelString lbl - = all ok (_UNPK_ lbl) + = all ok (unpackFS lbl) where ok c = isAlphaNum c || c == '_' || c == '.' -- The '.' appears in e.g. "foo.so" in the -- module part of a ExtName. Maybe it should be separate -cSEP = SLIT("_") -- official C separator pp_cSEP = char '_' \end{code} \begin{code} -pprFSInCStyle :: FAST_STRING -> SDoc -pprFSInCStyle fs = doubleQuotes (text (stringToC (_UNPK_ fs))) +pprFSInCStyle :: FastString -> SDoc +-- Assumes it contains only characters '\0'..'\xFF'! +pprFSInCStyle fs = pprStringInCStyle (unpackFS fs) -stringToC :: String -> String --- Convert a string to the form required by C in a C literal string --- Tthe hassle is what to do w/ strings like "ESC 0"... -stringToC "" = "" -stringToC [c] = charToC c -stringToC (c:cs) - -- if we have something "octifiable" in "c", we'd better "octify" - -- the rest of the string, too. - = if (c < ' ' || c > '~') - then (charToC c) ++ (concat (map char_to_C cs)) - else (charToC c) ++ (stringToC cs) - where - char_to_C c | c == '\n' = "\\n" -- use C escapes when we can - | c == '\a' = "\\a" - | c == '\b' = "\\b" -- ToDo: chk some of these... - | c == '\r' = "\\r" - | c == '\t' = "\\t" - | c == '\f' = "\\f" - | c == '\v' = "\\v" - | otherwise = '\\' : (octify (ord c)) +pprStringInCStyle :: String -> SDoc +pprStringInCStyle s = doubleQuotes (text (concatMap charToC s)) charToC :: Char -> String --- Convert a character to the form reqd in a C character literal -charToC c = if (c >= ' ' && c <= '~') -- non-portable... - then case c of - '\'' -> "\\'" - '\\' -> "\\\\" - '"' -> "\\\"" - '\n' -> "\\n" - '\a' -> "\\a" - '\b' -> "\\b" - '\r' -> "\\r" - '\t' -> "\\t" - '\f' -> "\\f" - '\v' -> "\\v" - _ -> [c] - else '\\' : (octify (ord c)) - -charToEasyHaskell :: Char -> String --- Convert a character to the form reqd in a Haskell character literal -charToEasyHaskell c - = if (c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9') - then [c] - else case c of - _ -> '\\' : show (ord c) - -octify :: Int -> String -octify n - = if n < 8 then - [chr (n + ord '0')] - else - octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')] +charToC '\"' = "\\\"" +charToC '\'' = "\\\'" +charToC '\\' = "\\\\" +charToC c | c >= ' ' && c <= '~' = [c] + | c > '\xFF' = panic ("charToC "++show c) + | otherwise = ['\\', + chr (ord '0' + ord c `div` 64), + chr (ord '0' + ord c `div` 8 `mod` 8), + chr (ord '0' + ord c `mod` 8)] \end{code} -