{-# LANGUAGE CPP, BangPatterns #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
{-# OPTIONS_HADDOCK prune #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Data.ByteString.Lazy (
ByteString,
empty,
singleton,
pack,
unpack,
fromStrict,
toStrict,
fromChunks,
toChunks,
foldrChunks,
foldlChunks,
cons,
cons',
snoc,
append,
head,
uncons,
unsnoc,
last,
tail,
init,
null,
length,
map,
reverse,
intersperse,
intercalate,
transpose,
foldl,
foldl',
foldl1,
foldl1',
foldr,
foldr1,
concat,
concatMap,
any,
all,
maximum,
minimum,
scanl,
mapAccumL,
mapAccumR,
repeat,
replicate,
cycle,
iterate,
unfoldr,
take,
drop,
splitAt,
takeWhile,
dropWhile,
span,
break,
group,
groupBy,
inits,
tails,
stripPrefix,
stripSuffix,
split,
splitWith,
isPrefixOf,
isSuffixOf,
elem,
notElem,
find,
filter,
partition,
index,
elemIndex,
elemIndexEnd,
elemIndices,
findIndex,
findIndexEnd,
findIndices,
count,
zip,
zipWith,
unzip,
copy,
getContents,
putStr,
putStrLn,
interact,
readFile,
writeFile,
appendFile,
hGetContents,
hGet,
hGetNonBlocking,
hPut,
hPutNonBlocking,
hPutStr,
) where
import Prelude hiding
(reverse,head,tail,last,init,null,length,map,lines,foldl,foldr,unlines
,concat,any,take,drop,splitAt,takeWhile,dropWhile,span,break,elem,filter,maximum
,minimum,all,concatMap,foldl1,foldr1,scanl, scanl1, scanr, scanr1
,repeat, cycle, interact, iterate,readFile,writeFile,appendFile,replicate
,getContents,getLine,putStr,putStrLn ,zip,zipWith,unzip,notElem)
import qualified Data.List as L
import qualified Data.ByteString as P (ByteString)
import qualified Data.ByteString as S
import qualified Data.ByteString.Internal as S
import qualified Data.ByteString.Unsafe as S
import Data.ByteString.Lazy.Internal
#if !(MIN_VERSION_base(4,8,0))
import Control.Applicative ((<$>))
import Data.Monoid (Monoid(..))
#endif
import Control.Monad (mplus)
import Data.Word (Word8)
import Data.Int (Int64)
import System.IO (Handle,openBinaryFile,stdin,stdout,withBinaryFile,IOMode(..)
,hClose)
import System.IO.Error (mkIOError, illegalOperationErrorType)
import System.IO.Unsafe
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr
import Foreign.Storable
empty :: ByteString
empty :: ByteString
empty = ByteString
Empty
{-# INLINE empty #-}
singleton :: Word8 -> ByteString
singleton :: Word8 -> ByteString
singleton Word8
w = ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString
S.singleton Word8
w) ByteString
Empty
{-# INLINE singleton #-}
pack :: [Word8] -> ByteString
pack :: [Word8] -> ByteString
pack = [Word8] -> ByteString
packBytes
unpack :: ByteString -> [Word8]
unpack :: ByteString -> [Word8]
unpack = ByteString -> [Word8]
unpackBytes
fromChunks :: [P.ByteString] -> ByteString
fromChunks :: [ByteString] -> ByteString
fromChunks [ByteString]
cs = (ByteString -> ByteString -> ByteString)
-> ByteString -> [ByteString] -> ByteString
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
L.foldr ByteString -> ByteString -> ByteString
chunk ByteString
Empty [ByteString]
cs
toChunks :: ByteString -> [P.ByteString]
toChunks :: ByteString -> [ByteString]
toChunks ByteString
cs = (ByteString -> [ByteString] -> [ByteString])
-> [ByteString] -> ByteString -> [ByteString]
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (:) [] ByteString
cs
fromStrict :: P.ByteString -> ByteString
fromStrict :: ByteString -> ByteString
fromStrict ByteString
bs | ByteString -> Bool
S.null ByteString
bs = ByteString
Empty
| Bool
otherwise = ByteString -> ByteString -> ByteString
Chunk ByteString
bs ByteString
Empty
toStrict :: ByteString -> S.ByteString
toStrict :: ByteString -> ByteString
toStrict = \ByteString
cs -> ByteString -> ByteString -> ByteString
goLen0 ByteString
cs ByteString
cs
where
goLen0 :: ByteString -> ByteString -> ByteString
goLen0 ByteString
_ ByteString
Empty = ByteString
S.empty
goLen0 ByteString
cs0 (Chunk ByteString
c ByteString
cs) | ByteString -> Bool
S.null ByteString
c = ByteString -> ByteString -> ByteString
goLen0 ByteString
cs0 ByteString
cs
goLen0 ByteString
cs0 (Chunk ByteString
c ByteString
cs) = ByteString -> ByteString -> ByteString -> ByteString
goLen1 ByteString
cs0 ByteString
c ByteString
cs
goLen1 :: ByteString -> ByteString -> ByteString -> ByteString
goLen1 ByteString
_ ByteString
bs ByteString
Empty = ByteString
bs
goLen1 ByteString
cs0 ByteString
bs (Chunk ByteString
c ByteString
cs)
| ByteString -> Bool
S.null ByteString
c = ByteString -> ByteString -> ByteString -> ByteString
goLen1 ByteString
cs0 ByteString
bs ByteString
cs
| Bool
otherwise =
ByteString -> Int -> ByteString -> ByteString
goLen ByteString
cs0 (String -> Int -> Int -> Int
S.checkedAdd String
"Lazy.concat" (ByteString -> Int
S.length ByteString
bs) (ByteString -> Int
S.length ByteString
c)) ByteString
cs
goLen :: ByteString -> Int -> ByteString -> ByteString
goLen ByteString
cs0 !Int
total (Chunk ByteString
c ByteString
cs) = ByteString -> Int -> ByteString -> ByteString
goLen ByteString
cs0 Int
total' ByteString
cs
where
total' :: Int
total' = String -> Int -> Int -> Int
S.checkedAdd String
"Lazy.concat" Int
total (ByteString -> Int
S.length ByteString
c)
goLen ByteString
cs0 Int
total ByteString
Empty =
Int -> (Ptr Word8 -> IO ()) -> ByteString
S.unsafeCreate Int
total ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> ByteString -> Ptr Word8 -> IO ()
goCopy ByteString
cs0 Ptr Word8
ptr
goCopy :: ByteString -> Ptr Word8 -> IO ()
goCopy ByteString
Empty !Ptr Word8
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
goCopy (Chunk (S.PS ForeignPtr Word8
_ Int
_ Int
0 ) ByteString
cs) !Ptr Word8
ptr = ByteString -> Ptr Word8 -> IO ()
goCopy ByteString
cs Ptr Word8
ptr
goCopy (Chunk (S.PS ForeignPtr Word8
fp Int
off Int
len) ByteString
cs) !Ptr Word8
ptr = do
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
S.unsafeWithForeignPtr ForeignPtr Word8
fp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
Ptr Word8 -> Ptr Word8 -> Int -> IO ()
S.memcpy Ptr Word8
ptr (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off) Int
len
ByteString -> Ptr Word8 -> IO ()
goCopy ByteString
cs (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
len)
null :: ByteString -> Bool
null :: ByteString -> Bool
null ByteString
Empty = Bool
True
null ByteString
_ = Bool
False
{-# INLINE null #-}
length :: ByteString -> Int64
length :: ByteString -> Int64
length ByteString
cs = (Int64 -> ByteString -> Int64) -> Int64 -> ByteString -> Int64
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
foldlChunks (\Int64
n ByteString
c -> Int64
n Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) Int64
0 ByteString
cs
{-# INLINE length #-}
infixr 5 `cons`, `cons'`
infixl 5 `snoc`
cons :: Word8 -> ByteString -> ByteString
cons :: Word8 -> ByteString -> ByteString
cons Word8
c ByteString
cs = ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString
S.singleton Word8
c) ByteString
cs
{-# INLINE cons #-}
cons' :: Word8 -> ByteString -> ByteString
cons' :: Word8 -> ByteString -> ByteString
cons' Word8
w (Chunk ByteString
c ByteString
cs) | ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
16 = ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString -> ByteString
S.cons Word8
w ByteString
c) ByteString
cs
cons' Word8
w ByteString
cs = ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString
S.singleton Word8
w) ByteString
cs
{-# INLINE cons' #-}
snoc :: ByteString -> Word8 -> ByteString
snoc :: ByteString -> Word8 -> ByteString
snoc ByteString
cs Word8
w = (ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString
singleton Word8
w) ByteString
cs
{-# INLINE snoc #-}
head :: ByteString -> Word8
head :: ByteString -> Word8
head ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"head"
head (Chunk ByteString
c ByteString
_) = ByteString -> Word8
S.unsafeHead ByteString
c
{-# INLINE head #-}
uncons :: ByteString -> Maybe (Word8, ByteString)
uncons :: ByteString -> Maybe (Word8, ByteString)
uncons ByteString
Empty = Maybe (Word8, ByteString)
forall a. Maybe a
Nothing
uncons (Chunk ByteString
c ByteString
cs)
= (Word8, ByteString) -> Maybe (Word8, ByteString)
forall a. a -> Maybe a
Just (ByteString -> Word8
S.unsafeHead ByteString
c,
if ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then ByteString
cs else ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs)
{-# INLINE uncons #-}
tail :: ByteString -> ByteString
tail :: ByteString -> ByteString
tail ByteString
Empty = String -> ByteString
forall a. String -> a
errorEmptyList String
"tail"
tail (Chunk ByteString
c ByteString
cs)
| ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = ByteString
cs
| Bool
otherwise = ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs
{-# INLINE tail #-}
last :: ByteString -> Word8
last :: ByteString -> Word8
last ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"last"
last (Chunk ByteString
c0 ByteString
cs0) = ByteString -> ByteString -> Word8
go ByteString
c0 ByteString
cs0
where go :: ByteString -> ByteString -> Word8
go ByteString
c ByteString
Empty = ByteString -> Word8
S.unsafeLast ByteString
c
go ByteString
_ (Chunk ByteString
c ByteString
cs) = ByteString -> ByteString -> Word8
go ByteString
c ByteString
cs
init :: ByteString -> ByteString
init :: ByteString -> ByteString
init ByteString
Empty = String -> ByteString
forall a. String -> a
errorEmptyList String
"init"
init (Chunk ByteString
c0 ByteString
cs0) = ByteString -> ByteString -> ByteString
go ByteString
c0 ByteString
cs0
where go :: ByteString -> ByteString -> ByteString
go ByteString
c ByteString
Empty | ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = ByteString
Empty
| Bool
otherwise = ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeInit ByteString
c) ByteString
Empty
go ByteString
c (Chunk ByteString
c' ByteString
cs) = ByteString -> ByteString -> ByteString
Chunk ByteString
c (ByteString -> ByteString -> ByteString
go ByteString
c' ByteString
cs)
unsnoc :: ByteString -> Maybe (ByteString, Word8)
unsnoc :: ByteString -> Maybe (ByteString, Word8)
unsnoc ByteString
Empty = Maybe (ByteString, Word8)
forall a. Maybe a
Nothing
unsnoc (Chunk ByteString
c ByteString
cs) = (ByteString, Word8) -> Maybe (ByteString, Word8)
forall a. a -> Maybe a
Just (ByteString -> ByteString
init (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs), ByteString -> Word8
last (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs))
append :: ByteString -> ByteString -> ByteString
append :: ByteString -> ByteString -> ByteString
append = ByteString -> ByteString -> ByteString
forall a. Monoid a => a -> a -> a
mappend
{-# INLINE append #-}
map :: (Word8 -> Word8) -> ByteString -> ByteString
map :: (Word8 -> Word8) -> ByteString -> ByteString
map Word8 -> Word8
f ByteString
s = ByteString -> ByteString
go ByteString
s
where
go :: ByteString -> ByteString
go ByteString
Empty = ByteString
Empty
go (Chunk ByteString
x ByteString
xs) = ByteString -> ByteString -> ByteString
Chunk ByteString
y ByteString
ys
where
y :: ByteString
y = (Word8 -> Word8) -> ByteString -> ByteString
S.map Word8 -> Word8
f ByteString
x
ys :: ByteString
ys = ByteString -> ByteString
go ByteString
xs
{-# INLINE map #-}
reverse :: ByteString -> ByteString
reverse :: ByteString -> ByteString
reverse ByteString
cs0 = ByteString -> ByteString -> ByteString
rev ByteString
Empty ByteString
cs0
where rev :: ByteString -> ByteString -> ByteString
rev ByteString
a ByteString
Empty = ByteString
a
rev ByteString
a (Chunk ByteString
c ByteString
cs) = ByteString -> ByteString -> ByteString
rev (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.reverse ByteString
c) ByteString
a) ByteString
cs
{-# INLINE reverse #-}
intersperse :: Word8 -> ByteString -> ByteString
intersperse :: Word8 -> ByteString -> ByteString
intersperse Word8
_ ByteString
Empty = ByteString
Empty
intersperse Word8
w (Chunk ByteString
c ByteString
cs) = ByteString -> ByteString -> ByteString
Chunk (Word8 -> ByteString -> ByteString
S.intersperse Word8
w ByteString
c)
((ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString -> ByteString)
-> (ByteString -> ByteString)
-> ByteString
-> ByteString
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
intersperse') ByteString
Empty ByteString
cs)
where intersperse' :: P.ByteString -> P.ByteString
intersperse' :: ByteString -> ByteString
intersperse' (S.PS ForeignPtr Word8
fp Int
o Int
l) =
Int -> (Ptr Word8 -> IO ()) -> ByteString
S.unsafeCreate (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
l) ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p' -> ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
S.unsafeWithForeignPtr ForeignPtr Word8
fp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
p' Word8
w
Ptr Word8 -> Ptr Word8 -> CULong -> Word8 -> IO ()
S.c_intersperse (Ptr Word8
p' Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o) (Int -> CULong
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l) Word8
w
transpose :: [ByteString] -> [ByteString]
transpose :: [ByteString] -> [ByteString]
transpose [ByteString]
css = ([Word8] -> ByteString) -> [[Word8]] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
L.map (\[Word8]
ss -> ByteString -> ByteString -> ByteString
Chunk ([Word8] -> ByteString
S.pack [Word8]
ss) ByteString
Empty)
([[Word8]] -> [[Word8]]
forall a. [[a]] -> [[a]]
L.transpose ((ByteString -> [Word8]) -> [ByteString] -> [[Word8]]
forall a b. (a -> b) -> [a] -> [b]
L.map ByteString -> [Word8]
unpack [ByteString]
css))
foldl :: (a -> Word8 -> a) -> a -> ByteString -> a
foldl :: forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl a -> Word8 -> a
f a
z = a -> ByteString -> a
go a
z
where go :: a -> ByteString -> a
go a
a ByteString
Empty = a
a
go a
a (Chunk ByteString
c ByteString
cs) = a -> ByteString -> a
go ((a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
S.foldl a -> Word8 -> a
f a
a ByteString
c) ByteString
cs
{-# INLINE foldl #-}
foldl' :: (a -> Word8 -> a) -> a -> ByteString -> a
foldl' :: forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' a -> Word8 -> a
f a
z = a -> ByteString -> a
go a
z
where go :: a -> ByteString -> a
go !a
a ByteString
Empty = a
a
go !a
a (Chunk ByteString
c ByteString
cs) = a -> ByteString -> a
go ((a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
S.foldl' a -> Word8 -> a
f a
a ByteString
c) ByteString
cs
{-# INLINE foldl' #-}
foldr :: (Word8 -> a -> a) -> a -> ByteString -> a
foldr :: forall a. (Word8 -> a -> a) -> a -> ByteString -> a
foldr Word8 -> a -> a
k a
z = (ByteString -> a -> a) -> a -> ByteString -> a
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks ((a -> ByteString -> a) -> ByteString -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Word8 -> a -> a) -> a -> ByteString -> a
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
S.foldr Word8 -> a -> a
k)) a
z
{-# INLINE foldr #-}
foldl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldl1 Word8 -> Word8 -> Word8
_ ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"foldl1"
foldl1 Word8 -> Word8 -> Word8
f (Chunk ByteString
c ByteString
cs) = (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl Word8 -> Word8 -> Word8
f (ByteString -> Word8
S.unsafeHead ByteString
c) (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs)
foldl1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldl1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldl1' Word8 -> Word8 -> Word8
_ ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"foldl1'"
foldl1' Word8 -> Word8 -> Word8
f (Chunk ByteString
c ByteString
cs) = (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' Word8 -> Word8 -> Word8
f (ByteString -> Word8
S.unsafeHead ByteString
c) (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs)
foldr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8
foldr1 Word8 -> Word8 -> Word8
_ ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"foldr1"
foldr1 Word8 -> Word8 -> Word8
f (Chunk ByteString
c0 ByteString
cs0) = ByteString -> ByteString -> Word8
go ByteString
c0 ByteString
cs0
where go :: ByteString -> ByteString -> Word8
go ByteString
c ByteString
Empty = (Word8 -> Word8 -> Word8) -> ByteString -> Word8
S.foldr1 Word8 -> Word8 -> Word8
f ByteString
c
go ByteString
c (Chunk ByteString
c' ByteString
cs) = (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
S.foldr Word8 -> Word8 -> Word8
f (ByteString -> ByteString -> Word8
go ByteString
c' ByteString
cs) ByteString
c
concat :: [ByteString] -> ByteString
concat :: [ByteString] -> ByteString
concat = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat
concatMap :: (Word8 -> ByteString) -> ByteString -> ByteString
concatMap :: (Word8 -> ByteString) -> ByteString -> ByteString
concatMap Word8 -> ByteString
_ ByteString
Empty = ByteString
Empty
concatMap Word8 -> ByteString
f (Chunk ByteString
c0 ByteString
cs0) = ByteString -> ByteString -> ByteString
to ByteString
c0 ByteString
cs0
where
go :: ByteString -> P.ByteString -> ByteString -> ByteString
go :: ByteString -> ByteString -> ByteString -> ByteString
go ByteString
Empty ByteString
c' ByteString
cs' = ByteString -> ByteString -> ByteString
to ByteString
c' ByteString
cs'
go (Chunk ByteString
c ByteString
cs) ByteString
c' ByteString
cs' = ByteString -> ByteString -> ByteString
Chunk ByteString
c (ByteString -> ByteString -> ByteString -> ByteString
go ByteString
cs ByteString
c' ByteString
cs')
to :: P.ByteString -> ByteString -> ByteString
to :: ByteString -> ByteString -> ByteString
to ByteString
c ByteString
cs | ByteString -> Bool
S.null ByteString
c = case ByteString
cs of
ByteString
Empty -> ByteString
Empty
(Chunk ByteString
c' ByteString
cs') -> ByteString -> ByteString -> ByteString
to ByteString
c' ByteString
cs'
| Bool
otherwise = ByteString -> ByteString -> ByteString -> ByteString
go (Word8 -> ByteString
f (ByteString -> Word8
S.unsafeHead ByteString
c)) (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs
any :: (Word8 -> Bool) -> ByteString -> Bool
any :: (Word8 -> Bool) -> ByteString -> Bool
any Word8 -> Bool
f ByteString
cs = (ByteString -> Bool -> Bool) -> Bool -> ByteString -> Bool
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (\ByteString
c Bool
rest -> (Word8 -> Bool) -> ByteString -> Bool
S.any Word8 -> Bool
f ByteString
c Bool -> Bool -> Bool
|| Bool
rest) Bool
False ByteString
cs
{-# INLINE any #-}
all :: (Word8 -> Bool) -> ByteString -> Bool
all :: (Word8 -> Bool) -> ByteString -> Bool
all Word8 -> Bool
f ByteString
cs = (ByteString -> Bool -> Bool) -> Bool -> ByteString -> Bool
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (\ByteString
c Bool
rest -> (Word8 -> Bool) -> ByteString -> Bool
S.all Word8 -> Bool
f ByteString
c Bool -> Bool -> Bool
&& Bool
rest) Bool
True ByteString
cs
{-# INLINE all #-}
maximum :: ByteString -> Word8
maximum :: ByteString -> Word8
maximum ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"maximum"
maximum (Chunk ByteString
c ByteString
cs) = (Word8 -> ByteString -> Word8) -> Word8 -> ByteString -> Word8
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
foldlChunks (\Word8
n ByteString
c' -> Word8
n Word8 -> Word8 -> Word8
forall a. Ord a => a -> a -> a
`max` ByteString -> Word8
S.maximum ByteString
c')
(ByteString -> Word8
S.maximum ByteString
c) ByteString
cs
{-# INLINE maximum #-}
minimum :: ByteString -> Word8
minimum :: ByteString -> Word8
minimum ByteString
Empty = String -> Word8
forall a. String -> a
errorEmptyList String
"minimum"
minimum (Chunk ByteString
c ByteString
cs) = (Word8 -> ByteString -> Word8) -> Word8 -> ByteString -> Word8
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
foldlChunks (\Word8
n ByteString
c' -> Word8
n Word8 -> Word8 -> Word8
forall a. Ord a => a -> a -> a
`min` ByteString -> Word8
S.minimum ByteString
c')
(ByteString -> Word8
S.minimum ByteString
c) ByteString
cs
{-# INLINE minimum #-}
mapAccumL :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString)
mapAccumL :: forall acc.
(acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
mapAccumL acc -> Word8 -> (acc, Word8)
f acc
s0 = acc -> ByteString -> (acc, ByteString)
go acc
s0
where
go :: acc -> ByteString -> (acc, ByteString)
go acc
s ByteString
Empty = (acc
s, ByteString
Empty)
go acc
s (Chunk ByteString
c ByteString
cs) = (acc
s'', ByteString -> ByteString -> ByteString
Chunk ByteString
c' ByteString
cs')
where (acc
s', ByteString
c') = (acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
forall acc.
(acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
S.mapAccumL acc -> Word8 -> (acc, Word8)
f acc
s ByteString
c
(acc
s'', ByteString
cs') = acc -> ByteString -> (acc, ByteString)
go acc
s' ByteString
cs
mapAccumR :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString)
mapAccumR :: forall acc.
(acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
mapAccumR acc -> Word8 -> (acc, Word8)
f acc
s0 = acc -> ByteString -> (acc, ByteString)
go acc
s0
where
go :: acc -> ByteString -> (acc, ByteString)
go acc
s ByteString
Empty = (acc
s, ByteString
Empty)
go acc
s (Chunk ByteString
c ByteString
cs) = (acc
s'', ByteString -> ByteString -> ByteString
Chunk ByteString
c' ByteString
cs')
where (acc
s'', ByteString
c') = (acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
forall acc.
(acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
S.mapAccumR acc -> Word8 -> (acc, Word8)
f acc
s' ByteString
c
(acc
s', ByteString
cs') = acc -> ByteString -> (acc, ByteString)
go acc
s ByteString
cs
scanl :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString
scanl :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString
scanl Word8 -> Word8 -> Word8
f Word8
z = (Word8, ByteString) -> ByteString
forall a b. (a, b) -> b
snd ((Word8, ByteString) -> ByteString)
-> (ByteString -> (Word8, ByteString)) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word8, ByteString) -> Word8 -> (Word8, ByteString))
-> (Word8, ByteString) -> ByteString -> (Word8, ByteString)
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl (Word8, ByteString) -> Word8 -> (Word8, ByteString)
k (Word8
z,Word8 -> ByteString
singleton Word8
z)
where
k :: (Word8, ByteString) -> Word8 -> (Word8, ByteString)
k (Word8
c,ByteString
acc) Word8
a = let n :: Word8
n = Word8 -> Word8 -> Word8
f Word8
c Word8
a in (Word8
n, ByteString
acc ByteString -> Word8 -> ByteString
`snoc` Word8
n)
{-# INLINE scanl #-}
iterate :: (Word8 -> Word8) -> Word8 -> ByteString
iterate :: (Word8 -> Word8) -> Word8 -> ByteString
iterate Word8 -> Word8
f = (Word8 -> Maybe (Word8, Word8)) -> Word8 -> ByteString
forall a. (a -> Maybe (Word8, a)) -> a -> ByteString
unfoldr (\Word8
x -> case Word8 -> Word8
f Word8
x of !Word8
x' -> (Word8, Word8) -> Maybe (Word8, Word8)
forall a. a -> Maybe a
Just (Word8
x', Word8
x'))
repeat :: Word8 -> ByteString
repeat :: Word8 -> ByteString
repeat Word8
w = ByteString
cs where cs :: ByteString
cs = ByteString -> ByteString -> ByteString
Chunk (Int -> Word8 -> ByteString
S.replicate Int
smallChunkSize Word8
w) ByteString
cs
replicate :: Int64 -> Word8 -> ByteString
replicate :: Int64 -> Word8 -> ByteString
replicate Int64
n Word8
w
| Int64
n Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
0 = ByteString
Empty
| Int64
n Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
smallChunkSize = ByteString -> ByteString -> ByteString
Chunk (Int -> Word8 -> ByteString
S.replicate (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n) Word8
w) ByteString
Empty
| Int64
r Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
0 = ByteString
cs
| Bool
otherwise = ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.unsafeTake (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
r) ByteString
c) ByteString
cs
where
c :: ByteString
c = Int -> Word8 -> ByteString
S.replicate Int
smallChunkSize Word8
w
cs :: ByteString
cs = Int64 -> ByteString
forall {t}. (Eq t, Num t) => t -> ByteString
nChunks Int64
q
(Int64
q, Int64
r) = Int64 -> Int64 -> (Int64, Int64)
forall a. Integral a => a -> a -> (a, a)
quotRem Int64
n (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
smallChunkSize)
nChunks :: t -> ByteString
nChunks t
0 = ByteString
Empty
nChunks t
m = ByteString -> ByteString -> ByteString
Chunk ByteString
c (t -> ByteString
nChunks (t
mt -> t -> t
forall a. Num a => a -> a -> a
-t
1))
cycle :: ByteString -> ByteString
cycle :: ByteString -> ByteString
cycle ByteString
Empty = String -> ByteString
forall a. String -> a
errorEmptyList String
"cycle"
cycle ByteString
cs = ByteString
cs' where cs' :: ByteString
cs' = (ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks ByteString -> ByteString -> ByteString
Chunk ByteString
cs' ByteString
cs
unfoldr :: (a -> Maybe (Word8, a)) -> a -> ByteString
unfoldr :: forall a. (a -> Maybe (Word8, a)) -> a -> ByteString
unfoldr a -> Maybe (Word8, a)
f a
z = Int -> a -> ByteString
unfoldChunk Int
32 a
z
where unfoldChunk :: Int -> a -> ByteString
unfoldChunk Int
n a
x =
case Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
S.unfoldrN Int
n a -> Maybe (Word8, a)
f a
x of
(ByteString
c, Maybe a
Nothing)
| ByteString -> Bool
S.null ByteString
c -> ByteString
Empty
| Bool
otherwise -> ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
Empty
(ByteString
c, Just a
x') -> ByteString -> ByteString -> ByteString
Chunk ByteString
c (Int -> a -> ByteString
unfoldChunk (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
2) a
x')
take :: Int64 -> ByteString -> ByteString
take :: Int64 -> ByteString -> ByteString
take Int64
i ByteString
_ | Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
0 = ByteString
Empty
take Int64
i ByteString
cs0 = Int64 -> ByteString -> ByteString
forall {t}. Integral t => t -> ByteString -> ByteString
take' Int64
i ByteString
cs0
where take' :: t -> ByteString -> ByteString
take' t
0 ByteString
_ = ByteString
Empty
take' t
_ ByteString
Empty = ByteString
Empty
take' t
n (Chunk ByteString
c ByteString
cs) =
if t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)
then ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.take (t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) ByteString
Empty
else ByteString -> ByteString -> ByteString
Chunk ByteString
c (t -> ByteString -> ByteString
take' (t
n t -> t -> t
forall a. Num a => a -> a -> a
- Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs)
drop :: Int64 -> ByteString -> ByteString
drop :: Int64 -> ByteString -> ByteString
drop Int64
i ByteString
p | Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
0 = ByteString
p
drop Int64
i ByteString
cs0 = Int64 -> ByteString -> ByteString
forall {t}. Integral t => t -> ByteString -> ByteString
drop' Int64
i ByteString
cs0
where drop' :: t -> ByteString -> ByteString
drop' t
0 ByteString
cs = ByteString
cs
drop' t
_ ByteString
Empty = ByteString
Empty
drop' t
n (Chunk ByteString
c ByteString
cs) =
if t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)
then ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.drop (t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) ByteString
cs
else t -> ByteString -> ByteString
drop' (t
n t -> t -> t
forall a. Num a => a -> a -> a
- Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
splitAt :: Int64 -> ByteString -> (ByteString, ByteString)
splitAt :: Int64 -> ByteString -> (ByteString, ByteString)
splitAt Int64
i ByteString
cs0 | Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
0 = (ByteString
Empty, ByteString
cs0)
splitAt Int64
i ByteString
cs0 = Int64 -> ByteString -> (ByteString, ByteString)
forall {a}.
Integral a =>
a -> ByteString -> (ByteString, ByteString)
splitAt' Int64
i ByteString
cs0
where splitAt' :: a -> ByteString -> (ByteString, ByteString)
splitAt' a
0 ByteString
cs = (ByteString
Empty, ByteString
cs)
splitAt' a
_ ByteString
Empty = (ByteString
Empty, ByteString
Empty)
splitAt' a
n (Chunk ByteString
c ByteString
cs) =
if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)
then (ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.take (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) ByteString
c) ByteString
Empty
,ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.drop (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) ByteString
c) ByteString
cs)
else let (ByteString
cs', ByteString
cs'') = a -> ByteString -> (ByteString, ByteString)
splitAt' (a
n a -> a -> a
forall a. Num a => a -> a -> a
- Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
in (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs', ByteString
cs'')
takeWhile :: (Word8 -> Bool) -> ByteString -> ByteString
takeWhile :: (Word8 -> Bool) -> ByteString -> ByteString
takeWhile Word8 -> Bool
f ByteString
cs0 = ByteString -> ByteString
takeWhile' ByteString
cs0
where takeWhile' :: ByteString -> ByteString
takeWhile' ByteString
Empty = ByteString
Empty
takeWhile' (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not (Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
f) ByteString
c of
Int
0 -> ByteString
Empty
Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
S.length ByteString
c -> ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.take Int
n ByteString
c) ByteString
Empty
| Bool
otherwise -> ByteString -> ByteString -> ByteString
Chunk ByteString
c (ByteString -> ByteString
takeWhile' ByteString
cs)
dropWhile :: (Word8 -> Bool) -> ByteString -> ByteString
dropWhile :: (Word8 -> Bool) -> ByteString -> ByteString
dropWhile Word8 -> Bool
f ByteString
cs0 = ByteString -> ByteString
dropWhile' ByteString
cs0
where dropWhile' :: ByteString -> ByteString
dropWhile' ByteString
Empty = ByteString
Empty
dropWhile' (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not (Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
f) ByteString
c of
Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
S.length ByteString
c -> ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.drop Int
n ByteString
c) ByteString
cs
| Bool
otherwise -> ByteString -> ByteString
dropWhile' ByteString
cs
break :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
break :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
break Word8 -> Bool
f ByteString
cs0 = ByteString -> (ByteString, ByteString)
break' ByteString
cs0
where break' :: ByteString -> (ByteString, ByteString)
break' ByteString
Empty = (ByteString
Empty, ByteString
Empty)
break' (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd Word8 -> Bool
f ByteString
c of
Int
0 -> (ByteString
Empty, ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
S.length ByteString
c -> (ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.take Int
n ByteString
c) ByteString
Empty
,ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.drop Int
n ByteString
c) ByteString
cs)
| Bool
otherwise -> let (ByteString
cs', ByteString
cs'') = ByteString -> (ByteString, ByteString)
break' ByteString
cs
in (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs', ByteString
cs'')
span :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
span :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
span Word8 -> Bool
p = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
break (Bool -> Bool
not (Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
p)
splitWith :: (Word8 -> Bool) -> ByteString -> [ByteString]
splitWith :: (Word8 -> Bool) -> ByteString -> [ByteString]
splitWith Word8 -> Bool
_ ByteString
Empty = []
splitWith Word8 -> Bool
p (Chunk ByteString
c0 ByteString
cs0) = [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [] ((Word8 -> Bool) -> ByteString -> [ByteString]
S.splitWith Word8 -> Bool
p ByteString
c0) ByteString
cs0
where comb :: [P.ByteString] -> [P.ByteString] -> ByteString -> [ByteString]
comb :: [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [ByteString]
acc (ByteString
s:[]) ByteString
Empty = [ByteString] -> ByteString
revChunks (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []
comb [ByteString]
acc (ByteString
s:[]) (Chunk ByteString
c ByteString
cs) = [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) ((Word8 -> Bool) -> ByteString -> [ByteString]
S.splitWith Word8 -> Bool
p ByteString
c) ByteString
cs
comb [ByteString]
acc (ByteString
s:[ByteString]
ss) ByteString
cs = [ByteString] -> ByteString
revChunks (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [] [ByteString]
ss ByteString
cs
{-# INLINE splitWith #-}
split :: Word8 -> ByteString -> [ByteString]
split :: Word8 -> ByteString -> [ByteString]
split Word8
_ ByteString
Empty = []
split Word8
w (Chunk ByteString
c0 ByteString
cs0) = [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [] (Word8 -> ByteString -> [ByteString]
S.split Word8
w ByteString
c0) ByteString
cs0
where comb :: [P.ByteString] -> [P.ByteString] -> ByteString -> [ByteString]
comb :: [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [ByteString]
acc (ByteString
s:[]) ByteString
Empty = [ByteString] -> ByteString
revChunks (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []
comb [ByteString]
acc (ByteString
s:[]) (Chunk ByteString
c ByteString
cs) = [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) (Word8 -> ByteString -> [ByteString]
S.split Word8
w ByteString
c) ByteString
cs
comb [ByteString]
acc (ByteString
s:[ByteString]
ss) ByteString
cs = [ByteString] -> ByteString
revChunks (ByteString
sByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
acc) ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> [ByteString] -> ByteString -> [ByteString]
comb [] [ByteString]
ss ByteString
cs
{-# INLINE split #-}
group :: ByteString -> [ByteString]
group :: ByteString -> [ByteString]
group = ByteString -> [ByteString]
go
where
go :: ByteString -> [ByteString]
go ByteString
Empty = []
go (Chunk ByteString
c ByteString
cs)
| ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = [ByteString] -> Word8 -> ByteString -> [ByteString]
to [ByteString
c] (ByteString -> Word8
S.unsafeHead ByteString
c) ByteString
cs
| Bool
otherwise = [ByteString] -> Word8 -> ByteString -> [ByteString]
to [Int -> ByteString -> ByteString
S.unsafeTake Int
1 ByteString
c] (ByteString -> Word8
S.unsafeHead ByteString
c) (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs)
to :: [ByteString] -> Word8 -> ByteString -> [ByteString]
to [ByteString]
acc !Word8
_ ByteString
Empty = [ByteString] -> ByteString
revNonEmptyChunks [ByteString]
acc ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []
to [ByteString]
acc !Word8
w (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
w) ByteString
c of
Int
0 -> [ByteString] -> ByteString
revNonEmptyChunks [ByteString]
acc
ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
go (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
Int
n | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
S.length ByteString
c -> [ByteString] -> Word8 -> ByteString -> [ByteString]
to (Int -> ByteString -> ByteString
S.unsafeTake Int
n ByteString
c ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
acc) Word8
w ByteString
cs
| Bool
otherwise -> [ByteString] -> ByteString
revNonEmptyChunks (Int -> ByteString -> ByteString
S.unsafeTake Int
n ByteString
c ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
acc)
ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
go (ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.unsafeDrop Int
n ByteString
c) ByteString
cs)
groupBy :: (Word8 -> Word8 -> Bool) -> ByteString -> [ByteString]
groupBy :: (Word8 -> Word8 -> Bool) -> ByteString -> [ByteString]
groupBy Word8 -> Word8 -> Bool
k = ByteString -> [ByteString]
go
where
go :: ByteString -> [ByteString]
go ByteString
Empty = []
go (Chunk ByteString
c ByteString
cs)
| ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = [ByteString] -> Word8 -> ByteString -> [ByteString]
to [ByteString
c] (ByteString -> Word8
S.unsafeHead ByteString
c) ByteString
cs
| Bool
otherwise = [ByteString] -> Word8 -> ByteString -> [ByteString]
to [Int -> ByteString -> ByteString
S.unsafeTake Int
1 ByteString
c] (ByteString -> Word8
S.unsafeHead ByteString
c) (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs)
to :: [ByteString] -> Word8 -> ByteString -> [ByteString]
to [ByteString]
acc !Word8
_ ByteString
Empty = [ByteString] -> ByteString
revNonEmptyChunks [ByteString]
acc ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []
to [ByteString]
acc !Word8
w (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not (Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word8 -> Bool
k Word8
w) ByteString
c of
Int
0 -> [ByteString] -> ByteString
revNonEmptyChunks [ByteString]
acc
ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
go (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
Int
n | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
S.length ByteString
c -> [ByteString] -> Word8 -> ByteString -> [ByteString]
to (Int -> ByteString -> ByteString
S.unsafeTake Int
n ByteString
c ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
acc) Word8
w ByteString
cs
| Bool
otherwise -> [ByteString] -> ByteString
revNonEmptyChunks (Int -> ByteString -> ByteString
S.unsafeTake Int
n ByteString
c ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
acc)
ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
go (ByteString -> ByteString -> ByteString
Chunk (Int -> ByteString -> ByteString
S.unsafeDrop Int
n ByteString
c) ByteString
cs)
intercalate :: ByteString -> [ByteString] -> ByteString
intercalate :: ByteString -> [ByteString] -> ByteString
intercalate ByteString
s = [ByteString] -> ByteString
concat ([ByteString] -> ByteString)
-> ([ByteString] -> [ByteString]) -> [ByteString] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
L.intersperse ByteString
s
index :: ByteString -> Int64 -> Word8
index :: ByteString -> Int64 -> Word8
index ByteString
_ Int64
i | Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
0 = String -> String -> Word8
forall a. String -> String -> a
moduleError String
"index" (String
"negative index: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int64 -> String
forall a. Show a => a -> String
show Int64
i)
index ByteString
cs0 Int64
i = ByteString -> Int64 -> Word8
forall {a}. (Show a, Integral a) => ByteString -> a -> Word8
index' ByteString
cs0 Int64
i
where index' :: ByteString -> a -> Word8
index' ByteString
Empty a
n = String -> String -> Word8
forall a. String -> String -> a
moduleError String
"index" (String
"index too large: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
n)
index' (Chunk ByteString
c ByteString
cs) a
n
| a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c) =
ByteString -> a -> Word8
index' ByteString
cs (a
n a -> a -> a
forall a. Num a => a -> a -> a
- Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c))
| Bool
otherwise = ByteString -> Int -> Word8
S.unsafeIndex ByteString
c (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n)
elemIndex :: Word8 -> ByteString -> Maybe Int64
elemIndex :: Word8 -> ByteString -> Maybe Int64
elemIndex Word8
w ByteString
cs0 = Int64 -> ByteString -> Maybe Int64
forall {a}. Num a => a -> ByteString -> Maybe a
elemIndex' Int64
0 ByteString
cs0
where elemIndex' :: a -> ByteString -> Maybe a
elemIndex' a
_ ByteString
Empty = Maybe a
forall a. Maybe a
Nothing
elemIndex' a
n (Chunk ByteString
c ByteString
cs) =
case Word8 -> ByteString -> Maybe Int
S.elemIndex Word8
w ByteString
c of
Maybe Int
Nothing -> a -> ByteString -> Maybe a
elemIndex' (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
Just Int
i -> a -> Maybe a
forall a. a -> Maybe a
Just (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)
elemIndexEnd :: Word8 -> ByteString -> Maybe Int64
elemIndexEnd :: Word8 -> ByteString -> Maybe Int64
elemIndexEnd = (Word8 -> Bool) -> ByteString -> Maybe Int64
findIndexEnd ((Word8 -> Bool) -> ByteString -> Maybe Int64)
-> (Word8 -> Word8 -> Bool) -> Word8 -> ByteString -> Maybe Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE elemIndexEnd #-}
elemIndices :: Word8 -> ByteString -> [Int64]
elemIndices :: Word8 -> ByteString -> [Int64]
elemIndices Word8
w ByteString
cs0 = Int64 -> ByteString -> [Int64]
forall {a}. Num a => a -> ByteString -> [a]
elemIndices' Int64
0 ByteString
cs0
where elemIndices' :: a -> ByteString -> [a]
elemIndices' a
_ ByteString
Empty = []
elemIndices' a
n (Chunk ByteString
c ByteString
cs) = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
L.map ((a -> a -> a
forall a. Num a => a -> a -> a
+a
n)(a -> a) -> (Int -> a) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (Word8 -> ByteString -> [Int]
S.elemIndices Word8
w ByteString
c)
[a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> ByteString -> [a]
elemIndices' (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
count :: Word8 -> ByteString -> Int64
count :: Word8 -> ByteString -> Int64
count Word8
w ByteString
cs = (Int64 -> ByteString -> Int64) -> Int64 -> ByteString -> Int64
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
foldlChunks (\Int64
n ByteString
c -> Int64
n Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> ByteString -> Int
S.count Word8
w ByteString
c)) Int64
0 ByteString
cs
findIndex :: (Word8 -> Bool) -> ByteString -> Maybe Int64
findIndex :: (Word8 -> Bool) -> ByteString -> Maybe Int64
findIndex Word8 -> Bool
k ByteString
cs0 = Int64 -> ByteString -> Maybe Int64
forall {a}. Num a => a -> ByteString -> Maybe a
findIndex' Int64
0 ByteString
cs0
where findIndex' :: a -> ByteString -> Maybe a
findIndex' a
_ ByteString
Empty = Maybe a
forall a. Maybe a
Nothing
findIndex' a
n (Chunk ByteString
c ByteString
cs) =
case (Word8 -> Bool) -> ByteString -> Maybe Int
S.findIndex Word8 -> Bool
k ByteString
c of
Maybe Int
Nothing -> a -> ByteString -> Maybe a
findIndex' (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
Just Int
i -> a -> Maybe a
forall a. a -> Maybe a
Just (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)
{-# INLINE findIndex #-}
findIndexEnd :: (Word8 -> Bool) -> ByteString -> Maybe Int64
findIndexEnd :: (Word8 -> Bool) -> ByteString -> Maybe Int64
findIndexEnd Word8 -> Bool
k = Int -> ByteString -> Maybe Int64
forall {a}. Num a => Int -> ByteString -> Maybe a
findIndexEnd' Int
0
where
findIndexEnd' :: Int -> ByteString -> Maybe a
findIndexEnd' Int
_ ByteString
Empty = Maybe a
forall a. Maybe a
Nothing
findIndexEnd' Int
n (Chunk ByteString
c ByteString
cs) =
let !n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
S.length ByteString
c
!i :: Maybe a
i = (Int -> a) -> Maybe Int -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> a) -> (Int -> Int) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+)) (Maybe Int -> Maybe a) -> Maybe Int -> Maybe a
forall a b. (a -> b) -> a -> b
$ (Word8 -> Bool) -> ByteString -> Maybe Int
S.findIndexEnd Word8 -> Bool
k ByteString
c
in Int -> ByteString -> Maybe a
findIndexEnd' Int
n' ByteString
cs Maybe a -> Maybe a -> Maybe a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Maybe a
i
{-# INLINE findIndexEnd #-}
find :: (Word8 -> Bool) -> ByteString -> Maybe Word8
find :: (Word8 -> Bool) -> ByteString -> Maybe Word8
find Word8 -> Bool
f ByteString
cs0 = ByteString -> Maybe Word8
find' ByteString
cs0
where find' :: ByteString -> Maybe Word8
find' ByteString
Empty = Maybe Word8
forall a. Maybe a
Nothing
find' (Chunk ByteString
c ByteString
cs) = case (Word8 -> Bool) -> ByteString -> Maybe Word8
S.find Word8 -> Bool
f ByteString
c of
Maybe Word8
Nothing -> ByteString -> Maybe Word8
find' ByteString
cs
Just Word8
w -> Word8 -> Maybe Word8
forall a. a -> Maybe a
Just Word8
w
{-# INLINE find #-}
findIndices :: (Word8 -> Bool) -> ByteString -> [Int64]
findIndices :: (Word8 -> Bool) -> ByteString -> [Int64]
findIndices Word8 -> Bool
k ByteString
cs0 = Int64 -> ByteString -> [Int64]
forall {a}. Num a => a -> ByteString -> [a]
findIndices' Int64
0 ByteString
cs0
where findIndices' :: a -> ByteString -> [a]
findIndices' a
_ ByteString
Empty = []
findIndices' a
n (Chunk ByteString
c ByteString
cs) = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
L.map ((a -> a -> a
forall a. Num a => a -> a -> a
+a
n)(a -> a) -> (Int -> a) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral) ((Word8 -> Bool) -> ByteString -> [Int]
S.findIndices Word8 -> Bool
k ByteString
c)
[a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> ByteString -> [a]
findIndices' (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
S.length ByteString
c)) ByteString
cs
elem :: Word8 -> ByteString -> Bool
elem :: Word8 -> ByteString -> Bool
elem Word8
w ByteString
cs = case Word8 -> ByteString -> Maybe Int64
elemIndex Word8
w ByteString
cs of Maybe Int64
Nothing -> Bool
False ; Maybe Int64
_ -> Bool
True
notElem :: Word8 -> ByteString -> Bool
notElem :: Word8 -> ByteString -> Bool
notElem Word8
w ByteString
cs = Bool -> Bool
not (Word8 -> ByteString -> Bool
elem Word8
w ByteString
cs)
filter :: (Word8 -> Bool) -> ByteString -> ByteString
filter :: (Word8 -> Bool) -> ByteString -> ByteString
filter Word8 -> Bool
p ByteString
s = ByteString -> ByteString
go ByteString
s
where
go :: ByteString -> ByteString
go ByteString
Empty = ByteString
Empty
go (Chunk ByteString
x ByteString
xs) = ByteString -> ByteString -> ByteString
chunk ((Word8 -> Bool) -> ByteString -> ByteString
S.filter Word8 -> Bool
p ByteString
x) (ByteString -> ByteString
go ByteString
xs)
{-# INLINE filter #-}
partition :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
partition :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
partition Word8 -> Bool
_ ByteString
Empty = (ByteString
Empty, ByteString
Empty)
partition Word8 -> Bool
p (Chunk ByteString
x ByteString
xs) = (ByteString -> ByteString -> ByteString
chunk ByteString
t ByteString
ts, ByteString -> ByteString -> ByteString
chunk ByteString
f ByteString
fs)
where
(ByteString
t, ByteString
f) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
S.partition Word8 -> Bool
p ByteString
x
(ByteString
ts, ByteString
fs) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
partition Word8 -> Bool
p ByteString
xs
isPrefixOf :: ByteString -> ByteString -> Bool
isPrefixOf :: ByteString -> ByteString -> Bool
isPrefixOf ByteString
Empty ByteString
_ = Bool
True
isPrefixOf ByteString
_ ByteString
Empty = Bool
False
isPrefixOf (Chunk ByteString
x ByteString
xs) (Chunk ByteString
y ByteString
ys)
| ByteString -> Int
S.length ByteString
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
S.length ByteString
y = ByteString
x ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
y Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
isPrefixOf ByteString
xs ByteString
ys
| ByteString -> Int
S.length ByteString
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
S.length ByteString
y = ByteString
x ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
yh Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
isPrefixOf ByteString
xs (ByteString -> ByteString -> ByteString
Chunk ByteString
yt ByteString
ys)
| Bool
otherwise = ByteString
xh ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
y Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
isPrefixOf (ByteString -> ByteString -> ByteString
Chunk ByteString
xt ByteString
xs) ByteString
ys
where (ByteString
xh,ByteString
xt) = Int -> ByteString -> (ByteString, ByteString)
S.splitAt (ByteString -> Int
S.length ByteString
y) ByteString
x
(ByteString
yh,ByteString
yt) = Int -> ByteString -> (ByteString, ByteString)
S.splitAt (ByteString -> Int
S.length ByteString
x) ByteString
y
stripPrefix :: ByteString -> ByteString -> Maybe ByteString
stripPrefix :: ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
Empty ByteString
bs = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bs
stripPrefix ByteString
_ ByteString
Empty = Maybe ByteString
forall a. Maybe a
Nothing
stripPrefix (Chunk ByteString
x ByteString
xs) (Chunk ByteString
y ByteString
ys)
| ByteString -> Int
S.length ByteString
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
S.length ByteString
y = if ByteString
x ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
y then ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
xs ByteString
ys else Maybe ByteString
forall a. Maybe a
Nothing
| ByteString -> Int
S.length ByteString
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
S.length ByteString
y = do ByteString
yt <- ByteString -> ByteString -> Maybe ByteString
S.stripPrefix ByteString
x ByteString
y
ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
xs (ByteString -> ByteString -> ByteString
Chunk ByteString
yt ByteString
ys)
| Bool
otherwise = do ByteString
xt <- ByteString -> ByteString -> Maybe ByteString
S.stripPrefix ByteString
y ByteString
x
ByteString -> ByteString -> Maybe ByteString
stripPrefix (ByteString -> ByteString -> ByteString
Chunk ByteString
xt ByteString
xs) ByteString
ys
isSuffixOf :: ByteString -> ByteString -> Bool
isSuffixOf :: ByteString -> ByteString -> Bool
isSuffixOf ByteString
x ByteString
y = ByteString -> ByteString
reverse ByteString
x ByteString -> ByteString -> Bool
`isPrefixOf` ByteString -> ByteString
reverse ByteString
y
stripSuffix :: ByteString -> ByteString -> Maybe ByteString
stripSuffix :: ByteString -> ByteString -> Maybe ByteString
stripSuffix ByteString
x ByteString
y = ByteString -> ByteString
reverse (ByteString -> ByteString) -> Maybe ByteString -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> ByteString -> Maybe ByteString
stripPrefix (ByteString -> ByteString
reverse ByteString
x) (ByteString -> ByteString
reverse ByteString
y)
zip :: ByteString -> ByteString -> [(Word8,Word8)]
zip :: ByteString -> ByteString -> [(Word8, Word8)]
zip = (Word8 -> Word8 -> (Word8, Word8))
-> ByteString -> ByteString -> [(Word8, Word8)]
forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
zipWith (,)
zipWith :: (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
zipWith :: forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
zipWith Word8 -> Word8 -> a
_ ByteString
Empty ByteString
_ = []
zipWith Word8 -> Word8 -> a
_ ByteString
_ ByteString
Empty = []
zipWith Word8 -> Word8 -> a
f (Chunk ByteString
a ByteString
as) (Chunk ByteString
b ByteString
bs) = ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
a ByteString
as ByteString
b ByteString
bs
where
go :: ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
x ByteString
xs ByteString
y ByteString
ys = Word8 -> Word8 -> a
f (ByteString -> Word8
S.unsafeHead ByteString
x) (ByteString -> Word8
S.unsafeHead ByteString
y)
a -> [a] -> [a]
forall a. a -> [a] -> [a]
: ByteString -> ByteString -> ByteString -> ByteString -> [a]
to (ByteString -> ByteString
S.unsafeTail ByteString
x) ByteString
xs (ByteString -> ByteString
S.unsafeTail ByteString
y) ByteString
ys
to :: ByteString -> ByteString -> ByteString -> ByteString -> [a]
to ByteString
x ByteString
Empty ByteString
_ ByteString
_ | ByteString -> Bool
S.null ByteString
x = []
to ByteString
_ ByteString
_ ByteString
y ByteString
Empty | ByteString -> Bool
S.null ByteString
y = []
to ByteString
x ByteString
xs ByteString
y ByteString
ys | Bool -> Bool
not (ByteString -> Bool
S.null ByteString
x)
Bool -> Bool -> Bool
&& Bool -> Bool
not (ByteString -> Bool
S.null ByteString
y) = ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
x ByteString
xs ByteString
y ByteString
ys
to ByteString
x ByteString
xs ByteString
_ (Chunk ByteString
y' ByteString
ys) | Bool -> Bool
not (ByteString -> Bool
S.null ByteString
x) = ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
x ByteString
xs ByteString
y' ByteString
ys
to ByteString
_ (Chunk ByteString
x' ByteString
xs) ByteString
y ByteString
ys | Bool -> Bool
not (ByteString -> Bool
S.null ByteString
y) = ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
x' ByteString
xs ByteString
y ByteString
ys
to ByteString
_ (Chunk ByteString
x' ByteString
xs) ByteString
_ (Chunk ByteString
y' ByteString
ys) = ByteString -> ByteString -> ByteString -> ByteString -> [a]
go ByteString
x' ByteString
xs ByteString
y' ByteString
ys
unzip :: [(Word8,Word8)] -> (ByteString,ByteString)
unzip :: [(Word8, Word8)] -> (ByteString, ByteString)
unzip [(Word8, Word8)]
ls = ([Word8] -> ByteString
pack (((Word8, Word8) -> Word8) -> [(Word8, Word8)] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
L.map (Word8, Word8) -> Word8
forall a b. (a, b) -> a
fst [(Word8, Word8)]
ls), [Word8] -> ByteString
pack (((Word8, Word8) -> Word8) -> [(Word8, Word8)] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
L.map (Word8, Word8) -> Word8
forall a b. (a, b) -> b
snd [(Word8, Word8)]
ls))
{-# INLINE unzip #-}
inits :: ByteString -> [ByteString]
inits :: ByteString -> [ByteString]
inits = (ByteString
Empty ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:) ([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
inits'
where inits' :: ByteString -> [ByteString]
inits' ByteString
Empty = []
inits' (Chunk ByteString
c ByteString
cs) = (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
L.map (ByteString -> ByteString -> ByteString
`Chunk` ByteString
Empty) ([ByteString] -> [ByteString]
forall a. [a] -> [a]
L.tail (ByteString -> [ByteString]
S.inits ByteString
c))
[ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
L.map (ByteString -> ByteString -> ByteString
Chunk ByteString
c) (ByteString -> [ByteString]
inits' ByteString
cs)
tails :: ByteString -> [ByteString]
tails :: ByteString -> [ByteString]
tails ByteString
Empty = ByteString
Empty ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []
tails cs :: ByteString
cs@(Chunk ByteString
c ByteString
cs')
| ByteString -> Int
S.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = ByteString
cs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
tails ByteString
cs'
| Bool
otherwise = ByteString
cs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
tails (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
S.unsafeTail ByteString
c) ByteString
cs')
copy :: ByteString -> ByteString
copy :: ByteString -> ByteString
copy ByteString
cs = (ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString -> ByteString)
-> (ByteString -> ByteString)
-> ByteString
-> ByteString
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
S.copy) ByteString
Empty ByteString
cs
hGetContentsN :: Int -> Handle -> IO ByteString
hGetContentsN :: Int -> Handle -> IO ByteString
hGetContentsN Int
k Handle
h = IO ByteString
lazyRead
where
lazyRead :: IO ByteString
lazyRead = IO ByteString -> IO ByteString
forall a. IO a -> IO a
unsafeInterleaveIO IO ByteString
loop
loop :: IO ByteString
loop = do
ByteString
c <- Handle -> Int -> IO ByteString
S.hGetSome Handle
h Int
k
if ByteString -> Bool
S.null ByteString
c
then Handle -> IO ()
hClose Handle
h IO () -> IO ByteString -> IO ByteString
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
else do ByteString
cs <- IO ByteString
lazyRead
ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
hGetN :: Int -> Handle -> Int -> IO ByteString
hGetN :: Int -> Handle -> Int -> IO ByteString
hGetN Int
k Handle
h Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Int -> IO ByteString
readChunks Int
n
where
readChunks :: Int -> IO ByteString
readChunks !Int
i = do
ByteString
c <- Handle -> Int -> IO ByteString
S.hGet Handle
h (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
i)
case ByteString -> Int
S.length ByteString
c of
Int
0 -> ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
Int
m -> do ByteString
cs <- Int -> IO ByteString
readChunks (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
m)
ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
hGetN Int
_ Handle
_ Int
0 = ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
hGetN Int
_ Handle
h Int
n = Handle -> String -> Int -> IO ByteString
forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
h String
"hGet" Int
n
hGetNonBlockingN :: Int -> Handle -> Int -> IO ByteString
hGetNonBlockingN :: Int -> Handle -> Int -> IO ByteString
hGetNonBlockingN Int
k Handle
h Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0= Int -> IO ByteString
readChunks Int
n
where
readChunks :: Int -> IO ByteString
readChunks !Int
i = do
ByteString
c <- Handle -> Int -> IO ByteString
S.hGetNonBlocking Handle
h (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
i)
case ByteString -> Int
S.length ByteString
c of
Int
0 -> ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
Int
m -> do ByteString
cs <- Int -> IO ByteString
readChunks (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
m)
ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ByteString -> ByteString
Chunk ByteString
c ByteString
cs)
hGetNonBlockingN Int
_ Handle
_ Int
0 = ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
hGetNonBlockingN Int
_ Handle
h Int
n = Handle -> String -> Int -> IO ByteString
forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
h String
"hGetNonBlocking" Int
n
illegalBufferSize :: Handle -> String -> Int -> IO a
illegalBufferSize :: forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
fn Int
sz =
IOError -> IO a
forall a. IOError -> IO a
ioError (IOErrorType -> String -> Maybe Handle -> Maybe String -> IOError
mkIOError IOErrorType
illegalOperationErrorType String
msg (Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
handle) Maybe String
forall a. Maybe a
Nothing)
where
msg :: String
msg = String
fn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": illegal ByteString size " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Int -> String -> String
forall a. Show a => Int -> a -> String -> String
showsPrec Int
9 Int
sz []
hGetContents :: Handle -> IO ByteString
hGetContents :: Handle -> IO ByteString
hGetContents = Int -> Handle -> IO ByteString
hGetContentsN Int
defaultChunkSize
hGet :: Handle -> Int -> IO ByteString
hGet :: Handle -> Int -> IO ByteString
hGet = Int -> Handle -> Int -> IO ByteString
hGetN Int
defaultChunkSize
hGetNonBlocking :: Handle -> Int -> IO ByteString
hGetNonBlocking :: Handle -> Int -> IO ByteString
hGetNonBlocking = Int -> Handle -> Int -> IO ByteString
hGetNonBlockingN Int
defaultChunkSize
readFile :: FilePath -> IO ByteString
readFile :: String -> IO ByteString
readFile String
f = String -> IOMode -> IO Handle
openBinaryFile String
f IOMode
ReadMode IO Handle -> (Handle -> IO ByteString) -> IO ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Handle -> IO ByteString
hGetContents
modifyFile :: IOMode -> FilePath -> ByteString -> IO ()
modifyFile :: IOMode -> String -> ByteString -> IO ()
modifyFile IOMode
mode String
f ByteString
txt = String -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile String
f IOMode
mode (Handle -> ByteString -> IO ()
`hPut` ByteString
txt)
writeFile :: FilePath -> ByteString -> IO ()
writeFile :: String -> ByteString -> IO ()
writeFile = IOMode -> String -> ByteString -> IO ()
modifyFile IOMode
WriteMode
appendFile :: FilePath -> ByteString -> IO ()
appendFile :: String -> ByteString -> IO ()
appendFile = IOMode -> String -> ByteString -> IO ()
modifyFile IOMode
AppendMode
getContents :: IO ByteString
getContents :: IO ByteString
getContents = Handle -> IO ByteString
hGetContents Handle
stdin
hPut :: Handle -> ByteString -> IO ()
hPut :: Handle -> ByteString -> IO ()
hPut Handle
h ByteString
cs = (ByteString -> IO () -> IO ()) -> IO () -> ByteString -> IO ()
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (\ByteString
c IO ()
rest -> Handle -> ByteString -> IO ()
S.hPut Handle
h ByteString
c IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
rest) (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) ByteString
cs
hPutNonBlocking :: Handle -> ByteString -> IO ByteString
hPutNonBlocking :: Handle -> ByteString -> IO ByteString
hPutNonBlocking Handle
_ ByteString
Empty = ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
hPutNonBlocking Handle
h bs :: ByteString
bs@(Chunk ByteString
c ByteString
cs) = do
ByteString
c' <- Handle -> ByteString -> IO ByteString
S.hPutNonBlocking Handle
h ByteString
c
case ByteString -> Int
S.length ByteString
c' of
Int
l' | Int
l' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
S.length ByteString
c -> Handle -> ByteString -> IO ByteString
hPutNonBlocking Handle
h ByteString
cs
Int
0 -> ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
Int
_ -> ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ByteString -> ByteString
Chunk ByteString
c' ByteString
cs)
hPutStr :: Handle -> ByteString -> IO ()
hPutStr :: Handle -> ByteString -> IO ()
hPutStr = Handle -> ByteString -> IO ()
hPut
putStr :: ByteString -> IO ()
putStr :: ByteString -> IO ()
putStr = Handle -> ByteString -> IO ()
hPut Handle
stdout
putStrLn :: ByteString -> IO ()
putStrLn :: ByteString -> IO ()
putStrLn ByteString
ps = Handle -> ByteString -> IO ()
hPut Handle
stdout ByteString
ps IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> ByteString -> IO ()
hPut Handle
stdout (Word8 -> ByteString
singleton Word8
0x0a)
{-# DEPRECATED putStrLn
"Use Data.ByteString.Lazy.Char8.putStrLn instead. (Functions that rely on ASCII encodings belong in Data.ByteString.Lazy.Char8)"
#-}
interact :: (ByteString -> ByteString) -> IO ()
interact :: (ByteString -> ByteString) -> IO ()
interact ByteString -> ByteString
transformer = ByteString -> IO ()
putStr (ByteString -> IO ())
-> (ByteString -> ByteString) -> ByteString -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
transformer (ByteString -> IO ()) -> IO ByteString -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO ByteString
getContents
errorEmptyList :: String -> a
errorEmptyList :: forall a. String -> a
errorEmptyList String
fun = String -> String -> a
forall a. String -> String -> a
moduleError String
fun String
"empty ByteString"
{-# NOINLINE errorEmptyList #-}
moduleError :: String -> String -> a
moduleError :: forall a. String -> String -> a
moduleError String
fun String
msg = String -> a
forall a. HasCallStack => String -> a
error (String
"Data.ByteString.Lazy." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fun String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char
':'Char -> String -> String
forall a. a -> [a] -> [a]
:Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
msg)
{-# NOINLINE moduleError #-}
revNonEmptyChunks :: [P.ByteString] -> ByteString
revNonEmptyChunks :: [ByteString] -> ByteString
revNonEmptyChunks [ByteString]
cs = (ByteString -> ByteString -> ByteString)
-> ByteString -> [ByteString] -> ByteString
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' ((ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip ByteString -> ByteString -> ByteString
Chunk) ByteString
Empty [ByteString]
cs
revChunks :: [P.ByteString] -> ByteString
revChunks :: [ByteString] -> ByteString
revChunks [ByteString]
cs = (ByteString -> ByteString -> ByteString)
-> ByteString -> [ByteString] -> ByteString
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' ((ByteString -> ByteString -> ByteString)
-> ByteString -> ByteString -> ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip ByteString -> ByteString -> ByteString
chunk) ByteString
Empty [ByteString]
cs
findIndexOrEnd :: (Word8 -> Bool) -> P.ByteString -> Int
findIndexOrEnd :: (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd Word8 -> Bool
k (S.PS ForeignPtr Word8
x Int
s Int
l) =
IO Int -> Int
forall a. IO a -> a
S.accursedUnutterablePerformIO (IO Int -> Int) -> IO Int -> Int
forall a b. (a -> b) -> a -> b
$
ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Int
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
x ((Ptr Word8 -> IO Int) -> IO Int)
-> (Ptr Word8 -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
f -> Ptr Word8 -> Int -> IO Int
go (Ptr Word8
f Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
s) Int
0
where
go :: Ptr Word8 -> Int -> IO Int
go !Ptr Word8
ptr !Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
l
| Bool
otherwise = do Word8
w <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
ptr
if Word8 -> Bool
k Word8
w
then Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
else Ptr Word8 -> Int -> IO Int
go (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
{-# INLINE findIndexOrEnd #-}