on a80bdad help sign in
text/Data/Text/Internal.hs
74 lines 2.0 kB history raw
1{-# LANGUAGE CPP, DeriveDataTypeable #-}
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.
18module 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
31#if defined(ASSERTS)
32import Control.Exception (assert)
33#endif
34import qualified Data.Text.Array as A
35import Data.Typeable (Typeable)
37-- | A space efficient, packed, unboxed Unicode text type.
38data Text = Text
39 {-# UNPACK #-} !A.Array -- payload
40 {-# UNPACK #-} !Int -- offset
41 {-# UNPACK #-} !Int -- length
42 deriving (Typeable)
44-- | Smart constructor.
45text :: A.Array -> Int -> Int -> Text
46text 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 #-}
58-- | /O(1)/ The empty 'Text'.
59empty :: Text
60empty = Text A.empty 0 0
61{-# INLINE [1] empty #-}
63-- | Construct a 'Text' without invisibly pinning its byte array in
64-- memory if its length has dwindled to zero.
65textP :: A.Array -> Int -> Int -> Text
66textP arr off len | len == 0 = empty
67 | otherwise = text arr off len
68{-# INLINE textP #-}
70-- | A useful 'show'-like function for debugging purposes.
71showText :: Text -> String
72showText (Text arr off len) =
73 "Text " ++ show (A.toList arr off len) ++ ' ' :
74 show off ++ ' ' : show len