File: Internal.hs

package info (click to toggle)
haskell-text-icu 0.8.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: haskell: 1,210; ansic: 1,147; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 2,146 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE DeriveDataTypeable, EmptyDataDecls, ForeignFunctionInterface #-}
-- |
-- Module      : Data.Text.ICU.Spoof.Internal
-- Copyright   : (c) 2015 Ben Hamilton
--
-- License     : BSD-style
-- Maintainer  : bgertzfield@gmail.com
-- Stability   : experimental
-- Portability : GHC
--
-- Internals of the spoofability check infrastructure.

module Data.Text.ICU.Spoof.Internal
    (
    -- * Unicode spoof checking API
    -- $api
    -- * Types
      MSpoof(..)
    , Spoof(..)
    , USpoof
    -- * Functions
    , withSpoof
    , wrap
    , wrapWithSerialized
    ) where

import Data.Typeable (Typeable)
import Data.Word (Word8)
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr)
import Foreign.Ptr (FunPtr, Ptr)
import Data.Text.ICU.Internal (newICUPtr)

-- $api
-- Low-level operations on spoof checkers.

-- | Opaque handle to a configurable spoof checker.
data USpoof

-- | Configurable spoof checker wrapping an opaque handle
-- and optionally wrapping a previously serialized instance.
data MSpoof = MSpoof {
    serializedBuf :: Maybe (ForeignPtr Word8)
  , spoofPtr :: {-# UNPACK #-} !(ForeignPtr USpoof)
} deriving (Typeable)

-- | Spoof checker type.
newtype Spoof = S MSpoof
    deriving (Typeable)

-- | Temporarily unwraps an 'MSpoof' to perform operations on its raw 'USpoof'
-- handle.
withSpoof :: MSpoof -> (Ptr USpoof -> IO a) -> IO a
withSpoof (MSpoof _ spoof) = withForeignPtr spoof
{-# INLINE withSpoof #-}

-- | Wraps a raw 'USpoof' handle in an 'MSpoof', closing the handle when
-- the last reference to the object is dropped.
wrap :: IO (Ptr USpoof) -> IO MSpoof
wrap = newICUPtr (MSpoof Nothing) uspoof_close
{-# INLINE wrap #-}

-- | Wraps a previously serialized spoof checker and raw 'USpoof' handle
-- in an 'MSpoof', closing the handle and releasing the 'ForeignPtr' when
-- the last reference to the object is dropped.
wrapWithSerialized :: ForeignPtr Word8 -> IO (Ptr USpoof) -> IO MSpoof
wrapWithSerialized s = newICUPtr (MSpoof $ Just s) uspoof_close
{-# INLINE wrapWithSerialized #-}

foreign import ccall unsafe "hs_text_icu.h &__hs_uspoof_close" uspoof_close
    :: FunPtr (Ptr USpoof -> IO ())