1 {-# LANGUAGE CPP, DeriveDataTypeable #-}
    2 
    3 -- |
    4 -- Module      : Data.Text.Internal
    5 -- Copyright   : (c) 2008, 2009 Tom Harper,
    6 --               (c) 2009, 2010 Bryan O'Sullivan,
    7 --               (c) 2009 Duncan Coutts
    8 --
    9 -- License     : BSD-style
   10 -- Maintainer  : bos@serpentine.com, rtomharper@googlemail.com,
   11 --               duncan@haskell.org
   12 -- Stability   : experimental
   13 -- Portability : GHC
   14 --
   15 -- Semi-public internals.  Most users should not need to use this
   16 -- module.
   17 
   18 module Data.Text.Internal
   19     (
   20     -- * Types
   21       Text(..)
   22     -- * Construction
   23     , text
   24     , textP
   25     -- * Code that must be here for accessibility
   26     , empty
   27     -- * Debugging
   28     , showText
   29     ) where
   30 
   31 #if defined(ASSERTS)
   32 import Control.Exception (assert)
   33 #endif
   34 import qualified Data.Text.Array as A
   35 import Data.Typeable (Typeable)
   36 
   37 -- | A space efficient, packed, unboxed Unicode text type.
   38 data Text = Text
   39     {-# UNPACK #-} !A.Array          -- payload
   40     {-# UNPACK #-} !Int              -- offset
   41     {-# UNPACK #-} !Int              -- length
   42     deriving (-- never enteredTypeable)
   43 
   44 -- | Smart constructor.
   45 text :: A.Array -> Int -> Int -> Text
   46 -- entered 196,699 timestext arr off len =
   47 #if defined(ASSERTS)
   48   let c    = A.unsafeIndex arr off
   49       alen = A.length arr
   50   in assert (len >= 0) .
   51      assert (off >= 0) .
   52      assert (alen == 0 || len == 0 || off < alen) .
   53      assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
   54 #endif
   55      Text arr off len
   56 {-# INLINE text #-}
   57 
   58 -- | /O(1)/ The empty 'Text'.
   59 empty :: Text
   60 -- entered 5642 timesempty = Text A.empty 0 0
   61 {-# INLINE [1] empty #-}
   62 
   63 -- | Construct a 'Text' without invisibly pinning its byte array in
   64 -- memory if its length has dwindled to zero.
   65 textP :: A.Array -> Int -> Int -> Text
   66 -- entered 165,658 timestextP arr off len | len == 0  = empty
   67                   | otherwise = text arr off len
   68 {-# INLINE textP #-}
   69 
   70 -- | A useful 'show'-like function for debugging purposes.
   71 showText :: Text -> String
   72 -- never enteredshowText (Text arr off len) =
   73     "Text " ++ show (A.toList arr off len) ++ ' ' :
   74             show off ++ ' ' : show len