@@ -26,7 +26,9 @@ module Codec.Binary.UTF8.String (
26
26
, utf8Encode
27
27
) where
28
28
29
+ #if __GLASGOW_HASKELL__ > 710
29
30
import qualified Data.List.NonEmpty as NE
31
+ #endif
30
32
import Data.Word (Word8 ,Word32 )
31
33
import Data.Bits ((.|.) ,(.&.) ,shiftL ,shiftR )
32
34
import Data.Char (chr ,ord )
@@ -47,6 +49,28 @@ replacement_character :: Char
47
49
replacement_character = ' \xfffd '
48
50
49
51
-- | Encode a single Haskell 'Char' to a list of 'Word8' values, in UTF8 format.
52
+ #if __GLASGOW_HASKELL__ < 802
53
+ encodeChar :: Char -> (Word8 , [Word8 ])
54
+ encodeChar = (\ (x, xs) -> (fromIntegral x, fmap fromIntegral xs)) . go . ord
55
+ where
56
+ go oc
57
+ | oc <= 0x7f = ( oc
58
+ , [] )
59
+
60
+ | oc <= 0x7ff = ( 0xc0 + (oc `shiftR` 6 )
61
+ , [ 0x80 + oc .&. 0x3f ])
62
+
63
+ | oc <= 0xffff = ( 0xe0 + (oc `shiftR` 12 )
64
+ , [ 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
65
+ , 0x80 + oc .&. 0x3f
66
+ ])
67
+
68
+ | otherwise = ( 0xf0 + (oc `shiftR` 18 )
69
+ , [ 0x80 + ((oc `shiftR` 12 ) .&. 0x3f )
70
+ , 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
71
+ , 0x80 + oc .&. 0x3f
72
+ ])
73
+ #else
50
74
encodeChar :: Char -> NE. NonEmpty Word8
51
75
encodeChar = fmap fromIntegral . go . ord
52
76
where
@@ -61,16 +85,21 @@ encodeChar = fmap fromIntegral . go . ord
61
85
[ 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
62
86
, 0x80 + oc .&. 0x3f
63
87
]
88
+
64
89
| otherwise = 0xf0 + (oc `shiftR` 18 ) NE. :|
65
90
[ 0x80 + ((oc `shiftR` 12 ) .&. 0x3f )
66
91
, 0x80 + ((oc `shiftR` 6 ) .&. 0x3f )
67
92
, 0x80 + oc .&. 0x3f
68
93
]
69
-
94
+ #endif
70
95
71
96
-- | Encode a Haskell 'String' to a list of 'Word8' values, in UTF8 format.
72
97
encode :: String -> [Word8 ]
98
+ #if __GLASGOW_HASKELL__ < 802
99
+ encode = concatMap ((\ (x, xs) -> x: xs) . encodeChar)
100
+ #else
73
101
encode = concatMap (NE. toList . encodeChar)
102
+ #endif
74
103
75
104
--
76
105
-- | Decode a UTF8 string packed into a list of 'Word8' values, directly to 'String'
0 commit comments