-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathUtf8.hs
More file actions
318 lines (284 loc) · 9.7 KB
/
Copy pathUtf8.hs
File metadata and controls
318 lines (284 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{-# LANGUAGE CPP, MagicHash, BangPatterns #-}
-- |
-- Module : Data.Text.Internal.Encoding.Utf8
-- Copyright : (c) 2008, 2009 Tom Harper,
-- (c) 2009, 2010 Bryan O'Sullivan,
-- (c) 2009 Duncan Coutts
-- (c) 2021 Andrew Lelechenko
--
-- License : BSD-style
-- Maintainer : bos@serpentine.com
-- Stability : experimental
-- Portability : GHC
--
-- /Warning/: this is an internal module, and does not have a stable
-- API or name. Functions in this module may not check or enforce
-- preconditions expected by public modules. Use at your own risk!
--
-- Basic UTF-8 validation and character manipulation.
module Data.Text.Internal.Encoding.Utf8
( utf8Length
, utf8LengthByLeader
-- Decomposition
, ord2
, ord3
, ord4
-- Construction
, chr2
, chr3
, chr4
-- * Validation
, validate1
, validate2
, validate3
, validate4
-- * Naive decoding
, DecoderState(..)
, utf8AcceptState
, utf8RejectState
, updateDecoderState
, DecoderResult(..)
, CodePoint(..)
, utf8DecodeStart
, utf8DecodeContinue
) where
#if defined(ASSERTS)
import Control.Exception (assert)
import GHC.Stack (HasCallStack)
#endif
import Data.Bits (Bits(..))
import Data.Char (ord, chr)
import GHC.Exts
import GHC.Word (Word8(..))
#if !MIN_VERSION_base(4,16,0)
-- harmless to import, except for warnings that it is unused.
import Data.Text.Internal.PrimCompat (word8ToWord#)
#endif
default(Int)
between :: Word8 -- ^ byte to check
-> Word8 -- ^ lower bound
-> Word8 -- ^ upper bound
-> Bool
between x y z = x >= y && x <= z
{-# INLINE between #-}
-- This is a branchless version of
-- utf8Length c
-- | ord c < 0x80 = 1
-- | ord c < 0x800 = 2
-- | ord c < 0x10000 = 3
-- | otherwise = 4
-- Implementation suggested by Alex Mason.
-- | Measure byte length of UTF-8 encoding for a given character.
--
-- @since 2.0
utf8Length :: Char -> Int
utf8Length (C# c) = I# ((1# +# geChar# c (chr# 0x80#)) +# (geChar# c (chr# 0x800#) +# geChar# c (chr# 0x10000#)))
{-# INLINE utf8Length #-}
-- | Measure byte length of UTF-8 encoding for characters,
-- starting with a given byte.
--
-- @since 2.0
utf8LengthByLeader :: Word8 -> Int
utf8LengthByLeader w
| w < 0xC0 = 1
| w < 0xE0 = 2
| w < 0xF0 = 3
| otherwise = 4
{-# INLINE utf8LengthByLeader #-}
-- | Encode a character as UTF-8 bytes assuming that exactly 2 are needed.
-- This precondition is not checked.
--
-- @since 1.1.0.0
ord2 ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Char -> (Word8,Word8)
ord2 c =
#if defined(ASSERTS)
assert (n >= 0x80 && n <= 0x07ff)
#endif
(x1,x2)
where
n = ord c
x1 = intToWord8 $ (n `shiftR` 6) + 0xC0
x2 = intToWord8 $ (n .&. 0x3F) + 0x80
{-# INLINE ord2 #-}
-- | Encode a character as UTF-8 bytes assuming that exactly 3 are needed.
-- This precondition is not checked.
--
-- @since 1.1.0.0
ord3 ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Char -> (Word8,Word8,Word8)
ord3 c =
#if defined(ASSERTS)
assert (n >= 0x0800 && n <= 0xffff)
#endif
(x1,x2,x3)
where
n = ord c
x1 = intToWord8 $ (n `shiftR` 12) + 0xE0
x2 = intToWord8 $ ((n `shiftR` 6) .&. 0x3F) + 0x80
x3 = intToWord8 $ (n .&. 0x3F) + 0x80
{-# INLINE ord3 #-}
-- | Encode a character as UTF-8 bytes assuming that exactly 4 are needed.
-- This precondition is not checked.
--
-- @since 1.1.0.0
ord4 ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Char -> (Word8,Word8,Word8,Word8)
ord4 c =
#if defined(ASSERTS)
assert (n >= 0x10000)
#endif
(x1,x2,x3,x4)
where
n = ord c
x1 = intToWord8 $ (n `shiftR` 18) + 0xF0
x2 = intToWord8 $ ((n `shiftR` 12) .&. 0x3F) + 0x80
x3 = intToWord8 $ ((n `shiftR` 6) .&. 0x3F) + 0x80
x4 = intToWord8 $ (n .&. 0x3F) + 0x80
{-# INLINE ord4 #-}
-- | @since 1.1.0.0
chr2 :: Word8 -> Word8 -> Char
chr2 (W8# x1#) (W8# x2#) = C# (chr# (z1# +# z2#))
where
!y1# = word2Int# (word8ToWord# x1#)
!y2# = word2Int# (word8ToWord# x2#)
!z1# = uncheckedIShiftL# (y1# -# 0xC0#) 6#
!z2# = y2# -# 0x80#
{-# INLINE chr2 #-}
-- | @since 1.1.0.0
chr3 :: Word8 -> Word8 -> Word8 -> Char
chr3 (W8# x1#) (W8# x2#) (W8# x3#) = C# (chr# (z1# +# z2# +# z3#))
where
!y1# = word2Int# (word8ToWord# x1#)
!y2# = word2Int# (word8ToWord# x2#)
!y3# = word2Int# (word8ToWord# x3#)
!z1# = uncheckedIShiftL# (y1# -# 0xE0#) 12#
!z2# = uncheckedIShiftL# (y2# -# 0x80#) 6#
!z3# = y3# -# 0x80#
{-# INLINE chr3 #-}
-- | @since 1.1.0.0
chr4 :: Word8 -> Word8 -> Word8 -> Word8 -> Char
chr4 (W8# x1#) (W8# x2#) (W8# x3#) (W8# x4#) =
C# (chr# (z1# +# z2# +# z3# +# z4#))
where
!y1# = word2Int# (word8ToWord# x1#)
!y2# = word2Int# (word8ToWord# x2#)
!y3# = word2Int# (word8ToWord# x3#)
!y4# = word2Int# (word8ToWord# x4#)
!z1# = uncheckedIShiftL# (y1# -# 0xF0#) 18#
!z2# = uncheckedIShiftL# (y2# -# 0x80#) 12#
!z3# = uncheckedIShiftL# (y3# -# 0x80#) 6#
!z4# = y4# -# 0x80#
{-# INLINE chr4 #-}
-- | @since 1.1.0.0
validate1 :: Word8 -> Bool
validate1 x1 = x1 <= 0x7F
{-# INLINE validate1 #-}
-- | @since 1.1.0.0
validate2 :: Word8 -> Word8 -> Bool
validate2 x1 x2 = between x1 0xC2 0xDF && between x2 0x80 0xBF
{-# INLINE validate2 #-}
-- | @since 1.1.0.0
validate3 :: Word8 -> Word8 -> Word8 -> Bool
{-# INLINE validate3 #-}
validate3 x1 x2 x3 = validate3_1 || validate3_2 || validate3_3 || validate3_4
where
validate3_1 = (x1 == 0xE0) &&
between x2 0xA0 0xBF &&
between x3 0x80 0xBF
validate3_2 = between x1 0xE1 0xEC &&
between x2 0x80 0xBF &&
between x3 0x80 0xBF
validate3_3 = x1 == 0xED &&
between x2 0x80 0x9F &&
between x3 0x80 0xBF
validate3_4 = between x1 0xEE 0xEF &&
between x2 0x80 0xBF &&
between x3 0x80 0xBF
-- | @since 1.1.0.0
validate4 :: Word8 -> Word8 -> Word8 -> Word8 -> Bool
{-# INLINE validate4 #-}
validate4 x1 x2 x3 x4 = validate4_1 || validate4_2 || validate4_3
where
validate4_1 = x1 == 0xF0 &&
between x2 0x90 0xBF &&
between x3 0x80 0xBF &&
between x4 0x80 0xBF
validate4_2 = between x1 0xF1 0xF3 &&
between x2 0x80 0xBF &&
between x3 0x80 0xBF &&
between x4 0x80 0xBF
validate4_3 = x1 == 0xF4 &&
between x2 0x80 0x8F &&
between x3 0x80 0xBF &&
between x4 0x80 0xBF
intToWord8 :: Int -> Word8
intToWord8 = fromIntegral
word8ToInt :: Word8 -> Int
word8ToInt = fromIntegral
-------------------------------------------------------------------------------
-- Naive UTF8 decoder.
-- See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for the explanation of the state machine.
newtype ByteClass = ByteClass Word8
byteToClass :: Word8 -> ByteClass
byteToClass n = ByteClass (W8# el#)
where
!(I# n#) = word8ToInt n
el# = indexWord8OffAddr# table# n#
table# :: Addr#
table# = "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\SOH\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\b\b\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\STX\n\ETX\ETX\ETX\ETX\ETX\ETX\ETX\ETX\ETX\ETX\ETX\ETX\EOT\ETX\ETX\v\ACK\ACK\ACK\ENQ\b\b\b\b\b\b\b\b\b\b\b"#
-- | @since 2.0
newtype DecoderState = DecoderState Word8
deriving (Eq, Show)
-- | @since 2.0.2
utf8AcceptState :: DecoderState
utf8AcceptState = DecoderState 0
-- | @since 2.0.2
utf8RejectState :: DecoderState
utf8RejectState = DecoderState 12
updateState :: ByteClass -> DecoderState -> DecoderState
updateState (ByteClass c) (DecoderState s) = DecoderState (W8# el#)
where
!(I# n#) = word8ToInt (c + s)
el# = indexWord8OffAddr# table# n#
table# :: Addr#
table# = "\NUL\f\CAN$<`T\f\f\f0H\f\f\f\f\f\f\f\f\f\f\f\f\f\NUL\f\f\f\f\f\NUL\f\NUL\f\f\f\CAN\f\f\f\f\f\CAN\f\CAN\f\f\f\f\f\f\f\f\f\CAN\f\f\f\f\f\CAN\f\f\f\f\f\f\f\CAN\f\f\f\f\f\f\f\f\f$\f$\f\f\f$\f\f\f\f\f$\f$\f\f\f$\f\f\f\f\f\f\f\f\f\f"#
-- | @since 2.0.2
updateDecoderState :: Word8 -> DecoderState -> DecoderState
updateDecoderState b s = updateState (byteToClass b) s
-- | @since 2.0
newtype CodePoint = CodePoint Int
-- | @since 2.0
data DecoderResult
= Accept !Char
| Incomplete !DecoderState !CodePoint
| Reject
-- | @since 2.0
utf8DecodeStart :: Word8 -> DecoderResult
utf8DecodeStart !w
| st == utf8AcceptState = Accept (chr (word8ToInt w))
| st == utf8RejectState = Reject
| otherwise = Incomplete st (CodePoint cp)
where
cl@(ByteClass cl') = byteToClass w
st = updateState cl utf8AcceptState
cp = word8ToInt $ (0xff `unsafeShiftR` word8ToInt cl') .&. w
-- | @since 2.0
utf8DecodeContinue :: Word8 -> DecoderState -> CodePoint -> DecoderResult
utf8DecodeContinue !w !st (CodePoint !cp)
| st' == utf8AcceptState = Accept (chr cp')
| st' == utf8RejectState = Reject
| otherwise = Incomplete st' (CodePoint cp')
where
cl = byteToClass w
st' = updateState cl st
cp' = (cp `shiftL` 6) .|. word8ToInt (w .&. 0x3f)