{-# LANGUAGE CPP, DeriveDataTypeable #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.Text.Internal
(
Text(..)
, text
, textP
, safe
, empty
, firstf
, showText
) where
#if defined(ASSERTS)
import Control.Exception (assert)
#endif
import Data.Bits ((.&.))
import qualified Data.Text.Array as A
import Data.Text.Internal.Unsafe.Char (ord)
import Data.Typeable (Typeable)
data Text = Text
{-# UNPACK #-} !A.Array
{-# UNPACK #-} !Int
{-# UNPACK #-} !Int
deriving (Typeable)
text_ :: A.Array -> Int -> Int -> Text
text_ arr off len =
#if defined(ASSERTS)
let c = A.unsafeIndex arr off
alen = A.length arr
in assert (len >= 0) .
assert (off >= 0) .
assert (alen == 0 || len == 0 || off < alen) .
assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
#endif
Text arr off len
{-# INLINE text_ #-}
empty :: Text
empty = Text A.empty 0 0
{-# INLINE [1] empty #-}
text :: A.Array -> Int -> Int -> Text
text arr off len | len == 0 = empty
| otherwise = text_ arr off len
{-# INLINE text #-}
textP :: A.Array -> Int -> Int -> Text
{-# DEPRECATED textP "Use text instead" #-}
textP = text
showText :: Text -> String
showText (Text arr off len) =
"Text " ++ show (A.toList arr off len) ++ ' ' :
show off ++ ' ' : show len
safe :: Char -> Char
safe c
| ord c .&. 0x1ff800 /= 0xd800 = c
| otherwise = '\xfffd'
{-# INLINE safe #-}
firstf :: (a -> c) -> Maybe (a,b) -> Maybe (c,b)
firstf f (Just (a, b)) = Just (f a, b)
firstf _ Nothing = Nothing