    module TextBuilderCore.Utf8View where

    import TextBuilderCore.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 :: Int -> Utf8View
    unicodeCodepoint Int
    x Word8 -> x
    case1 Word8 -> Word8 -> x
    case2 Word8 -> Word8 -> Word8 -> x
    case3 Word8 -> Word8 -> Word8 -> Word8 -> x
    case4
      | Int
    x Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0x80 = Word8 -> x
    case1 (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x)
      | Int
    x Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0x800 =
          Word8 -> Word8 -> x
    case2
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    6 Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0xC0)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)
      | Int
    x Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0x10000 =
          Word8 -> Word8 -> Word8 -> x
    case3
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    12 Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0xE0)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    6) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)
      | Bool
    otherwise =
          Word8 -> Word8 -> Word8 -> Word8 -> x
    case4
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    18 Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0xF0)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    12) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    6) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)
            (Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> Int -> Word8
    forall a b. (a -> b) -> a -> b
    $ (Int
    x Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .&. Int
    0x3F) Int -> Int -> Int
    forall a. Bits a => a -> a -> a
    .|. Int
    0x80)