{-# LANGUAGE BangPatterns, MagicHash, RankNTypes, PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
module Data.Text.Internal.Fusion.Common
(
singleton
, streamList
, unstreamList
, streamCString#
, cons
, snoc
, append
, head
, uncons
, last
, tail
, init
, null
, lengthI
, compareLengthI
, isSingleton
, map
, intercalate
, intersperse
, toCaseFold
, toLower
, toTitle
, toUpper
, justifyLeftI
, foldl
, foldl'
, foldl1
, foldl1'
, foldr
, foldr1
, concat
, concatMap
, any
, all
, maximum
, minimum
, scanl
, replicateCharI
, replicateI
, unfoldr
, unfoldrNI
, take
, drop
, takeWhile
, dropWhile
, isPrefixOf
, elem
, filter
, findBy
, indexI
, findIndexI
, countCharI
, zipWith
) where
import Prelude (Bool(..), Char, Eq, (==), Int, Integral, Maybe(..),
Ord(..), Ordering(..), String, (.), ($), (+), (-), (*), (++),
(&&), fromIntegral, otherwise)
import qualified Data.List as L hiding (head, tail)
import qualified Prelude as P
import Data.Bits (shiftL, shiftR, (.&.))
import Data.Char (isLetter, isSpace)
import GHC.Int (Int64(..))
import Data.Text.Internal.Encoding.Utf8 (chr2, chr3, chr4, utf8LengthByLeader)
import Data.Text.Internal.Fusion.Types
import Data.Text.Internal.Fusion.CaseMapping (foldMapping, lowerMapping, titleMapping,
upperMapping)
import Data.Text.Internal.Fusion.Size
import GHC.Exts (Char(..), Char#, chr#)
import GHC.Prim (Addr#, indexWord8OffAddr#)
import GHC.Stack (HasCallStack)
import GHC.Types (Int(..))
import Data.Text.Internal.Unsafe.Char (unsafeChr8)
import GHC.Word
singleton :: Char -> Stream Char
singleton :: Char -> Stream Char
singleton Char
c = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Bool -> Step Bool Char
next Bool
False (Int -> Size
codePointsSize Int
1)
where next :: Bool -> Step Bool Char
next Bool
False = forall s a. a -> s -> Step s a
Yield Char
c Bool
True
next Bool
True = forall s a. Step s a
Done
{-# INLINE [0] singleton #-}
streamList :: [a] -> Stream a
{-# INLINE [0] streamList #-}
streamList :: forall a. [a] -> Stream a
streamList [a]
s = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream forall {a}. [a] -> Step [a] a
next [a]
s Size
unknownSize
where next :: [a] -> Step [a] a
next [] = forall s a. Step s a
Done
next (a
x:[a]
xs) = forall s a. a -> s -> Step s a
Yield a
x [a]
xs
unstreamList :: Stream a -> [a]
unstreamList :: forall a. Stream a -> [a]
unstreamList (Stream s -> Step s a
next s
s0 Size
_len) = s -> [a]
unfold s
s0
where unfold :: s -> [a]
unfold !s
s = case s -> Step s a
next s
s of
Step s a
Done -> []
Skip s
s' -> s -> [a]
unfold s
s'
Yield a
x s
s' -> a
x forall a. a -> [a] -> [a]
: s -> [a]
unfold s
s'
{-# INLINE [0] unstreamList #-}
{-# RULES "STREAM streamList/unstreamList fusion" forall s. streamList (unstreamList s) = s #-}
streamCString# :: Addr# -> Stream Char
streamCString# :: Addr# -> Stream Char
streamCString# Addr#
addr = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Int -> Step Int Char
step Int
0 Size
unknownSize
where
step :: Int -> Step Int Char
step !Int
i
| Word8
b forall a. Eq a => a -> a -> Bool
== Word8
0 = forall s a. Step s a
Done
| Bool
otherwise = forall s a. a -> s -> Step s a
Yield Char
chr (Int
i forall a. Num a => a -> a -> a
+ Int
l)
where b :: Word8
b = Int -> Word8
at# Int
i
l :: Int
l = Word8 -> Int
utf8LengthByLeader Word8
b
next :: Int -> Word8
next Int
n = Int -> Word8
at# (Int
iforall a. Num a => a -> a -> a
+Int
n)
chr :: Char
chr = case Int
l of
Int
1 -> Word8 -> Char
unsafeChr8 Word8
b
Int
2 -> Word8 -> Word8 -> Char
chr2 Word8
b (Int -> Word8
next Int
1)
Int
3 -> Word8 -> Word8 -> Word8 -> Char
chr3 Word8
b (Int -> Word8
next Int
1) (Int -> Word8
next Int
2)
Int
_ -> Word8 -> Word8 -> Word8 -> Word8 -> Char
chr4 Word8
b (Int -> Word8
next Int
1) (Int -> Word8
next Int
2) (Int -> Word8
next Int
3)
at# :: Int -> Word8
at# (I# Int#
i#) = Word8# -> Word8
W8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
addr Int#
i#)
{-# INLINE [0] streamCString# #-}
data C s = C0 !s
| C1 !s
cons :: Char -> Stream Char -> Stream Char
cons :: Char -> Stream Char -> Stream Char
cons !Char
w (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream C s -> Step (C s) Char
next (forall s. s -> C s
C1 s
s0) (Size
len forall a. Num a => a -> a -> a
+ Int -> Size
codePointsSize Int
1)
where
next :: C s -> Step (C s) Char
next (C1 s
s) = forall s a. a -> s -> Step s a
Yield Char
w (forall s. s -> C s
C0 s
s)
next (C0 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> C s
C0 s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall s. s -> C s
C0 s
s')
{-# INLINE [0] cons #-}
data Snoc a = N
| J !a
snoc :: Stream Char -> Char -> Stream Char
snoc :: Stream Char -> Char -> Stream Char
snoc (Stream s -> Step s Char
next0 s
xs0 Size
len) Char
w = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Snoc s -> Step (Snoc s) Char
next (forall a. a -> Snoc a
J s
xs0) (Size
len forall a. Num a => a -> a -> a
+ Int -> Size
codePointsSize Int
1)
where
next :: Snoc s -> Step (Snoc s) Char
next (J s
xs) = case s -> Step s Char
next0 s
xs of
Step s Char
Done -> forall s a. a -> s -> Step s a
Yield Char
w forall a. Snoc a
N
Skip s
xs' -> forall s a. s -> Step s a
Skip (forall a. a -> Snoc a
J s
xs')
Yield Char
x s
xs' -> forall s a. a -> s -> Step s a
Yield Char
x (forall a. a -> Snoc a
J s
xs')
next Snoc s
N = forall s a. Step s a
Done
{-# INLINE [0] snoc #-}
data E l r = L !l
| R !r
append :: Stream Char -> Stream Char -> Stream Char
append :: Stream Char -> Stream Char -> Stream Char
append (Stream s -> Step s Char
next0 s
s01 Size
len1) (Stream s -> Step s Char
next1 s
s02 Size
len2) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream E s s -> Step (E s s) Char
next (forall l r. l -> E l r
L s
s01) (Size
len1 forall a. Num a => a -> a -> a
+ Size
len2)
where
next :: E s s -> Step (E s s) Char
next (L s
s1) = case s -> Step s Char
next0 s
s1 of
Step s Char
Done -> forall s a. s -> Step s a
Skip (forall l r. r -> E l r
R s
s02)
Skip s
s1' -> forall s a. s -> Step s a
Skip (forall l r. l -> E l r
L s
s1')
Yield Char
x s
s1' -> forall s a. a -> s -> Step s a
Yield Char
x (forall l r. l -> E l r
L s
s1')
next (R s
s2) = case s -> Step s Char
next1 s
s2 of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s2' -> forall s a. s -> Step s a
Skip (forall l r. r -> E l r
R s
s2')
Yield Char
x s
s2' -> forall s a. a -> s -> Step s a
Yield Char
x (forall l r. r -> E l r
R s
s2')
{-# INLINE [0] append #-}
head :: HasCallStack => Stream Char -> Char
head :: HasCallStack => Stream Char -> Char
head (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Char
loop_head s
s0
where
loop_head :: s -> Char
loop_head !s
s = case s -> Step s Char
next s
s of
Yield Char
x s
_ -> Char
x
Skip s
s' -> s -> Char
loop_head s
s'
Step s Char
Done -> forall a. HasCallStack => a
head_empty
{-# INLINE [0] head #-}
head_empty :: HasCallStack => a
head_empty :: forall a. HasCallStack => a
head_empty = forall a. HasCallStack => String -> String -> a
streamError String
"head" String
"Empty stream"
{-# NOINLINE head_empty #-}
uncons :: Stream Char -> Maybe (Char, Stream Char)
uncons :: Stream Char -> Maybe (Char, Stream Char)
uncons (Stream s -> Step s Char
next s
s0 Size
len) = s -> Maybe (Char, Stream Char)
loop_uncons s
s0
where
loop_uncons :: s -> Maybe (Char, Stream Char)
loop_uncons !s
s = case s -> Step s Char
next s
s of
Yield Char
x s
s1 -> forall a. a -> Maybe a
Just (Char
x, forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream s -> Step s Char
next s
s1 (Size
len forall a. Num a => a -> a -> a
- Int -> Size
codePointsSize Int
1))
Skip s
s' -> s -> Maybe (Char, Stream Char)
loop_uncons s
s'
Step s Char
Done -> forall a. Maybe a
Nothing
{-# INLINE [0] uncons #-}
last :: HasCallStack => Stream Char -> Char
last :: HasCallStack => Stream Char -> Char
last (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Char
loop0_last s
s0
where
loop0_last :: s -> Char
loop0_last !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"last"
Skip s
s' -> s -> Char
loop0_last s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_last Char
x s
s'
loop_last :: Char -> s -> Char
loop_last !Char
x !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Char
x
Skip s
s' -> Char -> s -> Char
loop_last Char
x s
s'
Yield Char
x' s
s' -> Char -> s -> Char
loop_last Char
x' s
s'
{-# INLINE[0] last #-}
tail :: HasCallStack => Stream Char -> Stream Char
tail :: HasCallStack => Stream Char -> Stream Char
tail (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream C s -> Step (C s) Char
next (forall s. s -> C s
C0 s
s0) (Size
len forall a. Num a => a -> a -> a
- Int -> Size
codePointsSize Int
1)
where
next :: C s -> Step (C s) Char
next (C0 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"tail"
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> C s
C0 s
s')
Yield Char
_ s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> C s
C1 s
s')
next (C1 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> C s
C1 s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall s. s -> C s
C1 s
s')
{-# INLINE [0] tail #-}
data Init s = Init0 !s
| Init1 {-# UNPACK #-} !Char !s
init :: HasCallStack => Stream Char -> Stream Char
init :: HasCallStack => Stream Char -> Stream Char
init (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Init s -> Step (Init s) Char
next (forall s. s -> Init s
Init0 s
s0) (Size
len forall a. Num a => a -> a -> a
- Int -> Size
codePointsSize Int
1)
where
next :: Init s -> Step (Init s) Char
next (Init0 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"init"
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> Init s
Init0 s
s')
Yield Char
x s
s' -> forall s a. s -> Step s a
Skip (forall s. Char -> s -> Init s
Init1 Char
x s
s')
next (Init1 Char
x s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. Char -> s -> Init s
Init1 Char
x s
s')
Yield Char
x' s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall s. Char -> s -> Init s
Init1 Char
x' s
s')
{-# INLINE [0] init #-}
null :: Stream Char -> Bool
null :: Stream Char -> Bool
null (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Bool
loop_null s
s0
where
loop_null :: s -> Bool
loop_null !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Bool
True
Yield Char
_ s
_ -> Bool
False
Skip s
s' -> s -> Bool
loop_null s
s'
{-# INLINE[0] null #-}
lengthI :: Integral a => Stream Char -> a
lengthI :: forall a. Integral a => Stream Char -> a
lengthI (Stream s -> Step s Char
next s
s0 Size
_len) = forall {t}. Num t => t -> s -> t
loop_length a
0 s
s0
where
loop_length :: t -> s -> t
loop_length !t
z s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> t
z
Skip s
s' -> t -> s -> t
loop_length t
z s
s'
Yield Char
_ s
s' -> t -> s -> t
loop_length (t
z forall a. Num a => a -> a -> a
+ t
1) s
s'
{-# INLINE[0] lengthI #-}
compareLengthI :: Integral a => Stream Char -> a -> Ordering
compareLengthI :: forall a. Integral a => Stream Char -> a -> Ordering
compareLengthI (Stream s -> Step s Char
next s
s0 Size
len) a
n
| a
n forall a. Ord a => a -> a -> Bool
< a
0 = Ordering
GT
| Just Ordering
r <- Size -> Size -> Maybe Ordering
compareSize Size
len Size
n' = Ordering
r
| Bool
otherwise = a -> s -> Ordering
loop_cmp a
0 s
s0
where
n' :: Size
n' = Int -> Size
codePointsSize forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
loop_cmp :: a -> s -> Ordering
loop_cmp !a
z s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> forall a. Ord a => a -> a -> Ordering
compare a
z a
n
Skip s
s' -> a -> s -> Ordering
loop_cmp a
z s
s'
Yield Char
_ s
s' | a
z forall a. Ord a => a -> a -> Bool
> a
n -> Ordering
GT
| Bool
otherwise -> a -> s -> Ordering
loop_cmp (a
z forall a. Num a => a -> a -> a
+ a
1) s
s'
{-# INLINE[0] compareLengthI #-}
isSingleton :: Stream Char -> Bool
isSingleton :: Stream Char -> Bool
isSingleton (Stream s -> Step s Char
next s
s0 Size
_len) = Int -> s -> Bool
loop Int
0 s
s0
where
loop :: Int -> s -> Bool
loop !Int
z s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Int
z forall a. Eq a => a -> a -> Bool
== (Int
1::Int)
Skip s
s' -> Int -> s -> Bool
loop Int
z s
s'
Yield Char
_ s
s'
| Int
z forall a. Ord a => a -> a -> Bool
>= Int
1 -> Bool
False
| Bool
otherwise -> Int -> s -> Bool
loop (Int
zforall a. Num a => a -> a -> a
+Int
1) s
s'
{-# INLINE[0] isSingleton #-}
map :: (Char -> Char) -> Stream Char -> Stream Char
map :: (Char -> Char) -> Stream Char -> Stream Char
map Char -> Char
f (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream s -> Step s Char
next s
s0 Size
len
where
next :: s -> Step s Char
next !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip s
s'
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield (Char -> Char
f Char
x) s
s'
{-# INLINE [0] map #-}
{-#
RULES "STREAM map/map fusion" forall f g s.
map f (map g s) = map (\x -> f (g x)) s
#-}
data I s = I1 !s
| I2 !s {-# UNPACK #-} !Char
| I3 !s
intersperse :: Char -> Stream Char -> Stream Char
intersperse :: Char -> Stream Char -> Stream Char
intersperse Char
c (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream I s -> Step (I s) Char
next (forall s. s -> I s
I1 s
s0) (Size
len forall a. Num a => a -> a -> a
+ Size
unknownSize)
where
next :: I s -> Step (I s) Char
next (I1 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> I s
I1 s
s')
Yield Char
x s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> Char -> I s
I2 s
s' Char
x)
next (I2 s
s Char
x) = forall s a. a -> s -> Step s a
Yield Char
x (forall s. s -> I s
I3 s
s)
next (I3 s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> I s
I3 s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
c (forall s. s -> Char -> I s
I2 s
s' Char
x)
{-# INLINE [0] intersperse #-}
caseConvert :: (Char# -> _ )
-> Stream Char -> Stream Char
caseConvert :: (Char# -> Int#) -> Stream Char -> Stream Char
caseConvert Char# -> Int#
remap (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream CC s -> Step (CC s) Char
next (forall s. s -> Int64 -> CC s
CC s
s0 Int64
0) (Size
len Size -> Size -> Size
`unionSize` (Size
3forall a. Num a => a -> a -> a
*Size
len))
where
next :: CC s -> Step (CC s) Char
next (CC s
s Int64
0) =
case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> Int64 -> CC s
CC s
s' Int64
0)
Yield c :: Char
c@(C# Char#
c#) s
s' -> case Int# -> Int64
I64# (Char# -> Int#
remap Char#
c#) of
Int64
0 -> forall s a. a -> s -> Step s a
Yield Char
c (forall s. s -> Int64 -> CC s
CC s
s' Int64
0)
Int64
ab -> let (Char
a, Int64
b) = Int64 -> (Char, Int64)
chopOffChar Int64
ab in
forall s a. a -> s -> Step s a
Yield Char
a (forall s. s -> Int64 -> CC s
CC s
s' Int64
b)
next (CC s
s Int64
ab) = let (Char
a, Int64
b) = Int64 -> (Char, Int64)
chopOffChar Int64
ab in forall s a. a -> s -> Step s a
Yield Char
a (forall s. s -> Int64 -> CC s
CC s
s Int64
b)
chopOffChar :: Int64 -> (Char, Int64)
chopOffChar :: Int64 -> (Char, Int64)
chopOffChar Int64
ab = (Int -> Char
chr Int
a, Int64
ab forall a. Bits a => a -> Int -> a
`shiftR` Int
21)
where
chr :: Int -> Char
chr (I# Int#
n) = Char# -> Char
C# (Int# -> Char#
chr# Int#
n)
mask :: Int64
mask = (Int64
1 forall a. Bits a => a -> Int -> a
`shiftL` Int
21) forall a. Num a => a -> a -> a
- Int64
1
a :: Int
a = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int64
ab forall a. Bits a => a -> a -> a
.&. Int64
mask
toCaseFold :: Stream Char -> Stream Char
toCaseFold :: Stream Char -> Stream Char
toCaseFold = (Char# -> Int#) -> Stream Char -> Stream Char
caseConvert Char# -> Int#
foldMapping
{-# INLINE [0] toCaseFold #-}
toUpper :: Stream Char -> Stream Char
toUpper :: Stream Char -> Stream Char
toUpper = (Char# -> Int#) -> Stream Char -> Stream Char
caseConvert Char# -> Int#
upperMapping
{-# INLINE [0] toUpper #-}
toLower :: Stream Char -> Stream Char
toLower :: Stream Char -> Stream Char
toLower = (Char# -> Int#) -> Stream Char -> Stream Char
caseConvert Char# -> Int#
lowerMapping
{-# INLINE [0] toLower #-}
toTitle :: Stream Char -> Stream Char
toTitle :: Stream Char -> Stream Char
toTitle (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream CC (PairS Bool s) -> Step (CC (PairS Bool s)) Char
next (forall s. s -> Int64 -> CC s
CC (Bool
False forall a b. a -> b -> PairS a b
:*: s
s0) Int64
0) (Size
len forall a. Num a => a -> a -> a
+ Size
unknownSize)
where
next :: CC (PairS Bool s) -> Step (CC (PairS Bool s)) Char
next (CC (Bool
letter :*: s
s) Int64
0) =
case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> Int64 -> CC s
CC (Bool
letter forall a b. a -> b -> PairS a b
:*: s
s') Int64
0)
Yield c :: Char
c@(C# Char#
c#) s
s'
| Bool
nonSpace, Bool
letter -> case Int# -> Int64
I64# (Char# -> Int#
lowerMapping Char#
c#) of
Int64
0 -> forall s a. a -> s -> Step s a
Yield Char
c (forall s. s -> Int64 -> CC s
CC (Bool
nonSpace forall a b. a -> b -> PairS a b
:*: s
s') Int64
0)
Int64
ab -> let (Char
a, Int64
b) = Int64 -> (Char, Int64)
chopOffChar Int64
ab in
forall s a. a -> s -> Step s a
Yield Char
a (forall s. s -> Int64 -> CC s
CC (Bool
nonSpace forall a b. a -> b -> PairS a b
:*: s
s') Int64
b)
| Bool
nonSpace -> case Int# -> Int64
I64# (Char# -> Int#
titleMapping Char#
c#) of
Int64
0 -> forall s a. a -> s -> Step s a
Yield Char
c (forall s. s -> Int64 -> CC s
CC (Bool
letter' forall a b. a -> b -> PairS a b
:*: s
s') Int64
0)
Int64
ab -> let (Char
a, Int64
b) = Int64 -> (Char, Int64)
chopOffChar Int64
ab in
forall s a. a -> s -> Step s a
Yield Char
a (forall s. s -> Int64 -> CC s
CC (Bool
letter' forall a b. a -> b -> PairS a b
:*: s
s') Int64
b)
| Bool
otherwise -> forall s a. a -> s -> Step s a
Yield Char
c (forall s. s -> Int64 -> CC s
CC (Bool
letter' forall a b. a -> b -> PairS a b
:*: s
s') Int64
0)
where nonSpace :: Bool
nonSpace = Bool -> Bool
P.not (Char -> Bool
isSpace Char
c)
letter' :: Bool
letter' = Char -> Bool
isLetter Char
c
next (CC PairS Bool s
s Int64
ab) = let (Char
a, Int64
b) = Int64 -> (Char, Int64)
chopOffChar Int64
ab in forall s a. a -> s -> Step s a
Yield Char
a (forall s. s -> Int64 -> CC s
CC PairS Bool s
s Int64
b)
{-# INLINE [0] toTitle #-}
data Justify i s = Just1 !i !s
| Just2 !i !s
justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
justifyLeftI :: forall a. Integral a => a -> Char -> Stream Char -> Stream Char
justifyLeftI a
k Char
c (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Justify a s -> Step (Justify a s) Char
next (forall i s. i -> s -> Justify i s
Just1 a
0 s
s0) (Size -> Size -> Size
larger (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
k forall a. Num a => a -> a -> a
* Char -> Size
charSize Char
c forall a. Num a => a -> a -> a
+ Size
len) Size
len)
where
next :: Justify a s -> Step (Justify a s) Char
next (Just1 a
n s
s) =
case s -> Step s Char
next0 s
s of
Step s Char
Done -> Justify a s -> Step (Justify a s) Char
next (forall i s. i -> s -> Justify i s
Just2 a
n s
s)
Skip s
s' -> forall s a. s -> Step s a
Skip (forall i s. i -> s -> Justify i s
Just1 a
n s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall i s. i -> s -> Justify i s
Just1 (a
nforall a. Num a => a -> a -> a
+a
1) s
s')
next (Just2 a
n s
s)
| a
n forall a. Ord a => a -> a -> Bool
< a
k = forall s a. a -> s -> Step s a
Yield Char
c (forall i s. i -> s -> Justify i s
Just2 (a
nforall a. Num a => a -> a -> a
+a
1) s
s)
| Bool
otherwise = forall s a. Step s a
Done
{-# INLINE next #-}
{-# INLINE [0] justifyLeftI #-}
foldl :: (b -> Char -> b) -> b -> Stream Char -> b
foldl :: forall b. (b -> Char -> b) -> b -> Stream Char -> b
foldl b -> Char -> b
f b
z0 (Stream s -> Step s Char
next s
s0 Size
_len) = b -> s -> b
loop_foldl b
z0 s
s0
where
loop_foldl :: b -> s -> b
loop_foldl b
z !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> b
z
Skip s
s' -> b -> s -> b
loop_foldl b
z s
s'
Yield Char
x s
s' -> b -> s -> b
loop_foldl (b -> Char -> b
f b
z Char
x) s
s'
{-# INLINE [0] foldl #-}
foldl' :: (b -> Char -> b) -> b -> Stream Char -> b
foldl' :: forall b. (b -> Char -> b) -> b -> Stream Char -> b
foldl' b -> Char -> b
f b
z0 (Stream s -> Step s Char
next s
s0 Size
_len) = b -> s -> b
loop_foldl' b
z0 s
s0
where
loop_foldl' :: b -> s -> b
loop_foldl' !b
z !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> b
z
Skip s
s' -> b -> s -> b
loop_foldl' b
z s
s'
Yield Char
x s
s' -> b -> s -> b
loop_foldl' (b -> Char -> b
f b
z Char
x) s
s'
{-# INLINE [0] foldl' #-}
foldl1 :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldl1 :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldl1 Char -> Char -> Char
f (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Char
loop0_foldl1 s
s0
where
loop0_foldl1 :: s -> Char
loop0_foldl1 !s
s = case s -> Step s Char
next s
s of
Skip s
s' -> s -> Char
loop0_foldl1 s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_foldl1 Char
x s
s'
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"foldl1"
loop_foldl1 :: Char -> s -> Char
loop_foldl1 Char
z !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Char
z
Skip s
s' -> Char -> s -> Char
loop_foldl1 Char
z s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_foldl1 (Char -> Char -> Char
f Char
z Char
x) s
s'
{-# INLINE [0] foldl1 #-}
foldl1' :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldl1' :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldl1' Char -> Char -> Char
f (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Char
loop0_foldl1' s
s0
where
loop0_foldl1' :: s -> Char
loop0_foldl1' !s
s = case s -> Step s Char
next s
s of
Skip s
s' -> s -> Char
loop0_foldl1' s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_foldl1' Char
x s
s'
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"foldl1"
loop_foldl1' :: Char -> s -> Char
loop_foldl1' !Char
z !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Char
z
Skip s
s' -> Char -> s -> Char
loop_foldl1' Char
z s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_foldl1' (Char -> Char -> Char
f Char
z Char
x) s
s'
{-# INLINE [0] foldl1' #-}
foldr :: (Char -> b -> b) -> b -> Stream Char -> b
foldr :: forall b. (Char -> b -> b) -> b -> Stream Char -> b
foldr Char -> b -> b
f b
z (Stream s -> Step s Char
next s
s0 Size
_len) = s -> b
loop_foldr s
s0
where
loop_foldr :: s -> b
loop_foldr !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> b
z
Skip s
s' -> s -> b
loop_foldr s
s'
Yield Char
x s
s' -> Char -> b -> b
f Char
x (s -> b
loop_foldr s
s')
{-# INLINE [0] foldr #-}
foldr1 :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldr1 :: HasCallStack => (Char -> Char -> Char) -> Stream Char -> Char
foldr1 Char -> Char -> Char
f (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Char
loop0_foldr1 s
s0
where
loop0_foldr1 :: s -> Char
loop0_foldr1 !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"foldr1"
Skip s
s' -> s -> Char
loop0_foldr1 s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_foldr1 Char
x s
s'
loop_foldr1 :: Char -> s -> Char
loop_foldr1 Char
x !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Char
x
Skip s
s' -> Char -> s -> Char
loop_foldr1 Char
x s
s'
Yield Char
x' s
s' -> Char -> Char -> Char
f Char
x (Char -> s -> Char
loop_foldr1 Char
x' s
s')
{-# INLINE [0] foldr1 #-}
intercalate :: Stream Char -> [Stream Char] -> Stream Char
intercalate :: Stream Char -> [Stream Char] -> Stream Char
intercalate Stream Char
s = [Stream Char] -> Stream Char
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
L.intersperse Stream Char
s)
{-# INLINE [0] intercalate #-}
concat :: [Stream Char] -> Stream Char
concat :: [Stream Char] -> Stream Char
concat = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
L.foldr Stream Char -> Stream Char -> Stream Char
append forall a. Stream a
empty
{-# INLINE [0] concat #-}
concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
concatMap Char -> Stream Char
f = forall b. (Char -> b -> b) -> b -> Stream Char -> b
foldr (Stream Char -> Stream Char -> Stream Char
append forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Stream Char
f) forall a. Stream a
empty
{-# INLINE [0] concatMap #-}
any :: (Char -> Bool) -> Stream Char -> Bool
any :: (Char -> Bool) -> Stream Char -> Bool
any Char -> Bool
p (Stream s -> Step s Char
next0 s
s0 Size
_len) = s -> Bool
loop_any s
s0
where
loop_any :: s -> Bool
loop_any !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> Bool
False
Skip s
s' -> s -> Bool
loop_any s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> Bool
True
| Bool
otherwise -> s -> Bool
loop_any s
s'
{-# INLINE [0] any #-}
all :: (Char -> Bool) -> Stream Char -> Bool
all :: (Char -> Bool) -> Stream Char -> Bool
all Char -> Bool
p (Stream s -> Step s Char
next0 s
s0 Size
_len) = s -> Bool
loop_all s
s0
where
loop_all :: s -> Bool
loop_all !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> Bool
True
Skip s
s' -> s -> Bool
loop_all s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> s -> Bool
loop_all s
s'
| Bool
otherwise -> Bool
False
{-# INLINE [0] all #-}
maximum :: HasCallStack => Stream Char -> Char
maximum :: HasCallStack => Stream Char -> Char
maximum (Stream s -> Step s Char
next0 s
s0 Size
_len) = s -> Char
loop0_maximum s
s0
where
loop0_maximum :: s -> Char
loop0_maximum !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"maximum"
Skip s
s' -> s -> Char
loop0_maximum s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_maximum Char
x s
s'
loop_maximum :: Char -> s -> Char
loop_maximum !Char
z !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> Char
z
Skip s
s' -> Char -> s -> Char
loop_maximum Char
z s
s'
Yield Char
x s
s'
| Char
x forall a. Ord a => a -> a -> Bool
> Char
z -> Char -> s -> Char
loop_maximum Char
x s
s'
| Bool
otherwise -> Char -> s -> Char
loop_maximum Char
z s
s'
{-# INLINE [0] maximum #-}
minimum :: HasCallStack => Stream Char -> Char
minimum :: HasCallStack => Stream Char -> Char
minimum (Stream s -> Step s Char
next0 s
s0 Size
_len) = s -> Char
loop0_minimum s
s0
where
loop0_minimum :: s -> Char
loop0_minimum !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall a. HasCallStack => String -> a
emptyError String
"minimum"
Skip s
s' -> s -> Char
loop0_minimum s
s'
Yield Char
x s
s' -> Char -> s -> Char
loop_minimum Char
x s
s'
loop_minimum :: Char -> s -> Char
loop_minimum !Char
z !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> Char
z
Skip s
s' -> Char -> s -> Char
loop_minimum Char
z s
s'
Yield Char
x s
s'
| Char
x forall a. Ord a => a -> a -> Bool
< Char
z -> Char -> s -> Char
loop_minimum Char
x s
s'
| Bool
otherwise -> Char -> s -> Char
loop_minimum Char
z s
s'
{-# INLINE [0] minimum #-}
scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
scanl Char -> Char -> Char
f Char
z0 (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Scan s -> Step (Scan s) Char
next (forall s. Char -> s -> Scan s
Scan1 Char
z0 s
s0) (Size
lenforall a. Num a => a -> a -> a
+Size
1)
where
{-# INLINE next #-}
next :: Scan s -> Step (Scan s) Char
next (Scan1 Char
z s
s) = forall s a. a -> s -> Step s a
Yield Char
z (forall s. Char -> s -> Scan s
Scan2 Char
z s
s)
next (Scan2 Char
z s
s) = case s -> Step s Char
next0 s
s of
Yield Char
x s
s' -> let !x' :: Char
x' = Char -> Char -> Char
f Char
z Char
x
in forall s a. a -> s -> Step s a
Yield Char
x' (forall s. Char -> s -> Scan s
Scan2 Char
x' s
s')
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. Char -> s -> Scan s
Scan2 Char
z s
s')
Step s Char
Done -> forall s a. Step s a
Done
{-# INLINE [0] scanl #-}
replicateCharI :: Integral a => a -> Char -> Stream Char
replicateCharI :: forall a. Integral a => a -> Char -> Stream Char
replicateCharI !a
n !Char
c
| a
n forall a. Ord a => a -> a -> Bool
< a
0 = forall a. Stream a
empty
| Bool
otherwise = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream a -> Step a Char
next a
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n)
where
next :: a -> Step a Char
next !a
i | a
i forall a. Ord a => a -> a -> Bool
>= a
n = forall s a. Step s a
Done
| Bool
otherwise = forall s a. a -> s -> Step s a
Yield Char
c (a
i forall a. Num a => a -> a -> a
+ a
1)
{-# INLINE [0] replicateCharI #-}
data RI s = RI !s {-# UNPACK #-} !Int64
replicateI :: Int64 -> Stream Char -> Stream Char
replicateI :: Int64 -> Stream Char -> Stream Char
replicateI Int64
n (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream RI s -> Step (RI s) Char
next (forall s. s -> Int64 -> RI s
RI s
s0 Int64
0) (Int64 -> Size
int64ToSize (forall a. Ord a => a -> a -> a
max Int64
0 Int64
n) forall a. Num a => a -> a -> a
* Size
len)
where
next :: RI s -> Step (RI s) Char
next (RI s
s Int64
k)
| Int64
k forall a. Ord a => a -> a -> Bool
>= Int64
n = forall s a. Step s a
Done
| Bool
otherwise = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. s -> Step s a
Skip (forall s. s -> Int64 -> RI s
RI s
s0 (Int64
kforall a. Num a => a -> a -> a
+Int64
1))
Skip s
s' -> forall s a. s -> Step s a
Skip (forall s. s -> Int64 -> RI s
RI s
s' Int64
k)
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall s. s -> Int64 -> RI s
RI s
s' Int64
k)
{-# INLINE [0] replicateI #-}
unfoldr :: (a -> Maybe (Char,a)) -> a -> Stream Char
unfoldr :: forall a. (a -> Maybe (Char, a)) -> a -> Stream Char
unfoldr a -> Maybe (Char, a)
f a
s0 = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream a -> Step a Char
next a
s0 Size
unknownSize
where
{-# INLINE next #-}
next :: a -> Step a Char
next !a
s = case a -> Maybe (Char, a)
f a
s of
Maybe (Char, a)
Nothing -> forall s a. Step s a
Done
Just (Char
w, a
s') -> forall s a. a -> s -> Step s a
Yield Char
w a
s'
{-# INLINE [0] unfoldr #-}
unfoldrNI :: Integral a => a -> (b -> Maybe (Char,b)) -> b -> Stream Char
unfoldrNI :: forall a b.
Integral a =>
a -> (b -> Maybe (Char, b)) -> b -> Stream Char
unfoldrNI a
n b -> Maybe (Char, b)
f b
s0 | a
n forall a. Ord a => a -> a -> Bool
< a
0 = forall a. Stream a
empty
| Bool
otherwise = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream PairS a b -> Step (PairS a b) Char
next (a
0 forall a b. a -> b -> PairS a b
:*: b
s0) (Int -> Size
maxSize forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
nforall a. Num a => a -> a -> a
*a
2))
where
{-# INLINE next #-}
next :: PairS a b -> Step (PairS a b) Char
next (a
z :*: b
s) = case b -> Maybe (Char, b)
f b
s of
Maybe (Char, b)
Nothing -> forall s a. Step s a
Done
Just (Char
w, b
s') | a
z forall a. Ord a => a -> a -> Bool
>= a
n -> forall s a. Step s a
Done
| Bool
otherwise -> forall s a. a -> s -> Step s a
Yield Char
w ((a
z forall a. Num a => a -> a -> a
+ a
1) forall a b. a -> b -> PairS a b
:*: b
s')
{-# INLINE unfoldrNI #-}
take :: Integral a => a -> Stream Char -> Stream Char
take :: forall a. Integral a => a -> Stream Char -> Stream Char
take a
n0 (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream forall {a}. (Ord a, Num a) => PairS a s -> Step (PairS a s) Char
next (a
n0' forall a b. a -> b -> PairS a b
:*: s
s0) (Size -> Size -> Size
smaller Size
len (Int -> Size
codePointsSize forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n0'))
where
n0' :: a
n0' = forall a. Ord a => a -> a -> a
max a
n0 a
0
{-# INLINE next #-}
next :: PairS a s -> Step (PairS a s) Char
next (a
n :*: s
s) | a
n forall a. Ord a => a -> a -> Bool
<= a
0 = forall s a. Step s a
Done
| Bool
otherwise = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (a
n forall a b. a -> b -> PairS a b
:*: s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x ((a
nforall a. Num a => a -> a -> a
-a
1) forall a b. a -> b -> PairS a b
:*: s
s')
{-# INLINE [0] take #-}
data Drop a s = NS !s
| JS !a !s
drop :: Integral a => a -> Stream Char -> Stream Char
drop :: forall a. Integral a => a -> Stream Char -> Stream Char
drop a
n0 (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream forall {a}. (Ord a, Num a) => Drop a s -> Step (Drop a s) Char
next (forall a s. a -> s -> Drop a s
JS a
n0' s
s0) (Size
len forall a. Num a => a -> a -> a
- Int -> Size
codePointsSize (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n0'))
where
n0' :: a
n0' = forall a. Ord a => a -> a -> a
max a
n0 a
0
{-# INLINE next #-}
next :: Drop a s -> Step (Drop a s) Char
next (JS a
n s
s)
| a
n forall a. Ord a => a -> a -> Bool
<= a
0 = forall s a. s -> Step s a
Skip (forall a s. s -> Drop a s
NS s
s)
| Bool
otherwise = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall a s. a -> s -> Drop a s
JS a
n s
s')
Yield Char
_ s
s' -> forall s a. s -> Step s a
Skip (forall a s. a -> s -> Drop a s
JS (a
nforall a. Num a => a -> a -> a
-a
1) s
s')
next (NS s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall a s. s -> Drop a s
NS s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall a s. s -> Drop a s
NS s
s')
{-# INLINE [0] drop #-}
takeWhile :: (Char -> Bool) -> Stream Char -> Stream Char
takeWhile :: (Char -> Bool) -> Stream Char -> Stream Char
takeWhile Char -> Bool
p (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream s -> Step s Char
next s
s0 (Size
len forall a. Num a => a -> a -> a
- Size
unknownSize)
where
{-# INLINE next #-}
next :: s -> Step s Char
next !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> forall s a. a -> s -> Step s a
Yield Char
x s
s'
| Bool
otherwise -> forall s a. Step s a
Done
{-# INLINE [0] takeWhile #-}
dropWhile :: (Char -> Bool) -> Stream Char -> Stream Char
dropWhile :: (Char -> Bool) -> Stream Char -> Stream Char
dropWhile Char -> Bool
p (Stream s -> Step s Char
next0 s
s0 Size
len) = forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream E s s -> Step (E s s) Char
next (forall l r. l -> E l r
L s
s0) (Size
len forall a. Num a => a -> a -> a
- Size
unknownSize)
where
{-# INLINE next #-}
next :: E s s -> Step (E s s) Char
next (L s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall l r. l -> E l r
L s
s')
Yield Char
x s
s' | Char -> Bool
p Char
x -> forall s a. s -> Step s a
Skip (forall l r. l -> E l r
L s
s')
| Bool
otherwise -> forall s a. a -> s -> Step s a
Yield Char
x (forall l r. r -> E l r
R s
s')
next (R s
s) = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip (forall l r. r -> E l r
R s
s')
Yield Char
x s
s' -> forall s a. a -> s -> Step s a
Yield Char
x (forall l r. r -> E l r
R s
s')
{-# INLINE [0] dropWhile #-}
isPrefixOf :: (Eq a) => Stream a -> Stream a -> Bool
isPrefixOf :: forall a. Eq a => Stream a -> Stream a -> Bool
isPrefixOf (Stream s -> Step s a
next1 s
s1 Size
_) (Stream s -> Step s a
next2 s
s2 Size
_) = Step s a -> Step s a -> Bool
loop (s -> Step s a
next1 s
s1) (s -> Step s a
next2 s
s2)
where
loop :: Step s a -> Step s a -> Bool
loop Step s a
Done Step s a
_ = Bool
True
loop Step s a
_ Step s a
Done = Bool
False
loop (Skip s
s1') (Skip s
s2') = Step s a -> Step s a -> Bool
loop (s -> Step s a
next1 s
s1') (s -> Step s a
next2 s
s2')
loop (Skip s
s1') Step s a
x2 = Step s a -> Step s a -> Bool
loop (s -> Step s a
next1 s
s1') Step s a
x2
loop Step s a
x1 (Skip s
s2') = Step s a -> Step s a -> Bool
loop Step s a
x1 (s -> Step s a
next2 s
s2')
loop (Yield a
x1 s
s1') (Yield a
x2 s
s2') = a
x1 forall a. Eq a => a -> a -> Bool
== a
x2 Bool -> Bool -> Bool
&&
Step s a -> Step s a -> Bool
loop (s -> Step s a
next1 s
s1') (s -> Step s a
next2 s
s2')
{-# INLINE [0] isPrefixOf #-}
elem :: Char -> Stream Char -> Bool
elem :: Char -> Stream Char -> Bool
elem Char
w (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Bool
loop_elem s
s0
where
loop_elem :: s -> Bool
loop_elem !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> Bool
False
Skip s
s' -> s -> Bool
loop_elem s
s'
Yield Char
x s
s' | Char
x forall a. Eq a => a -> a -> Bool
== Char
w -> Bool
True
| Bool
otherwise -> s -> Bool
loop_elem s
s'
{-# INLINE [0] elem #-}
findBy :: (Char -> Bool) -> Stream Char -> Maybe Char
findBy :: (Char -> Bool) -> Stream Char -> Maybe Char
findBy Char -> Bool
p (Stream s -> Step s Char
next s
s0 Size
_len) = s -> Maybe Char
loop_find s
s0
where
loop_find :: s -> Maybe Char
loop_find !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> forall a. Maybe a
Nothing
Skip s
s' -> s -> Maybe Char
loop_find s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> forall a. a -> Maybe a
Just Char
x
| Bool
otherwise -> s -> Maybe Char
loop_find s
s'
{-# INLINE [0] findBy #-}
indexI :: (HasCallStack, Integral a) => Stream Char -> a -> Char
indexI :: forall a. (HasCallStack, Integral a) => Stream Char -> a -> Char
indexI (Stream s -> Step s Char
next s
s0 Size
_len) a
n0
| a
n0 forall a. Ord a => a -> a -> Bool
< a
0 = forall a. HasCallStack => String -> String -> a
streamError String
"index" String
"Negative index"
| Bool
otherwise = forall {a}. (Eq a, Num a) => a -> s -> Char
loop_index a
n0 s
s0
where
loop_index :: a -> s -> Char
loop_index !a
n !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> forall a. HasCallStack => String -> String -> a
streamError String
"index" String
"Index too large"
Skip s
s' -> a -> s -> Char
loop_index a
n s
s'
Yield Char
x s
s' | a
n forall a. Eq a => a -> a -> Bool
== a
0 -> Char
x
| Bool
otherwise -> a -> s -> Char
loop_index (a
nforall a. Num a => a -> a -> a
-a
1) s
s'
{-# INLINE [0] indexI #-}
filter :: (Char -> Bool) -> Stream Char -> Stream Char
filter :: (Char -> Bool) -> Stream Char -> Stream Char
filter Char -> Bool
p (Stream s -> Step s Char
next0 s
s0 Size
len) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream s -> Step s Char
next s
s0 (Size
len forall a. Num a => a -> a -> a
- Size
unknownSize)
where
next :: s -> Step s Char
next !s
s = case s -> Step s Char
next0 s
s of
Step s Char
Done -> forall s a. Step s a
Done
Skip s
s' -> forall s a. s -> Step s a
Skip s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> forall s a. a -> s -> Step s a
Yield Char
x s
s'
| Bool
otherwise -> forall s a. s -> Step s a
Skip s
s'
{-# INLINE [0] filter #-}
{-# RULES
"STREAM filter/filter fusion" forall p q s.
filter p (filter q s) = filter (\x -> q x && p x) s
#-}
findIndexI :: Integral a => (Char -> Bool) -> Stream Char -> Maybe a
findIndexI :: forall a. Integral a => (Char -> Bool) -> Stream Char -> Maybe a
findIndexI Char -> Bool
p Stream Char
s = case forall a. Integral a => (Char -> Bool) -> Stream Char -> [a]
findIndicesI Char -> Bool
p Stream Char
s of
(a
i:[a]
_) -> forall a. a -> Maybe a
Just a
i
[a]
_ -> forall a. Maybe a
Nothing
{-# INLINE [0] findIndexI #-}
findIndicesI :: Integral a => (Char -> Bool) -> Stream Char -> [a]
findIndicesI :: forall a. Integral a => (Char -> Bool) -> Stream Char -> [a]
findIndicesI Char -> Bool
p (Stream s -> Step s Char
next s
s0 Size
_len) = forall {a}. Num a => a -> s -> [a]
loop_findIndex a
0 s
s0
where
loop_findIndex :: a -> s -> [a]
loop_findIndex !a
i !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> []
Skip s
s' -> a -> s -> [a]
loop_findIndex a
i s
s'
Yield Char
x s
s' | Char -> Bool
p Char
x -> a
i forall a. a -> [a] -> [a]
: a -> s -> [a]
loop_findIndex (a
iforall a. Num a => a -> a -> a
+a
1) s
s'
| Bool
otherwise -> a -> s -> [a]
loop_findIndex (a
iforall a. Num a => a -> a -> a
+a
1) s
s'
{-# INLINE [0] findIndicesI #-}
data Zip a b m = Z1 !a !b
| Z2 !a !b !m
zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
zipWith :: forall a b. (a -> a -> b) -> Stream a -> Stream a -> Stream b
zipWith a -> a -> b
f (Stream s -> Step s a
next0 s
sa0 Size
len1) (Stream s -> Step s a
next1 s
sb0 Size
len2) =
forall a s. (s -> Step s a) -> s -> Size -> Stream a
Stream Zip s s a -> Step (Zip s s a) b
next (forall a b m. a -> b -> Zip a b m
Z1 s
sa0 s
sb0) (Size -> Size -> Size
smaller Size
len1 Size
len2)
where
next :: Zip s s a -> Step (Zip s s a) b
next (Z1 s
sa s
sb) = case s -> Step s a
next0 s
sa of
Step s a
Done -> forall s a. Step s a
Done
Skip s
sa' -> forall s a. s -> Step s a
Skip (forall a b m. a -> b -> Zip a b m
Z1 s
sa' s
sb)
Yield a
a s
sa' -> forall s a. s -> Step s a
Skip (forall a b m. a -> b -> m -> Zip a b m
Z2 s
sa' s
sb a
a)
next (Z2 s
sa' s
sb a
a) = case s -> Step s a
next1 s
sb of
Step s a
Done -> forall s a. Step s a
Done
Skip s
sb' -> forall s a. s -> Step s a
Skip (forall a b m. a -> b -> m -> Zip a b m
Z2 s
sa' s
sb' a
a)
Yield a
b s
sb' -> forall s a. a -> s -> Step s a
Yield (a -> a -> b
f a
a a
b) (forall a b m. a -> b -> Zip a b m
Z1 s
sa' s
sb')
{-# INLINE [0] zipWith #-}
countCharI :: Integral a => Char -> Stream Char -> a
countCharI :: forall a. Integral a => Char -> Stream Char -> a
countCharI Char
a (Stream s -> Step s Char
next s
s0 Size
_len) = forall {t}. Num t => t -> s -> t
loop a
0 s
s0
where
loop :: t -> s -> t
loop !t
i !s
s = case s -> Step s Char
next s
s of
Step s Char
Done -> t
i
Skip s
s' -> t -> s -> t
loop t
i s
s'
Yield Char
x s
s' | Char
a forall a. Eq a => a -> a -> Bool
== Char
x -> t -> s -> t
loop (t
iforall a. Num a => a -> a -> a
+t
1) s
s'
| Bool
otherwise -> t -> s -> t
loop t
i s
s'
{-# INLINE [0] countCharI #-}
streamError :: HasCallStack => String -> String -> a
streamError :: forall a. HasCallStack => String -> String -> a
streamError String
func String
msg = forall a. HasCallStack => String -> a
P.error forall a b. (a -> b) -> a -> b
$ String
"Data.Text.Internal.Fusion.Common." forall a. [a] -> [a] -> [a]
++ String
func forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ String
msg
emptyError :: HasCallStack => String -> a
emptyError :: forall a. HasCallStack => String -> a
emptyError String
func = forall a. HasCallStack => String -> a
internalError String
func String
"Empty input"
internalError :: HasCallStack => String -> a
internalError :: forall a. HasCallStack => String -> a
internalError String
func = forall a. HasCallStack => String -> String -> a
streamError String
func String
"Internal error"
int64ToSize :: Int64 -> Size
int64ToSize :: Int64 -> Size
int64ToSize = forall a b. (Integral a, Num b) => a -> b
fromIntegral