1 {-# LANGUAGE CPP #-} 2 {-# OPTIONS_GHC -XMagicHash -XUnboxedTuples #-} 3 4 -- #prune 5 6 -- | 7 -- Module : Data.ByteString.Char8 8 -- Copyright : (c) Don Stewart 2006-2008 9 -- License : BSD-style 10 -- 11 -- Maintainer : dons@cse.unsw.edu.au 12 -- Stability : experimental 13 -- Portability : portable 14 -- 15 -- Manipulate 'ByteString's using 'Char' operations. All Chars will be 16 -- truncated to 8 bits. It can be expected that these functions will run 17 -- at identical speeds to their 'Word8' equivalents in "Data.ByteString". 18 -- 19 -- More specifically these byte strings are taken to be in the 20 -- subset of Unicode covered by code points 0-255. This covers 21 -- Unicode Basic Latin, Latin-1 Supplement and C0+C1 Controls. 22 -- 23 -- See: 24 -- 25 -- * <http://www.unicode.org/charts/> 26 -- 27 -- * <http://www.unicode.org/charts/PDF/U0000.pdf> 28 -- 29 -- * <http://www.unicode.org/charts/PDF/U0080.pdf> 30 -- 31 -- This module is intended to be imported @qualified@, to avoid name 32 -- clashes with "Prelude" functions. eg. 33 -- 34 -- > import qualified Data.ByteString.Char8 as B 35 -- 36 -- The Char8 interface to bytestrings provides an instance of IsString 37 -- for the ByteString type, enabling you to use string literals, and 38 -- have them implicitly packed to ByteStrings. Use -XOverloadedStrings 39 -- to enable this. 40 -- 41 42 module Data.ByteString.Char8 ( 43 44 -- * The @ByteString@ type 45 ByteString, -- abstract, instances: Eq, Ord, Show, Read, Data, Typeable, Monoid 46 47 -- * Introducing and eliminating 'ByteString's 48 empty, -- :: ByteString 49 singleton, -- :: Char -> ByteString 50 pack, -- :: String -> ByteString 51 unpack, -- :: ByteString -> String 52 53 -- * Basic interface 54 cons, -- :: Char -> ByteString -> ByteString 55 snoc, -- :: ByteString -> Char -> ByteString 56 append, -- :: ByteString -> ByteString -> ByteString 57 head, -- :: ByteString -> Char 58 uncons, -- :: ByteString -> Maybe (Char, ByteString) 59 last, -- :: ByteString -> Char 60 tail, -- :: ByteString -> ByteString 61 init, -- :: ByteString -> ByteString 62 null, -- :: ByteString -> Bool 63 length, -- :: ByteString -> Int 64 65 -- * Transformating ByteStrings 66 map, -- :: (Char -> Char) -> ByteString -> ByteString 67 reverse, -- :: ByteString -> ByteString 68 intersperse, -- :: Char -> ByteString -> ByteString 69 intercalate, -- :: ByteString -> [ByteString] -> ByteString 70 transpose, -- :: [ByteString] -> [ByteString] 71 72 -- * Reducing 'ByteString's (folds) 73 foldl, -- :: (a -> Char -> a) -> a -> ByteString -> a 74 foldl', -- :: (a -> Char -> a) -> a -> ByteString -> a 75 foldl1, -- :: (Char -> Char -> Char) -> ByteString -> Char 76 foldl1', -- :: (Char -> Char -> Char) -> ByteString -> Char 77 78 foldr, -- :: (Char -> a -> a) -> a -> ByteString -> a 79 foldr', -- :: (Char -> a -> a) -> a -> ByteString -> a 80 foldr1, -- :: (Char -> Char -> Char) -> ByteString -> Char 81 foldr1', -- :: (Char -> Char -> Char) -> ByteString -> Char 82 83 -- ** Special folds 84 concat, -- :: [ByteString] -> ByteString 85 concatMap, -- :: (Char -> ByteString) -> ByteString -> ByteString 86 any, -- :: (Char -> Bool) -> ByteString -> Bool 87 all, -- :: (Char -> Bool) -> ByteString -> Bool 88 maximum, -- :: ByteString -> Char 89 minimum, -- :: ByteString -> Char 90 91 -- * Building ByteStrings 92 -- ** Scans 93 scanl, -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString 94 scanl1, -- :: (Char -> Char -> Char) -> ByteString -> ByteString 95 scanr, -- :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString 96 scanr1, -- :: (Char -> Char -> Char) -> ByteString -> ByteString 97 98 -- ** Accumulating maps 99 mapAccumL, -- :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString) 100 mapAccumR, -- :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString) 101 102 -- ** Generating and unfolding ByteStrings 103 replicate, -- :: Int -> Char -> ByteString 104 unfoldr, -- :: (a -> Maybe (Char, a)) -> a -> ByteString 105 unfoldrN, -- :: Int -> (a -> Maybe (Char, a)) -> a -> (ByteString, Maybe a) 106 107 -- * Substrings 108 109 -- ** Breaking strings 110 take, -- :: Int -> ByteString -> ByteString 111 drop, -- :: Int -> ByteString -> ByteString 112 splitAt, -- :: Int -> ByteString -> (ByteString, ByteString) 113 takeWhile, -- :: (Char -> Bool) -> ByteString -> ByteString 114 dropWhile, -- :: (Char -> Bool) -> ByteString -> ByteString 115 span, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 116 spanEnd, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 117 break, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 118 breakEnd, -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 119 group, -- :: ByteString -> [ByteString] 120 groupBy, -- :: (Char -> Char -> Bool) -> ByteString -> [ByteString] 121 inits, -- :: ByteString -> [ByteString] 122 tails, -- :: ByteString -> [ByteString] 123 124 -- ** Breaking into many substrings 125 split, -- :: Char -> ByteString -> [ByteString] 126 splitWith, -- :: (Char -> Bool) -> ByteString -> [ByteString] 127 128 -- ** Breaking into lines and words 129 lines, -- :: ByteString -> [ByteString] 130 words, -- :: ByteString -> [ByteString] 131 unlines, -- :: [ByteString] -> ByteString 132 unwords, -- :: ByteString -> [ByteString] 133 134 -- * Predicates 135 isPrefixOf, -- :: ByteString -> ByteString -> Bool 136 isSuffixOf, -- :: ByteString -> ByteString -> Bool 137 isInfixOf, -- :: ByteString -> ByteString -> Bool 138 139 -- ** Search for arbitrary substrings 140 breakSubstring, -- :: ByteString -> ByteString -> (ByteString,ByteString) 141 findSubstring, -- :: ByteString -> ByteString -> Maybe Int 142 findSubstrings, -- :: ByteString -> ByteString -> [Int] 143 144 -- * Searching ByteStrings 145 146 -- ** Searching by equality 147 elem, -- :: Char -> ByteString -> Bool 148 notElem, -- :: Char -> ByteString -> Bool 149 150 -- ** Searching with a predicate 151 find, -- :: (Char -> Bool) -> ByteString -> Maybe Char 152 filter, -- :: (Char -> Bool) -> ByteString -> ByteString 153 -- partition -- :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 154 155 -- * Indexing ByteStrings 156 index, -- :: ByteString -> Int -> Char 157 elemIndex, -- :: Char -> ByteString -> Maybe Int 158 elemIndices, -- :: Char -> ByteString -> [Int] 159 elemIndexEnd, -- :: Char -> ByteString -> Maybe Int 160 findIndex, -- :: (Char -> Bool) -> ByteString -> Maybe Int 161 findIndices, -- :: (Char -> Bool) -> ByteString -> [Int] 162 count, -- :: Char -> ByteString -> Int 163 164 -- * Zipping and unzipping ByteStrings 165 zip, -- :: ByteString -> ByteString -> [(Char,Char)] 166 zipWith, -- :: (Char -> Char -> c) -> ByteString -> ByteString -> [c] 167 unzip, -- :: [(Char,Char)] -> (ByteString,ByteString) 168 169 -- * Ordered ByteStrings 170 sort, -- :: ByteString -> ByteString 171 172 -- * Reading from ByteStrings 173 readInt, -- :: ByteString -> Maybe (Int, ByteString) 174 readInteger, -- :: ByteString -> Maybe (Integer, ByteString) 175 176 -- * Low level CString conversions 177 178 -- ** Copying ByteStrings 179 copy, -- :: ByteString -> ByteString 180 181 -- ** Packing CStrings and pointers 182 packCString, -- :: CString -> IO ByteString 183 packCStringLen, -- :: CStringLen -> IO ByteString 184 185 -- ** Using ByteStrings as CStrings 186 useAsCString, -- :: ByteString -> (CString -> IO a) -> IO a 187 useAsCStringLen, -- :: ByteString -> (CStringLen -> IO a) -> IO a 188 189 -- * I\/O with 'ByteString's 190 191 -- ** Standard input and output 192 getLine, -- :: IO ByteString 193 getContents, -- :: IO ByteString 194 putStr, -- :: ByteString -> IO () 195 putStrLn, -- :: ByteString -> IO () 196 interact, -- :: (ByteString -> ByteString) -> IO () 197 198 -- ** Files 199 readFile, -- :: FilePath -> IO ByteString 200 writeFile, -- :: FilePath -> ByteString -> IO () 201 appendFile, -- :: FilePath -> ByteString -> IO () 202 -- mmapFile, -- :: FilePath -> IO ByteString 203 204 -- ** I\/O with Handles 205 hGetLine, -- :: Handle -> IO ByteString 206 hGetContents, -- :: Handle -> IO ByteString 207 hGet, -- :: Handle -> Int -> IO ByteString 208 hGetNonBlocking, -- :: Handle -> Int -> IO ByteString 209 hPut, -- :: Handle -> ByteString -> IO () 210 hPutStr, -- :: Handle -> ByteString -> IO () 211 hPutStrLn, -- :: Handle -> ByteString -> IO () 212 213 ) where 214 215 import qualified Prelude as P 216 import Prelude hiding (reverse,head,tail,last,init,null 217 ,length,map,lines,foldl,foldr,unlines 218 ,concat,any,take,drop,splitAt,takeWhile 219 ,dropWhile,span,break,elem,filter,unwords 220 ,words,maximum,minimum,all,concatMap 221 ,scanl,scanl1,scanr,scanr1 222 ,appendFile,readFile,writeFile 223 ,foldl1,foldr1,replicate 224 ,getContents,getLine,putStr,putStrLn,interact 225 ,zip,zipWith,unzip,notElem) 226 227 import qualified Data.ByteString as B 228 import qualified Data.ByteString.Internal as B 229 import qualified Data.ByteString.Unsafe as B 230 231 -- Listy functions transparently exported 232 import Data.ByteString (empty,null,length,tail,init,append 233 ,inits,tails,reverse,transpose 234 ,concat,take,drop,splitAt,intercalate 235 ,sort,isPrefixOf,isSuffixOf,isInfixOf 236 ,findSubstring,findSubstrings,breakSubstring,copy,group 237 238 ,getLine, getContents, putStr, putStrLn, interact 239 ,hGetContents, hGet, hPut, hPutStr, hPutStrLn 240 ,hGetLine, hGetNonBlocking 241 ,packCString,packCStringLen 242 ,useAsCString,useAsCStringLen 243 ) 244 245 import Data.ByteString.Internal (ByteString(PS), c2w, w2c, isSpaceWord8 246 ,inlinePerformIO) 247 248 #if defined(__GLASGOW_HASKELL__) 249 import Data.ByteString.Unsafe (unsafePackAddress) -- for the rule 250 #endif 251 252 import Data.Char ( isSpace ) 253 import qualified Data.List as List (intersperse) 254 255 import System.IO (openFile,hClose,hFileSize,IOMode(..)) 256 #ifndef __NHC__ 257 import Control.Exception (bracket) 258 #else 259 import IO (bracket) 260 #endif 261 import Foreign 262 263 #if defined(__GLASGOW_HASKELL__) 264 import GHC.Base (Char(..),unpackCString#,ord#,int2Word#) 265 import GHC.IOBase (IO(..),stToIO) 266 import GHC.Prim (Addr#,writeWord8OffAddr#,plusAddr#) 267 import GHC.Ptr (Ptr(..)) 268 import GHC.ST (ST(..)) 269 #endif 270 271 #if __GLASGOW_HASKELL__ >= 608 272 import Data.String 273 #endif 274 275 #define STRICT1(f) f a | a `seq` False = undefined 276 #define STRICT2(f) f a b | a `seq` b `seq` False = undefined 277 #define STRICT3(f) f a b c | a `seq` b `seq` c `seq` False = undefined 278 #define STRICT4(f) f a b c d | a `seq` b `seq` c `seq` d `seq` False = undefined 279 280 ------------------------------------------------------------------------ 281 282 -- | /O(1)/ Convert a 'Char' into a 'ByteString' 283 singleton :: Char -> ByteString 284 singleton = B.singleton . c2w 285 {-# INLINE singleton #-} 286 287 #if __GLASGOW_HASKELL__ >= 608 288 instance IsString ByteString where 289 fromString = pack 290 {-# INLINE fromString #-} 291 #endif 292 293 -- | /O(n)/ Convert a 'String' into a 'ByteString' 294 -- 295 -- For applications with large numbers of string literals, pack can be a 296 -- bottleneck. 297 pack :: String -> ByteString 298 #if !defined(__GLASGOW_HASKELL__) 299 300 pack str = B.unsafeCreate (P.length str) $ \p -> go p str 301 where go _ [] = return () 302 go p (x:xs) = poke p (c2w x) >> go (p `plusPtr` 1) xs 303 304 #else /* hack away */ 305 306 pack str = B.unsafeCreate (P.length str) $ \(Ptr p) -> stToIO (go p str) 307 where 308 go :: Addr# -> [Char] -> ST a () 309 go _ [] = return () 310 go p (C# c:cs) = writeByte p (int2Word# (ord# c)) >> go (p `plusAddr#` 1#) cs 311 312 writeByte p c = ST $ \s# -> 313 case writeWord8OffAddr# p 0# c s# of s2# -> (# s2#, () #) 314 {-# INLINE writeByte #-} 315 {-# INLINE [1] pack #-} 316 317 {-# RULES 318 "ByteString pack/packAddress" forall s . 319 pack (unpackCString# s) = inlinePerformIO (B.unsafePackAddress s) 320 #-} 321 322 #endif 323 324 -- | /O(n)/ Converts a 'ByteString' to a 'String'. 325 unpack :: ByteString -> [Char] 326 unpack = P.map w2c . B.unpack 327 {-# INLINE unpack #-} 328 329 -- | /O(n)/ 'cons' is analogous to (:) for lists, but of different 330 -- complexity, as it requires a memcpy. 331 cons :: Char -> ByteString -> ByteString 332 cons = B.cons . c2w 333 {-# INLINE cons #-} 334 335 -- | /O(n)/ Append a Char to the end of a 'ByteString'. Similar to 336 -- 'cons', this function performs a memcpy. 337 snoc :: ByteString -> Char -> ByteString 338 snoc p = B.snoc p . c2w 339 {-# INLINE snoc #-} 340 341 -- | /O(1)/ Extract the head and tail of a ByteString, returning Nothing 342 -- if it is empty. 343 uncons :: ByteString -> Maybe (Char, ByteString) 344 uncons bs = case B.uncons bs of 345 Nothing -> Nothing 346 Just (w, bs') -> Just (w2c w, bs') 347 {-# INLINE uncons #-} 348 349 -- | /O(1)/ Extract the first element of a ByteString, which must be non-empty. 350 head :: ByteString -> Char 351 head = w2c . B.head 352 {-# INLINE head #-} 353 354 -- | /O(1)/ Extract the last element of a packed string, which must be non-empty. 355 last :: ByteString -> Char 356 last = w2c . B.last 357 {-# INLINE last #-} 358 359 -- | /O(n)/ 'map' @f xs@ is the ByteString obtained by applying @f@ to each element of @xs@ 360 map :: (Char -> Char) -> ByteString -> ByteString 361 map f = B.map (c2w . f . w2c) 362 {-# INLINE map #-} 363 364 -- | /O(n)/ The 'intersperse' function takes a Char and a 'ByteString' 365 -- and \`intersperses\' that Char between the elements of the 366 -- 'ByteString'. It is analogous to the intersperse function on Lists. 367 intersperse :: Char -> ByteString -> ByteString 368 intersperse = B.intersperse . c2w 369 {-# INLINE intersperse #-} 370 371 -- | 'foldl', applied to a binary operator, a starting value (typically 372 -- the left-identity of the operator), and a ByteString, reduces the 373 -- ByteString using the binary operator, from left to right. 374 foldl :: (a -> Char -> a) -> a -> ByteString -> a 375 foldl f = B.foldl (\a c -> f a (w2c c)) 376 {-# INLINE foldl #-} 377 378 -- | 'foldl\'' is like foldl, but strict in the accumulator. 379 foldl' :: (a -> Char -> a) -> a -> ByteString -> a 380 foldl' f = B.foldl' (\a c -> f a (w2c c)) 381 {-# INLINE foldl' #-} 382 383 -- | 'foldr', applied to a binary operator, a starting value 384 -- (typically the right-identity of the operator), and a packed string, 385 -- reduces the packed string using the binary operator, from right to left. 386 foldr :: (Char -> a -> a) -> a -> ByteString -> a 387 foldr f = B.foldr (\c a -> f (w2c c) a) 388 {-# INLINE foldr #-} 389 390 -- | 'foldr\'' is a strict variant of foldr 391 foldr' :: (Char -> a -> a) -> a -> ByteString -> a 392 foldr' f = B.foldr' (\c a -> f (w2c c) a) 393 {-# INLINE foldr' #-} 394 395 -- | 'foldl1' is a variant of 'foldl' that has no starting value 396 -- argument, and thus must be applied to non-empty 'ByteStrings'. 397 foldl1 :: (Char -> Char -> Char) -> ByteString -> Char 398 foldl1 f ps = w2c (B.foldl1 (\x y -> c2w (f (w2c x) (w2c y))) ps) 399 {-# INLINE foldl1 #-} 400 401 -- | A strict version of 'foldl1' 402 foldl1' :: (Char -> Char -> Char) -> ByteString -> Char 403 foldl1' f ps = w2c (B.foldl1' (\x y -> c2w (f (w2c x) (w2c y))) ps) 404 {-# INLINE foldl1' #-} 405 406 -- | 'foldr1' is a variant of 'foldr' that has no starting value argument, 407 -- and thus must be applied to non-empty 'ByteString's 408 foldr1 :: (Char -> Char -> Char) -> ByteString -> Char 409 foldr1 f ps = w2c (B.foldr1 (\x y -> c2w (f (w2c x) (w2c y))) ps) 410 {-# INLINE foldr1 #-} 411 412 -- | A strict variant of foldr1 413 foldr1' :: (Char -> Char -> Char) -> ByteString -> Char 414 foldr1' f ps = w2c (B.foldr1' (\x y -> c2w (f (w2c x) (w2c y))) ps) 415 {-# INLINE foldr1' #-} 416 417 -- | Map a function over a 'ByteString' and concatenate the results 418 concatMap :: (Char -> ByteString) -> ByteString -> ByteString 419 concatMap f = B.concatMap (f . w2c) 420 {-# INLINE concatMap #-} 421 422 -- | Applied to a predicate and a ByteString, 'any' determines if 423 -- any element of the 'ByteString' satisfies the predicate. 424 any :: (Char -> Bool) -> ByteString -> Bool 425 any f = B.any (f . w2c) 426 {-# INLINE any #-} 427 428 -- | Applied to a predicate and a 'ByteString', 'all' determines if 429 -- all elements of the 'ByteString' satisfy the predicate. 430 all :: (Char -> Bool) -> ByteString -> Bool 431 all f = B.all (f . w2c) 432 {-# INLINE all #-} 433 434 -- | 'maximum' returns the maximum value from a 'ByteString' 435 maximum :: ByteString -> Char 436 maximum = w2c . B.maximum 437 {-# INLINE maximum #-} 438 439 -- | 'minimum' returns the minimum value from a 'ByteString' 440 minimum :: ByteString -> Char 441 minimum = w2c . B.minimum 442 {-# INLINE minimum #-} 443 444 -- | The 'mapAccumL' function behaves like a combination of 'map' and 445 -- 'foldl'; it applies a function to each element of a ByteString, 446 -- passing an accumulating parameter from left to right, and returning a 447 -- final value of this accumulator together with the new list. 448 mapAccumL :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString) 449 mapAccumL f = B.mapAccumL (\acc w -> case f acc (w2c w) of (acc', c) -> (acc', c2w c)) 450 451 -- | The 'mapAccumR' function behaves like a combination of 'map' and 452 -- 'foldr'; it applies a function to each element of a ByteString, 453 -- passing an accumulating parameter from right to left, and returning a 454 -- final value of this accumulator together with the new ByteString. 455 mapAccumR :: (acc -> Char -> (acc, Char)) -> acc -> ByteString -> (acc, ByteString) 456 mapAccumR f = B.mapAccumR (\acc w -> case f acc (w2c w) of (acc', c) -> (acc', c2w c)) 457 458 -- | 'scanl' is similar to 'foldl', but returns a list of successive 459 -- reduced values from the left: 460 -- 461 -- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] 462 -- 463 -- Note that 464 -- 465 -- > last (scanl f z xs) == foldl f z xs. 466 scanl :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString 467 scanl f z = B.scanl (\a b -> c2w (f (w2c a) (w2c b))) (c2w z) 468 469 -- | 'scanl1' is a variant of 'scanl' that has no starting value argument: 470 -- 471 -- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...] 472 scanl1 :: (Char -> Char -> Char) -> ByteString -> ByteString 473 scanl1 f = B.scanl1 (\a b -> c2w (f (w2c a) (w2c b))) 474 475 -- | scanr is the right-to-left dual of scanl. 476 scanr :: (Char -> Char -> Char) -> Char -> ByteString -> ByteString 477 scanr f z = B.scanr (\a b -> c2w (f (w2c a) (w2c b))) (c2w z) 478 479 -- | 'scanr1' is a variant of 'scanr' that has no starting value argument. 480 scanr1 :: (Char -> Char -> Char) -> ByteString -> ByteString 481 scanr1 f = B.scanr1 (\a b -> c2w (f (w2c a) (w2c b))) 482 483 -- | /O(n)/ 'replicate' @n x@ is a ByteString of length @n@ with @x@ 484 -- the value of every element. The following holds: 485 -- 486 -- > replicate w c = unfoldr w (\u -> Just (u,u)) c 487 -- 488 -- This implemenation uses @memset(3)@ 489 replicate :: Int -> Char -> ByteString 490 replicate w = B.replicate w . c2w 491 {-# INLINE replicate #-} 492 493 -- | /O(n)/, where /n/ is the length of the result. The 'unfoldr' 494 -- function is analogous to the List \'unfoldr\'. 'unfoldr' builds a 495 -- ByteString from a seed value. The function takes the element and 496 -- returns 'Nothing' if it is done producing the ByteString or returns 497 -- 'Just' @(a,b)@, in which case, @a@ is the next character in the string, 498 -- and @b@ is the seed value for further production. 499 -- 500 -- Examples: 501 -- 502 -- > unfoldr (\x -> if x <= '9' then Just (x, succ x) else Nothing) '0' == "0123456789" 503 unfoldr :: (a -> Maybe (Char, a)) -> a -> ByteString 504 unfoldr f x0 = B.unfoldr (fmap k . f) x0 505 where k (i, j) = (c2w i, j) 506 507 -- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a ByteString from a seed 508 -- value. However, the length of the result is limited by the first 509 -- argument to 'unfoldrN'. This function is more efficient than 'unfoldr' 510 -- when the maximum length of the result is known. 511 -- 512 -- The following equation relates 'unfoldrN' and 'unfoldr': 513 -- 514 -- > unfoldrN n f s == take n (unfoldr f s) 515 unfoldrN :: Int -> (a -> Maybe (Char, a)) -> a -> (ByteString, Maybe a) 516 unfoldrN n f w = B.unfoldrN n ((k `fmap`) . f) w 517 where k (i,j) = (c2w i, j) 518 {-# INLINE unfoldrN #-} 519 520 -- | 'takeWhile', applied to a predicate @p@ and a ByteString @xs@, 521 -- returns the longest prefix (possibly empty) of @xs@ of elements that 522 -- satisfy @p@. 523 takeWhile :: (Char -> Bool) -> ByteString -> ByteString 524 takeWhile f = B.takeWhile (f . w2c) 525 {-# INLINE takeWhile #-} 526 527 -- | 'dropWhile' @p xs@ returns the suffix remaining after 'takeWhile' @p xs@. 528 dropWhile :: (Char -> Bool) -> ByteString -> ByteString 529 dropWhile f = B.dropWhile (f . w2c) 530 #if defined(__GLASGOW_HASKELL__) 531 {-# INLINE [1] dropWhile #-} 532 #endif 533 534 {-# RULES 535 "ByteString specialise dropWhile isSpace -> dropSpace" 536 dropWhile isSpace = dropSpace 537 #-} 538 539 -- | 'break' @p@ is equivalent to @'span' ('not' . p)@. 540 break :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 541 break f = B.break (f . w2c) 542 #if defined(__GLASGOW_HASKELL__) 543 {-# INLINE [1] break #-} 544 #endif 545 546 {-# RULES 547 "ByteString specialise break (x==)" forall x. 548 break ((==) x) = breakChar x 549 "ByteString specialise break (==x)" forall x. 550 break (==x) = breakChar x 551 #-} 552 553 -- INTERNAL: 554 555 -- | 'breakChar' breaks its ByteString argument at the first occurence 556 -- of the specified char. It is more efficient than 'break' as it is 557 -- implemented with @memchr(3)@. I.e. 558 -- 559 -- > break (=='c') "abcd" == breakChar 'c' "abcd" 560 -- 561 breakChar :: Char -> ByteString -> (ByteString, ByteString) 562 breakChar c p = case elemIndex c p of 563 Nothing -> (p,empty) 564 Just n -> (B.unsafeTake n p, B.unsafeDrop n p) 565 {-# INLINE breakChar #-} 566 567 -- | 'span' @p xs@ breaks the ByteString into two segments. It is 568 -- equivalent to @('takeWhile' p xs, 'dropWhile' p xs)@ 569 span :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 570 span f = B.span (f . w2c) 571 {-# INLINE span #-} 572 573 -- | 'spanEnd' behaves like 'span' but from the end of the 'ByteString'. 574 -- We have 575 -- 576 -- > spanEnd (not.isSpace) "x y z" == ("x y ","z") 577 -- 578 -- and 579 -- 580 -- > spanEnd (not . isSpace) ps 581 -- > == 582 -- > let (x,y) = span (not.isSpace) (reverse ps) in (reverse y, reverse x) 583 -- 584 spanEnd :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 585 spanEnd f = B.spanEnd (f . w2c) 586 {-# INLINE spanEnd #-} 587 588 -- | 'breakEnd' behaves like 'break' but from the end of the 'ByteString' 589 -- 590 -- breakEnd p == spanEnd (not.p) 591 breakEnd :: (Char -> Bool) -> ByteString -> (ByteString, ByteString) 592 breakEnd f = B.breakEnd (f . w2c) 593 {-# INLINE breakEnd #-} 594 595 {- 596 -- | 'breakChar' breaks its ByteString argument at the first occurence 597 -- of the specified Char. It is more efficient than 'break' as it is 598 -- implemented with @memchr(3)@. I.e. 599 -- 600 -- > break (=='c') "abcd" == breakChar 'c' "abcd" 601 -- 602 breakChar :: Char -> ByteString -> (ByteString, ByteString) 603 breakChar = B.breakByte . c2w 604 {-# INLINE breakChar #-} 605 606 -- | 'spanChar' breaks its ByteString argument at the first 607 -- occurence of a Char other than its argument. It is more efficient 608 -- than 'span (==)' 609 -- 610 -- > span (=='c') "abcd" == spanByte 'c' "abcd" 611 -- 612 spanChar :: Char -> ByteString -> (ByteString, ByteString) 613 spanChar = B.spanByte . c2w 614 {-# INLINE spanChar #-} 615 -} 616 617 -- | /O(n)/ Break a 'ByteString' into pieces separated by the byte 618 -- argument, consuming the delimiter. I.e. 619 -- 620 -- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"] 621 -- > split 'a' "aXaXaXa" == ["","X","X","X",""] 622 -- > split 'x' "x" == ["",""] 623 -- 624 -- and 625 -- 626 -- > intercalate [c] . split c == id 627 -- > split == splitWith . (==) 628 -- 629 -- As for all splitting functions in this library, this function does 630 -- not copy the substrings, it just constructs new 'ByteStrings' that 631 -- are slices of the original. 632 -- 633 split :: Char -> ByteString -> [ByteString] 634 split = B.split . c2w 635 {-# INLINE split #-} 636 637 -- | /O(n)/ Splits a 'ByteString' into components delimited by 638 -- separators, where the predicate returns True for a separator element. 639 -- The resulting components do not contain the separators. Two adjacent 640 -- separators result in an empty component in the output. eg. 641 -- 642 -- > splitWith (=='a') "aabbaca" == ["","","bb","c",""] 643 -- 644 splitWith :: (Char -> Bool) -> ByteString -> [ByteString] 645 splitWith f = B.splitWith (f . w2c) 646 {-# INLINE splitWith #-} 647 -- the inline makes a big difference here. 648 649 {- 650 -- | Like 'splitWith', except that sequences of adjacent separators are 651 -- treated as a single separator. eg. 652 -- 653 -- > tokens (=='a') "aabbaca" == ["bb","c"] 654 -- 655 tokens :: (Char -> Bool) -> ByteString -> [ByteString] 656 tokens f = B.tokens (f . w2c) 657 {-# INLINE tokens #-} 658 -} 659 660 -- | The 'groupBy' function is the non-overloaded version of 'group'. 661 groupBy :: (Char -> Char -> Bool) -> ByteString -> [ByteString] 662 groupBy k = B.groupBy (\a b -> k (w2c a) (w2c b)) 663 664 -- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0. 665 index :: ByteString -> Int -> Char 666 index = (w2c .) . B.index 667 {-# INLINE index #-} 668 669 -- | /O(n)/ The 'elemIndex' function returns the index of the first 670 -- element in the given 'ByteString' which is equal (by memchr) to the 671 -- query element, or 'Nothing' if there is no such element. 672 elemIndex :: Char -> ByteString -> Maybe Int 673 elemIndex = B.elemIndex . c2w 674 {-# INLINE elemIndex #-} 675 676 -- | /O(n)/ The 'elemIndexEnd' function returns the last index of the 677 -- element in the given 'ByteString' which is equal to the query 678 -- element, or 'Nothing' if there is no such element. The following 679 -- holds: 680 -- 681 -- > elemIndexEnd c xs == 682 -- > (-) (length xs - 1) `fmap` elemIndex c (reverse xs) 683 -- 684 elemIndexEnd :: Char -> ByteString -> Maybe Int 685 elemIndexEnd = B.elemIndexEnd . c2w 686 {-# INLINE elemIndexEnd #-} 687 688 -- | /O(n)/ The 'elemIndices' function extends 'elemIndex', by returning 689 -- the indices of all elements equal to the query element, in ascending order. 690 elemIndices :: Char -> ByteString -> [Int] 691 elemIndices = B.elemIndices . c2w 692 {-# INLINE elemIndices #-} 693 694 -- | The 'findIndex' function takes a predicate and a 'ByteString' and 695 -- returns the index of the first element in the ByteString satisfying the predicate. 696 findIndex :: (Char -> Bool) -> ByteString -> Maybe Int 697 findIndex f = B.findIndex (f . w2c) 698 {-# INLINE findIndex #-} 699 700 -- | The 'findIndices' function extends 'findIndex', by returning the 701 -- indices of all elements satisfying the predicate, in ascending order. 702 findIndices :: (Char -> Bool) -> ByteString -> [Int] 703 findIndices f = B.findIndices (f . w2c) 704 705 -- | count returns the number of times its argument appears in the ByteString 706 -- 707 -- > count = length . elemIndices 708 -- 709 -- Also 710 -- 711 -- > count '\n' == length . lines 712 -- 713 -- But more efficiently than using length on the intermediate list. 714 count :: Char -> ByteString -> Int 715 count c = B.count (c2w c) 716 717 -- | /O(n)/ 'elem' is the 'ByteString' membership predicate. This 718 -- implementation uses @memchr(3)@. 719 elem :: Char -> ByteString -> Bool 720 elem c = B.elem (c2w c) 721 {-# INLINE elem #-} 722 723 -- | /O(n)/ 'notElem' is the inverse of 'elem' 724 notElem :: Char -> ByteString -> Bool 725 notElem c = B.notElem (c2w c) 726 {-# INLINE notElem #-} 727 728 -- | /O(n)/ 'filter', applied to a predicate and a ByteString, 729 -- returns a ByteString containing those characters that satisfy the 730 -- predicate. 731 filter :: (Char -> Bool) -> ByteString -> ByteString 732 filter f = B.filter (f . w2c) 733 {-# INLINE filter #-} 734 735 {- 736 -- | /O(n)/ and /O(n\/c) space/ A first order equivalent of /filter . 737 -- (==)/, for the common case of filtering a single Char. It is more 738 -- efficient to use /filterChar/ in this case. 739 -- 740 -- > filterChar == filter . (==) 741 -- 742 -- filterChar is around 10x faster, and uses much less space, than its 743 -- filter equivalent 744 -- 745 filterChar :: Char -> ByteString -> ByteString 746 filterChar c ps = replicate (count c ps) c 747 {-# INLINE filterChar #-} 748 749 {-# RULES 750 "ByteString specialise filter (== x)" forall x. 751 filter ((==) x) = filterChar x 752 "ByteString specialise filter (== x)" forall x. 753 filter (== x) = filterChar x 754 #-} 755 -} 756 757 -- | /O(n)/ The 'find' function takes a predicate and a ByteString, 758 -- and returns the first element in matching the predicate, or 'Nothing' 759 -- if there is no such element. 760 find :: (Char -> Bool) -> ByteString -> Maybe Char 761 find f ps = w2c `fmap` B.find (f . w2c) ps 762 {-# INLINE find #-} 763 764 {- 765 -- | /O(n)/ A first order equivalent of /filter . (==)/, for the common 766 -- case of filtering a single Char. It is more efficient to use 767 -- filterChar in this case. 768 -- 769 -- > filterChar == filter . (==) 770 -- 771 -- filterChar is around 10x faster, and uses much less space, than its 772 -- filter equivalent 773 -- 774 filterChar :: Char -> ByteString -> ByteString 775 filterChar c = B.filterByte (c2w c) 776 {-# INLINE filterChar #-} 777 778 -- | /O(n)/ A first order equivalent of /filter . (\/=)/, for the common 779 -- case of filtering a single Char out of a list. It is more efficient 780 -- to use /filterNotChar/ in this case. 781 -- 782 -- > filterNotChar == filter . (/=) 783 -- 784 -- filterNotChar is around 3x faster, and uses much less space, than its 785 -- filter equivalent 786 -- 787 filterNotChar :: Char -> ByteString -> ByteString 788 filterNotChar c = B.filterNotByte (c2w c) 789 {-# INLINE filterNotChar #-} 790 -} 791 792 -- | /O(n)/ 'zip' takes two ByteStrings and returns a list of 793 -- corresponding pairs of Chars. If one input ByteString is short, 794 -- excess elements of the longer ByteString are discarded. This is 795 -- equivalent to a pair of 'unpack' operations, and so space 796 -- usage may be large for multi-megabyte ByteStrings 797 zip :: ByteString -> ByteString -> [(Char,Char)] 798 zip ps qs 799 | B.null ps || B.null qs = [] 800 | otherwise = (unsafeHead ps, unsafeHead qs) : zip (B.unsafeTail ps) (B.unsafeTail qs) 801 802 -- | 'zipWith' generalises 'zip' by zipping with the function given as 803 -- the first argument, instead of a tupling function. For example, 804 -- @'zipWith' (+)@ is applied to two ByteStrings to produce the list 805 -- of corresponding sums. 806 zipWith :: (Char -> Char -> a) -> ByteString -> ByteString -> [a] 807 zipWith f = B.zipWith ((. w2c) . f . w2c) 808 809 -- | 'unzip' transforms a list of pairs of Chars into a pair of 810 -- ByteStrings. Note that this performs two 'pack' operations. 811 unzip :: [(Char,Char)] -> (ByteString,ByteString) 812 unzip ls = (pack (P.map fst ls), pack (P.map snd ls)) 813 {-# INLINE unzip #-} 814 815 -- | A variety of 'head' for non-empty ByteStrings. 'unsafeHead' omits 816 -- the check for the empty case, which is good for performance, but 817 -- there is an obligation on the programmer to provide a proof that the 818 -- ByteString is non-empty. 819 unsafeHead :: ByteString -> Char 820 unsafeHead = w2c . B.unsafeHead 821 {-# INLINE unsafeHead #-} 822 823 -- --------------------------------------------------------------------- 824 -- Things that depend on the encoding 825 826 {-# RULES 827 "ByteString specialise break -> breakSpace" 828 break isSpace = breakSpace 829 #-} 830 831 -- | 'breakSpace' returns the pair of ByteStrings when the argument is 832 -- broken at the first whitespace byte. I.e. 833 -- 834 -- > break isSpace == breakSpace 835 -- 836 breakSpace :: ByteString -> (ByteString,ByteString) 837 breakSpace (PS x s l) = inlinePerformIO $ withForeignPtr x $ \p -> do 838 i <- firstspace (p `plusPtr` s) 0 l 839 return $! case () of {_ 840 | i == 0 -> (empty, PS x s l) 841 | i == l -> (PS x s l, empty) 842 | otherwise -> (PS x s i, PS x (s+i) (l-i)) 843 } 844 {-# INLINE breakSpace #-} 845 846 firstspace :: Ptr Word8 -> Int -> Int -> IO Int 847 STRICT3(firstspace) 848 firstspace ptr n m 849 | n >= m = return n 850 | otherwise = do w <- peekByteOff ptr n 851 if (not . isSpaceWord8) w then firstspace ptr (n+1) m else return n 852 853 -- | 'dropSpace' efficiently returns the 'ByteString' argument with 854 -- white space Chars removed from the front. It is more efficient than 855 -- calling dropWhile for removing whitespace. I.e. 856 -- 857 -- > dropWhile isSpace == dropSpace 858 -- 859 dropSpace :: ByteString -> ByteString 860 dropSpace (PS x s l) = inlinePerformIO $ withForeignPtr x $ \p -> do 861 i <- firstnonspace (p `plusPtr` s) 0 l 862 return $! if i == l then empty else PS x (s+i) (l-i) 863 {-# INLINE dropSpace #-} 864 865 firstnonspace :: Ptr Word8 -> Int -> Int -> IO Int 866 STRICT3(firstnonspace) 867 firstnonspace ptr n m 868 | n >= m = return n 869 | otherwise = do w <- peekElemOff ptr n 870 if isSpaceWord8 w then firstnonspace ptr (n+1) m else return n 871 872 {- 873 -- | 'dropSpaceEnd' efficiently returns the 'ByteString' argument with 874 -- white space removed from the end. I.e. 875 -- 876 -- > reverse . (dropWhile isSpace) . reverse == dropSpaceEnd 877 -- 878 -- but it is more efficient than using multiple reverses. 879 -- 880 dropSpaceEnd :: ByteString -> ByteString 881 dropSpaceEnd (PS x s l) = inlinePerformIO $ withForeignPtr x $ \p -> do 882 i <- lastnonspace (p `plusPtr` s) (l-1) 883 return $! if i == (-1) then empty else PS x s (i+1) 884 {-# INLINE dropSpaceEnd #-} 885 886 lastnonspace :: Ptr Word8 -> Int -> IO Int 887 STRICT2(lastnonspace) 888 lastnonspace ptr n 889 | n < 0 = return n 890 | otherwise = do w <- peekElemOff ptr n 891 if isSpaceWord8 w then lastnonspace ptr (n-1) else return n 892 -} 893 894 -- | 'lines' breaks a ByteString up into a list of ByteStrings at 895 -- newline Chars. The resulting strings do not contain newlines. 896 -- 897 lines :: ByteString -> [ByteString] 898 lines ps 899 | null ps = [] 900 | otherwise = case search ps of 901 Nothing -> [ps] 902 Just n -> take n ps : lines (drop (n+1) ps) 903 where search = elemIndex '\n' 904 {-# INLINE lines #-} 905 906 {- 907 -- Just as fast, but more complex. Should be much faster, I thought. 908 lines :: ByteString -> [ByteString] 909 lines (PS _ _ 0) = [] 910 lines (PS x s l) = inlinePerformIO $ withForeignPtr x $ \p -> do 911 let ptr = p `plusPtr` s 912 913 STRICT1(loop) 914 loop n = do 915 let q = memchr (ptr `plusPtr` n) 0x0a (fromIntegral (l-n)) 916 if q == nullPtr 917 then return [PS x (s+n) (l-n)] 918 else do let i = q `minusPtr` ptr 919 ls <- loop (i+1) 920 return $! PS x (s+n) (i-n) : ls 921 loop 0 922 -} 923 924 -- | 'unlines' is an inverse operation to 'lines'. It joins lines, 925 -- after appending a terminating newline to each. 926 unlines :: [ByteString] -> ByteString 927 unlines [] = empty 928 unlines ss = (concat $ List.intersperse nl ss) `append` nl -- half as much space 929 where nl = singleton '\n' 930 931 -- | 'words' breaks a ByteString up into a list of words, which 932 -- were delimited by Chars representing white space. 933 words :: ByteString -> [ByteString] 934 words = P.filter (not . B.null) . B.splitWith isSpaceWord8 935 {-# INLINE words #-} 936 937 -- | The 'unwords' function is analogous to the 'unlines' function, on words. 938 unwords :: [ByteString] -> ByteString 939 unwords = intercalate (singleton ' ') 940 {-# INLINE unwords #-} 941 942 -- --------------------------------------------------------------------- 943 -- Reading from ByteStrings 944 945 -- | readInt reads an Int from the beginning of the ByteString. If there is no 946 -- integer at the beginning of the string, it returns Nothing, otherwise 947 -- it just returns the int read, and the rest of the string. 948 readInt :: ByteString -> Maybe (Int, ByteString) 949 readInt as 950 | null as = Nothing 951 | otherwise = 952 case unsafeHead as of 953 '-' -> loop True 0 0 (B.unsafeTail as) 954 '+' -> loop False 0 0 (B.unsafeTail as) 955 _ -> loop False 0 0 as 956 957 where loop :: Bool -> Int -> Int -> ByteString -> Maybe (Int, ByteString) 958 STRICT4(loop) 959 loop neg i n ps 960 | null ps = end neg i n ps 961 | otherwise = 962 case B.unsafeHead ps of 963 w | w >= 0x30 964 && w <= 0x39 -> loop neg (i+1) 965 (n * 10 + (fromIntegral w - 0x30)) 966 (B.unsafeTail ps) 967 | otherwise -> end neg i n ps 968 969 end _ 0 _ _ = Nothing 970 end True _ n ps = Just (negate n, ps) 971 end _ _ n ps = Just (n, ps) 972 973 -- | readInteger reads an Integer from the beginning of the ByteString. If 974 -- there is no integer at the beginning of the string, it returns Nothing, 975 -- otherwise it just returns the int read, and the rest of the string. 976 readInteger :: ByteString -> Maybe (Integer, ByteString) 977 readInteger as 978 | null as = Nothing 979 | otherwise = 980 case unsafeHead as of 981 '-' -> first (B.unsafeTail as) >>= \(n, bs) -> return (-n, bs) 982 '+' -> first (B.unsafeTail as) 983 _ -> first as 984 985 where first ps | null ps = Nothing 986 | otherwise = 987 case B.unsafeHead ps of 988 w | w >= 0x30 && w <= 0x39 -> Just $ 989 loop 1 (fromIntegral w - 0x30) [] (B.unsafeTail ps) 990 | otherwise -> Nothing 991 992 loop :: Int -> Int -> [Integer] 993 -> ByteString -> (Integer, ByteString) 994 STRICT4(loop) 995 loop d acc ns ps 996 | null ps = combine d acc ns empty 997 | otherwise = 998 case B.unsafeHead ps of 999 w | w >= 0x30 && w <= 0x39 -> 1000 if d == 9 then loop 1 (fromIntegral w - 0x30) 1001 (toInteger acc : ns) 1002 (B.unsafeTail ps) 1003 else loop (d+1) 1004 (10*acc + (fromIntegral w - 0x30)) 1005 ns (B.unsafeTail ps) 1006 | otherwise -> combine d acc ns ps 1007 1008 combine _ acc [] ps = (toInteger acc, ps) 1009 combine d acc ns ps = 1010 ((10^d * combine1 1000000000 ns + toInteger acc), ps) 1011 1012 combine1 _ [n] = n 1013 combine1 b ns = combine1 (b*b) $ combine2 b ns 1014 1015 combine2 b (n:m:ns) = let t = m*b + n in t `seq` (t : combine2 b ns) 1016 combine2 _ ns = ns 1017 1018 ------------------------------------------------------------------------ 1019 -- For non-binary text processing: 1020 1021 -- | Read an entire file strictly into a 'ByteString'. This is far more 1022 -- efficient than reading the characters into a 'String' and then using 1023 -- 'pack'. It also may be more efficient than opening the file and 1024 -- reading it using hGet. 1025 readFile :: FilePath -> IO ByteString 1026 readFile f = bracket (openFile f ReadMode) hClose 1027 (\h -> hFileSize h >>= hGet h . fromIntegral) 1028 1029 -- | Write a 'ByteString' to a file. 1030 writeFile :: FilePath -> ByteString -> IO () 1031 writeFile f txt = bracket (openFile f WriteMode) hClose 1032 (\h -> hPut h txt) 1033 1034 -- | Append a 'ByteString' to a file. 1035 appendFile :: FilePath -> ByteString -> IO () 1036 appendFile f txt = bracket (openFile f AppendMode) hClose 1037 (\h -> hPut h txt) 1038