on master help sign in
Table parsing: remove redundant explicit types in favor of explicit reader functions
commitbeef71e7b72225c067ba1fee70f883ba1b627840 authorJonathan Daugherty <cygnus@foobox.com> dateThu 5 Mar 2020, 14:20 (7 years ago) parentb54772a
1 file changed+6−26
src/Graphics/Vty/UnicodeWidthTable/IO.hs+6−26
src/Graphics/Vty/UnicodeWidthTable/IO.hs+6−26
@@ -48,30 +48,14 @@ writeUnicodeWidthTable path table = do
4848widthTableMagic :: Word32
4949widthTableMagic = 0xc1a9f7e0
5050
51−-- NB: This parsing code uses explicit types for parsing each value to
52−-- catch situations where someone changes the types of the fields of
53−-- WidthTableRange or other values and in doing so would silently break
54−-- the parser. If the parser used a style like
55−--
56−-- WidthTableRange <$> get <*> get <*> get
57−--
58−-- then that would result in valid Haskell code that would fail to read
59−-- files that it would have previously parsed. We want those errors
60−-- caught by GHc. At the time of this writing, the library data types
61−-- *happen* to match the binary layout of the version 1 format, but that
62−-- could definitely change in later formats. In those cases we should
63−-- deal with the difference by doing the appropriate conversions.
64−
6551tableParser :: Get UnicodeWidthTable
6652tableParser = do
67− magic :: Word32
68− <- getWord32le
53+ magic <- getWord32le
6954
7055 when (magic /= widthTableMagic) $
7156 fail "Table magic number invalid"
7257
73− version :: Word8
74− <- get
58+ version <- getWord8
7559
7660 case version of
7761 1 -> tableV1Parser
@@ -79,16 +63,12 @@ tableParser = do
7963
8064tableV1Parser :: Get UnicodeWidthTable
8165tableV1Parser = do
82− numRanges :: Word32
83− <- getWord32le
66+ numRanges <- getWord32le
8467
8568 let parseRange = do
86− start :: Word32
87− <- getWord32le
88− size :: Word32
89− <- getWord32le
90− cols :: Word8
91− <- get
69+ start <- getWord32le
70+ size <- getWord32le
71+ cols <- getWord8
9272 return WidthTableRange { rangeStart = start
9373 , rangeSize = size
9474 , rangeColumns = cols