on master help sign in
Switch to stricter Maybe type for streaming

--HG-- extra : convert_revision : b1de1eeab2ffe915e919a2857e82c506e720f482

commit1c007ddd6b359cf2366e5f28d3ad876a940ff2db authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 06:16 (18 years ago) parente9d0852
3 files changed+51−47
Data/Text/Encoding/Fusion.hs+3−5 Data/Text/Fusion/Common.hs+41−41 Data/Text/Fusion/Internal.hs+7−1
Data/Text/Encoding/Fusion.hs+3−5
@@ -44,6 +44,7 @@ import Data.ByteString as B
4444import Data.ByteString.Internal (ByteString(..), mallocByteString, memcpy)
4545import Data.Char (ord)
4646import Data.Text.Fusion (Step(..), Stream(..))
47+import Data.Text.Fusion.Internal (M(..))
4748import Data.Text.UnsafeChar (unsafeChr, unsafeChr8, unsafeChr32)
4849import Data.Word (Word8, Word16, Word32)
4950import Foreign.ForeignPtr (withForeignPtr, ForeignPtr)
@@ -55,14 +56,11 @@ import qualified Data.Text.Encoding.Utf8 as U8
5556import qualified Data.Text.Encoding.Utf16 as U16
5657import qualified Data.Text.Encoding.Utf32 as U32
5758
58−-- Specialised, strict Maybe-like type.
59−data M = N
60− | J {-# UNPACK #-} !Word8
61− deriving (Eq, Ord, Show)
59+type M8 = M Word8
6260
6361-- Restreaming state.
6462data S s = S {-# UNPACK #-} !s
65− {-# UNPACK #-} !M {-# UNPACK #-} !M {-# UNPACK #-} !M
63+ {-# UNPACK #-} !M8 {-# UNPACK #-} !M8 {-# UNPACK #-} !M8
6664
6765streamASCII :: ByteString -> Stream Char
6866streamASCII bs = Stream next 0 l
Data/Text/Fusion/Common.hs+41−41
@@ -138,14 +138,14 @@ cons w (Stream next0 s0 len) = Stream next (S2 :!: s0) (len+2) -- HINT maybe too
138138
139139-- | /O(n)/ Adds a character to the end of a stream.
140140snoc :: Stream Char -> Char -> Stream Char
141−snoc (Stream next0 xs0 len) w = Stream next (Just xs0) (len+2) -- HINT maybe too high
141+snoc (Stream next0 xs0 len) w = Stream next (J xs0) (len+2) -- HINT maybe too high
142142 where
143143 {-# INLINE next #-}
144− next (Just xs) = case next0 xs of
145− Done -> Yield w Nothing
146− Skip xs' -> Skip (Just xs')
147− Yield x xs' -> Yield x (Just xs')
148− next Nothing = Done
144+ next (J xs) = case next0 xs of
145+ Done -> Yield w N
146+ Skip xs' -> Skip (J xs')
147+ Yield x xs' -> Yield x (J xs')
148+ next N = Done
149149{-# INLINE [0] snoc #-}
150150
151151-- | /O(n)/ Appends one Stream to the other.
@@ -221,17 +221,17 @@ tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1) -- HINT maybe to
221221-- | /O(1)/ Returns all but the last character of a Stream Char, which
222222-- must be non-empty.
223223init :: Stream Char -> Stream Char
224−init (Stream next0 s0 len) = Stream next (Nothing :!: s0) (len-1) -- HINT maybe too high
224+init (Stream next0 s0 len) = Stream next (N :!: s0) (len-1) -- HINT maybe too high
225225 where
226226 {-# INLINE next #-}
227− next (Nothing :!: s) = case next0 s of
228− Done -> emptyError "init"
229− Skip s' -> Skip (Nothing :!: s')
230− Yield x s' -> Skip (Just x :!: s')
231− next (Just x :!: s) = case next0 s of
232− Done -> Done
233− Skip s' -> Skip (Just x :!: s')
234− Yield x' s' -> Yield x (Just x' :!: s')
227+ next (N :!: s) = case next0 s of
228+ Done -> emptyError "init"
229+ Skip s' -> Skip (N :!: s')
230+ Yield x s' -> Skip (J x :!: s')
231+ next (J x :!: s) = case next0 s of
232+ Done -> Done
233+ Skip s' -> Skip (J x :!: s')
234+ Yield x' s' -> Yield x (J x' :!: s')
235235{-# INLINE [0] init #-}
236236
237237-- | /O(1)/ Tests whether a Stream Char is empty or not.
@@ -277,18 +277,18 @@ map f (Stream next0 s0 len) = Stream next s0 len -- HINT depends on f
277277-- | /O(n)/ Take a character and place it between each of the
278278-- characters of a 'Stream Char'.
279279intersperse :: Char -> Stream Char -> Stream Char
280−intersperse c (Stream next0 s0 len) = Stream next (s0 :!: Nothing :!: S1) len -- HINT maybe too low
280+intersperse c (Stream next0 s0 len) = Stream next (s0 :!: N :!: S1) len -- HINT maybe too low
281281 where
282282 {-# INLINE next #-}
283− next (s :!: Nothing :!: S1) = case next0 s of
283+ next (s :!: N :!: S1) = case next0 s of
284284 Done -> Done
285− Skip s' -> Skip (s' :!: Nothing :!: S1)
286− Yield x s' -> Skip (s' :!: Just x :!: S1)
287− next (s :!: Just x :!: S1) = Yield x (s :!: Nothing :!: S2)
288− next (s :!: Nothing :!: S2) = case next0 s of
285+ Skip s' -> Skip (s' :!: N :!: S1)
286+ Yield x s' -> Skip (s' :!: J x :!: S1)
287+ next (s :!: J x :!: S1) = Yield x (s :!: N :!: S2)
288+ next (s :!: N :!: S2) = case next0 s of
289289 Done -> Done
290− Skip s' -> Skip (s' :!: Nothing :!: S2)
291− Yield x s' -> Yield c (s' :!: Just x :!: S1)
290+ Skip s' -> Skip (s' :!: N :!: S2)
291+ Yield x s' -> Yield c (s' :!: J x :!: S1)
292292 next _ = internalError "intersperse"
293293{-# INLINE [0] intersperse #-}
294294
@@ -550,19 +550,19 @@ take n0 (Stream next0 s0 len) = Stream next (n0 :!: s0) (min 0 (len - fromIntegr
550550-- length of the stream.
551551drop :: Integral a => a -> Stream Char -> Stream Char
552552drop n0 (Stream next0 s0 len) =
553− Stream next (Just ((max 0 n0)) :!: s0) (len - fromIntegral n0) -- HINT maybe too high
553+ Stream next (J (max 0 n0) :!: s0) (len - fromIntegral n0) -- HINT maybe too high
554554 where
555555 {-# INLINE next #-}
556− next (Just !n :!: s)
557− | n == 0 = Skip (Nothing :!: s)
556+ next (J n :!: s)
557+ | n == 0 = Skip (N :!: s)
558558 | otherwise = case next0 s of
559559 Done -> Done
560− Skip s' -> Skip (Just n :!: s')
561− Yield _ s' -> Skip (Just (n-1) :!: s')
562− next (Nothing :!: s) = case next0 s of
560+ Skip s' -> Skip (J n :!: s')
561+ Yield _ s' -> Skip (J (n-1) :!: s')
562+ next (N :!: s) = case next0 s of
563563 Done -> Done
564− Skip s' -> Skip (Nothing :!: s')
565− Yield x s' -> Yield x (Nothing :!: s')
564+ Skip s' -> Skip (N :!: s')
565+ Yield x s' -> Yield x (N :!: s')
566566{-# INLINE [0] drop #-}
567567
568568-- | takeWhile, applied to a predicate @p@ and a stream, returns the
@@ -703,18 +703,18 @@ findIndicesI p (Stream next s0 _len) = loop_findIndex 0 s0
703703-- | zipWith generalises 'zip' by zipping with the function given as
704704-- the first argument, instead of a tupling function.
705705zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
706−zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!: sb0 :!: Nothing) (min len1 len2)
706+zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!: sb0 :!: N) (min len1 len2)
707707 where
708708 {-# INLINE next #-}
709− next (sa :!: sb :!: Nothing) = case next0 sa of
710− Done -> Done
711− Skip sa' -> Skip (sa' :!: sb :!: Nothing)
712− Yield a sa' -> Skip (sa' :!: sb :!: Just a)
713−
714− next (sa' :!: sb :!: Just a) = case next1 sb of
715− Done -> Done
716− Skip sb' -> Skip (sa' :!: sb' :!: Just a)
717− Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
709+ next (sa :!: sb :!: N) = case next0 sa of
710+ Done -> Done
711+ Skip sa' -> Skip (sa' :!: sb :!: N)
712+ Yield a sa' -> Skip (sa' :!: sb :!: J a)
713+
714+ next (sa' :!: sb :!: J a) = case next1 sb of
715+ Done -> Done
716+ Skip sb' -> Skip (sa' :!: sb' :!: J a)
717+ Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: N)
718718{-# INLINE [0] zipWith #-}
719719
720720-- | /O(n)/ The 'elemIndexI' function returns the index of the first
Data/Text/Fusion/Internal.hs+7−1
@@ -15,13 +15,19 @@
1515
1616module Data.Text.Fusion.Internal
1717 (
18− PairS(..)
18+ M(..)
19+ , PairS(..)
1920 , Switch(..)
2021 , Step(..)
2122 , Stream(..)
2223 , empty
2324 ) where
2425
26+-- | Specialised, strict Maybe-like type.
27+data M a = N
28+ | J {-# UNPACK #-} !a
29+ deriving (Eq, Ord, Show)
30+
2531infixl 2 :!:
2632data PairS a b = !a :!: !b
2733 deriving (Eq, Ord, Read, Show)