Skip to content

Commit cb0e60e

Browse files
committed
Adds CPP to modify encodeChar for ghc7
1 parent 55c6fca commit cb0e60e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Codec/Binary/UTF8/String.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ replacement_character :: Char
4747
replacement_character = '\xfffd'
4848

4949
-- | Encode a single Haskell 'Char' to a list of 'Word8' values, in UTF8 format.
50+
#if __GLASGOW_HASKELL__ < 802
51+
encodeChar :: Char -> (Word8, [Word8])
52+
encodeChar = (\(x, xs) -> (fromIntegral x, fmap fromIntegral xs)) . go . ord
53+
where
54+
go oc
55+
| oc <= 0x7f = ( oc
56+
, [])
57+
58+
| oc <= 0x7ff = ( 0xc0 + (oc `shiftR` 6)
59+
, [ 0x80 + oc .&. 0x3f ])
60+
61+
| oc <= 0xffff = ( 0xe0 + (oc `shiftR` 12)
62+
, [ 0x80 + ((oc `shiftR` 6) .&. 0x3f)
63+
, 0x80 + oc .&. 0x3f
64+
])
65+
66+
| otherwise = ( 0xf0 + (oc `shiftR` 18)
67+
, [ 0x80 + ((oc `shiftR` 12) .&. 0x3f)
68+
, 0x80 + ((oc `shiftR` 6) .&. 0x3f)
69+
, 0x80 + oc .&. 0x3f
70+
])
71+
#else
5072
encodeChar :: Char -> NE.NonEmpty Word8
5173
encodeChar = fmap fromIntegral . go . ord
5274
where
@@ -61,16 +83,22 @@ encodeChar = fmap fromIntegral . go . ord
6183
[ 0x80 + ((oc `shiftR` 6) .&. 0x3f)
6284
, 0x80 + oc .&. 0x3f
6385
]
86+
6487
| otherwise = 0xf0 + (oc `shiftR` 18) NE.:|
6588
[ 0x80 + ((oc `shiftR` 12) .&. 0x3f)
6689
, 0x80 + ((oc `shiftR` 6) .&. 0x3f)
6790
, 0x80 + oc .&. 0x3f
6891
]
92+
#endif
6993

7094

7195
-- | Encode a Haskell 'String' to a list of 'Word8' values, in UTF8 format.
7296
encode :: String -> [Word8]
97+
#if __GLASGOW_HASKELL__ < 802
98+
encode = concatMap (\(x, xs) -> x:xs . encodeChar)
99+
#else
73100
encode = concatMap (NE.toList . encodeChar)
101+
#endif
74102

75103
--
76104
-- | Decode a UTF8 string packed into a list of 'Word8' values, directly to 'String'

0 commit comments

Comments
 (0)