on c06d2e2 help sign in
text/Data/Text/Utf8.hs
133 lines 4.2 kB history raw
1{-# LANGUAGE MagicHash #-}
2module Data.Text.Utf8 where
4import Data.Char(ord)
5import Data.Bits
7import GHC.Exts
8import GHC.Prim
9import GHC.Word
11between :: Word8 -> Word8 -> Word8 -> Bool
12between x y z = x >= y && x <= z
13{-# INLINE between #-}
15ord2 :: Char -> (Word8,Word8)
16ord2 c = (x1,x2)
17 where
18 n = ord c
19 x1 = fromIntegral $ (shiftR n 6) + (0xC0 :: Int) :: Word8
20 x2 = fromIntegral $ (n .&. 0x3F) + (0x80 :: Int) :: Word8
22ord3 :: Char -> (Word8,Word8,Word8)
23ord3 c = (x1,x2,x3)
24 where
25 n = ord c
26 x1 = fromIntegral $ (shiftR n 12) + (0xE0::Int) :: Word8
27 x2 = fromIntegral $ ((shiftR n 6) .&. (0x3F::Int)) + (0x80::Int) :: Word8
28 x3 = fromIntegral $ (n .&. (0x3F::Int)) + (0x80::Int) :: Word8
30ord4 :: Char -> (Word8,Word8,Word8,Word8)
31ord4 c = (x1,x2,x3,x4)
32 where
33 n = ord c
34 x1 = fromIntegral $ (shiftR n 18) + (0xF0::Int) :: Word8
35 x2 = fromIntegral $ ((shiftR n 12) .&. (0x3F::Int)) + (0x80::Int) :: Word8
36 x3 = fromIntegral $ ((shiftR n 6) .&. (0x3F::Int)) + (0x80::Int) :: Word8
37 x4 = fromIntegral $ (n .&. (0x3F::Int)) + (0x80::Int) :: Word8
39chr2 :: Word8 -> Word8 -> Char
40chr2 (W8# x1#) (W8# x2#) = C# (chr# (z1# +# z2#))
41 where
42 y1# = word2Int# x1#
43 y2# = word2Int# x2#
44 z1# = uncheckedIShiftL# (y1# -# 0xC0#) 6#
45 z2# = y2# -# 0x8F#
46{-# INLINE chr2 #-}
48chr3 :: Word8 -> Word8 -> Word8 -> Char
49chr3 (W8# x1#) (W8# x2#) (W8# x3#) = C# (chr# (z1# +# z2# +# z3#))
50 where
51 y1# = word2Int# x1#
52 y2# = word2Int# x2#
53 y3# = word2Int# x3#
54 z1# = uncheckedIShiftL# (y1# -# 0xE0#) 12#
55 z2# = uncheckedIShiftL# (y2# -# 0x80#) 6#
56 z3# = y3# -# 0x80#
57{-# INLINE chr3 #-}
59chr4 :: Word8 -> Word8 -> Word8 -> Word8 -> Char
60chr4 (W8# x1#) (W8# x2#) (W8# x3#) (W8# x4#) =
61 C# (chr# (z1# +# z2# +# z3# +# z4#))
62 where
63 y1# = word2Int# x1#
64 y2# = word2Int# x2#
65 y3# = word2Int# x3#
66 y4# = word2Int# x4#
67 z1# = uncheckedIShiftL# (y1# -# 0xF0#) 18#
68 z2# = uncheckedIShiftL# (y2# -# 0x80#) 12#
69 z3# = uncheckedIShiftL# (y3# -# 0x80#) 6#
70 z4# = y4# -# 0x80#
71{-# INLINE chr4 #-}
73validate1 :: Word8 -> Bool
74validate1 x1 = between x1 0x00 0x7F
75{-# INLINE validate1 #-}
77validate2 :: Word8 -> Word8 -> Bool
78validate2 x1 x2 = between x1 0xC2 0xDF && between x2 0x80 0xBF
79{-# INLINE validate2 #-}
81validate3 :: Word8 -> Word8 -> Word8 -> Bool
82validate3 x1 x2 x3 = validate3_1 x1 x2 x3 ||
83 validate3_2 x1 x2 x3 ||
84 validate3_3 x1 x2 x3 ||
85 validate3_4 x1 x2 x3
86{-# INLINE validate3 #-}
88validate4 :: Word8 -> Word8 -> Word8 -> Word8 -> Bool
89validate4 x1 x2 x3 x4 = validate4_1 x1 x2 x3 x4 ||
90 validate4_2 x1 x2 x3 x4 ||
91 validate4_3 x1 x2 x3 x4
92{-# INLINE validate4 #-}
96validate3_1 x1 x2 x3 = (x1 == 0xE0) &&
97 between x2 0xA0 0xBF &&
98 between x3 0x80 0xBF
99{-# INLINE validate3_1 #-}
101validate3_2 x1 x2 x3 = between x1 0xE1 0xEC &&
102 between x2 0x80 0xBF &&
103 between x3 0x80 0xBF
104{-# INLINE validate3_2 #-}
106validate3_3 x1 x2 x3 = x1 == 0xED &&
107 between x2 0x80 0x9F &&
108 between x3 0x80 0xBF
109{-# INLINE validate3_3 #-}
111validate3_4 x1 x2 x3 = between x1 0xEE 0xEF &&
112 between x2 0x80 0xBF &&
113 between x2 0x80 0xBF
114{-# INLINE validate3_4 #-}
117validate4_1 x1 x2 x3 x4 = x1 == 0xF0 &&
118 between x2 0x90 0xBF &&
119 between x3 0x80 0xBF &&
120 between x4 0x80 0xBF
121{-# INLINE validate4_1 #-}
123validate4_2 x1 x2 x3 x4 = between x1 0xF1 0xF3 &&
124 between x2 0x80 0xBF &&
125 between x3 0x80 0xBF &&
126 between x4 0x80 0xBF
127{-# INLINE validate4_2 #-}
129validate4_3 x1 x2 x3 x4 = x1 == 0xF4 &&
130 between x2 0x80 0x8F &&
131 between x3 0x80 0xBF &&
132 between x4 0x80 0xBF
133{-# INLINE validate4_3 #-}