@@ -47,6 +47,28 @@ replacement_character :: Char
47
47
replacement_character = ' \xfffd '
48
48
49
49
-- | 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
50
72
encodeChar :: Char -> NE. NonEmpty Word8
51
73
encodeChar = fmap fromIntegral . go . ord
52
74
where
@@ -61,16 +83,22 @@ encodeChar = fmap fromIntegral . go . ord
61
83
[ 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
62
84
, 0x80 + oc .&. 0x3f
63
85
]
86
+
64
87
| otherwise = 0xf0 + (oc `shiftR` 18 ) NE. :|
65
88
[ 0x80 + ((oc `shiftR` 12 ) .&. 0x3f )
66
89
, 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
67
90
, 0x80 + oc .&. 0x3f
68
91
]
92
+ #endif
69
93
70
94
71
95
-- | Encode a Haskell 'String' to a list of 'Word8' values, in UTF8 format.
72
96
encode :: String -> [Word8 ]
97
+ #if __GLASGOW_HASKELL__ < 802
98
+ encode = concatMap (\ (x, xs) -> x: xs . encodeChar)
99
+ #else
73
100
encode = concatMap (NE. toList . encodeChar)
101
+ #endif
74
102
75
103
--
76
104
-- | Decode a UTF8 string packed into a list of 'Word8' values, directly to 'String'
0 commit comments