1{-# LANGUAGE ExistentialQuantification, BangPatterns, MagicHash #-}
2--TODO: eliminate the need to supress these warnings:
3{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-unused-matches #-}
5module Data.Text.Fusion where
7import Prelude hiding (map, tail, head, foldr, filter,concat)
10import Control.Exception(assert)
11import Control.Monad(liftM2)
12import Control.Monad.ST(runST,ST)
15import qualified Data.ByteString as B
16import qualified Data.ByteString.Unsafe as B
17import Data.ByteString.Internal(ByteString(..),mallocByteString,memcpy)
18import qualified Data.List as L
19import Data.Word(Word8, Word16, Word32)
20import Foreign.ForeignPtr(withForeignPtr,ForeignPtr)
21import Foreign.Storable(pokeByteOff)
22import GHC.Exts (Int(..), (+#))
23import System.IO.Unsafe(unsafePerformIO)
25import Data.Text.Internal(Text(..),empty)
26import Data.Text.UnsafeChar(unsafeChr,unsafeChr8,unsafeChr32)
27import qualified Data.Text.Utf8 as U8
28import qualified Data.Text.Utf16 as U16
29import qualified Data.Text.Utf32 as U32
34data PairS a b = !a :!: !b
38data EitherS a b = LeftS !a | RightS !b
40data Stream a = forall s. Stream (s -> Step s a) !s {-# UNPACK #-}!Int
46data Encoding = ASCII | Utf8 | Utf16BE | Utf16LE | Utf32BE | Utf32LE
47 deriving (Read, Show, Eq)
49-- | /O(n)/ Convert a Text into a Stream Char.
50stream :: Text -> Stream Char
51stream (Text arr off len) = Stream next off len
57 | n >= 0xD800 && n <= 0xDBFF = Yield (U16.chr2 n n2) (i + 2)
58 | otherwise = Yield (unsafeChr n) (i + 1)
61 n2 = unsafeAt arr (i + 1)
62{-# INLINE [0] stream #-}
64-- | /O(n)/ Convert a Stream Char into a Text.
65unstream :: Stream Char -> Text
66unstream (Stream next0 s0 len) = x `seq` Text (fst x) 0 (snd x)
68 x :: ((UArray Int Word16),Int)
69 x = runST ((unsafeNewArray_ (0,len+1) :: ST s (STUArray s Int Word16))
70 >>= (\arr -> loop arr 0 (len+1) s0))
72 | i + 1 > max = do arr' <- unsafeNewArray_ (0,max*2)
74 Done -> liftM2 (,) (unsafeFreezeSTUArray arr) (return i)
75 _ -> copy arr arr' >> loop arr' i (max*2) s
76 | otherwise = case next0 s of
77 Done -> liftM2 (,) (unsafeFreezeSTUArray arr) (return i)
78 Skip s' -> loop arr i max s'
81 unsafeWrite arr i (fromIntegral n :: Word16)
85 unsafeWrite arr (i + 1) r
93 l = fromIntegral $ (shiftR m 10) + (0xD800 :: Int)
95 r = fromIntegral $ (m .&. (0x3FF :: Int)) + (0xDC00 :: Int)
96{-# INLINE [0] unstream #-}
100 (_,max) <- getBounds src
104 | i > max = return ()
105 | otherwise = do v <- unsafeRead src i
109-- | /O(n)/ Determines if two streams are equal.
110eq :: Ord a => Stream a -> Stream a -> Bool
111eq (Stream next1 s1 _) (Stream next2 s2 _) = compare (next1 s1) (next2 s2)
113 compare Done Done = True
114 compare Done _ = False
115 compare _ Done = False
116 compare (Skip s1') (Skip s2') = compare (next1 s1') (next2 s2')
117 compare (Skip s1') x2 = compare (next1 s1') x2
118 compare x1 (Skip s2') = compare x1 (next2 s2')
119 compare (Yield x1 s1') (Yield x2 s2') = x1 == x2 &&
120 compare (next1 s1') (next2 s2')
121{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
124-- | /O(n) Convert a ByteString into a Stream Char, using the specified encoding standard.
125stream_bs :: Encoding -> ByteString -> Stream Char
126stream_bs ASCII bs = Stream next 0 l
132 | otherwise = Yield (unsafeChr8 x1) (i+1)
134 x1 = B.unsafeIndex bs i
135stream_bs Utf8 bs = Stream next 0 l
141 | U8.validate1 x1 = Yield (unsafeChr8 x1) (i+1)
142 | i+1 < l && U8.validate2 x1 x2 = Yield (U8.chr2 x1 x2) (i+2)
143 | i+2 < l && U8.validate3 x1 x2 x3 = Yield (U8.chr3 x1 x2 x3) (i+3)
144 | i+3 < l && U8.validate4 x1 x2 x3 x4 = Yield (U8.chr4 x1 x2 x3 x4) (i+4)
145 | otherwise = error "bsStream: bad UTF-8 stream"
151 index = B.unsafeIndex bs
152stream_bs Utf16LE bs = Stream next 0 l
158 | i+1 < l && U16.validate1 x1 = Yield (unsafeChr x1) (i+2)
159 | i+3 < l && U16.validate2 x1 x2 = Yield (U16.chr2 x1 x2) (i+4)
160 | otherwise = error $ "bsStream: bad UTF-16LE stream"
162 x1 = (shiftL (index (i + 1)) 8) + (index i)
163 x2 = (shiftL (index (i + 3)) 8) + (index (i + 2))
164 index = fromIntegral . B.unsafeIndex bs :: Int -> Word16
165stream_bs Utf16BE bs = Stream next 0 l
171 | i+1 < l && U16.validate1 x1 = Yield (unsafeChr x1) (i+2)
172 | i+3 < l && U16.validate2 x1 x2 = Yield (U16.chr2 x1 x2) (i+4)
173 | otherwise = error $ "bsStream: bad UTF16-BE stream "
175 x1 = (shiftL (index i) 8) + (index (i + 1))
176 x2 = (shiftL (index (i + 2)) 8) + (index (i + 3))
177 index = fromIntegral . B.unsafeIndex bs :: Int -> Word16
178stream_bs Utf32BE bs = Stream next 0 l
184 | i+3 < l && U32.validate x = Yield (unsafeChr32 x) (i+4)
185 | otherwise = error "bsStream: bad UTF-32BE stream"
187 x = shiftL x1 24 + shiftL x2 16 + shiftL x3 8 + x4
192 index = fromIntegral . B.unsafeIndex bs :: Int -> Word32
193stream_bs Utf32LE bs = Stream next 0 l
199 | i+3 < l && U32.validate x = Yield (unsafeChr32 x) (i+4)
200 | otherwise = error "bsStream: bad UTF-32LE stream"
202 x = shiftL x4 24 + shiftL x3 16 + shiftL x2 8 + x1
207 index = fromIntegral . B.unsafeIndex bs :: Int -> Word32
208{-# INLINE [0] stream_bs #-}
210-- | /O(n)/ Convert a Stream Char into a Stream Word8 using the specified encoding standard.
211restream :: Encoding -> Stream Char -> Stream Word8
212restream ASCII (Stream next0 s0 len) = Stream next s0 (len*2)
214 next !s = case next0 s of
217 Yield x xs -> Yield x' xs
218 where x' = fromIntegral (ord x) :: Word8
219restream Utf8 (Stream next0 s0 len) =
220 Stream next ((Just s0) :!: Nothing :!: Nothing :!: Nothing) (len*2)
223 next ((Just s) :!: Nothing :!: Nothing :!: Nothing) = case next0 s of
225 Skip s' -> Skip ((Just s') :!: Nothing :!: Nothing :!: Nothing)
227 | n <= 0x7F -> Yield c ((Just xs) :!: Nothing :!: Nothing :!: Nothing)
228 | n <= 0x07FF -> Yield (fst c2) ((Just xs) :!: (Just $ snd c2) :!: Nothing :!: Nothing)
229 | n <= 0xFFFF -> Yield (fst3 c3) ((Just xs) :!: (Just $ snd3 c3) :!: (Just $ trd3 c3) :!: Nothing)
230 | otherwise -> Yield (fst4 c4) ((Just xs) :!: (Just $ snd4 c4) :!: (Just $ trd4 c4) :!: (Just $ fth4 c4))
237 next ((Just s) :!: (Just x2) :!: Nothing :!: Nothing) = Yield x2 ((Just s) :!: Nothing :!: Nothing :!: Nothing)
238 next ((Just s) :!: (Just x2) :!: x3 :!: Nothing) = Yield x2 ((Just s) :!: x3 :!: Nothing :!: Nothing)
239 next ((Just s) :!: (Just x2) :!: x3 :!: x4) = Yield x2 ((Just s) :!: x3 :!: x4 :!: Nothing)
240restream Utf16BE (Stream next0 s0 len) =
241 Stream next (Just s0 :!: Nothing :!: Nothing :!: Nothing) (len*2)
244 next (Just s :!: Nothing :!: Nothing :!: Nothing) = case next0 s of
246 Skip s' -> Skip (Just s' :!: Nothing :!: Nothing :!: Nothing)
248 | n < 0x10000 -> Yield (fromIntegral $ shiftR n 8) (Just xs :!: Just (fromIntegral n) :!: Nothing :!: Nothing)
249 | otherwise -> Yield c1 (Just xs :!: Just c2 :!: Just c3 :!: Just c4)
253 c1 = fromIntegral (shiftR n1 18 + 0xD8)
254 c2 = fromIntegral (shiftR n1 10)
256 c3 = fromIntegral (shiftR n2 8 + 0xDC)
258 next ((Just s) :!: (Just x2) :!: Nothing :!: Nothing) = Yield x2 ((Just s) :!: Nothing :!: Nothing :!: Nothing)
259 next ((Just s) :!: (Just x2) :!: x3 :!: Nothing) = Yield x2 ((Just s) :!: x3 :!: Nothing :!: Nothing)
260 next ((Just s) :!: (Just x2) :!: x3 :!: x4) = Yield x2 ((Just s) :!: x3 :!: x4 :!: Nothing)
261restream Utf16LE (Stream next0 s0 len) =
262 Stream next (Just s0 :!: Nothing :!: Nothing :!: Nothing) (len*2)
265 next (Just s :!: Nothing :!: Nothing :!: Nothing) = case next0 s of
267 Skip s' -> Skip (Just s' :!: Nothing :!: Nothing :!: Nothing)
269 | n < 0x10000 -> Yield (fromIntegral n) (Just xs :!: Just (fromIntegral $ shiftR n 8) :!: Nothing :!: Nothing)
270 | otherwise -> Yield c1 (Just xs :!: Just c2 :!: Just c3 :!: Just c4)
274 c2 = fromIntegral (shiftR n1 18 + 0xD8)
275 c1 = fromIntegral (shiftR n1 10)
277 c4 = fromIntegral (shiftR n2 8 + 0xDC)
279 next ((Just s) :!: (Just x2) :!: Nothing :!: Nothing) = Yield x2 ((Just s) :!: Nothing :!: Nothing :!: Nothing)
280 next ((Just s) :!: (Just x2) :!: x3 :!: Nothing) = Yield x2 ((Just s) :!: x3 :!: Nothing :!: Nothing)
281 next ((Just s) :!: (Just x2) :!: x3 :!: x4) = Yield x2 ((Just s) :!: x3 :!: x4 :!: Nothing)
282restream Utf32BE (Stream next0 s0 len) =
283 Stream next (Just s0 :!: Nothing :!: Nothing :!: Nothing) (len*2)
286 next (Just s :!: Nothing :!: Nothing :!: Nothing) = case next0 s of
288 Skip s' -> Skip (Just s' :!: Nothing :!: Nothing :!: Nothing)
289 Yield x xs -> Yield c1 (Just xs :!: Just c2 :!: Just c3 :!: Just c4)
292 c1 = fromIntegral $ shiftR n 24
293 c2 = fromIntegral $ shiftR n 16
294 c3 = fromIntegral $ shiftR n 8
296 next ((Just s) :!: (Just x2) :!: Nothing :!: Nothing) = Yield x2 ((Just s) :!: Nothing :!: Nothing :!: Nothing)
297 next ((Just s) :!: (Just x2) :!: x3 :!: Nothing) = Yield x2 ((Just s) :!: x3 :!: Nothing :!: Nothing)
298 next ((Just s) :!: (Just x2) :!: x3 :!: x4) = Yield x2 ((Just s) :!: x3 :!: x4 :!: Nothing)
299restream Utf32LE (Stream next0 s0 len) =
300 Stream next (Just s0 :!: Nothing :!: Nothing :!: Nothing) (len*2)
303 next (Just s :!: Nothing :!: Nothing :!: Nothing) = case next0 s of
305 Skip s' -> Skip (Just s' :!: Nothing :!: Nothing :!: Nothing)
306 Yield x xs -> Yield c1 (Just xs :!: Just c2 :!: Just c3 :!: Just c4)
309 c4 = fromIntegral $ shiftR n 24
310 c3 = fromIntegral $ shiftR n 16
311 c2 = fromIntegral $ shiftR n 8
313 next ((Just s) :!: (Just x2) :!: Nothing :!: Nothing) = Yield x2 ((Just s) :!: Nothing :!: Nothing :!: Nothing)
314 next ((Just s) :!: (Just x2) :!: x3 :!: Nothing) = Yield x2 ((Just s) :!: x3 :!: Nothing :!: Nothing)
315 next ((Just s) :!: (Just x2) :!: x3 :!: x4) = Yield x2 ((Just s) :!: x3 :!: x4 :!: Nothing)
316{-# INLINE restream #-}
327-- | /O(n)/ Convert a Stream Word8 to a ByteString
328unstream_bs :: Stream Word8 -> ByteString
329unstream_bs (Stream next s0 len) = unsafePerformIO $ do
330 fp0 <- mallocByteString len
333 loop !fp !n !off !s = case next s of
334 Done -> trimUp fp n off
335 Skip s' -> loop fp n off s'
337 | n == off -> realloc fp n off s' x
339 withForeignPtr fp $ \p -> pokeByteOff p off x
341 {-# NOINLINE realloc #-}
342 realloc fp n off s x = do
345 withForeignPtr fp' $ \p -> pokeByteOff p off x
346 loop fp' n' (off+1) s
347 {-# NOINLINE trimUp #-}
348 trimUp fp _ off = return $! PS fp 0 off
349 copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
350 copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
351 dest <- mallocByteString destLen
352 withForeignPtr src $ \src' ->
353 withForeignPtr dest $ \dest' ->
354 memcpy dest' src' (fromIntegral destLen)
356{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
358-- ----------------------------------------------------------------------------
359-- * Basic stream functions
361-- | /O(n)/ Adds a character to the front of a Stream Char.
362cons :: Char -> Stream Char -> Stream Char
363cons w (Stream next0 s0 len) = Stream next (S2 :!: s0) (len+2)
366 next (S2 :!: s) = Yield w (S1 :!: s)
367 next (S1 :!: s) = case next0 s of
369 Skip s' -> Skip (S1 :!: s')
370 Yield x s' -> Yield x (S1 :!: s')
371{-# INLINE [0] cons #-}
373-- | /O(n)/ Adds a character to the end of a stream.
374snoc :: Stream Char -> Char -> Stream Char
375snoc (Stream next0 xs0 len) w = Stream next (Just xs0) (len+2)
378 next (Just xs) = case next0 xs of
379 Done -> Yield w Nothing
380 Skip xs' -> Skip (Just xs')
381 Yield x xs' -> Yield x (Just xs')
383{-# INLINE [0] snoc #-}
385-- | /O(n)/ Appends one Stream to the other.
386append :: Stream Char -> Stream Char -> Stream Char
387append (Stream next0 s01 len1) (Stream next1 s02 len2) =
388 Stream next (Left s01) (len1 + len2)
391 next (Left s1) = case next0 s1 of
392 Done -> Skip (Right s02)
393 Skip s1' -> Skip (Left s1')
394 Yield x s1' -> Yield x (Left s1')
395 next (Right s2) = case next1 s2 of
397 Skip s2' -> Skip (Right s2')
398 Yield x s2' -> Yield x (Right s2')
399{-# INLINE [0] append #-}
401-- | /O(1)/ Returns the first character of a Text, which must be non-empty.
402-- Subject to array fusion.
403head :: Stream Char -> Char
404head (Stream next s0 len) = loop_head s0
406 loop_head !s = case next s of
408 Skip s' -> loop_head s'
409 Done -> error "head: Empty list"
410{-# INLINE [0] head #-}
412-- | /O(n)/ Returns the last character of a Stream Char, which must be non-empty.
413last :: Stream Char -> Char
414last (Stream next s0 len) = loop0_last s0
416 loop0_last !s = case next s of
417 Done -> error "last: Empty list"
418 Skip s' -> seq s' $ loop0_last s'
419 Yield x s' -> seq s' $ loop_last x s'
420 loop_last !x !s = case next s of
422 Skip s' -> seq s' $ loop_last x s'
423 Yield x' s' -> seq s' $ loop_last x' s'
424{-# INLINE[0] last #-}
426-- | /O(1)/ Returns all characters after the head of a Stream Char, which must
428tail :: Stream Char -> Stream Char
429tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1)
432 next (False :!: s) = case next0 s of
434 Skip s' -> Skip (False :!: s')
435 Yield _ s' -> Skip (True :!: s')
436 next (True :!: s) = case next0 s of
438 Skip s' -> Skip (True :!: s')
439 Yield x s' -> Yield x (True :!: s')
440{-# INLINE [0] tail #-}
443-- | /O(1)/ Returns all but the last character of a Stream Char, which
445init :: Stream Char -> Stream Char
446init (Stream next0 s0 len) = Stream next (Nothing :!: s0) (len-1)
449 next (Nothing :!: s) = case next0 s of
450 Done -> errorEmptyList "init"
451 Skip s' -> seq s' $ Skip (Nothing :!: s')
452 Yield x s' -> seq s' $ Skip (Just x :!: s')
453 next (Just x :!: s) = case next0 s of
455 Skip s' -> seq s' $ Skip (Just x :!: s')
456 Yield x' s' -> seq s' $ Yield x (Just x' :!: s')
457{-# INLINE [0] init #-}
459-- | /O(1)/ Tests whether a Stream Char is empty or not.
460null :: Stream Char -> Bool
461null (Stream next s0 len) = loop_null s0
463 loop_null !s = case next s of
466 Skip s' -> loop_null s'
467{-# INLINE[0] null #-}
469-- | /O(n)/ Returns the number of characters in a text.
470length :: Stream Char -> Int
471length (Stream next s0 len) = loop_length 0# s0
474 loop_length z# s = case next s of
476 Skip s' -> loop_length z# s'
477 Yield _ s' -> loop_length (z# +# 1#) s'
478{-# INLINE[0] length #-}
480-- ----------------------------------------------------------------------------
481-- * Stream transformations
483-- | /O(n)/ 'map' @f @xs is the Stream Char obtained by applying @f@ to each element of
485map :: (Char -> Char) -> Stream Char -> Stream Char
486map f (Stream next0 s0 len) = Stream next s0 len
489 next !s = case next0 s of
492 Yield x s' -> Yield (f x) s'
493{-# INLINE [0] map #-}
496 RULES "STREAM map/map fusion" forall f g s.
497 map f (map g s) = map (\x -> f (g x)) s
500-- | /O(n)/ The 'intersperse' function takes a character and places it between each of
501-- the characters of a Stream.
502intersperse :: Char -> Stream Char -> Stream Char
503intersperse c (Stream next0 s0 len) = Stream next (s0 :!: Nothing :!: S1) len
506 next (s :!: Nothing :!: S1) = case next0 s of
508 Skip s' -> Skip (s' :!: Nothing :!: S1)
509 Yield x s' -> Skip (s' :!: Just x :!: S1)
510 next (s :!: Just x :!: S1) = Yield x (s :!: Nothing :!: S2)
511 next (s :!: Nothing :!: S2) = case next0 s of
513 Skip s' -> Skip (s' :!: Nothing :!: S2)
514 Yield x s' -> Yield c (s' :!: Just x :!: S1)
516-- ----------------------------------------------------------------------------
517-- * Reducing Streams (folds)
519-- | foldl, applied to a binary operator, a starting value (typically the
520-- left-identity of the operator), and a Stream, reduces the Stream using the
521-- binary operator, from left to right.
522foldl :: (b -> Char -> b) -> b -> Stream Char -> b
523foldl f z0 (Stream next s0 len) = loop_foldl z0 s0
525 loop_foldl z !s = case next s of
527 Skip s' -> loop_foldl z s'
528 Yield x s' -> loop_foldl (f z x) s'
529{-# INLINE [0] foldl #-}
531-- | A strict version of foldl.
532foldl' :: (b -> Char -> b) -> b -> Stream Char -> b
533foldl' f z0 (Stream next s0 len) = loop_foldl' z0 s0
535 loop_foldl' !z !s = case next s of
537 Skip s' -> loop_foldl' z s'
538 Yield x s' -> loop_foldl' (f z x) s'
539{-# INLINE [0] foldl' #-}
541-- | foldl1 is a variant of foldl that has no starting value argument,
542-- and thus must be applied to non-empty Streams.
543foldl1 :: (Char -> Char -> Char) -> Stream Char -> Char
544foldl1 f (Stream next s0 len) = loop0_foldl1 s0
546 loop0_foldl1 !s = case next s of
547 Skip s' -> loop0_foldl1 s'
548 Yield x s' -> loop_foldl1 x s'
549 Done -> errorEmptyList "foldl1"
550 loop_foldl1 z !s = case next s of
552 Skip s' -> loop_foldl1 z s'
553 Yield x s' -> loop_foldl1 (f z x) s'
554{-# INLINE [0] foldl1 #-}
556-- | A strict version of foldl1.
557foldl1' :: (Char -> Char -> Char) -> Stream Char -> Char
558foldl1' f (Stream next s0 len) = loop0_foldl1' s0
560 loop0_foldl1' !s = case next s of
561 Skip s' -> loop0_foldl1' s'
562 Yield x s' -> loop_foldl1' x s'
563 Done -> errorEmptyList "foldl1"
564 loop_foldl1' !z !s = case next s of
566 Skip s' -> loop_foldl1' z s'
567 Yield x s' -> loop_foldl1' (f z x) s'
568{-# INLINE [0] foldl1' #-}
570-- | 'foldr', applied to a binary operator, a starting value (typically the
571-- right-identity of the operator), and a stream, reduces the stream using the
572-- binary operator, from right to left.
573foldr :: (Char -> b -> b) -> b -> Stream Char -> b
574foldr f z (Stream next s0 len) = loop_foldr s0
576 loop_foldr !s = case next s of
578 Skip s' -> loop_foldr s'
579 Yield x s' -> f x (loop_foldr s')
580{-# INLINE [0] foldr #-}
582-- | foldr1 is a variant of 'foldr' that has no starting value argument,
583-- and thust must be applied to non-empty streams.
584-- Subject to array fusion.
585foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
586foldr1 f (Stream next s0 len) = loop0_foldr1 s0
588 loop0_foldr1 !s = case next s of
589 Done -> error "foldr1"
590 Skip s' -> loop0_foldr1 s'
591 Yield x s' -> loop_foldr1 x s'
593 loop_foldr1 x !s = case next s of
595 Skip s' -> loop_foldr1 x s'
596 Yield x' s' -> f x (loop_foldr1 x' s')
597{-# INLINE [0] foldr1 #-}
599-- ----------------------------------------------------------------------------
602-- | /O(n)/ Concatenate a list of streams. Subject to array fusion.
603concat :: [Stream Char] -> Stream Char
604concat = L.foldr append (Stream next Done 0)
608-- | Map a function over a stream that results in a steram and concatenate the
610concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
611concatMap f = foldr (append . f) (stream empty)
613-- | /O(n)/ any @p @xs determines if any character in the stream
614-- @xs@ satisifes the predicate @p@.
615any :: (Char -> Bool) -> Stream Char -> Bool
616any p (Stream next0 s0 len) = loop_any s0
618 loop_any !s = case next0 s of
620 Skip s' -> seq s' $ loop_any s'
621 Yield x s' | p x -> True
622 | otherwise -> seq s' $ loop_any s'
624-- | /O(n)/ all @p @xs determines if all characters in the 'Text'
625-- @xs@ satisify the predicate @p@.
626all :: (Char -> Bool) -> Stream Char -> Bool
627all p (Stream next0 s0 len) = loop_all s0
629 loop_all !s = case next0 s of
631 Skip s' -> seq s' $ loop_all s'
632 Yield x s' | p x -> seq s' $ loop_all s'
635-- | /O(n)/ maximum returns the maximum value from a stream, which must be
637maximum :: Stream Char -> Char
638maximum (Stream next0 s0 len) = loop0_maximum s0
640 loop0_maximum !s = case next0 s of
641 Done -> errorEmptyList "maximum"
642 Skip s' -> seq s' $ loop0_maximum s'
643 Yield x s' -> seq s' $ loop_maximum x s'
644 loop_maximum !z !s = case next0 s of
646 Skip s' -> seq s' $ loop_maximum z s'
648 | x > z -> seq s' $ loop_maximum x s'
649 | otherwise -> seq s' $ loop_maximum z s'
651-- | /O(n)/ minimum returns the minimum value from a 'Text', which must be
653minimum :: Stream Char -> Char
654minimum (Stream next0 s0 len) = loop0_minimum s0
656 loop0_minimum !s = case next0 s of
657 Done -> errorEmptyList "minimum"
658 Skip s' -> seq s' $ loop0_minimum s'
659 Yield x s' -> seq s' $ loop_minimum x s'
660 loop_minimum !z !s = case next0 s of
662 Skip s' -> seq s' $ loop_minimum z s'
664 | x < z -> seq s' $ loop_minimum x s'
665 | otherwise -> seq s' $ loop_minimum z s'
670-- -----------------------------------------------------------------------------
673-- -----------------------------------------------------------------------------
674-- ** Generating and unfolding streams
676-- | /O(n)/, where @n@ is the length of the result. The unfoldr function
677-- is analogous to the List 'unfoldr'. unfoldr builds a stream
678-- from a seed value. The function takes the element and returns
679-- Nothing if it is done producing the stream or returns Just
680-- (a,b), in which case, a is the next Char in the string, and b is
681-- the seed value for further production.
682unfoldr :: (a -> Maybe (Char,a)) -> a -> Stream Char
683unfoldr f s0 = Stream next s0 1
686 next !s = case f s of
688 Just (w, s') -> Yield w s'
689{-# INLINE [0] unfoldr #-}
691-- | O(n) Like unfoldr, unfoldrN builds a stream from a seed
692-- value. However, the length of the result should be limited by the
693-- first argument to unfoldrN. This function is more efficient than
694-- unfoldr when the maximum length of the result and correct,
695-- otherwise its complexity performance is similar to 'unfoldr'
696unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Stream Char
697unfoldrN n f s0 = Stream next (0 :!: s0) (n*2)
700 next (z :!: s) = case f s of
702 Just (w, s') | z >= n -> Done
703 | otherwise -> Yield w ((z + 1) :!: s')
704-------------------------------------------------------------------------------
707-- | /O(n)/ take n, applied to a stream, returns the prefix of the
708-- stream of length @n@, or the stream itself if @n@ is greater than the
709-- length of the stream.
710take :: Int -> Stream Char -> Stream Char
711take n0 (Stream next0 s0 len) = Stream next (n0 :!: s0) len
714 next (n :!: s) | n <= 0 = Done
715 | otherwise = case next0 s of
717 Skip s' -> Skip (n :!: s')
718 Yield x s' -> Yield x ((n-1) :!: s')
719{-# INLINE [0] take #-}
721-- | /O(n)/ drop n, applied to a stream, returns the suffix of the
722-- stream of length @n@, or the empty stream if @n@ is greater than the
723-- length of the stream.
724drop :: Int -> Stream Char -> Stream Char
725drop n0 (Stream next0 s0 len) = Stream next (Just ((max 0 n0)) :!: s0) (len - n0)
729 | n == 0 = Skip (Nothing :!: s)
730 | otherwise = case next0 s of
732 Skip s' -> Skip (Just n :!: s')
733 Yield _ s' -> Skip (Just (n-1) :!: s')
734 next (Nothing :!: s) = case next0 s of
736 Skip s' -> Skip (Nothing :!: s')
737 Yield x s' -> Yield x (Nothing :!: s')
738{-# INLINE [0] drop #-}
740-- | takeWhile, applied to a predicate @p@ and a stream, returns the
741-- longest prefix (possibly empty) of elements that satisfy p.
742takeWhile :: (Char -> Bool) -> Stream Char -> Stream Char
743takeWhile p (Stream next0 s0 len) = Stream next s0 len
746 next !s = case next0 s of
749 Yield x s' | p x -> Yield x s'
751{-# INLINE [0] takeWhile #-}
753-- | dropWhile @p @xs returns the suffix remaining after takeWhile @p @xs.
754dropWhile :: (Char -> Bool) -> Stream Char -> Stream Char
755dropWhile p (Stream next0 s0 len) = Stream next (S1 :!: s0) len
758 next (S1 :!: s) = case next0 s of
760 Skip s' -> Skip (S1 :!: s')
761 Yield x s' | p x -> Skip (S1 :!: s')
762 | otherwise -> Yield x (S2 :!: s')
763 next (S2 :!: s) = case next0 s of
765 Skip s' -> Skip (S2 :!: s')
766 Yield x s' -> Yield x (S2 :!: s')
767{-# INLINE [0] dropWhile #-}
769-- ----------------------------------------------------------------------------
772-------------------------------------------------------------------------------
773-- ** Searching by equality
775-- | /O(n)/ elem is the stream membership predicate.
776elem :: Char -> Stream Char -> Bool
777elem w (Stream next s0 len) = loop_elem s0
779 loop_elem !s = case next s of
781 Skip s' -> loop_elem s'
782 Yield x s' | x == w -> True
783 | otherwise -> loop_elem s'
784{-# INLINE [0] elem #-}
786-------------------------------------------------------------------------------
787-- ** Searching with a predicate
789-- | /O(n)/ The 'find' function takes a predicate and a stream,
790-- and returns the first element in matching the predicate, or 'Nothing'
791-- if there is no such element.
793find :: (Char -> Bool) -> Stream Char -> Maybe Char
794find p (Stream next s0 len) = loop_find s0
796 loop_find !s = case next s of
798 Skip s' -> loop_find s'
799 Yield x s' | p x -> Just x
800 | otherwise -> loop_find s'
801{-# INLINE [0] find #-}
803-- | /O(n)/ 'filter', applied to a predicate and a stream,
804-- returns a stream containing those characters that satisfy the
806filter :: (Char -> Bool) -> Stream Char -> Stream Char
807filter p (Stream next0 s0 len) = Stream next s0 len
810 next !s = case next0 s of
813 Yield x s' | p x -> Yield x s'
814 | otherwise -> Skip s'
815{-# INLINE [0] filter #-}
818 "Stream filter/filter fusion" forall p q s.
819 filter p (filter q s) = filter (\x -> q x && p x) s
822-------------------------------------------------------------------------------
823-- ** Indexing streams
825-- | /O(1)/ stream index (subscript) operator, starting from 0.
826index :: Stream Char -> Int -> Char
827index (Stream next s0 len) n0
828 | n0 < 0 = error "Stream.(!!): negative index"
829 | otherwise = loop_index n0 s0
831 loop_index !n !s = case next s of
832 Done -> error "Stream.(!!): index too large"
833 Skip s' -> loop_index n s'
834 Yield x s' | n == 0 -> x
835 | otherwise -> loop_index (n-1) s'
836{-# INLINE [0] index #-}
838-- | The 'findIndex' function takes a predicate and a stream and
839-- returns the index of the first element in the stream
840-- satisfying the predicate.
841findIndex :: (Char -> Bool) -> Stream Char -> Maybe Int
842findIndex p (Stream next s0 len) = loop_findIndex 0 s0
844 loop_findIndex !i !s = case next s of
846 Skip s' -> loop_findIndex i s' -- hmm. not caught by QC
847 Yield x s' | p x -> Just i
848 | otherwise -> loop_findIndex (i+1) s'
849{-# INLINE [0] findIndex #-}
851-- | /O(n)/ The 'elemIndex' function returns the index of the first
852-- element in the given stream which is equal to the query
853-- element, or 'Nothing' if there is no such element.
854elemIndex :: Char -> Stream Char -> Maybe Int
855elemIndex a (Stream next s0 len) = loop_elemIndex 0 s0
857 loop_elemIndex !i !s = case next s of
859 Skip s' -> loop_elemIndex i s'
860 Yield x s' | a == x -> Just i
861 | otherwise -> loop_elemIndex (i+1) s'
862{-# INLINE [0] elemIndex #-}
864-------------------------------------------------------------------------------
867-- | zipWith generalises 'zip' by zipping with the function given as
868-- the first argument, instead of a tupling function.
869zipWith :: (Char -> Char -> Char) -> Stream Char -> Stream Char -> Stream Char
870zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!: sb0 :!: Nothing) (min len1 len2)
873 next (sa :!: sb :!: Nothing) = case next0 sa of
875 Skip sa' -> Skip (sa' :!: sb :!: Nothing)
876 Yield a sa' -> Skip (sa' :!: sb :!: Just a)
878 next (sa' :!: sb :!: Just a) = case next1 sb of
880 Skip sb' -> Skip (sa' :!: sb' :!: Just a)
881 Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
882{-# INLINE [0] zipWith #-}
884errorEmptyList :: String -> a
886 error ("Prelude." ++ fun ++ ": empty list")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list