1 {-# LANGUAGE CPP #-}
    2 {-# GHC_OPTIONS -fno-warn-orphans #-}
    3 
    4 -- #prune
    5 
    6 -- |
    7 -- Module      : Data.ByteString.Lazy.Char8
    8 -- Copyright   : (c) Don Stewart 2006
    9 -- License     : BSD-style
   10 --
   11 -- Maintainer  : dons@cse.unsw.edu.au
   12 -- Stability   : experimental
   13 -- Portability : non-portable (imports Data.ByteString.Lazy)
   14 --
   15 -- Manipulate /lazy/ 'ByteString's using 'Char' operations. All Chars will
   16 -- be truncated to 8 bits. It can be expected that these functions will
   17 -- run at identical speeds to their 'Data.Word.Word8' equivalents in
   18 -- "Data.ByteString.Lazy".
   19 --
   20 -- This module is intended to be imported @qualified@, to avoid name
   21 -- clashes with "Prelude" functions.  eg.
   22 --
   23 -- > import qualified Data.ByteString.Lazy.Char8 as C
   24 --
   25 
   26 module Data.ByteString.Lazy.Char8 (
   27 
   28         -- * The @ByteString@ type
   29         ByteString,             -- instances: Eq, Ord, Show, Read, Data, Typeable
   30 
   31         -- * Introducing and eliminating 'ByteString's
   32         empty,                  -- :: ByteString
   33         singleton,              -- :: Char   -> ByteString
   34         pack,                   -- :: String -> ByteString
   35         unpack,                 -- :: ByteString -> String
   36         fromChunks,             -- :: [Strict.ByteString] -> ByteString
   37         toChunks,               -- :: ByteString -> [Strict.ByteString]
   38 
   39         -- * Basic interface
   40         cons,                   -- :: Char -> ByteString -> ByteString
   41         cons',                  -- :: Char -> ByteString -> ByteString
   42         snoc,                   -- :: ByteString -> Char -> ByteString
   43         append,                 -- :: ByteString -> ByteString -> ByteString
   44         head,                   -- :: ByteString -> Char
   45         uncons,                 -- :: ByteString -> Maybe (Char, ByteString)
   46         last,                   -- :: ByteString -> Char
   47         tail,                   -- :: ByteString -> ByteString
   48         init,                   -- :: ByteString -> ByteString
   49         null,                   -- :: ByteString -> Bool
   50         length,                 -- :: ByteString -> Int64
   51 
   52         -- * Transforming ByteStrings
   53         map,                    -- :: (Char -> Char) -> ByteString -> ByteString
   54         reverse,                -- :: ByteString -> ByteString
   55         intersperse,            -- :: Char -> ByteString -> ByteString
   56         intercalate,            -- :: ByteString -> [ByteString] -> ByteString
   57         transpose,              -- :: [ByteString] -> [ByteString]
   58 
   59         -- * Reducing 'ByteString's (folds)
   60         foldl,                  -- :: (a -> Char -> a) -> a -> ByteString -> a
   61         foldl',                 -- :: (a -> Char -> a) -> a -> ByteString -> a
   62         foldl1,                 -- :: (Char -> Char -> Char) -> ByteString -> Char
   63         foldl1',                -- :: (Char -> Char -> Char) -> ByteString -> Char
   64         foldr,                  -- :: (Char -> a -> a) -> a -> ByteString -> a
   65         foldr1,                 -- :: (Char -> Char -> Char) -> ByteString -> Char
   66 
   67         -- ** Special folds
   68         concat,                 -- :: [ByteString] -> ByteString
   69         concatMap,              -- :: (Char -> ByteString) -> ByteString -> ByteString
   70         any,                    -- :: (Char -> Bool) -> ByteString -> Bool
   71         all,                    -- :: (Char -> Bool) -> ByteString -> Bool
   72         maximum,                -- :: ByteString -> Char
   73         minimum,                -- :: ByteString -> Char
   74 
   75         -- * Building ByteStrings
   76         -- ** Scans
   77         scanl,                  -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
   78 --      scanl1,                 -- :: (Char -> Char -> Char) -> ByteString -> ByteString
   79 --      scanr,                  -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
   80 --      scanr1,                 -- :: (Char -> Char -> Char) -> ByteString -> ByteString
   81 
   82         -- ** Accumulating maps
   83         mapAccumL,              -- :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
   84         mapAccumR,              -- :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
   85 
   86         -- ** Infinite ByteStrings
   87         repeat,                 -- :: Char -> ByteString
   88         replicate,              -- :: Int64 -> Char -> ByteString
   89         cycle,                  -- :: ByteString -> ByteString
   90         iterate,                -- :: (Char -> Char) -> Char -> ByteString
   91 
   92         -- ** Unfolding ByteStrings
   93         unfoldr,                -- :: (a -> Maybe (Char, a)) -> a -> ByteString
   94 
   95         -- * Substrings
   96 
   97         -- ** Breaking strings
   98         take,                   -- :: Int64 -> ByteString -> ByteString
   99         drop,                   -- :: Int64 -> ByteString -> ByteString
  100         splitAt,                -- :: Int64 -> ByteString -> (ByteString, ByteString)
  101         takeWhile,              -- :: (Char -> Bool) -> ByteString -> ByteString
  102         dropWhile,              -- :: (Char -> Bool) -> ByteString -> ByteString
  103         span,                   -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
  104         break,                  -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
  105         group,                  -- :: ByteString -> [ByteString]
  106         groupBy,                -- :: (Char -> Char -> Bool) -> ByteString -> [ByteString]
  107         inits,                  -- :: ByteString -> [ByteString]
  108         tails,                  -- :: ByteString -> [ByteString]
  109 
  110         -- ** Breaking into many substrings
  111         split,                  -- :: Char -> ByteString -> [ByteString]
  112         splitWith,              -- :: (Char -> Bool) -> ByteString -> [ByteString]
  113 
  114         -- ** Breaking into lines and words
  115         lines,                  -- :: ByteString -> [ByteString]
  116         words,                  -- :: ByteString -> [ByteString]
  117         unlines,                -- :: [ByteString] -> ByteString
  118         unwords,                -- :: ByteString -> [ByteString]
  119 
  120         -- * Predicates
  121         isPrefixOf,             -- :: ByteString -> ByteString -> Bool
  122 --      isSuffixOf,             -- :: ByteString -> ByteString -> Bool
  123 
  124         -- * Searching ByteStrings
  125 
  126         -- ** Searching by equality
  127         elem,                   -- :: Char -> ByteString -> Bool
  128         notElem,                -- :: Char -> ByteString -> Bool
  129 
  130         -- ** Searching with a predicate
  131         find,                   -- :: (Char -> Bool) -> ByteString -> Maybe Char
  132         filter,                 -- :: (Char -> Bool) -> ByteString -> ByteString
  133 --      partition               -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
  134 
  135         -- * Indexing ByteStrings
  136         index,                  -- :: ByteString -> Int64 -> Char
  137         elemIndex,              -- :: Char -> ByteString -> Maybe Int64
  138         elemIndices,            -- :: Char -> ByteString -> [Int64]
  139         findIndex,              -- :: (Char -> Bool) -> ByteString -> Maybe Int64
  140         findIndices,            -- :: (Char -> Bool) -> ByteString -> [Int64]
  141         count,                  -- :: Char -> ByteString -> Int64
  142 
  143         -- * Zipping and unzipping ByteStrings
  144         zip,                    -- :: ByteString -> ByteString -> [(Char,Char)]
  145         zipWith,                -- :: (Char -> Char -> c) -> ByteString -> ByteString -> [c]
  146 --      unzip,                  -- :: [(Char,Char)] -> (ByteString,ByteString)
  147 
  148         -- * Ordered ByteStrings
  149 --        sort,                   -- :: ByteString -> ByteString
  150 
  151         -- * Low level conversions
  152         -- ** Copying ByteStrings
  153         copy,                   -- :: ByteString -> ByteString
  154 
  155         -- * Reading from ByteStrings
  156         readInt,
  157         readInteger,
  158 
  159         -- * I\/O with 'ByteString's
  160 
  161         -- ** Standard input and output
  162         getContents,            -- :: IO ByteString
  163         putStr,                 -- :: ByteString -> IO ()
  164         putStrLn,               -- :: ByteString -> IO ()
  165         interact,               -- :: (ByteString -> ByteString) -> IO ()
  166 
  167         -- ** Files
  168         readFile,               -- :: FilePath -> IO ByteString
  169         writeFile,              -- :: FilePath -> ByteString -> IO ()
  170         appendFile,             -- :: FilePath -> ByteString -> IO ()
  171 
  172         -- ** I\/O with Handles
  173         hGetContents,           -- :: Handle -> IO ByteString
  174         hGet,                   -- :: Handle -> Int64 -> IO ByteString
  175         hGetNonBlocking,        -- :: Handle -> Int64 -> IO ByteString
  176         hPut,                   -- :: Handle -> ByteString -> IO ()
  177 
  178   ) where
  179 
  180 -- Functions transparently exported
  181 import Data.ByteString.Lazy 
  182         (ByteString, fromChunks, toChunks
  183         ,empty,null,length,tail,init,append,reverse,transpose,cycle
  184         ,concat,take,drop,splitAt,intercalate,isPrefixOf,group,inits,tails,copy
  185         ,hGetContents, hGet, hPut, getContents
  186         ,hGetNonBlocking
  187         ,putStr, putStrLn, interact)
  188 
  189 -- Functions we need to wrap.
  190 import qualified Data.ByteString.Lazy as L
  191 import qualified Data.ByteString as B
  192 import qualified Data.ByteString.Internal as B
  193 import qualified Data.ByteString.Unsafe as B
  194 import Data.ByteString.Lazy.Internal
  195 
  196 import Data.ByteString.Internal (w2c, c2w, isSpaceWord8)
  197 
  198 import Data.Int (Int64)
  199 import qualified Data.List as List
  200 
  201 import Prelude hiding           
  202         (reverse,head,tail,last,init,null,length,map,lines,foldl,foldr,unlines
  203         ,concat,any,take,drop,splitAt,takeWhile,dropWhile,span,break,elem,filter
  204         ,unwords,words,maximum,minimum,all,concatMap,scanl,scanl1,foldl1,foldr1
  205         ,readFile,writeFile,appendFile,replicate,getContents,getLine,putStr,putStrLn
  206         ,zip,zipWith,unzip,notElem,repeat,iterate,interact,cycle)
  207 
  208 import System.IO            (hClose,openFile,IOMode(..))
  209 #ifndef __NHC__
  210 import Control.Exception    (bracket)
  211 #else
  212 import IO                   (bracket)
  213 #endif
  214 
  215 #if __GLASGOW_HASKELL__ >= 608
  216 import Data.String
  217 #endif
  218 
  219 #define STRICT1(f) f a | a `seq` False = undefined
  220 #define STRICT2(f) f a b | a `seq` b `seq` False = undefined
  221 #define STRICT3(f) f a b c | a `seq` b `seq` c `seq` False = undefined
  222 #define STRICT4(f) f a b c d | a `seq` b `seq` c `seq` d `seq` False = undefined
  223 #define STRICT5(f) f a b c d e | a `seq` b `seq` c `seq` d `seq` e `seq` False = undefined
  224 
  225 ------------------------------------------------------------------------
  226 
  227 -- | /O(1)/ Convert a 'Char' into a 'ByteString'
  228 singleton :: Char -> ByteString
  229 singleton = L.singleton . c2w
  230 {-# INLINE singleton #-}
  231 
  232 #if __GLASGOW_HASKELL__ >= 608
  233 instance IsString ByteString where
  234     fromString = pack
  235     {-# INLINE fromString #-}
  236 #endif
  237 
  238 -- | /O(n)/ Convert a 'String' into a 'ByteString'. 
  239 pack :: [Char] -> ByteString
  240 pack = L.pack. List.map c2w
  241 
  242 -- | /O(n)/ Converts a 'ByteString' to a 'String'.
  243 unpack :: ByteString -> [Char]
  244 unpack = List.map w2c . L.unpack
  245 {-# INLINE unpack #-}
  246 
  247 -- | /O(1)/ 'cons' is analogous to '(:)' for lists.
  248 cons :: Char -> ByteString -> ByteString
  249 cons = L.cons . c2w
  250 {-# INLINE cons #-}
  251 
  252 -- | /O(1)/ Unlike 'cons', 'cons\'' is
  253 -- strict in the ByteString that we are consing onto. More precisely, it forces
  254 -- the head and the first chunk. It does this because, for space efficiency, it
  255 -- may coalesce the new byte onto the first \'chunk\' rather than starting a
  256 -- new \'chunk\'.
  257 --
  258 -- So that means you can't use a lazy recursive contruction like this:
  259 --
  260 -- > let xs = cons\' c xs in xs
  261 --
  262 -- You can however use 'cons', as well as 'repeat' and 'cycle', to build
  263 -- infinite lazy ByteStrings.
  264 --
  265 cons' :: Char -> ByteString -> ByteString
  266 cons' = L.cons' . c2w
  267 {-# INLINE cons' #-}
  268 
  269 -- | /O(n)/ Append a Char to the end of a 'ByteString'. Similar to
  270 -- 'cons', this function performs a memcpy.
  271 snoc :: ByteString -> Char -> ByteString
  272 snoc p = L.snoc p . c2w
  273 {-# INLINE snoc #-}
  274 
  275 -- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.
  276 head :: ByteString -> Char
  277 head = w2c . L.head
  278 {-# INLINE head #-}
  279 
  280 -- | /O(1)/ Extract the head and tail of a ByteString, returning Nothing
  281 -- if it is empty.
  282 uncons :: ByteString -> Maybe (Char, ByteString)
  283 uncons bs = case L.uncons bs of
  284                   Nothing -> Nothing
  285                   Just (w, bs') -> Just (w2c w, bs')
  286 {-# INLINE uncons #-}
  287 
  288 -- | /O(1)/ Extract the last element of a packed string, which must be non-empty.
  289 last :: ByteString -> Char
  290 last = w2c . L.last
  291 {-# INLINE last #-}
  292 
  293 -- | /O(n)/ 'map' @f xs@ is the ByteString obtained by applying @f@ to each element of @xs@
  294 map :: (Char -> Char) -> ByteString -> ByteString
  295 map f = L.map (c2w . f . w2c)
  296 {-# INLINE map #-}
  297 
  298 -- | /O(n)/ The 'intersperse' function takes a Char and a 'ByteString'
  299 -- and \`intersperses\' that Char between the elements of the
  300 -- 'ByteString'.  It is analogous to the intersperse function on Lists.
  301 intersperse :: Char -> ByteString -> ByteString
  302 intersperse = L.intersperse . c2w
  303 {-# INLINE intersperse #-}
  304 
  305 -- | 'foldl', applied to a binary operator, a starting value (typically
  306 -- the left-identity of the operator), and a ByteString, reduces the
  307 -- ByteString using the binary operator, from left to right.
  308 foldl :: (a -> Char -> a) -> a -> ByteString -> a
  309 foldl f = L.foldl (\a c -> f a (w2c c))
  310 {-# INLINE foldl #-}
  311 
  312 -- | 'foldl\'' is like foldl, but strict in the accumulator.
  313 foldl' :: (a -> Char -> a) -> a -> ByteString -> a
  314 foldl' f = L.foldl' (\a c -> f a (w2c c))
  315 {-# INLINE foldl' #-}
  316 
  317 -- | 'foldr', applied to a binary operator, a starting value
  318 -- (typically the right-identity of the operator), and a packed string,
  319 -- reduces the packed string using the binary operator, from right to left.
  320 foldr :: (Char -> a -> a) -> a -> ByteString -> a
  321 foldr f = L.foldr (\c a -> f (w2c c) a)
  322 {-# INLINE foldr #-}
  323 
  324 -- | 'foldl1' is a variant of 'foldl' that has no starting value
  325 -- argument, and thus must be applied to non-empty 'ByteStrings'.
  326 foldl1 :: (Char -> Char -> Char) -> ByteString -> Char
  327 foldl1 f ps = w2c (L.foldl1 (\x y -> c2w (f (w2c x) (w2c y))) ps)
  328 {-# INLINE foldl1 #-}
  329 
  330 -- | 'foldl1\'' is like 'foldl1', but strict in the accumulator.
  331 foldl1' :: (Char -> Char -> Char) -> ByteString -> Char
  332 foldl1' f ps = w2c (L.foldl1' (\x y -> c2w (f (w2c x) (w2c y))) ps)
  333 
  334 -- | 'foldr1' is a variant of 'foldr' that has no starting value argument,
  335 -- and thus must be applied to non-empty 'ByteString's
  336 foldr1 :: (Char -> Char -> Char) -> ByteString -> Char
  337 foldr1 f ps = w2c (L.foldr1 (\x y -> c2w (f (w2c x) (w2c y))) ps)
  338 {-# INLINE foldr1 #-}
  339 
  340 -- | Map a function over a 'ByteString' and concatenate the results
  341 concatMap :: (Char -> ByteString) -> ByteString -> ByteString
  342 concatMap f = L.concatMap (f . w2c)
  343 {-# INLINE concatMap #-}
  344 
  345 -- | Applied to a predicate and a ByteString, 'any' determines if
  346 -- any element of the 'ByteString' satisfies the predicate.
  347 any :: (Char -> Bool) -> ByteString -> Bool
  348 any f = L.any (f . w2c)
  349 {-# INLINE any #-}
  350 
  351 -- | Applied to a predicate and a 'ByteString', 'all' determines if
  352 -- all elements of the 'ByteString' satisfy the predicate.
  353 all :: (Char -> Bool) -> ByteString -> Bool
  354 all f = L.all (f . w2c)
  355 {-# INLINE all #-}
  356 
  357 -- | 'maximum' returns the maximum value from a 'ByteString'
  358 maximum :: ByteString -> Char
  359 maximum = w2c . L.maximum
  360 {-# INLINE maximum #-}
  361 
  362 -- | 'minimum' returns the minimum value from a 'ByteString'
  363 minimum :: ByteString -> Char
  364 minimum = w2c . L.minimum
  365 {-# INLINE minimum #-}
  366 
  367 -- ---------------------------------------------------------------------
  368 -- Building ByteStrings
  369 
  370 -- | 'scanl' is similar to 'foldl', but returns a list of successive
  371 -- reduced values from the left. This function will fuse.
  372 --
  373 -- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
  374 --
  375 -- Note that
  376 --
  377 -- > last (scanl f z xs) == foldl f z xs.
  378 scanl :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
  379 scanl f z = L.scanl (\a b -> c2w (f (w2c a) (w2c b))) (c2w z)
  380 
  381 -- | The 'mapAccumL' function behaves like a combination of 'map' and
  382 -- 'foldl'; it applies a function to each element of a ByteString,
  383 -- passing an accumulating parameter from left to right, and returning a
  384 -- final value of this accumulator together with the new ByteString.
  385 mapAccumL :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
  386 mapAccumL f = L.mapAccumL (\a w -> case f a (w2c w) of (a',c) -> (a', c2w c))
  387 
  388 -- | The 'mapAccumR' function behaves like a combination of 'map' and
  389 -- 'foldr'; it applies a function to each element of a ByteString,
  390 -- passing an accumulating parameter from right to left, and returning a
  391 -- final value of this accumulator together with the new ByteString.
  392 mapAccumR :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
  393 mapAccumR f = L.mapAccumR (\acc w -> case f acc (w2c w) of (acc', c) -> (acc', c2w c))
  394 
  395 ------------------------------------------------------------------------
  396 -- Generating and unfolding ByteStrings
  397 
  398 -- | @'iterate' f x@ returns an infinite ByteString of repeated applications
  399 -- of @f@ to @x@:
  400 --
  401 -- > iterate f x == [x, f x, f (f x), ...]
  402 --
  403 iterate :: (Char -> Char) -> Char -> ByteString
  404 iterate f = L.iterate (c2w . f . w2c) . c2w
  405 
  406 -- | @'repeat' x@ is an infinite ByteString, with @x@ the value of every
  407 -- element.
  408 --
  409 repeat :: Char -> ByteString
  410 repeat = L.repeat . c2w
  411 
  412 -- | /O(n)/ @'replicate' n x@ is a ByteString of length @n@ with @x@
  413 -- the value of every element.
  414 --
  415 replicate :: Int64 -> Char -> ByteString
  416 replicate w c = L.replicate w (c2w c)
  417 
  418 -- | /O(n)/ The 'unfoldr' function is analogous to the List \'unfoldr\'.
  419 -- 'unfoldr' builds a ByteString from a seed value.  The function takes
  420 -- the element and returns 'Nothing' if it is done producing the
  421 -- ByteString or returns 'Just' @(a,b)@, in which case, @a@ is a
  422 -- prepending to the ByteString and @b@ is used as the next element in a
  423 -- recursive call.
  424 unfoldr :: (a -> Maybe (Char, a)) -> a -> ByteString
  425 unfoldr f = L.unfoldr $ \a -> case f a of
  426                                     Nothing      -> Nothing
  427                                     Just (c, a') -> Just (c2w c, a')
  428 
  429 ------------------------------------------------------------------------
  430 
  431 -- | 'takeWhile', applied to a predicate @p@ and a ByteString @xs@,
  432 -- returns the longest prefix (possibly empty) of @xs@ of elements that
  433 -- satisfy @p@.
  434 takeWhile :: (Char -> Bool) -> ByteString -> ByteString
  435 takeWhile f = L.takeWhile (f . w2c)
  436 {-# INLINE takeWhile #-}
  437 
  438 -- | 'dropWhile' @p xs@ returns the suffix remaining after 'takeWhile' @p xs@.
  439 dropWhile :: (Char -> Bool) -> ByteString -> ByteString
  440 dropWhile f = L.dropWhile (f . w2c)
  441 {-# INLINE dropWhile #-}
  442 
  443 -- | 'break' @p@ is equivalent to @'span' ('not' . p)@.
  444 break :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
  445 break f = L.break (f . w2c)
  446 {-# INLINE break #-}
  447 
  448 -- | 'span' @p xs@ breaks the ByteString into two segments. It is
  449 -- equivalent to @('takeWhile' p xs, 'dropWhile' p xs)@
  450 span :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
  451 span f = L.span (f . w2c)
  452 {-# INLINE span #-}
  453 
  454 {-
  455 -- | 'breakChar' breaks its ByteString argument at the first occurence
  456 -- of the specified Char. It is more efficient than 'break' as it is
  457 -- implemented with @memchr(3)@. I.e.
  458 -- 
  459 -- > break (=='c') "abcd" == breakChar 'c' "abcd"
  460 --
  461 breakChar :: Char -> ByteString -> (ByteString, ByteString)
  462 breakChar = L.breakByte . c2w
  463 {-# INLINE breakChar #-}
  464 
  465 -- | 'spanChar' breaks its ByteString argument at the first
  466 -- occurence of a Char other than its argument. It is more efficient
  467 -- than 'span (==)'
  468 --
  469 -- > span  (=='c') "abcd" == spanByte 'c' "abcd"
  470 --
  471 spanChar :: Char -> ByteString -> (ByteString, ByteString)
  472 spanChar = L.spanByte . c2w
  473 {-# INLINE spanChar #-}
  474 -}
  475 
  476 --
  477 -- TODO, more rules for breakChar*
  478 --
  479 
  480 -- | /O(n)/ Break a 'ByteString' into pieces separated by the byte
  481 -- argument, consuming the delimiter. I.e.
  482 --
  483 -- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
  484 -- > split 'a'  "aXaXaXa"    == ["","X","X","X"]
  485 -- > split 'x'  "x"          == ["",""]
  486 -- 
  487 -- and
  488 --
  489 -- > intercalate [c] . split c == id
  490 -- > split == splitWith . (==)
  491 -- 
  492 -- As for all splitting functions in this library, this function does
  493 -- not copy the substrings, it just constructs new 'ByteStrings' that
  494 -- are slices of the original.
  495 --
  496 split :: Char -> ByteString -> [ByteString]
  497 split = L.split . c2w
  498 {-# INLINE split #-}
  499 
  500 -- | /O(n)/ Splits a 'ByteString' into components delimited by
  501 -- separators, where the predicate returns True for a separator element.
  502 -- The resulting components do not contain the separators.  Two adjacent
  503 -- separators result in an empty component in the output.  eg.
  504 --
  505 -- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
  506 --
  507 splitWith :: (Char -> Bool) -> ByteString -> [ByteString]
  508 splitWith f = L.splitWith (f . w2c)
  509 {-# INLINE splitWith #-}
  510 
  511 -- | The 'groupBy' function is the non-overloaded version of 'group'.
  512 groupBy :: (Char -> Char -> Bool) -> ByteString -> [ByteString]
  513 groupBy k = L.groupBy (\a b -> k (w2c a) (w2c b))
  514 
  515 -- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0.
  516 index :: ByteString -> Int64 -> Char
  517 index = (w2c .) . L.index
  518 {-# INLINE index #-}
  519 
  520 -- | /O(n)/ The 'elemIndex' function returns the index of the first
  521 -- element in the given 'ByteString' which is equal (by memchr) to the
  522 -- query element, or 'Nothing' if there is no such element.
  523 elemIndex :: Char -> ByteString -> Maybe Int64
  524 elemIndex = L.elemIndex . c2w
  525 {-# INLINE elemIndex #-}
  526 
  527 -- | /O(n)/ The 'elemIndices' function extends 'elemIndex', by returning
  528 -- the indices of all elements equal to the query element, in ascending order.
  529 elemIndices :: Char -> ByteString -> [Int64]
  530 elemIndices = L.elemIndices . c2w
  531 {-# INLINE elemIndices #-}
  532 
  533 -- | The 'findIndex' function takes a predicate and a 'ByteString' and
  534 -- returns the index of the first element in the ByteString satisfying the predicate.
  535 findIndex :: (Char -> Bool) -> ByteString -> Maybe Int64
  536 findIndex f = L.findIndex (f . w2c)
  537 {-# INLINE findIndex #-}
  538 
  539 -- | The 'findIndices' function extends 'findIndex', by returning the
  540 -- indices of all elements satisfying the predicate, in ascending order.
  541 findIndices :: (Char -> Bool) -> ByteString -> [Int64]
  542 findIndices f = L.findIndices (f . w2c)
  543 
  544 -- | count returns the number of times its argument appears in the ByteString
  545 --
  546 -- > count      == length . elemIndices
  547 -- > count '\n' == length . lines
  548 --
  549 -- But more efficiently than using length on the intermediate list.
  550 count :: Char -> ByteString -> Int64
  551 count c = L.count (c2w c)
  552 
  553 -- | /O(n)/ 'elem' is the 'ByteString' membership predicate. This
  554 -- implementation uses @memchr(3)@.
  555 elem :: Char -> ByteString -> Bool
  556 elem c = L.elem (c2w c)
  557 {-# INLINE elem #-}
  558 
  559 -- | /O(n)/ 'notElem' is the inverse of 'elem'
  560 notElem :: Char -> ByteString -> Bool
  561 notElem c = L.notElem (c2w c)
  562 {-# INLINE notElem #-}
  563 
  564 -- | /O(n)/ 'filter', applied to a predicate and a ByteString,
  565 -- returns a ByteString containing those characters that satisfy the
  566 -- predicate.
  567 filter :: (Char -> Bool) -> ByteString -> ByteString
  568 filter f = L.filter (f . w2c)
  569 {-# INLINE filter #-}
  570 
  571 {-
  572 -- | /O(n)/ and /O(n\/c) space/ A first order equivalent of /filter .
  573 -- (==)/, for the common case of filtering a single Char. It is more
  574 -- efficient to use /filterChar/ in this case.
  575 --
  576 -- > filterChar == filter . (==)
  577 --
  578 -- filterChar is around 10x faster, and uses much less space, than its
  579 -- filter equivalent
  580 --
  581 filterChar :: Char -> ByteString -> ByteString
  582 filterChar c ps = replicate (count c ps) c
  583 {-# INLINE filterChar #-}
  584 
  585 {-# RULES
  586   "ByteString specialise filter (== x)" forall x.
  587       filter ((==) x) = filterChar x
  588   #-}
  589 
  590 {-# RULES
  591   "ByteString specialise filter (== x)" forall x.
  592      filter (== x) = filterChar x
  593   #-}
  594 -}
  595 
  596 -- | /O(n)/ The 'find' function takes a predicate and a ByteString,
  597 -- and returns the first element in matching the predicate, or 'Nothing'
  598 -- if there is no such element.
  599 find :: (Char -> Bool) -> ByteString -> Maybe Char
  600 find f ps = w2c `fmap` L.find (f . w2c) ps
  601 {-# INLINE find #-}
  602 
  603 {-
  604 -- | /O(n)/ A first order equivalent of /filter . (==)/, for the common
  605 -- case of filtering a single Char. It is more efficient to use
  606 -- filterChar in this case.
  607 --
  608 -- > filterChar == filter . (==)
  609 --
  610 -- filterChar is around 10x faster, and uses much less space, than its
  611 -- filter equivalent
  612 --
  613 filterChar :: Char -> ByteString -> ByteString
  614 filterChar c = L.filterByte (c2w c)
  615 {-# INLINE filterChar #-}
  616 
  617 -- | /O(n)/ A first order equivalent of /filter . (\/=)/, for the common
  618 -- case of filtering a single Char out of a list. It is more efficient
  619 -- to use /filterNotChar/ in this case.
  620 --
  621 -- > filterNotChar == filter . (/=)
  622 --
  623 -- filterNotChar is around 3x faster, and uses much less space, than its
  624 -- filter equivalent
  625 --
  626 filterNotChar :: Char -> ByteString -> ByteString
  627 filterNotChar c = L.filterNotByte (c2w c)
  628 {-# INLINE filterNotChar #-}
  629 -}
  630 
  631 -- | /O(n)/ 'zip' takes two ByteStrings and returns a list of
  632 -- corresponding pairs of Chars. If one input ByteString is short,
  633 -- excess elements of the longer ByteString are discarded. This is
  634 -- equivalent to a pair of 'unpack' operations, and so space
  635 -- usage may be large for multi-megabyte ByteStrings
  636 zip :: ByteString -> ByteString -> [(Char,Char)]
  637 zip ps qs
  638     | L.null ps || L.null qs = []
  639     | otherwise = (head ps, head qs) : zip (L.tail ps) (L.tail qs)
  640 
  641 -- | 'zipWith' generalises 'zip' by zipping with the function given as
  642 -- the first argument, instead of a tupling function.  For example,
  643 -- @'zipWith' (+)@ is applied to two ByteStrings to produce the list
  644 -- of corresponding sums.
  645 zipWith :: (Char -> Char -> a) -> ByteString -> ByteString -> [a]
  646 zipWith f = L.zipWith ((. w2c) . f . w2c)
  647 
  648 -- | 'lines' breaks a ByteString up into a list of ByteStrings at
  649 -- newline Chars. The resulting strings do not contain newlines.
  650 --
  651 -- As of bytestring 0.9.0.3, this function is stricter than its 
  652 -- list cousin.
  653 --
  654 lines :: ByteString -> [ByteString]
  655 lines Empty          = []
  656 lines (Chunk c0 cs0) = loop0 c0 cs0
  657     where
  658     -- this is a really performance sensitive function but the
  659     -- chunked representation makes the general case a bit expensive
  660     -- however assuming a large chunk size and normalish line lengths
  661     -- we will find line endings much more frequently than chunk
  662     -- endings so it makes sense to optimise for that common case.
  663     -- So we partition into two special cases depending on whether we
  664     -- are keeping back a list of chunks that will eventually be output
  665     -- once we get to the end of the current line.
  666 
  667     -- the common special case where we have no existing chunks of
  668     -- the current line
  669     loop0 :: B.ByteString -> ByteString -> [ByteString]
  670     loop0 c cs =
  671         case B.elemIndex (c2w '\n') c of
  672             Nothing -> case cs of
  673                            Empty  | B.null c  ->                 []
  674                                   | otherwise -> Chunk c Empty : []
  675                            (Chunk c' cs')
  676                                | B.null c  -> loop0 c'     cs'
  677                                | otherwise -> loop  c' [c] cs'
  678 
  679             Just n | n /= 0    -> Chunk (B.unsafeTake n c) Empty
  680                                 : loop0 (B.unsafeDrop (n+1) c) cs
  681                    | otherwise -> Empty
  682                                 : loop0 (B.unsafeTail c) cs
  683 
  684     -- the general case when we are building a list of chunks that are
  685     -- part of the same line
  686     loop :: B.ByteString -> [B.ByteString] -> ByteString -> [ByteString]
  687     loop c line cs =
  688         case B.elemIndex (c2w '\n') c of
  689             Nothing ->
  690                 case cs of
  691                     Empty -> let c' = revChunks (c : line)
  692                               in c' `seq` (c' : [])
  693 
  694                     (Chunk c' cs') -> loop c' (c : line) cs'
  695 
  696             Just n ->
  697                 let c' = revChunks (B.unsafeTake n c : line)
  698                  in c' `seq` (c' : loop0 (B.unsafeDrop (n+1) c) cs)
  699 
  700 {-
  701 
  702 This function is too strict!  Consider,
  703 
  704 > prop_lazy =
  705     (L.unpack . head . lazylines $ L.append (L.pack "a\nb\n") (error "failed"))
  706   ==
  707     "a"
  708 
  709 fails.  Here's a properly lazy version of 'lines' for lazy bytestrings
  710 
  711     lazylines           :: L.ByteString -> [L.ByteString]
  712     lazylines s
  713         | L.null s  = []
  714         | otherwise =
  715             let (l,s') = L.break ((==) '\n') s
  716             in l : if L.null s' then []
  717                                 else lazylines (L.tail s')
  718 
  719 we need a similarly lazy, but efficient version.
  720 
  721 -}
  722 
  723 
  724 -- | 'unlines' is an inverse operation to 'lines'.  It joins lines,
  725 -- after appending a terminating newline to each.
  726 unlines :: [ByteString] -> ByteString
  727 unlines [] = empty
  728 unlines ss = (concat $ List.intersperse nl ss) `append` nl -- half as much space
  729     where nl = singleton '\n'
  730 
  731 -- | 'words' breaks a ByteString up into a list of words, which
  732 -- were delimited by Chars representing white space. And
  733 --
  734 -- > tokens isSpace = words
  735 --
  736 words :: ByteString -> [ByteString]
  737 words = List.filter (not . L.null) . L.splitWith isSpaceWord8
  738 {-# INLINE words #-}
  739 
  740 -- | The 'unwords' function is analogous to the 'unlines' function, on words.
  741 unwords :: [ByteString] -> ByteString
  742 unwords = intercalate (singleton ' ')
  743 {-# INLINE unwords #-}
  744 
  745 -- | readInt reads an Int from the beginning of the ByteString.  If
  746 -- there is no integer at the beginning of the string, it returns
  747 -- Nothing, otherwise it just returns the int read, and the rest of the
  748 -- string.
  749 readInt :: ByteString -> Maybe (Int, ByteString)
  750 readInt Empty        = Nothing
  751 readInt (Chunk x xs) =
  752         case w2c (B.unsafeHead x) of
  753             '-' -> loop True  0 0 (B.unsafeTail x) xs
  754             '+' -> loop False 0 0 (B.unsafeTail x) xs
  755             _   -> loop False 0 0 x xs
  756 
  757     where loop :: Bool -> Int -> Int -> B.ByteString -> ByteString -> Maybe (Int, ByteString)
  758           STRICT5(loop)
  759           loop neg i n c cs
  760               | B.null c = case cs of
  761                              Empty          -> end  neg i n c  cs
  762                              (Chunk c' cs') -> loop neg i n c' cs'
  763               | otherwise =
  764                   case B.unsafeHead c of
  765                     w | w >= 0x30
  766                      && w <= 0x39 -> loop neg (i+1)
  767                                           (n * 10 + (fromIntegral w - 0x30))
  768                                           (B.unsafeTail c) cs
  769                       | otherwise -> end neg i n c cs
  770 
  771           end _   0 _ _  _   = Nothing
  772           end neg _ n c cs = let n' | neg       = negate n
  773                                     | otherwise = n
  774                                  c' = chunk c cs
  775                               in n' `seq` c' `seq` Just $! (n', c')
  776 
  777 
  778 -- | readInteger reads an Integer from the beginning of the ByteString.  If
  779 -- there is no integer at the beginning of the string, it returns Nothing,
  780 -- otherwise it just returns the int read, and the rest of the string.
  781 readInteger :: ByteString -> Maybe (Integer, ByteString)
  782 readInteger Empty = Nothing
  783 readInteger (Chunk c0 cs0) =
  784         case w2c (B.unsafeHead c0) of
  785             '-' -> first (B.unsafeTail c0) cs0 >>= \(n, cs') -> return (-n, cs')
  786             '+' -> first (B.unsafeTail c0) cs0
  787             _   -> first c0 cs0
  788 
  789     where first c cs
  790               | B.null c = case cs of
  791                   Empty          -> Nothing
  792                   (Chunk c' cs') -> first' c' cs'
  793               | otherwise = first' c cs
  794 
  795           first' c cs = case B.unsafeHead c of
  796               w | w >= 0x30 && w <= 0x39 -> Just $
  797                   loop 1 (fromIntegral w - 0x30) [] (B.unsafeTail c) cs
  798                 | otherwise              -> Nothing
  799 
  800           loop :: Int -> Int -> [Integer]
  801                -> B.ByteString -> ByteString -> (Integer, ByteString)
  802           STRICT5(loop)
  803           loop d acc ns c cs
  804               | B.null c = case cs of
  805                              Empty          -> combine d acc ns c cs
  806                              (Chunk c' cs') -> loop d acc ns c' cs'
  807               | otherwise =
  808                   case B.unsafeHead c of
  809                    w | w >= 0x30 && w <= 0x39 ->
  810                        if d < 9 then loop (d+1)
  811                                           (10*acc + (fromIntegral w - 0x30))
  812                                           ns (B.unsafeTail c) cs
  813                                 else loop 1 (fromIntegral w - 0x30)
  814                                           (fromIntegral acc : ns)
  815                                           (B.unsafeTail c) cs
  816                      | otherwise -> combine d acc ns c cs
  817 
  818           combine _ acc [] c cs = end (fromIntegral acc) c cs
  819           combine d acc ns c cs =
  820               end (10^d * combine1 1000000000 ns + fromIntegral acc) c cs
  821 
  822           combine1 _ [n] = n
  823           combine1 b ns  = combine1 (b*b) $ combine2 b ns
  824 
  825           combine2 b (n:m:ns) = let t = n+m*b in t `seq` (t : combine2 b ns)
  826           combine2 _ ns       = ns
  827 
  828           end n c cs = let c' = chunk c cs
  829                         in c' `seq` (n, c')
  830 
  831 -- | Read an entire file /lazily/ into a 'ByteString'. Use 'text mode'
  832 -- on Windows to interpret newlines
  833 readFile :: FilePath -> IO ByteString
  834 readFile f = openFile f ReadMode >>= hGetContents
  835 
  836 -- | Write a 'ByteString' to a file.
  837 writeFile :: FilePath -> ByteString -> IO ()
  838 writeFile f txt = bracket (openFile f WriteMode) hClose
  839     (\hdl -> hPut hdl txt)
  840 
  841 -- | Append a 'ByteString' to a file.
  842 appendFile :: FilePath -> ByteString -> IO ()
  843 appendFile f txt = bracket (openFile f AppendMode) hClose
  844     (\hdl -> hPut hdl txt)
  845 
  846 
  847 -- ---------------------------------------------------------------------
  848 -- Internal utilities
  849 
  850 -- reverse a list of possibly-empty chunks into a lazy ByteString
  851 revChunks :: [B.ByteString] -> ByteString
  852 revChunks cs = List.foldl' (flip chunk) Empty cs