Skip to content

Commit b510549

Browse files
committed
Fix warnings about non-exhaustive patterns
The utf8encode function always returned a list of at least one element, but the type system didn't prove this, leading to spurious warnings when taking the first element of the returned list. Fix this by returning a tuple with the first element separate instead. (I would have used Data.List.NonEmpty but it looks like we still support ancient GHC versions that didn't yet have it.)
1 parent c6c8870 commit b510549

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

templates/wrappers.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,30 @@ import qualified Data.Bits
3030

3131
-- | Encode a Haskell String to a list of Word8 values, in UTF8 format.
3232
utf8Encode :: Char -> [Word8]
33-
utf8Encode = map fromIntegral . go . ord
33+
utf8Encode = uncurry (:) . utf8Encode'
34+
35+
utf8Encode' :: Char -> (Word8, [Word8])
36+
utf8Encode' c = (fromIntegral x, map fromIntegral xs)
3437
where
3538
go oc
36-
| oc <= 0x7f = [oc]
39+
| oc <= 0x7f = ( oc
40+
, [
41+
])
3742

38-
| oc <= 0x7ff = [ 0xc0 + (oc `Data.Bits.shiftR` 6)
39-
, 0x80 + oc Data.Bits..&. 0x3f
40-
]
43+
| oc <= 0x7ff = ( 0xc0 + (oc `Data.Bits.shiftR` 6)
44+
, [0x80 + oc Data.Bits..&. 0x3f
45+
])
4146

42-
| oc <= 0xffff = [ 0xe0 + (oc `Data.Bits.shiftR` 12)
43-
, 0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f)
47+
| oc <= 0xffff = ( 0xe0 + (oc `Data.Bits.shiftR` 12)
48+
, [0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f)
4449
, 0x80 + oc Data.Bits..&. 0x3f
45-
]
46-
| otherwise = [ 0xf0 + (oc `Data.Bits.shiftR` 18)
47-
, 0x80 + ((oc `Data.Bits.shiftR` 12) Data.Bits..&. 0x3f)
50+
])
51+
| otherwise = ( 0xf0 + (oc `Data.Bits.shiftR` 18)
52+
, [0x80 + ((oc `Data.Bits.shiftR` 12) Data.Bits..&. 0x3f)
4853
, 0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f)
4954
, 0x80 + oc Data.Bits..&. 0x3f
50-
]
55+
])
56+
(x, xs) = go (ord c)
5157

5258
#endif
5359

@@ -72,7 +78,7 @@ alexGetByte :: AlexInput -> Maybe (Byte,AlexInput)
7278
alexGetByte (p,c,(b:bs),s) = Just (b,(p,c,bs,s))
7379
alexGetByte (_,_,[],[]) = Nothing
7480
alexGetByte (p,_,[],(c:s)) = let p' = alexMove p c
75-
(b:bs) = utf8Encode c
81+
(b, bs) = utf8Encode' c
7682
in p' `seq` Just (b, (p', c, bs, s))
7783
#endif
7884

@@ -334,9 +340,8 @@ alexScanTokens str = go ('\n',[],str)
334340
alexGetByte :: AlexInput -> Maybe (Byte,AlexInput)
335341
alexGetByte (c,(b:bs),s) = Just (b,(c,bs,s))
336342
alexGetByte (_,[],[]) = Nothing
337-
alexGetByte (_,[],(c:s)) = case utf8Encode c of
338-
(b:bs) -> Just (b, (c, bs, s))
339-
[] -> Nothing
343+
alexGetByte (_,[],(c:s)) = let (b, bs) = utf8Encode' c
344+
in Just (b, (c, bs, s))
340345
#endif
341346

342347

0 commit comments

Comments
 (0)