on master help sign in
Rename textP to text

Replace the old text smart constructor with the slightly smarter one we've had all along that ensures that it doesn't pin its array if it's empty.

commit7217d9336996a6e1a1b97f7e73b811f1364e718f authorBryan O'Sullivan <bos@serpentine.com> dateFri 7 Mar 2014, 15:14 (13 years ago) parente2334d9
6 files changed+43−39
Data/Text.hs+20−20 Data/Text/Encoding.hs+3−3 Data/Text/Internal.hs+10−6 Data/Text/Internal/Fusion.hs+2−2 Data/Text/Internal/Private.hs+4−4 Data/Text/Lazy.hs+4−4
Data/Text.hs+20−20
@@ -213,7 +213,7 @@ import qualified Data.Text.Internal.Fusion as S
213213import qualified Data.Text.Internal.Fusion.Common as S
214214import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
215215import Data.Text.Internal.Private (span_)
216−import Data.Text.Internal (Text(..), empty, firstf, safe, textP)
216+import Data.Text.Internal (Text(..), empty, firstf, safe, text)
217217import qualified Prelude as P
218218import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
219219 reverseIter_, unsafeHead, unsafeTail)
@@ -452,7 +452,7 @@ head t = S.head (stream t)
452452uncons :: Text -> Maybe (Char, Text)
453453uncons t@(Text arr off len)
454454 | len <= 0 = Nothing
455− | otherwise = Just (c, textP arr (off+d) (len-d))
455+ | otherwise = Just (c, text arr (off+d) (len-d))
456456 where Iter c d = iter t 0
457457{-# INLINE [1] uncons #-}
458458
@@ -483,7 +483,7 @@ last (Text arr off len)
483483tail :: Text -> Text
484484tail t@(Text arr off len)
485485 | len <= 0 = emptyError "tail"
486− | otherwise = textP arr (off+d) (len-d)
486+ | otherwise = text arr (off+d) (len-d)
487487 where d = iter_ t 0
488488{-# INLINE [1] tail #-}
489489
@@ -498,8 +498,8 @@ tail t@(Text arr off len)
498498-- be non-empty. Subject to fusion.
499499init :: Text -> Text
500500init (Text arr off len) | len <= 0 = emptyError "init"
501− | n >= 0xDC00 && n <= 0xDFFF = textP arr off (len-2)
502− | otherwise = textP arr off (len-1)
501+ | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
502+ | otherwise = text arr off (len-1)
503503 where
504504 n = A.unsafeIndex arr (off+len-1)
505505{-# INLINE [1] init #-}
@@ -1004,7 +1004,7 @@ take :: Int -> Text -> Text
10041004take n t@(Text arr off len)
10051005 | n <= 0 = empty
10061006 | n >= len = t
1007− | otherwise = textP arr off (iterN n t)
1007+ | otherwise = text arr off (iterN n t)
10081008{-# INLINE [1] take #-}
10091009
10101010iterN :: Int -> Text -> Int
@@ -1031,7 +1031,7 @@ takeEnd :: Int -> Text -> Text
10311031takeEnd n t@(Text arr off len)
10321032 | n <= 0 = empty
10331033 | n >= len = t
1034− | otherwise = textP arr (off+i) (len-i)
1034+ | otherwise = text arr (off+i) (len-i)
10351035 where i = iterNEnd n t
10361036
10371037iterNEnd :: Int -> Text -> Int
@@ -1049,7 +1049,7 @@ drop :: Int -> Text -> Text
10491049drop n t@(Text arr off len)
10501050 | n <= 0 = t
10511051 | n >= len = empty
1052− | otherwise = textP arr (off+i) (len-i)
1052+ | otherwise = text arr (off+i) (len-i)
10531053 where i = iterN n t
10541054{-# INLINE [1] drop #-}
10551055
@@ -1070,7 +1070,7 @@ dropEnd :: Int -> Text -> Text
10701070dropEnd n t@(Text arr off len)
10711071 | n <= 0 = t
10721072 | n >= len = empty
1073− | otherwise = textP arr off (iterNEnd n t)
1073+ | otherwise = text arr off (iterNEnd n t)
10741074
10751075-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
10761076-- returns the longest prefix (possibly empty) of elements that
@@ -1079,7 +1079,7 @@ takeWhile :: (Char -> Bool) -> Text -> Text
10791079takeWhile p t@(Text arr off len) = loop 0
10801080 where loop !i | i >= len = t
10811081 | p c = loop (i+d)
1082− | otherwise = textP arr off i
1082+ | otherwise = text arr off i
10831083 where Iter c d = iter t i
10841084{-# INLINE [1] takeWhile #-}
10851085
@@ -1165,7 +1165,7 @@ splitAt n t@(Text arr off len)
11651165 | n <= 0 = (empty, t)
11661166 | n >= len = (t, empty)
11671167 | otherwise = let k = iterN n t
1168− in (textP arr off k, textP arr (off+k) (len-k))
1168+ in (text arr off k, text arr (off+k) (len-k))
11691169
11701170-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
11711171-- a pair whose first element is the longest prefix (possibly empty)
@@ -1188,7 +1188,7 @@ groupBy p = loop
11881188 where
11891189 loop t@(Text arr off len)
11901190 | null t = []
1191− | otherwise = textP arr off n : loop (textP arr (off+n) (len-n))
1191+ | otherwise = text arr off n : loop (text arr (off+n) (len-n))
11921192 where Iter c d = iter t 0
11931193 n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
11941194
@@ -1247,8 +1247,8 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
12471247 | isSingleton pat = split (== unsafeHead pat) src
12481248 | otherwise = go 0 (indices pat src)
12491249 where
1250− go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
1251− go s _ = [textP arr (s+off) (len-s)]
1250+ go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1251+ go s _ = [text arr (s+off) (len-s)]
12521252{-# INLINE [1] splitOn #-}
12531253
12541254{-# RULES
@@ -1340,7 +1340,7 @@ breakOn pat src@(Text arr off len)
13401340 | null pat = emptyError "breakOn"
13411341 | otherwise = case indices pat src of
13421342 [] -> (src, empty)
1343− (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1343+ (x:_) -> (text arr off x, text arr (off+x) (len-x))
13441344{-# INLINE breakOn #-}
13451345
13461346-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1382,7 +1382,7 @@ breakOnAll pat src@(Text arr off slen)
13821382 | otherwise = L.map step (indices pat src)
13831383 where
13841384 step x = (chunk 0 x, chunk x (slen-x))
1385− chunk !n !l = textP arr (n+off) l
1385+ chunk !n !l = text arr (n+off) l
13861386{-# INLINE breakOnAll #-}
13871387
13881388-------------------------------------------------------------------------------
@@ -1582,7 +1582,7 @@ isInfixOf needle haystack
15821582-- > fnordLength _ = -1
15831583stripPrefix :: Text -> Text -> Maybe Text
15841584stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1585− | p `isPrefixOf` t = Just $! textP arr (off+plen) (len-plen)
1585+ | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
15861586 | otherwise = Nothing
15871587
15881588-- | /O(n)/ Find the longest non-empty common prefix of two strings
@@ -1602,8 +1602,8 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
16021602 where
16031603 go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
16041604 | i > 0 = Just (Text arr0 off0 i,
1605− textP arr0 (off0+i) (len0-i),
1606− textP arr1 (off1+j) (len1-j))
1605+ text arr0 (off0+i) (len0-i),
1606+ text arr1 (off1+j) (len1-j))
16071607 | otherwise = Nothing
16081608 where Iter a d0 = iter t0 i
16091609 Iter b d1 = iter t1 j
@@ -1628,7 +1628,7 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
16281628-- > quuxLength _ = -1
16291629stripSuffix :: Text -> Text -> Maybe Text
16301630stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1631− | p `isSuffixOf` t = Just $! textP arr off (len-plen)
1631+ | p `isSuffixOf` t = Just $! text arr off (len-plen)
16321632 | otherwise = Nothing
16331633
16341634-- | Add a list of non-negative numbers. Errors out on overflow.
Data/Text/Encoding.hs+3−3
@@ -86,7 +86,7 @@ import Data.ByteString as B
8686import Data.ByteString.Internal as B hiding (c2w)
8787import Data.Text ()
8888import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
89−import Data.Text.Internal (Text(..), safe, textP)
89+import Data.Text.Internal (Text(..), safe, text)
9090import Data.Text.Internal.Private (runText)
9191import Data.Text.Internal.Unsafe.Char (unsafeWrite)
9292import Data.Text.Internal.Unsafe.Shift (shiftR)
@@ -130,7 +130,7 @@ decodeASCII = decodeUtf8
130130-- 'decodeLatin1' is semantically equivalent to
131131-- @Data.Text.pack . Data.ByteString.Char8.unpack@
132132decodeLatin1 :: ByteString -> Text
133−decodeLatin1 (PS fp off len) = textP a 0 len
133+decodeLatin1 (PS fp off len) = text a 0 len
134134 where
135135 a = A.run (A.new len >>= unsafeIOToST . go)
136136 go dest = withForeignPtr fp $ \ptr -> do
@@ -285,7 +285,7 @@ streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
285285 codepoint <- peek codepointPtr
286286 chunkText <- unsafeSTToIO $ do
287287 arr <- A.unsafeFreeze dest
288− return $! textP arr 0 (fromIntegral n)
288+ return $! text arr 0 (fromIntegral n)
289289 lastPtr <- peek curPtrPtr
290290 let left = lastPtr `minusPtr` curPtr
291291 undecoded = case state of
Data/Text/Internal.hs+10−6
@@ -54,8 +54,8 @@ data Text = Text
5454 deriving (Typeable)
5555
5656-- | Smart constructor.
57−text :: A.Array -> Int -> Int -> Text
58−text arr off len =
57+text_ :: A.Array -> Int -> Int -> Text
58+text_ arr off len =
5959#if defined(ASSERTS)
6060 let c = A.unsafeIndex arr off
6161 alen = A.length arr
@@ -65,7 +65,7 @@ text arr off len =
6565 assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
6666#endif
6767 Text arr off len
68−{-# INLINE text #-}
68+{-# INLINE text_ #-}
6969
7070-- | /O(1)/ The empty 'Text'.
7171empty :: Text
@@ -74,10 +74,14 @@ empty = Text A.empty 0 0
7474
7575-- | Construct a 'Text' without invisibly pinning its byte array in
7676-- memory if its length has dwindled to zero.
77−textP :: A.Array -> Int -> Int -> Text
78−textP arr off len | len == 0 = empty
77+text :: A.Array -> Int -> Int -> Text
78+text arr off len | len == 0 = empty
7979 | otherwise = text arr off len
80−{-# INLINE textP #-}
80+{-# INLINE text #-}
81+
82+textP :: A.Array -> Int -> Int -> Text
83+{-# DEPRECATED textP "Use text instead" #-}
84+textP = text
8185
8286-- | A useful 'show'-like function for debugging purposes.
8387showText :: Text -> String
Data/Text/Internal/Fusion.hs+2−2
@@ -132,7 +132,7 @@ length = S.lengthI
132132reverse :: Stream Char -> Text
133133reverse (Stream next s len0)
134134 | isEmpty len0 = I.empty
135− | otherwise = I.textP arr off' len'
135+ | otherwise = I.text arr off' len'
136136 where
137137 len0' = upperBound 4 (larger len0 4)
138138 (arr, (off', len')) = A.run2 (A.new len0' >>= loop s (len0'-1) len0')
@@ -210,7 +210,7 @@ countChar = S.countCharI
210210-- function to each element of a 'Text', passing an accumulating
211211-- parameter from left to right, and returns a final 'Text'.
212212mapAccumL :: (a -> Char -> (a,Char)) -> a -> Stream Char -> (a, Text)
213−mapAccumL f z0 (Stream next0 s0 len) = (nz,I.textP na 0 nl)
213+mapAccumL f z0 (Stream next0 s0 len) = (nz, I.text na 0 nl)
214214 where
215215 (na,(nz,nl)) = A.run2 (A.new mlen >>= \arr -> outer arr mlen z0 s0 0)
216216 where mlen = upperBound 4 len
Data/Text/Internal/Private.hs+4−4
@@ -16,14 +16,14 @@ module Data.Text.Internal.Private
1616 ) where
1717
1818import Control.Monad.ST (ST, runST)
19−import Data.Text.Internal (Text(..), textP)
19+import Data.Text.Internal (Text(..), text)
2020import Data.Text.Unsafe (Iter(..), iter)
2121import qualified Data.Text.Array as A
2222
2323span_ :: (Char -> Bool) -> Text -> (# Text, Text #)
2424span_ p t@(Text arr off len) = (# hd,tl #)
25− where hd = textP arr off k
26− tl = textP arr (off+k) (len-k)
25+ where hd = text arr off k
26+ tl = text arr (off+k) (len-k)
2727 !k = loop 0
2828 loop !i | i < len && p c = loop (i+d)
2929 | otherwise = i
@@ -33,5 +33,5 @@ span_ p t@(Text arr off len) = (# hd,tl #)
3333runText :: (forall s. (A.MArray s -> Int -> ST s Text) -> ST s Text) -> Text
3434runText act = runST (act $ \ !marr !len -> do
3535 arr <- A.unsafeFreeze marr
36− return $! textP arr 0 len)
36+ return $! text arr 0 len)
3737{-# INLINE runText #-}
Data/Text/Lazy.hs+4−4
@@ -215,7 +215,7 @@ import qualified Data.Text.Internal.Lazy.Fusion as S
215215import Data.Text.Internal.Fusion.Types (PairS(..))
216216import Data.Text.Internal.Lazy.Fusion (stream, unstream)
217217import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks, foldrChunks)
218−import Data.Text.Internal (firstf, safe, textP)
218+import Data.Text.Internal (firstf, safe, text)
219219import qualified Data.Text.Internal.Functions as F
220220import Data.Text.Internal.Lazy.Search (indices)
221221#if __GLASGOW_HASKELL__ >= 702
@@ -1002,7 +1002,7 @@ dropWords i t0
10021002 where drop' 0 ts = ts
10031003 drop' _ Empty = Empty
10041004 drop' n (Chunk (T.Text arr off len) ts)
1005− | n < len' = chunk (textP arr (off+n') (len-n')) ts
1005+ | n < len' = chunk (text arr (off+n') (len-n')) ts
10061006 | otherwise = drop' (n - len') ts
10071007 where len' = fromIntegral len
10081008 n' = fromIntegral n
@@ -1113,8 +1113,8 @@ splitAtWord _ Empty = empty :*: empty
11131113splitAtWord x (Chunk c@(T.Text arr off len) cs)
11141114 | y >= len = let h :*: t = splitAtWord (x-fromIntegral len) cs
11151115 in Chunk c h :*: t
1116− | otherwise = chunk (textP arr off y) empty :*:
1117− chunk (textP arr (off+y) (len-y)) cs
1116+ | otherwise = chunk (text arr off y) empty :*:
1117+ chunk (text arr (off+y) (len-y)) cs
11181118 where y = fromIntegral x
11191119
11201120-- | /O(n+m)/ Find the first instance of @needle@ (which must be