formatContainsVar,
decode_c,
encode_c,
+ encode_c',
prop_encode_c_decode_c_roundtrip
) where
where
expand (Const s) = s
expand (Var name j esc)
- | esc = justify j $ encode_c_strict $ getvar name
+ | esc = justify j $ encode_c' isSpace $ getvar name
| otherwise = justify j $ getvar name
getvar name = fromMaybe "" $ M.lookup name vars
justify UnJustified s = s
encode_c :: String -> FormatString
encode_c = encode_c' (const False)
-{- Encodes more strictly, including whitespace. -}
-encode_c_strict :: String -> FormatString
-encode_c_strict = encode_c' isSpace
-
+{- Encodes special characters, as well as any matching the predicate. -}
encode_c' :: (Char -> Bool) -> String -> FormatString
encode_c' p = concatMap echar
where
| ord c < 0x20 = e_asc c -- low ascii
| ord c >= 256 = e_utf c -- unicode
| ord c > 0x7E = e_asc c -- high ascii
- | p c = e_asc c -- unprintable ascii
- | otherwise = [c] -- printable ascii
+ | p c = e_asc c
+ | otherwise = [c]
-- unicode character is decomposed to individual Word8s,
-- and each is shown in octal
e_utf c = showoctal =<< (Codec.Binary.UTF8.String.encode [c] :: [Word8])