{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE CPP #-}
module Basement.UTF8.Helper
where
import Basement.Compat.Base
import Basement.Compat.Primitive
import Basement.Types.OffsetSize
import Basement.UTF8.Types
import Basement.Bits
import GHC.Prim
import GHC.Types
import GHC.Word
maskContinuation# :: Word# -> Word#
maskContinuation# :: Word# -> Word#
maskContinuation# Word#
v = Word# -> Word# -> Word#
and# Word#
v Word#
0x3f##
{-# INLINE maskContinuation# #-}
maskHeader2# :: Word# -> Word#
Word#
h = Word# -> Word# -> Word#
and# Word#
h Word#
0x1f##
{-# INLINE maskHeader2# #-}
maskHeader3# :: Word# -> Word#
Word#
h = Word# -> Word# -> Word#
and# Word#
h Word#
0xf##
{-# INLINE maskHeader3# #-}
maskHeader4# :: Word# -> Word#
Word#
h = Word# -> Word# -> Word#
and# Word#
h Word#
0x7##
{-# INLINE maskHeader4# #-}
or3# :: Word# -> Word# -> Word# -> Word#
or3# :: Word# -> Word# -> Word# -> Word#
or3# Word#
a Word#
b Word#
c = Word# -> Word# -> Word#
or# Word#
a (Word# -> Word# -> Word#
or# Word#
b Word#
c)
{-# INLINE or3# #-}
or4# :: Word# -> Word# -> Word# -> Word# -> Word#
or4# :: Word# -> Word# -> Word# -> Word# -> Word#
or4# Word#
a Word#
b Word#
c Word#
d = Word# -> Word# -> Word#
or# (Word# -> Word# -> Word#
or# Word#
a Word#
b) (Word# -> Word# -> Word#
or# Word#
c Word#
d)
{-# INLINE or4# #-}
toChar# :: Word# -> Char
toChar# :: Word# -> Char
toChar# Word#
w = Char# -> Char
C# (Int# -> Char#
chr# (Word# -> Int#
word2Int# Word#
w))
{-# INLINE toChar# #-}
toChar1 :: StepASCII -> Char
toChar1 :: StepASCII -> Char
toChar1 (StepASCII (W8# Word8#
w)) = Char# -> Char
C# (Word8# -> Char#
word8ToChar# Word8#
w)
toChar2 :: StepASCII -> Word8 -> Char
toChar2 :: StepASCII -> Word8 -> Char
toChar2 (StepASCII (W8# Word8#
b1)) (W8# Word8#
b2) =
Word# -> Char
toChar# (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader2# Word#
w1) Int#
6#) (Word# -> Word#
maskContinuation# Word#
w2))
where
w1 :: Word#
w1 = Word8# -> Word#
word8ToWord# Word8#
b1
w2 :: Word#
w2 = Word8# -> Word#
word8ToWord# Word8#
b2
toChar3 :: StepASCII -> Word8 -> Word8 -> Char
toChar3 :: StepASCII -> Word8 -> Word8 -> Char
toChar3 (StepASCII (W8# Word8#
b1)) (W8# Word8#
b2) (W8# Word8#
b3) =
Word# -> Char
toChar# (Word# -> Word# -> Word# -> Word#
or3# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader3# Word#
w1) Int#
12#)
(Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskContinuation# Word#
w2) Int#
6#)
(Word# -> Word#
maskContinuation# Word#
w3)
)
where
w1 :: Word#
w1 = Word8# -> Word#
word8ToWord# Word8#
b1
w2 :: Word#
w2 = Word8# -> Word#
word8ToWord# Word8#
b2
w3 :: Word#
w3 = Word8# -> Word#
word8ToWord# Word8#
b3
toChar4 :: StepASCII -> Word8 -> Word8 -> Word8 -> Char
toChar4 :: StepASCII -> Word8 -> Word8 -> Word8 -> Char
toChar4 (StepASCII (W8# Word8#
b1)) (W8# Word8#
b2) (W8# Word8#
b3) (W8# Word8#
b4) =
Word# -> Char
toChar# (Word# -> Word# -> Word# -> Word# -> Word#
or4# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader4# Word#
w1) Int#
18#)
(Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskContinuation# Word#
w2) Int#
12#)
(Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskContinuation# Word#
w3) Int#
6#)
(Word# -> Word#
maskContinuation# Word#
w4)
)
where
w1 :: Word#
w1 = Word8# -> Word#
word8ToWord# Word8#
b1
w2 :: Word#
w2 = Word8# -> Word#
word8ToWord# Word8#
b2
w3 :: Word#
w3 = Word8# -> Word#
word8ToWord# Word8#
b3
w4 :: Word#
w4 = Word8# -> Word#
word8ToWord# Word8#
b4
data UTF8Char =
UTF8_1 {-# UNPACK #-} !Word8
| UTF8_2 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
| UTF8_3 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
| UTF8_4 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
asUTF8Char :: Char -> UTF8Char
asUTF8Char :: Char -> UTF8Char
asUTF8Char !(C# Char#
c)
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x80## ) = UTF8Char
encode1
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x800## ) = UTF8Char
encode2
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x10000##) = UTF8Char
encode3
| Bool
otherwise = UTF8Char
encode4
where
!x :: Word#
x = Int# -> Word#
int2Word# (Char# -> Int#
ord# Char#
c)
encode1 :: UTF8Char
encode1 = Word8 -> UTF8Char
UTF8_1 (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x))
encode2 :: UTF8Char
encode2 =
let !x1 :: Word8
x1 = Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#) Word#
0xc0##))
!x2 :: Word8
x2 = Word# -> Word8
toContinuation Word#
x
in Word8 -> Word8 -> UTF8Char
UTF8_2 Word8
x1 Word8
x2
encode3 :: UTF8Char
encode3 =
let !x1 :: Word8
x1 = Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
12#) Word#
0xe0##))
!x2 :: Word8
x2 = Word# -> Word8
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#)
!x3 :: Word8
x3 = Word# -> Word8
toContinuation Word#
x
in Word8 -> Word8 -> Word8 -> UTF8Char
UTF8_3 Word8
x1 Word8
x2 Word8
x3
encode4 :: UTF8Char
encode4 =
let !x1 :: Word8
x1 = Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
18#) Word#
0xf0##))
!x2 :: Word8
x2 = Word# -> Word8
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
12#)
!x3 :: Word8
x3 = Word# -> Word8
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#)
!x4 :: Word8
x4 = Word# -> Word8
toContinuation Word#
x
in Word8 -> Word8 -> Word8 -> Word8 -> UTF8Char
UTF8_4 Word8
x1 Word8
x2 Word8
x3 Word8
x4
toContinuation :: Word# -> Word8
toContinuation :: Word# -> Word8
toContinuation Word#
w = Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# (Word# -> Word# -> Word#
or# (Word# -> Word# -> Word#
and# Word#
w Word#
0x3f##) Word#
0x80##))
{-# INLINE toContinuation #-}
numBytes :: UTF8Char -> CountOf Word8
numBytes :: UTF8Char -> CountOf Word8
numBytes UTF8_1{} = forall ty. Int -> CountOf ty
CountOf Int
1
numBytes UTF8_2{} = forall ty. Int -> CountOf ty
CountOf Int
2
numBytes UTF8_3{} = forall ty. Int -> CountOf ty
CountOf Int
3
numBytes UTF8_4{} = forall ty. Int -> CountOf ty
CountOf Int
4
skipNextHeaderValue :: Word8 -> CountOf Word8
!Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0xC0 = forall ty. Int -> CountOf ty
CountOf Int
1
| Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0xE0 = forall ty. Int -> CountOf ty
CountOf Int
2
| Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0xF0 = forall ty. Int -> CountOf ty
CountOf Int
3
| Bool
otherwise = forall ty. Int -> CountOf ty
CountOf Int
4
{-# INLINE skipNextHeaderValue #-}
headerIsAscii :: StepASCII -> Bool
(StepASCII Word8
x) = Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0x80
charToBytes :: Int -> CountOf Word8
charToBytes :: Int -> CountOf Word8
charToBytes Int
c
| Int
c forall a. Ord a => a -> a -> Bool
< Int
0x80 = forall ty. Int -> CountOf ty
CountOf Int
1
| Int
c forall a. Ord a => a -> a -> Bool
< Int
0x800 = forall ty. Int -> CountOf ty
CountOf Int
2
| Int
c forall a. Ord a => a -> a -> Bool
< Int
0x10000 = forall ty. Int -> CountOf ty
CountOf Int
3
| Int
c forall a. Ord a => a -> a -> Bool
< Int
0x110000 = forall ty. Int -> CountOf ty
CountOf Int
4
| Bool
otherwise = forall a. HasCallStack => [Char] -> a
error ([Char]
"invalid code point: " forall a. Monoid a => a -> a -> a
`mappend` forall a. Show a => a -> [Char]
show Int
c)
encodeCharUTF8 :: Char -> CharUTF8
encodeCharUTF8 :: Char -> CharUTF8
encodeCharUTF8 !(C# Char#
c)
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x80## ) = Word32 -> CharUTF8
CharUTF8 (Word32# -> Word32
W32# (Word# -> Word32#
wordToWord32# Word#
x))
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x800## ) = Word32 -> CharUTF8
CharUTF8 (Word32# -> Word32
W32# (Word# -> Word32#
wordToWord32# Word#
encode2))
| Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x10000##) = Word32 -> CharUTF8
CharUTF8 (Word32# -> Word32
W32# (Word# -> Word32#
wordToWord32# Word#
encode3))
| Bool
otherwise = Word32 -> CharUTF8
CharUTF8 (Word32# -> Word32
W32# (Word# -> Word32#
wordToWord32# Word#
encode4))
where
!x :: Word#
x = Int# -> Word#
int2Word# (Char# -> Int#
ord# Char#
c)
mask2 :: Word#
mask2 = Word#
0x0000bfdf##
mask3 :: Word#
mask3 = Word#
0x00bfbfef##
mask4 :: Word#
mask4 = Word#
0xbfbfbff7##
set2 :: Word#
set2 = Word#
0x000080c0##
set3 :: Word#
set3 = Word#
0x008080e0##
set4 :: Word#
set4 = Word#
0x808080f0##
encode2 :: Word#
encode2 = Word# -> Word# -> Word#
and# Word#
mask2 (Word# -> Word# -> Word# -> Word#
or3# Word#
set2
(Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#)
(Word# -> Int# -> Word#
uncheckedShiftL# Word#
x Int#
8# )
)
encode3 :: Word#
encode3 = Word# -> Word# -> Word#
and# Word#
mask3 (Word# -> Word# -> Word# -> Word# -> Word#
or4# Word#
set3
(Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
12#)
(Word# -> Word# -> Word#
and# Word#
0x3f00## (Word# -> Int# -> Word#
uncheckedShiftL# Word#
x Int#
2#))
(Word# -> Int# -> Word#
uncheckedShiftL# Word#
x Int#
16# )
)
encode4 :: Word#
encode4 = Word# -> Word# -> Word#
and# Word#
mask4 (Word# -> Word# -> Word# -> Word# -> Word#
or4# Word#
set4
(Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
18#)
(Word# -> Word# -> Word#
or# (Word# -> Word# -> Word#
and# Word#
0x3f00## (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
4#))
(Word# -> Word# -> Word#
and# Word#
0x3f0000## (Word# -> Int# -> Word#
uncheckedShiftL# Word#
x Int#
10#))
)
(Word# -> Int# -> Word#
uncheckedShiftL# Word#
x Int#
24# )
)
decodeCharUTF8 :: CharUTF8 -> Char
decodeCharUTF8 :: CharUTF8 -> Char
decodeCharUTF8 c :: CharUTF8
c@(CharUTF8 !(W32# Word32#
w_))
| CharUTF8 -> Bool
isCharUTF8Case1 CharUTF8
c = Word# -> Char
toChar# Word#
w
| CharUTF8 -> Bool
isCharUTF8Case2 CharUTF8
c = Char
encode2
| CharUTF8 -> Bool
isCharUTF8Case3 CharUTF8
c = Char
encode3
| Bool
otherwise = Char
encode4
where
w :: Word#
w = Word32# -> Word#
word32ToWord# Word32#
w_
encode2 :: Char
encode2 =
Word# -> Char
toChar# (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader2# Word#
w) Int#
6#)
(Word# -> Word#
maskContinuation# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
8#))
)
encode3 :: Char
encode3 =
Word# -> Char
toChar# (Word# -> Word# -> Word# -> Word#
or3# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader3# Word#
w) Int#
12#)
(Word# -> Int# -> Word#
uncheckedShiftRL# (Word# -> Word# -> Word#
and# Word#
0x3f00## Word#
w) Int#
8#)
(Word# -> Word#
maskContinuation# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
16#))
)
encode4 :: Char
encode4 =
Word# -> Char
toChar# (Word# -> Word# -> Word# -> Word# -> Word#
or4# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word#
maskHeader4# Word#
w) Int#
18#)
(Word# -> Int# -> Word#
uncheckedShiftRL# (Word# -> Word# -> Word#
and# Word#
0x3f00## Word#
w) Int#
10#)
(Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
0x3f0000## Word#
w) Int#
4#)
(Word# -> Word#
maskContinuation# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
24#))
)
isCharUTF8Case1 :: CharUTF8 -> Bool
isCharUTF8Case1 :: CharUTF8 -> Bool
isCharUTF8Case1 (CharUTF8 !Word32
w) = (Word32
w forall bits. BitOps bits => bits -> bits -> bits
.&. Word32
0x80) forall a. Eq a => a -> a -> Bool
== Word32
0
{-# INLINE isCharUTF8Case1 #-}
isCharUTF8Case2 :: CharUTF8 -> Bool
isCharUTF8Case2 :: CharUTF8 -> Bool
isCharUTF8Case2 (CharUTF8 !Word32
w) = (Word32
w forall bits. BitOps bits => bits -> bits -> bits
.&. Word32
0x20) forall a. Eq a => a -> a -> Bool
== Word32
0
{-# INLINE isCharUTF8Case2 #-}
isCharUTF8Case3 :: CharUTF8 -> Bool
isCharUTF8Case3 :: CharUTF8 -> Bool
isCharUTF8Case3 (CharUTF8 !Word32
w) = (Word32
w forall bits. BitOps bits => bits -> bits -> bits
.&. Word32
0x10) forall a. Eq a => a -> a -> Bool
== Word32
0
{-# INLINE isCharUTF8Case3 #-}
isCharUTF8Case4 :: CharUTF8 -> Bool
isCharUTF8Case4 :: CharUTF8 -> Bool
isCharUTF8Case4 (CharUTF8 !Word32
w) = (Word32
w forall bits. BitOps bits => bits -> bits -> bits
.&. Word32
0x08) forall a. Eq a => a -> a -> Bool
== Word32
0
{-# INLINE isCharUTF8Case4 #-}