on master help sign in
Expose and fix some subtle bugs.

--HG-- extra : convert_revision : f7287b8eec0048f6fb4da6a513ab0197df73c8ca

commitd387502a54f10fd3d1fcc1925ab1bc854988953f authorBryan O'Sullivan <bos@serpentine.com> dateSun 4 Oct 2009, 04:39 (17 years ago) parentd2e7cbe
4 files changed+14−14
Data/Text/Fusion.hs+5−6 Data/Text/Fusion/Common.hs+4−5 Data/Text/Fusion/Internal.hs+1−3 tests/Properties.hs+4
Data/Text/Fusion.hs+5−6
@@ -54,7 +54,7 @@ import Data.Bits ((.&.))
5454import Data.Char (ord)
5555import Data.Text.Internal (Text(..))
5656import Data.Text.UnsafeChar (unsafeChr, unsafeWrite)
57−import Data.Text.UnsafeShift (shiftR)
57+import Data.Text.UnsafeShift (shiftL, shiftR)
5858import qualified Data.Text.Array as A
5959import qualified Data.Text.Fusion.Common as S
6060import Data.Text.Fusion.Internal
@@ -97,9 +97,7 @@ reverseStream (Text arr off len) = Stream next (off+len-1) (maxSize len)
9797
9898-- | /O(n)/ Convert a 'Stream Char' into a 'Text'.
9999unstream :: Stream Char -> Text
100−unstream (Stream next0 s0 len)
101− | isEmpty len = I.empty
102− | otherwise = I.textP (P.fst a) 0 (P.snd a)
100+unstream (Stream next0 s0 len) = I.textP (P.fst a) 0 (P.snd a)
103101 where
104102 mlen = upperBound 4 len
105103 a = A.run2 (A.unsafeNew mlen >>= (\arr -> loop arr mlen s0 0))
@@ -107,8 +105,9 @@ unstream (Stream next0 s0 len)
107105 | i + 1 >= top = case next0 s of
108106 Done -> return (arr, i)
109107 _ -> do
110− arr' <- A.unsafeNew (top*2)
111− A.copy arr arr' >> loop arr' (top*2) s i
108+ let top' = (top `shiftL` 1) + 1
109+ arr' <- A.unsafeNew top'
110+ A.copy arr arr' >> loop arr' top' s i
112111 | otherwise = case next0 s of
113112 Done -> return (arr, i)
114113 Skip s' -> loop arr top s' i
Data/Text/Fusion/Common.hs+4−5
@@ -118,7 +118,6 @@ singleton c = Stream next False 1
118118
119119streamList :: [a] -> Stream a
120120{-# INLINE [0] streamList #-}
121−streamList [] = empty
122121streamList s = Stream next s unknownSize
123122 where next [] = Done
124123 next (x:xs) = Yield x xs
@@ -220,12 +219,12 @@ tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1)
220219 where
221220 {-# INLINE next #-}
222221 next (False :!: s) = case next0 s of
223− Done -> emptyError "tail"
224− Skip s' -> Skip (False :!: s')
222+ Done -> emptyError "tail"
223+ Skip s' -> Skip (False :!: s')
225224 Yield _ s' -> Skip (True :!: s')
226225 next (True :!: s) = case next0 s of
227− Done -> Done
228− Skip s' -> Skip (True :!: s')
226+ Done -> Done
227+ Skip s' -> Skip (True :!: s')
229228 Yield x s' -> Yield x (True :!: s')
230229{-# INLINE [0] tail #-}
231230
Data/Text/Fusion/Internal.hs+1−3
@@ -48,12 +48,10 @@ data Step s a = Done
4848 | Skip !s
4949 | Yield !a !s
5050
51−{-
52−instance Show a => Show (Step s a)
51+instance (Show a) => Show (Step s a)
5352 where show Done = "Done"
5453 show (Skip _) = "Skip"
5554 show (Yield x _) = "Yield " ++ show x
56−-}
5755
5856instance (Eq a) => Eq (Stream a) where
5957 (==) = eq
tests/Properties.hs+4
@@ -156,6 +156,7 @@ s_snoc x = (++ [x]) `eqP` (unpackS . (flip S.snoc) x)
156156t_snoc x = (++ [x]) `eqP` (unpackS . (flip T.snoc) x)
157157tl_snoc x = (++ [x]) `eqP` (unpackS . (flip TL.snoc) x)
158158s_append s = (s++) `eqP` (unpackS . S.append (S.streamList s))
159+s_append_s s = (s++) `eqP` (unpackS . S.unstream . S.append (S.streamList s))
159160sf_append p s = (L.filter p s++) `eqP` (unpackS . S.append (S.filter p $ S.streamList s))
160161t_append s = (s++) `eqP` (unpackS . T.append (packS s))
161162
@@ -175,6 +176,7 @@ sf_last p = (last . L.filter p) `eqP` (S.last . S.filter p)
175176t_last = last `eqP` T.last
176177tl_last = last `eqP` TL.last
177178s_tail = tail `eqP` (unpackS . S.tail)
179+s_tail_s = tail `eqP` (unpackS . S.unstream . S.tail)
178180sf_tail p = (tail . L.filter p) `eqP` (unpackS . S.tail . S.filter p)
179181t_tail = tail `eqP` (unpackS . T.tail)
180182tl_tail = tail `eqP` (unpackS . TL.tail)
@@ -585,6 +587,7 @@ tests = [
585587 testProperty "t_snoc" t_snoc,
586588 testProperty "tl_snoc" tl_snoc,
587589 testProperty "s_append" s_append,
590+ testProperty "s_append_s" s_append_s,
588591 testProperty "sf_append" sf_append,
589592 testProperty "t_append" t_append,
590593 testProperty "s_uncons" s_uncons,
@@ -600,6 +603,7 @@ tests = [
600603 testProperty "t_last" t_last,
601604 testProperty "tl_last" tl_last,
602605 testProperty "s_tail" s_tail,
606+ testProperty "s_tail_s" s_tail_s,
603607 testProperty "sf_tail" sf_tail,
604608 testProperty "t_tail" t_tail,
605609 testProperty "tl_tail" tl_tail,