on master help sign in
UnicodeWidthTable.IO: expose bytestring parser
commit2a76f765f4d818f711d9bd177731499d00f2fb99 authorJonathan Daugherty <cygnus@foobox.com> dateThu 5 Mar 2020, 14:37 (7 years ago) parent69dfa28
1 file changed+11−7
src/Graphics/Vty/UnicodeWidthTable/IO.hs+11−7
src/Graphics/Vty/UnicodeWidthTable/IO.hs+11−7
@@ -1,6 +1,7 @@
11{-# LANGUAGE CPP #-}
22module Graphics.Vty.UnicodeWidthTable.IO
33 ( readUnicodeWidthTable
4+ , parseUnicodeWidthTable
45 , writeUnicodeWidthTable
56 )
67where
@@ -21,20 +22,23 @@ import Graphics.Vty.UnicodeWidthTable.Types
2122-- This either returns a successfully parsed table or a table parsing
2223-- error message. This does not handle I/O exceptions.
2324readUnicodeWidthTable :: FilePath -> IO (Either String UnicodeWidthTable)
24−readUnicodeWidthTable path = do
25− body <- BSL.readFile path
26− case runGetOrFail tableParser body of
25+readUnicodeWidthTable path = parseUnicodeWidthTable <$> BSL.readFile path
26+
27+-- | Parse a binary unicode width table.
28+parseUnicodeWidthTable :: BSL.ByteString -> Either String UnicodeWidthTable
29+parseUnicodeWidthTable bs =
30+ case runGetOrFail tableParser bs of
2731 Left (_, _, msg) ->
28− return $ Left msg
32+ Left msg
2933
3034 -- Even if we parsed a table, leftover bytes indicate something
3135 -- could be wrong.
3236 Right (remainingBytes, _, _) | not (BSL.null remainingBytes) ->
33− return $ Left $ "Error: " <> show (BSL.length remainingBytes) <>
34− " byte(s) left unconsumed"
37+ Left $ "Error: " <> show (BSL.length remainingBytes) <>
38+ " byte(s) left unconsumed"
3539
3640 Right (_, _, table) ->
37− return $ Right table
41+ Right table
3842
3943-- | Write the unicode width table to the specified path.
4044--