@@ -9,7 +9,7 @@ module Text (
99 trim , spaces , guessEncoding , dropLastUTF8 ,
1010 readIntM , showInt ,
1111 displayWidth , toMaxWidth , toWidth ,
12- toText , isLineSafe ,
12+ toText , isLineSafe , uncontrol ,
1313 encodeFS ,
1414) where
1515
@@ -44,9 +44,10 @@ readIntM = fmap fst . P.readInt
4444showInt :: Int -> ByteString
4545showInt = P. pack . show
4646
47- -- | If seeming ISO-8859-1, convert to UTF-8.
47+ -- | If seeming ISO-8859-1, convert to UTF-8, and blank control chars .
4848guessEncoding :: ByteString -> ByteString
4949guessEncoding bs =
50+ P. map (uncontrol ' ' ) $
5051 if UTF8. replacement_char `elem` UTF8. toString bs
5152 then UTF8. fromString $ P. unpack bs
5253 else bs
@@ -56,24 +57,25 @@ dropLastUTF8 :: ByteString -> ByteString
5657dropLastUTF8 = P. dropEnd 1 . P. dropWhileEnd isCB
5758 where isCB b = b >= '\ 128 ' && b < '\ 192 '
5859
60+ -- XXX when we drop GHC 9.4 we can use its filepath's function
5961-- | Filesystem encoding for CLI (PEP 383).
6062encodeFS :: String -> IO ByteString
6163encodeFS str = do
62- encoding <- getFileSystemEncoding
63- GHC. withCStringLen encoding str P. packCStringLen
64+ enc <- getFileSystemEncoding
65+ GHC. withCStringLen enc str P. packCStringLen
6466
6567-- | Can file be sent to decoder?
6668isLineSafe :: ByteString -> Bool
6769isLineSafe = P. all (`notElem` ['\ 0 ', ' \r ' , ' \n ' ])
6870
6971-- | Blot out control characters.
70- uncontrol :: Char -> Char
71- uncontrol c | isControl c = UTF8. replacement_char
72- | True = c
72+ uncontrol :: Char -> Char -> Char
73+ uncontrol sub c | isControl c = sub
74+ | True = c
7375
7476-- | ByteString to displayable text.
7577toText :: ByteString -> ByteString
76- toText = UTF8. fromString . map uncontrol . UTF8. toString
78+ toText = UTF8. fromString . map ( uncontrol UTF8. replacement_char) . UTF8. toString
7779
7880-- Width-aware operations on UTF-8 'ByteString's, using libc 'wcwidth'.
7981-- A UTF-8 runtime locale is presumed; counts may differ otherwise.
0 commit comments