packages feed

MicroHs-0.17.4.0 / lib / Data / ByteString / Lazy / Internal.hs

module Data.ByteString.Lazy.Internal whereimport Control.DeepSeq.Classimport qualified Data.ByteString.Internal as Simport qualified Data.ByteString.Unsafe as Simport qualified Data.ByteString.Char8 as Cimport Data.Word.Word8import Data.List.NonEmpty_Typedata ByteString = Empty | Chunk !S.StrictByteString ByteStringtype LazyByteString = ByteStringinstance Eq ByteString where  (==) = eqinstance Ord ByteString where  compare = cmpinstance Semigroup ByteString where  (<>) = append  sconcat (b :| bs) = concat (b : bs)  stimes = timesinstance Monoid ByteString where  mempty = Empty  mconcat = concatinstance NFData ByteString where  rnf Empty        = ()  rnf (Chunk c cs) = rnf c `seq` rnf csinstance Show ByteString where  showsPrec p bs = showsPrec p (unpackChars bs)instance Read ByteString where  readsPrec p bs = [(packChars x, y) | (x, y) <- readsPrec p bs]instance IsString ByteString where  fromString = packChars-------------------------------------------------------------------------- Packing and unpacking from listspackBytes :: [Word8] -> ByteStringpackBytes cs0 =    packChunks 32 cs0  where    packChunks n cs = case splitAt n cs of      (bs, [])  -> chunk (S.pack bs) Empty      (bs, cs') -> Chunk (S.pack bs) (packChunks (min (n * 2) smallChunkSize) cs')packChars :: [Char] -> ByteStringpackChars cs0 = packChunks 32 cs0  where    packChunks n cs = case splitAt n cs of      (bs, [])  -> chunk (C.pack bs) Empty      (bs, cs') -> Chunk (C.pack bs) (packChunks (min (n * 2) smallChunkSize) cs')unpackBytes :: ByteString -> [Word8]unpackBytes Empty        = []unpackBytes (Chunk c cs) = S.unpack c ++ unpackBytes csunpackChars :: ByteString -> [Char]unpackChars Empty        = []unpackChars (Chunk c cs) = C.unpack c ++ unpackChars cschunk :: S.StrictByteString -> ByteString -> ByteStringchunk c cs  | S.null c = cs  | otherwise = Chunk c csfoldrChunks :: (S.StrictByteString -> a -> a) -> a -> ByteString -> afoldrChunks f z = go  where go Empty        = z        go (Chunk c cs) = f c (go cs)foldlChunks :: (a -> S.StrictByteString -> a) -> a -> ByteString -> afoldlChunks f = go  where go !a Empty        = a        go !a (Chunk c cs) = go (f a c) csdefaultChunkSize :: IntdefaultChunkSize = 32 * k - chunkOverhead   where k = 1024smallChunkSize :: IntsmallChunkSize = 4 * k - chunkOverhead   where k = 1024chunkOverhead :: IntchunkOverhead = 2 * _wordSizeeq :: ByteString -> ByteString -> Booleq Empty Empty = Trueeq Empty _     = Falseeq _     Empty = Falseeq (Chunk a as) (Chunk b bs) =  let    al = S.length a    bl = S.length b  in case compare al bl of    LT -> a == S.unsafeTake al b && eq as (Chunk (S.unsafeDrop al b) bs)    EQ -> a == b && eq as bs    GT -> S.unsafeTake bl a == b && eq (Chunk (S.unsafeDrop bl a) as) bscmp :: ByteString -> ByteString -> Orderingcmp Empty Empty = EQcmp Empty _     = LTcmp _     Empty = GTcmp (Chunk a as) (Chunk b bs) =  let    al = S.length a    bl = S.length b  in case compare al bl of    LT -> case compare a (S.unsafeTake al b) of            EQ     -> cmp as (Chunk (S.unsafeDrop al b) bs)            result -> result    EQ -> case compare a b of            EQ     -> cmp as bs            result -> result    GT -> case compare (S.unsafeTake bl a) b of            EQ     -> cmp (Chunk (S.unsafeDrop bl a) as) bs            result -> resultappend :: ByteString -> ByteString -> ByteStringappend xs ys = foldrChunks Chunk ys xsconcat :: [ByteString] -> ByteStringconcat = foldr append Emptytimes :: Integral a => a -> ByteString -> ByteStringtimes 0 _ = Emptytimes n lbs0  | n < 0 = error "stimes: non-negative multiplier expected"  | otherwise = case lbs0 of    Empty -> Empty    Chunk bs lbs -> Chunk bs (go lbs)  where    go Empty = times (n-1) lbs0    go (Chunk c cs) = Chunk c (go cs)fromStrict :: S.StrictByteString -> LazyByteStringfromStrict bs = chunk bs EmptytoStrict :: LazyByteString -> S.StrictByteStringtoStrict = foldlChunks S.append S.empty