on master help sign in
Use a specialised tuple for iter

--HG-- extra : convert_revision : d004bfe268c82c4c08a9cca315bef0c130611c11

commit6b87918825402decf08057e409d7c9f967b4065e authorBryan O'Sullivan <bos@serpentine.com> dateSun 15 Aug 2010, 21:01 (16 years ago) parentdfd8d2c
3 files changed+16−13
Data/Text.hs+8−8 Data/Text/Lazy/Fusion.hs+2−2 Data/Text/Unsafe.hs+6−3
Data/Text.hs+8−8
@@ -195,7 +195,7 @@ import qualified Data.Text.Fusion.Common as S
195195import Data.Text.Fusion (stream, reverseStream, unstream)
196196import Data.Text.Internal (Text(..), empty, text, textP)
197197import qualified Prelude as P
198−import Data.Text.Unsafe (iter, iter_, reverseIter, unsafeHead, unsafeTail)
198+import Data.Text.Unsafe (Iter(..), iter, iter_, reverseIter, unsafeHead, unsafeTail)
199199import Data.Text.UnsafeChar (unsafeChr)
200200import qualified Data.Text.Encoding.Utf16 as U16
201201import Data.Text.Search (indices)
@@ -329,7 +329,7 @@ uncons :: Text -> Maybe (Char, Text)
329329uncons t@(Text arr off len)
330330 | len <= 0 = Nothing
331331 | otherwise = Just (c, textP arr (off+d) (len-d))
332− where (c,d) = iter t 0
332+ where Iter c d = iter t 0
333333{-# INLINE [1] uncons #-}
334334
335335-- | Lifted from Control.Arrow and specialized.
@@ -811,7 +811,7 @@ takeWhile p t@(Text arr off len) = loop 0
811811 where loop !i | i >= len = t
812812 | p c = loop (i+d)
813813 | otherwise = textP arr off i
814− where (c,d) = iter t i
814+ where Iter c d = iter t i
815815{-# INLINE [1] takeWhile #-}
816816
817817{-# RULES
@@ -828,7 +828,7 @@ dropWhile p t@(Text arr off len) = loop 0 0
828828 where loop !i !l | l >= len = empty
829829 | p c = loop (i+d) (l+d)
830830 | otherwise = Text arr (off+i) (len-l)
831− where (c,d) = iter t i
831+ where Iter c d = iter t i
832832{-# INLINE [1] dropWhile #-}
833833
834834{-# RULES
@@ -912,7 +912,7 @@ spanBy p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
912912 where k = loop 0
913913 loop !i | i >= len || not (p c) = i
914914 | otherwise = loop (i+d)
915− where (c,d) = iter t i
915+ where Iter c d = iter t i
916916{-# INLINE spanBy #-}
917917
918918-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is
@@ -928,7 +928,7 @@ groupBy p = loop
928928 loop t@(Text arr off len)
929929 | null t = []
930930 | otherwise = text arr off n : loop (text arr (off+n) (len-n))
931− where (c,d) = iter t 0
931+ where Iter c d = iter t 0
932932 n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
933933
934934-- | Returns the /array/ index (in units of 'Word16') at which a
@@ -938,7 +938,7 @@ findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
938938findAIndexOrEnd q t@(Text _arr _off len) = go 0
939939 where go !i | i >= len || q c = i
940940 | otherwise = go (i+d)
941− where (c,d) = iter t i
941+ where Iter c d = iter t i
942942
943943-- | /O(n)/ Group characters in a string by equality.
944944group :: Text -> [Text]
@@ -1209,7 +1209,7 @@ words t@(Text arr off len) = loop 0 0
12091209 then loop (start+1) (start+1)
12101210 else Text arr (start+off) (n-start) : loop (n+d) (n+d)
12111211 | otherwise = loop start (n+d)
1212− where (c,d) = iter t n
1212+ where Iter c d = iter t n
12131213{-# INLINE words #-}
12141214
12151215-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
Data/Text/Lazy/Fusion.hs+2−2
@@ -30,7 +30,7 @@ import qualified Data.Text.Internal as I
3030import qualified Data.Text.Array as A
3131import Data.Text.UnsafeChar (unsafeWrite)
3232import Data.Text.UnsafeShift (shiftL)
33−import Data.Text.Unsafe (iter)
33+import Data.Text.Unsafe (Iter(..), iter)
3434import Data.Int (Int64)
3535
3636default(Int64)
@@ -43,7 +43,7 @@ stream text = Stream next (text :*: 0) 4 -- random HINT
4343 next (txt@(Chunk t@(I.Text _ _ len) ts) :*: i)
4444 | i >= len = next (ts :*: 0)
4545 | otherwise = Yield c (txt :*: i+d)
46− where (c,d) = iter t i
46+ where Iter c d = iter t i
4747{-# INLINE [0] stream #-}
4848
4949-- | /O(n)/ Convert a 'Stream Char' into a 'Text', using the given
Data/Text/Unsafe.hs+6−3
@@ -14,6 +14,7 @@ module Data.Text.Unsafe
1414 (
1515 inlineInterleaveST
1616 , inlinePerformIO
17+ , Iter(..)
1718 , iter
1819 , iter_
1920 , reverseIter
@@ -61,13 +62,15 @@ unsafeTail t@(Text arr off len) =
6162 where d = iter_ t 0
6263{-# INLINE unsafeTail #-}
6364
65+data Iter = Iter {-# UNPACK #-} !Char {-# UNPACK #-} !Int
66+
6467-- | /O(1)/ Iterate (unsafely) one step forwards through a UTF-16
6568-- array, returning the current character and the delta to add to give
6669-- the next offset to iterate at.
67−iter :: Text -> Int -> (Char,Int)
70+iter :: Text -> Int -> Iter
6871iter (Text arr off _len) i
69− | m < 0xD800 || m > 0xDBFF = (unsafeChr m, 1)
70− | otherwise = (chr2 m n, 2)
72+ | m < 0xD800 || m > 0xDBFF = Iter (unsafeChr m) 1
73+ | otherwise = Iter (chr2 m n) 2
7174 where m = A.unsafeIndex arr j
7275 n = A.unsafeIndex arr k
7376 j = off + i