1 {-# OPTIONS_GHC -cpp -optc-O1 -fno-warn-orphans #-}
3 -- -optc-O2 breaks with 4.0.4 gcc on debian
5 -- Module : Data.ByteString.Lazy.Char8
6 -- Copyright : (c) Don Stewart 2006
9 -- Maintainer : dons@cse.unsw.edu.au
10 -- Stability : experimental
11 -- Portability : portable (tested with GHC>=6.4.1 and Hugs 2005)
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 Word8 equivalents in
18 -- "Data.ByteString.Lazy".
20 -- This module is intended to be imported @qualified@, to avoid name
21 -- clashes with "Prelude" functions. eg.
23 -- > import qualified Data.ByteString.Lazy.Char8 as C
26 module Data.ByteString.Lazy.Char8 (
28 -- * The @ByteString@ type
29 ByteString(..), -- instances: Eq, Ord, Show, Read, Data, Typeable
31 -- * Introducing and eliminating 'ByteString's
32 empty, -- :: ByteString
33 singleton, -- :: Char -> ByteString
34 pack, -- :: String -> ByteString
35 unpack, -- :: ByteString -> String
38 cons, -- :: Char -> ByteString -> ByteString
39 snoc, -- :: ByteString -> Char -> ByteString
40 append, -- :: ByteString -> ByteString -> ByteString
41 head, -- :: ByteString -> Char
42 last, -- :: ByteString -> Char
43 tail, -- :: ByteString -> ByteString
44 init, -- :: ByteString -> ByteString
45 null, -- :: ByteString -> Bool
46 length, -- :: ByteString -> Int64
48 -- * Transformating ByteStrings
49 map, -- :: (Char -> Char) -> ByteString -> ByteString
50 reverse, -- :: ByteString -> ByteString
51 -- intersperse, -- :: Char -> ByteString -> ByteString
52 transpose, -- :: [ByteString] -> [ByteString]
54 -- * Reducing 'ByteString's (folds)
55 foldl, -- :: (a -> Char -> a) -> a -> ByteString -> a
56 foldl', -- :: (a -> Char -> a) -> a -> ByteString -> a
57 foldl1, -- :: (Char -> Char -> Char) -> ByteString -> Char
58 foldl1', -- :: (Char -> Char -> Char) -> ByteString -> Char
59 foldr, -- :: (Char -> a -> a) -> a -> ByteString -> a
60 foldr1, -- :: (Char -> Char -> Char) -> ByteString -> Char
63 concat, -- :: [ByteString] -> ByteString
64 concatMap, -- :: (Char -> ByteString) -> ByteString -> ByteString
65 any, -- :: (Char -> Bool) -> ByteString -> Bool
66 all, -- :: (Char -> Bool) -> ByteString -> Bool
67 maximum, -- :: ByteString -> Char
68 minimum, -- :: ByteString -> Char
70 -- * Building ByteStrings
72 scanl, -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
73 -- scanl1, -- :: (Char -> Char -> Char) -> ByteString -> ByteString
74 -- scanr, -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
75 -- scanr1, -- :: (Char -> Char -> Char) -> ByteString -> ByteString
77 -- ** Accumulating maps
78 mapAccumL, -- :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
79 mapIndexed, -- :: (Int64 -> Char -> Char) -> ByteString -> ByteString
81 -- ** Infinite ByteStrings
82 repeat, -- :: Char -> ByteString
83 replicate, -- :: Int64 -> Char -> ByteString
84 cycle, -- :: ByteString -> ByteString
85 iterate, -- :: (Char -> Char) -> Char -> ByteString
88 unfoldr, -- :: (a -> Maybe (Char, a)) -> a -> ByteString
92 -- ** Breaking strings
93 take, -- :: Int64 -> ByteString -> ByteString
94 drop, -- :: Int64 -> ByteString -> ByteString
95 splitAt, -- :: Int64 -> ByteString -> (ByteString, ByteString)
96 takeWhile, -- :: (Char -> Bool) -> ByteString -> ByteString
97 dropWhile, -- :: (Char -> Bool) -> ByteString -> ByteString
98 span, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
99 break, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
100 group, -- :: ByteString -> [ByteString]
101 groupBy, -- :: (Char -> Char -> Bool) -> ByteString -> [ByteString]
102 inits, -- :: ByteString -> [ByteString]
103 tails, -- :: ByteString -> [ByteString]
105 -- ** Breaking and dropping on specific Chars
106 breakChar, -- :: Char -> ByteString -> (ByteString, ByteString)
107 spanChar, -- :: Char -> ByteString -> (ByteString, ByteString)
109 -- ** Breaking into many substrings
110 split, -- :: Char -> ByteString -> [ByteString]
111 splitWith, -- :: (Char -> Bool) -> ByteString -> [ByteString]
112 tokens, -- :: (Char -> Bool) -> ByteString -> [ByteString]
114 -- ** Breaking into lines and words
115 lines, -- :: ByteString -> [ByteString]
116 words, -- :: ByteString -> [ByteString]
117 unlines, -- :: [ByteString] -> ByteString
118 unwords, -- :: ByteString -> [ByteString]
120 -- ** Joining strings
121 join, -- :: ByteString -> [ByteString] -> ByteString
122 joinWithChar, -- :: Char -> ByteString -> ByteString -> ByteString
125 isPrefixOf, -- :: ByteString -> ByteString -> Bool
126 -- isSuffixOf, -- :: ByteString -> ByteString -> Bool
128 -- * Searching ByteStrings
130 -- ** Searching by equality
131 elem, -- :: Char -> ByteString -> Bool
132 notElem, -- :: Char -> ByteString -> Bool
133 filterChar, -- :: Char -> ByteString -> ByteString
134 filterNotChar, -- :: Char -> ByteString -> ByteString
136 -- ** Searching with a predicate
137 find, -- :: (Char -> Bool) -> ByteString -> Maybe Char
138 filter, -- :: (Char -> Bool) -> ByteString -> ByteString
139 -- partition -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
141 -- * Indexing ByteStrings
142 index, -- :: ByteString -> Int64 -> Char
143 elemIndex, -- :: Char -> ByteString -> Maybe Int64
144 elemIndices, -- :: Char -> ByteString -> [Int64]
145 findIndex, -- :: (Char -> Bool) -> ByteString -> Maybe Int64
146 findIndices, -- :: (Char -> Bool) -> ByteString -> [Int64]
147 count, -- :: Char -> ByteString -> Int64
149 -- * Zipping and unzipping ByteStrings
150 zip, -- :: ByteString -> ByteString -> [(Char,Char)]
151 zipWith, -- :: (Char -> Char -> c) -> ByteString -> ByteString -> [c]
152 -- unzip, -- :: [(Char,Char)] -> (ByteString,ByteString)
154 -- * Ordered ByteStrings
155 -- sort, -- :: ByteString -> ByteString
157 -- * Reading from ByteStrings
160 -- * I\/O with 'ByteString's
162 -- ** Standard input and output
163 getContents, -- :: IO ByteString
164 putStr, -- :: ByteString -> IO ()
165 putStrLn, -- :: ByteString -> IO ()
166 interact, -- :: (ByteString -> ByteString) -> IO ()
169 readFile, -- :: FilePath -> IO ByteString
170 writeFile, -- :: FilePath -> ByteString -> IO ()
171 appendFile, -- :: FilePath -> ByteString -> IO ()
173 -- ** I\/O with Handles
174 hGetContents, -- :: Handle -> IO ByteString
175 hGetContentsN, -- :: Int -> Handle -> IO ByteString
176 hGet, -- :: Handle -> Int64 -> IO ByteString
177 hGetN, -- :: Int -> Handle -> Int64 -> IO ByteString
178 hPut, -- :: Handle -> ByteString -> IO ()
179 #if defined(__GLASGOW_HASKELL__)
180 hGetNonBlocking, -- :: Handle -> IO ByteString
181 hGetNonBlockingN, -- :: Int -> Handle -> IO ByteString
185 -- Functions transparently exported
186 import Data.ByteString.Lazy
188 ,empty,null,length,tail,init,append,reverse,transpose
189 ,concat,take,drop,splitAt,join,isPrefixOf,group,inits, tails
190 ,hGetContentsN, hGetN, hGetContents, hGet, hPut, getContents
191 #if defined(__GLASGOW_HASKELL__)
192 ,hGetNonBlocking, hGetNonBlockingN
195 ,readFile, writeFile, appendFile)
197 -- Functions we need to wrap.
198 import qualified Data.ByteString.Lazy as L
199 import qualified Data.ByteString as B
200 import qualified Data.ByteString.Base as B
201 import Data.ByteString.Base (w2c, c2w, isSpaceWord8)
203 import Data.Int (Int64)
204 import qualified Data.List as List (intersperse)
206 import qualified Prelude as P
207 import Prelude hiding
208 (reverse,head,tail,last,init,null,length,map,lines,foldl,foldr,unlines
209 ,concat,any,take,drop,splitAt,takeWhile,dropWhile,span,break,elem,filter
210 ,unwords,words,maximum,minimum,all,concatMap,scanl,scanl1,foldl1,foldr1
211 ,readFile,writeFile,appendFile,replicate,getContents,getLine,putStr,putStrLn
212 ,zip,zipWith,unzip,notElem,repeat,iterate)
214 #define STRICT1(f) f a | a `seq` False = undefined
215 #define STRICT2(f) f a b | a `seq` b `seq` False = undefined
216 #define STRICT3(f) f a b c | a `seq` b `seq` c `seq` False = undefined
217 #define STRICT4(f) f a b c d | a `seq` b `seq` c `seq` d `seq` False = undefined
218 #define STRICT5(f) f a b c d e | a `seq` b `seq` c `seq` d `seq` e `seq` False = undefined
220 ------------------------------------------------------------------------
222 -- | /O(1)/ Convert a 'Char' into a 'ByteString'
223 singleton :: Char -> ByteString
224 singleton = L.singleton . c2w
225 {-# INLINE singleton #-}
227 -- | /O(n)/ Convert a 'String' into a 'ByteString'.
228 pack :: [Char] -> ByteString
229 pack = L.packWith c2w
231 -- | /O(n)/ Converts a 'ByteString' to a 'String'.
232 unpack :: ByteString -> [Char]
233 unpack = L.unpackWith w2c
234 {-# INLINE unpack #-}
236 -- | /O(n)/ 'cons' is analogous to (:) for lists, but of different
237 -- complexity, as it requires a memcpy.
238 cons :: Char -> ByteString -> ByteString
242 -- | /O(n)/ Append a Char to the end of a 'ByteString'. Similar to
243 -- 'cons', this function performs a memcpy.
244 snoc :: ByteString -> Char -> ByteString
245 snoc p = L.snoc p . c2w
248 -- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.
249 head :: ByteString -> Char
253 -- | /O(1)/ Extract the last element of a packed string, which must be non-empty.
254 last :: ByteString -> Char
258 -- | /O(n)/ 'map' @f xs@ is the ByteString obtained by applying @f@ to each element of @xs@
259 map :: (Char -> Char) -> ByteString -> ByteString
260 map f = L.map (c2w . f . w2c)
263 -- | 'foldl', applied to a binary operator, a starting value (typically
264 -- the left-identity of the operator), and a ByteString, reduces the
265 -- ByteString using the binary operator, from left to right.
266 foldl :: (a -> Char -> a) -> a -> ByteString -> a
267 foldl f = L.foldl (\a c -> f a (w2c c))
270 -- | 'foldl\'' is like foldl, but strict in the accumulator.
271 foldl' :: (a -> Char -> a) -> a -> ByteString -> a
272 foldl' f = L.foldl' (\a c -> f a (w2c c))
273 {-# INLINE foldl' #-}
275 -- | 'foldr', applied to a binary operator, a starting value
276 -- (typically the right-identity of the operator), and a packed string,
277 -- reduces the packed string using the binary operator, from right to left.
278 foldr :: (Char -> a -> a) -> a -> ByteString -> a
279 foldr f = L.foldr (\c a -> f (w2c c) a)
282 -- | 'foldl1' is a variant of 'foldl' that has no starting value
283 -- argument, and thus must be applied to non-empty 'ByteStrings'.
284 foldl1 :: (Char -> Char -> Char) -> ByteString -> Char
285 foldl1 f ps = w2c (L.foldl1 (\x y -> c2w (f (w2c x) (w2c y))) ps)
286 {-# INLINE foldl1 #-}
288 -- | 'foldl1\'' is like 'foldl1', but strict in the accumulator.
289 foldl1' :: (Char -> Char -> Char) -> ByteString -> Char
290 foldl1' f ps = w2c (L.foldl1' (\x y -> c2w (f (w2c x) (w2c y))) ps)
292 -- | 'foldr1' is a variant of 'foldr' that has no starting value argument,
293 -- and thus must be applied to non-empty 'ByteString's
294 foldr1 :: (Char -> Char -> Char) -> ByteString -> Char
295 foldr1 f ps = w2c (L.foldr1 (\x y -> c2w (f (w2c x) (w2c y))) ps)
296 {-# INLINE foldr1 #-}
298 -- | Map a function over a 'ByteString' and concatenate the results
299 concatMap :: (Char -> ByteString) -> ByteString -> ByteString
300 concatMap f = L.concatMap (f . w2c)
301 {-# INLINE concatMap #-}
303 -- | Applied to a predicate and a ByteString, 'any' determines if
304 -- any element of the 'ByteString' satisfies the predicate.
305 any :: (Char -> Bool) -> ByteString -> Bool
306 any f = L.any (f . w2c)
309 -- | Applied to a predicate and a 'ByteString', 'all' determines if
310 -- all elements of the 'ByteString' satisfy the predicate.
311 all :: (Char -> Bool) -> ByteString -> Bool
312 all f = L.all (f . w2c)
315 -- | 'maximum' returns the maximum value from a 'ByteString'
316 maximum :: ByteString -> Char
317 maximum = w2c . L.maximum
318 {-# INLINE maximum #-}
320 -- | 'minimum' returns the minimum value from a 'ByteString'
321 minimum :: ByteString -> Char
322 minimum = w2c . L.minimum
323 {-# INLINE minimum #-}
325 -- ---------------------------------------------------------------------
326 -- Building ByteStrings
328 -- | 'scanl' is similar to 'foldl', but returns a list of successive
329 -- reduced values from the left. This function will fuse.
331 -- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
335 -- > last (scanl f z xs) == foldl f z xs.
336 scanl :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString
337 scanl f z = L.scanl (\a b -> c2w (f (w2c a) (w2c b))) (c2w z)
339 -- | The 'mapAccumL' function behaves like a combination of 'map' and
340 -- 'foldl'; it applies a function to each element of a ByteString,
341 -- passing an accumulating parameter from left to right, and returning a
342 -- final value of this accumulator together with the new ByteString.
343 mapAccumL :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString)
344 mapAccumL f = L.mapAccumL (\a w -> case f a (w2c w) of (a',c) -> (a', c2w c))
346 -- | /O(n)/ map Char functions, provided with the index at each position
347 mapIndexed :: (Int -> Char -> Char) -> ByteString -> ByteString
348 mapIndexed f = L.mapIndexed (\i w -> c2w (f i (w2c w)))
350 ------------------------------------------------------------------------
351 -- Generating and unfolding ByteStrings
353 -- | @'iterate' f x@ returns an infinite ByteString of repeated applications
356 -- > iterate f x == [x, f x, f (f x), ...]
358 iterate :: (Char -> Char) -> Char -> ByteString
359 iterate f = L.iterate (c2w . f . w2c) . c2w
361 -- | @'repeat' x@ is an infinite ByteString, with @x@ the value of every
364 repeat :: Char -> ByteString
365 repeat = L.repeat . c2w
367 -- | /O(n)/ @'replicate' n x@ is a ByteString of length @n@ with @x@
368 -- the value of every element.
370 replicate :: Int64 -> Char -> ByteString
371 replicate w c = L.replicate w (c2w c)
373 -- | /O(n)/ The 'unfoldr' function is analogous to the List \'unfoldr\'.
374 -- 'unfoldr' builds a ByteString from a seed value. The function takes
375 -- the element and returns 'Nothing' if it is done producing the
376 -- ByteString or returns 'Just' @(a,b)@, in which case, @a@ is a
377 -- prepending to the ByteString and @b@ is used as the next element in a
379 unfoldr :: (a -> Maybe (Char, a)) -> a -> ByteString
380 unfoldr f = L.unfoldr $ \a -> case f a of
382 Just (c, a') -> Just (c2w c, a')
384 ------------------------------------------------------------------------
386 -- | 'takeWhile', applied to a predicate @p@ and a ByteString @xs@,
387 -- returns the longest prefix (possibly empty) of @xs@ of elements that
389 takeWhile :: (Char -> Bool) -> ByteString -> ByteString
390 takeWhile f = L.takeWhile (f . w2c)
391 {-# INLINE takeWhile #-}
393 -- | 'dropWhile' @p xs@ returns the suffix remaining after 'takeWhile' @p xs@.
394 dropWhile :: (Char -> Bool) -> ByteString -> ByteString
395 dropWhile f = L.dropWhile (f . w2c)
396 {-# INLINE dropWhile #-}
398 -- | 'break' @p@ is equivalent to @'span' ('not' . p)@.
399 break :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
400 break f = L.break (f . w2c)
403 -- | 'span' @p xs@ breaks the ByteString into two segments. It is
404 -- equivalent to @('takeWhile' p xs, 'dropWhile' p xs)@
405 span :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
406 span f = L.span (f . w2c)
409 -- | 'breakChar' breaks its ByteString argument at the first occurence
410 -- of the specified Char. It is more efficient than 'break' as it is
411 -- implemented with @memchr(3)@. I.e.
413 -- > break (=='c') "abcd" == breakChar 'c' "abcd"
415 breakChar :: Char -> ByteString -> (ByteString, ByteString)
416 breakChar = L.breakByte . c2w
417 {-# INLINE breakChar #-}
419 -- | 'spanChar' breaks its ByteString argument at the first
420 -- occurence of a Char other than its argument. It is more efficient
423 -- > span (=='c') "abcd" == spanByte 'c' "abcd"
425 spanChar :: Char -> ByteString -> (ByteString, ByteString)
426 spanChar = L.spanByte . c2w
427 {-# INLINE spanChar #-}
429 -- | /O(n)/ Break a 'ByteString' into pieces separated by the byte
430 -- argument, consuming the delimiter. I.e.
432 -- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
433 -- > split 'a' "aXaXaXa" == ["","X","X","X"]
434 -- > split 'x' "x" == ["",""]
438 -- > join [c] . split c == id
439 -- > split == splitWith . (==)
441 -- As for all splitting functions in this library, this function does
442 -- not copy the substrings, it just constructs new 'ByteStrings' that
443 -- are slices of the original.
445 split :: Char -> ByteString -> [ByteString]
446 split = L.split . c2w
449 -- | /O(n)/ Splits a 'ByteString' into components delimited by
450 -- separators, where the predicate returns True for a separator element.
451 -- The resulting components do not contain the separators. Two adjacent
452 -- separators result in an empty component in the output. eg.
454 -- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
456 splitWith :: (Char -> Bool) -> ByteString -> [ByteString]
457 splitWith f = L.splitWith (f . w2c)
458 {-# INLINE splitWith #-}
460 -- | Like 'splitWith', except that sequences of adjacent separators are
461 -- treated as a single separator. eg.
463 -- > tokens (=='a') "aabbaca" == ["bb","c"]
465 tokens :: (Char -> Bool) -> ByteString -> [ByteString]
466 tokens f = L.tokens (f . w2c)
467 {-# INLINE tokens #-}
469 -- | The 'groupBy' function is the non-overloaded version of 'group'.
470 groupBy :: (Char -> Char -> Bool) -> ByteString -> [ByteString]
471 groupBy k = L.groupBy (\a b -> k (w2c a) (w2c b))
473 -- | /O(n)/ joinWithChar. An efficient way to join to two ByteStrings with a
474 -- char. Around 4 times faster than the generalised join.
476 joinWithChar :: Char -> ByteString -> ByteString -> ByteString
477 joinWithChar = L.joinWithByte . c2w
478 {-# INLINE joinWithChar #-}
480 -- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0.
481 index :: ByteString -> Int64 -> Char
482 index = (w2c .) . L.index
485 -- | /O(n)/ The 'elemIndex' function returns the index of the first
486 -- element in the given 'ByteString' which is equal (by memchr) to the
487 -- query element, or 'Nothing' if there is no such element.
488 elemIndex :: Char -> ByteString -> Maybe Int64
489 elemIndex = L.elemIndex . c2w
490 {-# INLINE elemIndex #-}
492 -- | /O(n)/ The 'elemIndices' function extends 'elemIndex', by returning
493 -- the indices of all elements equal to the query element, in ascending order.
494 elemIndices :: Char -> ByteString -> [Int64]
495 elemIndices = L.elemIndices . c2w
496 {-# INLINE elemIndices #-}
498 -- | The 'findIndex' function takes a predicate and a 'ByteString' and
499 -- returns the index of the first element in the ByteString satisfying the predicate.
500 findIndex :: (Char -> Bool) -> ByteString -> Maybe Int64
501 findIndex f = L.findIndex (f . w2c)
502 {-# INLINE findIndex #-}
504 -- | The 'findIndices' function extends 'findIndex', by returning the
505 -- indices of all elements satisfying the predicate, in ascending order.
506 findIndices :: (Char -> Bool) -> ByteString -> [Int64]
507 findIndices f = L.findIndices (f . w2c)
509 -- | count returns the number of times its argument appears in the ByteString
511 -- > count == length . elemIndices
512 -- > count '\n' == length . lines
514 -- But more efficiently than using length on the intermediate list.
515 count :: Char -> ByteString -> Int64
516 count c = L.count (c2w c)
518 -- | /O(n)/ 'elem' is the 'ByteString' membership predicate. This
519 -- implementation uses @memchr(3)@.
520 elem :: Char -> ByteString -> Bool
521 elem c = L.elem (c2w c)
524 -- | /O(n)/ 'notElem' is the inverse of 'elem'
525 notElem :: Char -> ByteString -> Bool
526 notElem c = L.notElem (c2w c)
527 {-# INLINE notElem #-}
529 -- | /O(n)/ 'filter', applied to a predicate and a ByteString,
530 -- returns a ByteString containing those characters that satisfy the
532 filter :: (Char -> Bool) -> ByteString -> ByteString
533 filter f = L.filter (f . w2c)
534 {-# INLINE filter #-}
536 -- | /O(n)/ The 'find' function takes a predicate and a ByteString,
537 -- and returns the first element in matching the predicate, or 'Nothing'
538 -- if there is no such element.
539 find :: (Char -> Bool) -> ByteString -> Maybe Char
540 find f ps = w2c `fmap` L.find (f . w2c) ps
543 -- | /O(n)/ A first order equivalent of /filter . (==)/, for the common
544 -- case of filtering a single Char. It is more efficient to use
545 -- filterChar in this case.
547 -- > filterChar == filter . (==)
549 -- filterChar is around 10x faster, and uses much less space, than its
552 filterChar :: Char -> ByteString -> ByteString
553 filterChar c = L.filterByte (c2w c)
554 {-# INLINE filterChar #-}
556 -- | /O(n)/ A first order equivalent of /filter . (\/=)/, for the common
557 -- case of filtering a single Char out of a list. It is more efficient
558 -- to use /filterNotChar/ in this case.
560 -- > filterNotChar == filter . (/=)
562 -- filterNotChar is around 3x faster, and uses much less space, than its
565 filterNotChar :: Char -> ByteString -> ByteString
566 filterNotChar c = L.filterNotByte (c2w c)
567 {-# INLINE filterNotChar #-}
569 -- | /O(n)/ 'zip' takes two ByteStrings and returns a list of
570 -- corresponding pairs of Chars. If one input ByteString is short,
571 -- excess elements of the longer ByteString are discarded. This is
572 -- equivalent to a pair of 'unpack' operations, and so space
573 -- usage may be large for multi-megabyte ByteStrings
574 zip :: ByteString -> ByteString -> [(Char,Char)]
576 | L.null ps || L.null qs = []
577 | otherwise = (head ps, head qs) : zip (L.tail ps) (L.tail qs)
579 -- | 'zipWith' generalises 'zip' by zipping with the function given as
580 -- the first argument, instead of a tupling function. For example,
581 -- @'zipWith' (+)@ is applied to two ByteStrings to produce the list
582 -- of corresponding sums.
583 zipWith :: (Char -> Char -> a) -> ByteString -> ByteString -> [a]
584 zipWith f = L.zipWith ((. w2c) . f . w2c)
586 -- | 'lines' breaks a ByteString up into a list of ByteStrings at
587 -- newline Chars. The resulting strings do not contain newlines.
589 lines :: ByteString -> [ByteString]
591 lines (LPS (x:xs)) = loop0 x xs
593 -- this is a really performance sensitive function but the
594 -- chunked representation makes the general case a bit expensive
595 -- however assuming a large chunk size and normalish line lengths
596 -- we will find line endings much more frequently than chunk
597 -- endings so it makes sense to optimise for that common case.
598 -- So we partition into two special cases depending on whether we
599 -- are keeping back a list of chunks that will eventually be output
600 -- once we get to the end of the current line.
602 -- the common special case where we have no existing chunks of
604 loop0 :: B.ByteString -> [B.ByteString] -> [ByteString]
607 case B.elemIndex (c2w '\n') ps of
608 Nothing -> case pss of
610 | otherwise -> LPS [ps] : []
612 | B.null ps -> loop0 ps' pss'
613 | otherwise -> loop ps' [ps] pss'
615 Just n | n /= 0 -> LPS [B.unsafeTake n ps]
616 : loop0 (B.unsafeDrop (n+1) ps) pss
617 | otherwise -> loop0 (B.unsafeTail ps) pss
619 -- the general case when we are building a list of chunks that are
620 -- part of the same line
621 loop :: B.ByteString -> [B.ByteString] -> [B.ByteString] -> [ByteString]
624 case B.elemIndex (c2w '\n') ps of
627 [] -> let ps' | B.null ps = P.reverse line
628 | otherwise = P.reverse (ps : line)
629 in ps' `seq` (LPS ps' : [])
632 | B.null ps -> loop ps' line pss'
633 | otherwise -> loop ps' (ps : line) pss'
636 let ps' | n == 0 = P.reverse line
637 | otherwise = P.reverse (B.unsafeTake n ps : line)
638 in ps' `seq` (LPS ps' : loop0 (B.unsafeDrop (n+1) ps) pss)
640 -- | 'unlines' is an inverse operation to 'lines'. It joins lines,
641 -- after appending a terminating newline to each.
642 unlines :: [ByteString] -> ByteString
644 unlines ss = (concat $ List.intersperse nl ss) `append` nl -- half as much space
645 where nl = singleton '\n'
647 -- | 'words' breaks a ByteString up into a list of words, which
648 -- were delimited by Chars representing white space. And
650 -- > tokens isSpace = words
652 words :: ByteString -> [ByteString]
653 words = L.tokens isSpaceWord8
656 -- | The 'unwords' function is analogous to the 'unlines' function, on words.
657 unwords :: [ByteString] -> ByteString
658 unwords = join (singleton ' ')
659 {-# INLINE unwords #-}
661 -- | readInt reads an Int from the beginning of the ByteString. If
662 -- there is no integer at the beginning of the string, it returns
663 -- Nothing, otherwise it just returns the int read, and the rest of the
665 readInt :: ByteString -> Maybe (Int, ByteString)
666 readInt (LPS []) = Nothing
667 readInt (LPS (x:xs)) =
668 case w2c (B.unsafeHead x) of
669 '-' -> loop True 0 0 (B.unsafeTail x) xs
670 '+' -> loop False 0 0 (B.unsafeTail x) xs
671 _ -> loop False 0 0 x xs
673 where loop :: Bool -> Int -> Int -> B.ByteString -> [B.ByteString] -> Maybe (Int, ByteString)
676 | B.null ps = case pss of
677 [] -> end neg i n ps pss
678 (ps':pss') -> loop neg i n ps' pss'
680 case B.unsafeHead ps of
682 && w <= 0x39 -> loop neg (i+1)
683 (n * 10 + (fromIntegral w - 0x30))
684 (B.unsafeTail ps) pss
685 | otherwise -> end neg i n ps pss
687 end _ 0 _ _ _ = Nothing
688 end neg _ n ps pss = let n' | neg = negate n
690 ps' | B.null ps = pss
692 in n' `seq` ps' `seq` Just $! (n', LPS ps')