File: Utf16View.hs

package info (click to toggle)
haskell-text-builder-dev 0.3.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 124 kB
  • sloc: haskell: 1,186; makefile: 5
file content (48 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download
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
module TextBuilderDev.Utf16View where

import TextBuilderDev.Prelude
import qualified TextBuilderDev.Unicode as Unicode

-- |
-- A matching function, which chooses the continuation to run.
type Utf16View =
  forall x. (Word16 -> x) -> (Word16 -> Word16 -> x) -> x

{-# INLINE char #-}
char :: Char -> Utf16View
char x =
  unicodeCodePoint (ord x)

{-# INLINE unicodeCodePoint #-}
unicodeCodePoint :: Int -> Utf16View
unicodeCodePoint x case1 case2 =
  if x < 0x10000
    then case1 (fromIntegral x)
    else case2 case2Unit1 case2Unit2
  where
    m =
      x - 0x10000
    case2Unit1 =
      fromIntegral (shiftR m 10 + 0xD800)
    case2Unit2 =
      fromIntegral ((m .&. 0x3FF) + 0xDC00)

{-# INLINE utf8CodeUnits1 #-}
utf8CodeUnits1 :: Word8 -> Utf16View
utf8CodeUnits1 x case1 _ =
  case1 (fromIntegral x)

{-# INLINE utf8CodeUnits2 #-}
utf8CodeUnits2 :: Word8 -> Word8 -> Utf16View
utf8CodeUnits2 byte1 byte2 case1 _ =
  case1 (shiftL (fromIntegral byte1 - 0xC0) 6 + fromIntegral byte2 - 0x80)

{-# INLINE utf8CodeUnits3 #-}
utf8CodeUnits3 :: Word8 -> Word8 -> Word8 -> Utf16View
utf8CodeUnits3 byte1 byte2 byte3 =
  unicodeCodePoint (Unicode.utf8CodeUnits3 byte1 byte2 byte3)

{-# INLINE utf8CodeUnits4 #-}
utf8CodeUnits4 :: Word8 -> Word8 -> Word8 -> Word8 -> Utf16View
utf8CodeUnits4 byte1 byte2 byte3 byte4 =
  unicodeCodePoint (Unicode.utf8CodeUnits4 byte1 byte2 byte3 byte4)