on 0.4 help sign in
text/Data/Text/Internal.hs
71 lines 2.0 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
3-- |
4-- Module : Data.Text.Internal
5-- Copyright : (c) Tom Harper 2008-2009,
6-- (c) Bryan O'Sullivan 2009,
7-- (c) Duncan Coutts 2009
8--
9-- License : BSD-style
10-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
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
31import Control.Exception (assert)
32import qualified Data.Text.Array as A
33import Data.Typeable (Typeable)
34import Data.Word (Word16)
36-- | A space efficient, packed, unboxed Unicode text type.
37data Text = Text
38 {-# UNPACK #-} !(A.Array Word16) -- payload
39 {-# UNPACK #-} !Int -- offset
40 {-# UNPACK #-} !Int -- length
41 deriving (Typeable)
43-- | Smart constructor.
44text :: A.Array Word16 -> Int -> Int -> Text
45text arr off len =
46 assert (len >= 0) .
47 assert (off >= 0) .
48 assert (alen == 0 || len == 0 || off < alen) .
49 assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
50 Text arr off len
51 where c = A.unsafeIndex arr off
52 alen = A.length arr
53{-# INLINE text #-}
55-- | /O(1)/ The empty 'Text'.
56empty :: Text
57empty = Text A.empty 0 0
58{-# INLINE [1] empty #-}
60-- | Construct a 'Text' without invisibly pinning its byte array in
61-- memory if its length has dwindled to zero.
62textP :: A.Array Word16 -> Int -> Int -> Text
63textP arr off len | len == 0 = empty
64 | otherwise = text arr off len
65{-# INLINE textP #-}
67-- | A useful 'show'-like function for debugging purposes.
68showText :: Text -> String
69showText (Text arr off len) =
70 "Text " ++ (show . take (off+len) . A.toList) arr ++ ' ' :
71 show off ++ ' ' : show len