Skip to content

Commit c894f77

Browse files
Blank ID3 control chars; add tests; other touches.
1 parent 29c0066 commit c894f77

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

State.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Playlist (FileArray, DirArray)
1414
import Style (Line, Segment(Seg), UIStyle(warnings))
1515

1616
import Data.ByteString (hPut)
17-
import GHC.Records
17+
import GHC.Records (HasField(..))
1818
import System.Clock (TimeSpec(..))
1919
import System.IO (hFlush)
2020
import System.Process (ProcessHandle, waitForProcess)

Text.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4444
showInt :: Int -> ByteString
4545
showInt = 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.
4848
guessEncoding :: ByteString -> ByteString
4949
guessEncoding 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
5657
dropLastUTF8 = 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).
6062
encodeFS :: String -> IO ByteString
6163
encodeFS 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?
6668
isLineSafe :: ByteString -> Bool
6769
isLineSafe = 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.
7577
toText :: 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.

test/TextSpec.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ tests = testGroup "Text"
4444
[ testCase "ASCII" $ guessEncoding "abc" @?= "abc"
4545
, testCase "ISO-8859" $ guessEncoding "encöde" @?= u"encöde"
4646
, testCase "UTF-8" $ guessEncoding (u"encöde") @?= u"encöde"
47+
, testCase "control" $ guessEncoding (u"en\3öde") @?= u"en öde"
4748
]
4849
, testGroup "displayWidth"
4950
[ testCase "empty" $ displayWidth "" @?= 0
@@ -86,6 +87,10 @@ tests = testGroup "Text"
8687
, testCase "pads after a wide-char content too"
8788
$ toWidth 5 (u"中a") @?= u"中a "
8889
]
90+
, testGroup "toText"
91+
[ testCase "Unicode" $ toText (u"encöde") @?= u"encöde"
92+
, testCase "bad bytes" $ toText ("no\130b\8y") @?= u"no�b�y"
93+
]
8994
]
9095

9196

0 commit comments

Comments
 (0)