@@ -47,6 +47,28 @@ replacement_character :: Char
4747replacement_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
5072encodeChar :: Char -> NE. NonEmpty Word8
5173encodeChar = fmap fromIntegral . go . ord
5274 where
@@ -61,16 +83,21 @@ 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 ]
69-
92+ #endif
7093
7194-- | Encode a Haskell 'String' to a list of 'Word8' values, in UTF8 format.
7295encode :: String -> [Word8 ]
96+ #if __GLASGOW_HASKELL__ < 802
97+ encode = concatMap ((\ (x, xs) -> x: xs) . encodeChar)
98+ #else
7399encode = concatMap (NE. toList . encodeChar)
100+ #endif
74101
75102--
76103-- | Decode a UTF8 string packed into a list of 'Word8' values, directly to 'String'
0 commit comments