papa-0.7.0: src/Papa/Bytestring/Data/ByteString/Lazy/Char8.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Redundant lambda" -}
-- Reason: a function whose byte string follows other arguments (@map f@,
-- @split c@) takes the byte string in a lambda, because GHC inlines a
-- function only once it is applied to the arguments on the left of its
-- definition: so a partial application, passed to a higher-order
-- function, is inlined too.
{- HLINT ignore "Avoid restricted function" -}
-- Reason: this module defines total head, last, tail, init, maximum and
-- minimum, whose names HLint restricts whatever module they come from; and
-- calls "Data.ByteString.Lazy"'s maximum and minimum only on a byte string that is
-- not empty, for their speed.
-- | Lazy byte strings as characters @\'\\0\'@ to @\'\\255\'@, with total
-- versions of the partial functions of "Data.ByteString.Lazy.Char8", no silent
-- truncation, and I/O that returns its errors. Import qualified:
--
-- > import qualified Papa.Bytestring.Data.ByteString.Lazy.Char8 as LazyChar8
--
-- "Data.ByteString.Lazy.Char8" stores a character as its code point modulo 256,
-- so that @pack "\\256"@ is @"\\NUL"@, and @elem \'\\256\' "\\NUL"@ is
-- 'Data.Bool.True'. Here:
--
-- * the functions that build a byte string from characters return 'Data.Maybe.Nothing'
-- for a character above @\'\\255\'@: 'pack', 'singleton', 'cons', 'cons'', 'snoc', 'repeat',
-- 'intersperse', 'replicate', 'map', 'unfoldr', 'unzip';
-- * the functions that look for a character do not find one above
-- @\'\\255\'@: 'elem', 'notElem', 'elemIndex', 'elemIndices',
-- 'elemIndexEnd', 'count', 'split';
-- * the folds 'foldl1', 'foldl1'', 'foldr1', 'foldr1'' fold characters, not
-- bytes, so that an intermediate character above @\'\\255\'@ is kept;
-- * the functions whose function argument builds characters into a byte
-- string are not exported: @mapAccumL@, @mapAccumR@, @scanl@, @scanl1@,
-- @scanr@, @scanr1@, @iterate@, @packZipWith@. Use
-- "Papa.Bytestring.Data.ByteString.Lazy"'s, on bytes.
--
-- The functions that throw are replaced by functions of the same names that
-- return 'Data.Maybe.Nothing' there instead: 'head', 'last', 'tail', 'init', 'foldl1',
-- 'foldl1'', 'foldr1', 'foldr1'', 'maximum', 'minimum', 'cycle' for an empty
-- byte string, and 'index' for an index out of range.
--
-- Its I/O actions that write, or read a given number of bytes, are replaced
-- by actions of the same names that return their
-- 'Control.Exception.IOException' ('Papa.Base.System.IO.Except.IOExcept').
-- Its lazy readers (@readFile@, @getContents@, @hGetContents@, @interact@)
-- are lazy I/O, which throws from pure code once the handle fails, and are
-- not exported: read a strict byte string with
-- "Papa.Bytestring.Data.ByteString.Char8" and convert it with
-- 'X.fromStrict'.
--
-- For text, use "Papa.Text.Data.Text" and UTF-8.
--
-- @since 0.7.0
module Papa.Bytestring.Data.ByteString.Lazy.Char8 (
module X,
head,
last,
tail,
init,
foldl1,
foldl1',
foldr1,
foldr1',
maximum,
minimum,
index,
cons',
repeat,
cycle,
pack,
singleton,
cons,
snoc,
intersperse,
replicate,
map,
unfoldr,
unzip,
elem,
notElem,
elemIndex,
elemIndices,
elemIndexEnd,
count,
split,
putStr,
putStrLn,
writeFile,
appendFile,
hGet,
hGetNonBlocking,
hPut,
hPutNonBlocking,
hPutStr,
hPutStrLn,
) where
import Control.Applicative (liftA2, (*>))
import Control.Monad (guard)
import Data.Bool (Bool (False), bool, not, (&&))
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as X hiding (
appendFile,
cons,
cons',
count,
cycle,
elem,
elemIndex,
elemIndexEnd,
elemIndices,
foldl1,
foldl1',
foldr1,
foldr1',
getContents,
hGet,
hGetContents,
hGetNonBlocking,
hPut,
hPutNonBlocking,
hPutStr,
hPutStrLn,
head,
index,
init,
interact,
intersperse,
iterate,
last,
map,
mapAccumL,
mapAccumR,
maximum,
minimum,
notElem,
pack,
packZipWith,
putStr,
putStrLn,
readFile,
repeat,
replicate,
scanl,
scanl1,
scanr,
scanr1,
singleton,
snoc,
split,
tail,
unfoldr,
unzip,
writeFile,
)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Char (Char)
import Data.Function (const, (.))
import Data.Functor (fmap, (<$))
import Data.Int (Int, Int64)
import qualified Data.List as List
import Data.Maybe (Maybe)
import Data.Tuple (fst, snd, uncurry)
import Papa.Base.System.IO.Except (IOExcept, exceptIO)
import Papa.Internal.Char8 (latin1, latin1s)
import System.IO (FilePath, Handle)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> :set -XOverloadedStrings
-- >>> import Papa
-- >>> import qualified Data.List as L
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Bytestring.Data.ByteString.Lazy.Char8 as LazyChar8
-- >>> import qualified Data.ByteString.Lazy.Char8 as C
-- >>> import qualified System.IO.Error as IO
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genChar = Gen.frequency [(9, Gen.latin1), (1, Gen.element "\256\1000\1114111")]
-- >>> let genString = Gen.string (Range.linear 0 20) genChar
-- >>> let genBytes = C.pack <$> Gen.string (Range.linear 0 20) Gen.latin1
-- | Total 'C.head': the first character. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.head "abc"
-- Just 'a'
--
-- >>> LazyChar8.head ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.head b === preview _head (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
head :: ByteString -> Maybe Char
head =
fmap fst . C.uncons
{-# INLINE head #-}
-- | Total 'C.last': the last character. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.last "abc"
-- Just 'c'
--
-- >>> LazyChar8.last ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.last b === preview _last (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
last :: ByteString -> Maybe Char
last =
fmap snd . C.unsnoc
{-# INLINE last #-}
-- | Total 'C.tail': all but the first character. 'Data.Maybe.Nothing' for the empty
-- byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.tail "abc"
-- Just "bc"
--
-- >>> LazyChar8.tail ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap LazyChar8.unpack (LazyChar8.tail b) === preview _tail (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
tail :: ByteString -> Maybe ByteString
tail =
fmap snd . C.uncons
{-# INLINE tail #-}
-- | Total 'C.init': all but the last character. 'Data.Maybe.Nothing' for the empty
-- byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.init "abc"
-- Just "ab"
--
-- >>> LazyChar8.init ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap LazyChar8.unpack (LazyChar8.init b) === preview _init (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
init :: ByteString -> Maybe ByteString
init =
fmap fst . C.unsnoc
{-# INLINE init #-}
-- | Total 'C.foldl1': a left fold of the characters, seeded with the first.
-- 'Data.Maybe.Nothing' for the empty byte string. Unlike "Data.ByteString.Char8"'s, an
-- intermediate character above @\'\\255\'@ is kept.
--
-- ==== __Examples__
--
-- >>> LazyChar8.foldl1 max "abca"
-- Just 'c'
--
-- >>> LazyChar8.foldl1 (\a c -> bool a '\300' (c == 'b')) "ab"
-- Just '\300'
--
-- >>> LazyChar8.foldl1 max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.foldl1 max b === maximumOf folded (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1 :: (Char -> Char -> Char) -> ByteString -> Maybe Char
foldl1 f =
fmap (uncurry (C.foldl f)) . C.uncons
{-# INLINE foldl1 #-}
-- | Total 'C.foldl1'': a strict left fold of the characters, seeded with the
-- first. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.foldl1' max "abca"
-- Just 'c'
--
-- >>> LazyChar8.foldl1' max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.foldl1' min b === LazyChar8.foldl1 min b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1' :: (Char -> Char -> Char) -> ByteString -> Maybe Char
foldl1' f =
fmap (uncurry (C.foldl' f)) . C.uncons
{-# INLINE foldl1' #-}
-- | Total 'C.foldr1': a right fold of the characters, seeded with the last.
-- 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.foldr1 max "abca"
-- Just 'c'
--
-- >>> LazyChar8.foldr1 max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.foldr1 min b === minimumOf folded (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1 :: (Char -> Char -> Char) -> ByteString -> Maybe Char
foldr1 f =
fmap (\(b, c) -> C.foldr f c b) . C.unsnoc
{-# INLINE foldr1 #-}
-- | Total 'C.foldr1'': a strict right fold of the characters, seeded with the
-- last. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.foldr1' max "abca"
-- Just 'c'
--
-- >>> LazyChar8.foldr1' max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.foldr1' min b === LazyChar8.foldr1 min b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1' :: (Char -> Char -> Char) -> ByteString -> Maybe Char
foldr1' f =
fmap (\(b, c) -> C.foldr' f c b) . C.unsnoc
{-# INLINE foldr1' #-}
-- | Total 'C.maximum': the greatest character. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.maximum "abca"
-- Just 'c'
--
-- >>> LazyChar8.maximum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.maximum b === maximumOf folded (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximum :: ByteString -> Maybe Char
maximum b =
C.maximum b <$ guard (not (B.null b))
{-# INLINE maximum #-}
-- | Total 'C.minimum': the least character. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.minimum "bcab"
-- Just 'a'
--
-- >>> LazyChar8.minimum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyChar8.minimum b === minimumOf folded (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimum :: ByteString -> Maybe Char
minimum b =
C.minimum b <$ guard (not (B.null b))
{-# INLINE minimum #-}
-- | Total 'C.index': the character at a position, counting from 0. 'Data.Maybe.Nothing'
-- for a negative index or one past the end.
--
-- ==== __Examples__
--
-- >>> LazyChar8.index "abc" 1
-- Just 'b'
--
-- >>> LazyChar8.index "abc" 3
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; i <- forAll (Gen.int (Range.linear (-3) 25)); LazyChar8.index b (intCast i) === (LazyChar8.unpack b !! i)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
index :: ByteString -> Int64 -> Maybe Char
index =
C.indexMaybe
{-# INLINE index #-}
-- | Checked 'C.cons'': a character before a byte string, strictly in the
-- first chunk. 'Data.Maybe.Nothing' for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.cons' 'a' "bc"
-- Just "abc"
--
-- >>> LazyChar8.cons' '\256' "bc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.cons' c b === LazyChar8.cons c b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
cons' :: Char -> ByteString -> Maybe ByteString
cons' c =
\b -> C.cons' c b <$ guard (latin1 c)
{-# INLINE cons' #-}
-- | Checked 'C.repeat': the infinite repetition of a character. 'Data.Maybe.Nothing'
-- for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.take 3 <$> LazyChar8.repeat 'a'
-- Just "aaa"
--
-- >>> LazyChar8.repeat '\256'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; fmap (LazyChar8.take 5) (LazyChar8.repeat c) === LazyChar8.replicate 5 c
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
repeat :: Char -> Maybe ByteString
repeat c =
C.repeat c <$ guard (latin1 c)
{-# INLINE repeat #-}
-- | Total 'C.cycle': the infinite repetition of a byte string. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyChar8.take 7 <$> LazyChar8.cycle "abc"
-- Just "abcabca"
--
-- >>> LazyChar8.cycle ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap (LazyChar8.take (LazyChar8.length b)) (LazyChar8.cycle b) === (b <$ guard (not (LazyChar8.null b)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
cycle :: ByteString -> Maybe ByteString
cycle b =
C.cycle b <$ guard (not (B.null b))
{-# INLINE cycle #-}
-- | Checked 'C.pack': the byte string of some characters. 'Data.Maybe.Nothing' if one is
-- above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.pack "caf\233"
-- Just "caf\233"
--
-- >>> LazyChar8.pack "\256"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; fmap LazyChar8.unpack (LazyChar8.pack s) === (s <$ guard (P.all (<= '\255') s))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
pack :: [Char] -> Maybe ByteString
pack s =
C.pack s <$ latin1s s
{-# INLINE pack #-}
-- | Checked 'C.singleton': the byte string of a character. 'Data.Maybe.Nothing' for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.singleton 'a'
-- Just "a"
--
-- >>> LazyChar8.singleton '\256'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; LazyChar8.singleton c === LazyChar8.pack [c]
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
singleton :: Char -> Maybe ByteString
singleton c =
C.singleton c <$ guard (latin1 c)
{-# INLINE singleton #-}
-- | Checked 'C.cons': a character before a byte string. 'Data.Maybe.Nothing' for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.cons 'a' "bc"
-- Just "abc"
--
-- >>> LazyChar8.cons '\256' "bc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.cons c b === fmap (<> b) (LazyChar8.singleton c)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
cons :: Char -> ByteString -> Maybe ByteString
cons c =
\b -> C.cons c b <$ guard (latin1 c)
{-# INLINE cons #-}
-- | Checked 'C.snoc': a character after a byte string. 'Data.Maybe.Nothing' for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.snoc "ab" 'c'
-- Just "abc"
--
-- >>> LazyChar8.snoc "ab" '\256'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.snoc b c === fmap (b <>) (LazyChar8.singleton c)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
snoc :: ByteString -> Char -> Maybe ByteString
snoc b c =
C.snoc b c <$ guard (latin1 c)
{-# INLINE snoc #-}
-- | Checked 'C.intersperse': a character between each two of a byte
-- string's. 'Data.Maybe.Nothing' for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.intersperse ',' "abc"
-- Just "a,b,c"
--
-- >>> LazyChar8.intersperse '\256' "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; fmap LazyChar8.unpack (LazyChar8.intersperse c b) === (P.intersperse c (LazyChar8.unpack b) <$ guard (c <= '\255'))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
intersperse :: Char -> ByteString -> Maybe ByteString
intersperse c =
\b -> C.intersperse c b <$ guard (latin1 c)
{-# INLINE intersperse #-}
-- | Checked 'C.replicate': a character repeated a number of times (none for
-- a number below 1). 'Data.Maybe.Nothing' for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.replicate 3 'a'
-- Just "aaa"
--
-- >>> LazyChar8.replicate 3 '\256'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int (Range.linear (-2) 10)); c <- forAll genChar; LazyChar8.replicate (intCast n) c === (LazyChar8.pack (P.replicate n c) <* guard (c <= '\255'))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
replicate :: Int64 -> Char -> Maybe ByteString
replicate n c =
C.replicate n c <$ guard (latin1 c)
{-# INLINE replicate #-}
-- | Checked 'C.map': a function of each character. 'Data.Maybe.Nothing' if it gives a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.map toUpper "abc"
-- Just "ABC"
--
-- >>> LazyChar8.map (P.const '\256') "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; c <- forAll genChar; LazyChar8.map (\x -> bool c 'x' (x == 'a')) b === LazyChar8.pack (fmap (\x -> bool c 'x' (x == 'a')) (LazyChar8.unpack b))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
map :: (Char -> Char) -> ByteString -> Maybe ByteString
map f =
\b -> C.map f b <$ guard (C.all (latin1 . f) b)
{-# INLINE map #-}
-- | Checked 'C.unfoldr': the byte string of the characters that a function
-- unfolds from a seed. 'Data.Maybe.Nothing' if one is above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.unfoldr P.uncons "abc"
-- Just "abc"
--
-- >>> LazyChar8.unfoldr P.uncons "a\256"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; LazyChar8.unfoldr P.uncons s === LazyChar8.pack s
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
unfoldr :: (a -> Maybe (Char, a)) -> a -> Maybe ByteString
unfoldr f =
pack . List.unfoldr f
{-# INLINE unfoldr #-}
-- | Checked 'C.unzip': the byte strings of the first and second characters
-- of some pairs. 'Data.Maybe.Nothing' if one is above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.unzip [('a', 'b'), ('c', 'd')]
-- Just ("ac","bd")
--
-- >>> LazyChar8.unzip [('a', '\256')]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; t <- forAll genString; LazyChar8.unzip (P.zip s t) === liftA2 (,) (LazyChar8.pack (fmap P.fst (P.zip s t))) (LazyChar8.pack (fmap P.snd (P.zip s t)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
unzip :: [(Char, Char)] -> Maybe (ByteString, ByteString)
unzip ps =
liftA2 (,) (pack (fmap fst ps)) (pack (fmap snd ps))
{-# INLINE unzip #-}
-- | Checked 'C.elem': whether a character is in a byte string; never for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.elem 'b' "abc"
-- True
--
-- >>> LazyChar8.elem '\256' "\NUL"
-- False
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.elem c b === P.elem c (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
elem :: Char -> ByteString -> Bool
elem c =
\b -> latin1 c && C.elem c b
{-# INLINE elem #-}
-- | Checked 'C.notElem': whether a character is not in a byte string; always
-- for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.notElem 'd' "abc"
-- True
--
-- >>> LazyChar8.notElem '\256' "\NUL"
-- True
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.notElem c b === P.notElem c (LazyChar8.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
notElem :: Char -> ByteString -> Bool
notElem c =
not . elem c
{-# INLINE notElem #-}
-- | Checked 'C.elemIndex': the position of the first occurrence of a
-- character; 'Data.Maybe.Nothing' for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.elemIndex 'b' "abcb"
-- Just 1
--
-- >>> LazyChar8.elemIndex '\256' "\NUL"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.elemIndex c b === fmap intCast (L.elemIndex c (LazyChar8.unpack b))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
elemIndex :: Char -> ByteString -> Maybe Int64
elemIndex c =
\b -> guard (latin1 c) *> C.elemIndex c b
{-# INLINE elemIndex #-}
-- | Checked 'C.elemIndices': the positions of the occurrences of a
-- character; none for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.elemIndices 'b' "abcb"
-- [1,3]
--
-- >>> LazyChar8.elemIndices '\256' "\NUL"
-- []
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.elemIndices c b === fmap intCast (L.elemIndices c (LazyChar8.unpack b))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
elemIndices :: Char -> ByteString -> [Int64]
elemIndices c =
\b -> bool [] (C.elemIndices c b) (latin1 c)
{-# INLINE elemIndices #-}
-- | Checked 'C.elemIndexEnd': the position of the last occurrence of a
-- character; 'Data.Maybe.Nothing' for a character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.elemIndexEnd 'b' "abcb"
-- Just 3
--
-- >>> LazyChar8.elemIndexEnd '\256' "\NUL"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.elemIndexEnd c b === fmap intCast (preview _last (L.elemIndices c (LazyChar8.unpack b)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
elemIndexEnd :: Char -> ByteString -> Maybe Int64
elemIndexEnd c =
\b -> guard (latin1 c) *> C.elemIndexEnd c b
{-# INLINE elemIndexEnd #-}
-- | Checked 'C.count': the number of occurrences of a character; 0 for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.count 'b' "abcb"
-- 2
--
-- >>> LazyChar8.count '\256' "\NUL"
-- 0
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.count c b === intCast (P.length (L.elemIndices c (LazyChar8.unpack b)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
count :: Char -> ByteString -> Int64
count c =
\b -> bool 0 (C.count c b) (latin1 c)
{-# INLINE count #-}
-- | Checked 'C.split': the pieces of a byte string between occurrences of a
-- character (none for the empty byte string); the whole byte string for a
-- character above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> LazyChar8.split ',' "a,b,,c"
-- ["a","b","","c"]
--
-- >>> LazyChar8.split '\256' "a\NULb"
-- ["a\NULb"]
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; b <- forAll genBytes; LazyChar8.split c b === bool (LazyChar8.splitWith (== c) b) [] (LazyChar8.null b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
split :: Char -> ByteString -> [ByteString]
split c =
\b -> bool (B.splitWith (const False) b) (C.split c b) (latin1 c)
{-# INLINE split #-}
-- | Write to 'System.IO.stdout'.
--
-- @since 0.7.0
putStr :: ByteString -> IOExcept ()
putStr =
exceptIO . C.putStr
{-# INLINE putStr #-}
-- | Write to 'System.IO.stdout', and a newline byte.
--
-- @since 0.7.0
putStrLn :: ByteString -> IOExcept ()
putStrLn =
exceptIO . C.putStrLn
{-# INLINE putStrLn #-}
-- | Write a file, replacing it.
--
-- @since 0.7.0
writeFile :: FilePath -> ByteString -> IOExcept ()
writeFile p =
exceptIO . C.writeFile p
{-# INLINE writeFile #-}
-- | Append to a file.
--
-- @since 0.7.0
appendFile :: FilePath -> ByteString -> IOExcept ()
appendFile p =
exceptIO . C.appendFile p
{-# INLINE appendFile #-}
-- | Read up to the given number of bytes from a handle, waiting for all of
-- them unless the end is reached. An error for a negative number.
--
-- @since 0.7.0
hGet :: Handle -> Int -> IOExcept ByteString
hGet h =
exceptIO . C.hGet h
{-# INLINE hGet #-}
-- | Read up to the given number of bytes from a handle, without waiting. An
-- error for a negative number.
--
-- @since 0.7.0
hGetNonBlocking :: Handle -> Int -> IOExcept ByteString
hGetNonBlocking h =
exceptIO . C.hGetNonBlocking h
{-# INLINE hGetNonBlocking #-}
-- | Write to a handle.
--
-- @since 0.7.0
hPut :: Handle -> ByteString -> IOExcept ()
hPut h =
exceptIO . C.hPut h
{-# INLINE hPut #-}
-- | Write to a handle without waiting, and return what was not written.
--
-- @since 0.7.0
hPutNonBlocking :: Handle -> ByteString -> IOExcept ByteString
hPutNonBlocking h =
exceptIO . C.hPutNonBlocking h
{-# INLINE hPutNonBlocking #-}
-- | Write to a handle. This is 'hPut'.
--
-- @since 0.7.0
hPutStr :: Handle -> ByteString -> IOExcept ()
hPutStr h =
exceptIO . C.hPutStr h
{-# INLINE hPutStr #-}
-- | Write to a handle, and a newline byte.
--
-- @since 0.7.0
hPutStrLn :: Handle -> ByteString -> IOExcept ()
hPutStrLn h =
exceptIO . C.hPutStrLn h
{-# INLINE hPutStrLn #-}
-- $teardown
-- The doctests of the other modules do not take string literals as text.
--
-- >>> :set -XNoOverloadedStrings