File: Utf8View.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 (33 lines) | stat: -rw-r--r-- 990 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
module TextBuilderDev.Utf8View where

import TextBuilderDev.Prelude

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

{-# INLINE unicodeCodePoint #-}
unicodeCodePoint :: Int -> Utf8View
unicodeCodePoint x case1 case2 case3 case4
  | x < 0x80 = case1 (fromIntegral x)
  | x < 0x800 =
      case2
        (fromIntegral $ x `shiftR` 6 .|. 0xC0)
        (fromIntegral $ (x .&. 0x3F) .|. 0x80)
  | x < 0x10000 =
      case3
        (fromIntegral $ x `shiftR` 12 .|. 0xE0)
        (fromIntegral $ (x `shiftR` 6) .&. 0x3F .|. 0x80)
        (fromIntegral $ (x .&. 0x3F) .|. 0x80)
  | otherwise =
      case4
        (fromIntegral $ x `shiftR` 18 .|. 0xF0)
        (fromIntegral $ (x `shiftR` 12) .&. 0x3F .|. 0x80)
        (fromIntegral $ (x `shiftR` 6) .&. 0x3F .|. 0x80)
        (fromIntegral $ (x .&. 0x3F) .|. 0x80)