packages feed

papa-0.7.0: src/Papa/Bytestring/Data/ByteString/Lazy.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

{- 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, with total versions of the partial functions of
-- "Data.ByteString.Lazy", and I/O that returns its errors. Import qualified:
--
-- > import qualified Papa.Bytestring.Data.ByteString.Lazy as LazyByteString
--
-- The functions of "Data.ByteString.Lazy" that throw are replaced by
-- functions of the same names that return 'Data.Maybe.Nothing' there instead:
--
-- * for an empty byte string: 'head', 'last', 'tail', 'init', 'foldl1',
--   'foldl1'', 'foldr1', 'foldr1'', 'maximum', 'minimum', 'cycle';
-- * for an index out of range: 'index' (which is 'X.indexMaybe').
--
-- 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'):
-- 'putStr', 'writeFile', 'appendFile', 'hPut', 'hPutNonBlocking', 'hPutStr',
-- 'hGet', 'hGetNonBlocking'.
--
-- 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" and convert it with 'X.fromStrict'.
--
-- 'X.repeat', 'X.iterate' and 'cycle' build an infinite byte string: a
-- function that reads all of it does not return.
--
-- @since 0.7.0
module Papa.Bytestring.Data.ByteString.Lazy (
  module X,
  head,
  last,
  tail,
  init,
  foldl1,
  foldl1',
  foldr1,
  foldr1',
  maximum,
  minimum,
  cycle,
  index,
  putStr,
  writeFile,
  appendFile,
  hPut,
  hPutNonBlocking,
  hPutStr,
  hGet,
  hGetNonBlocking,
) where

import Control.Monad (guard)
import Data.Bool (not)
import Data.ByteString.Lazy as X hiding (
  appendFile,
  cycle,
  foldl1,
  foldl1',
  foldr1,
  foldr1',
  getContents,
  hGet,
  hGetContents,
  hGetNonBlocking,
  hPut,
  hPutNonBlocking,
  hPutStr,
  head,
  index,
  init,
  interact,
  last,
  maximum,
  minimum,
  putStr,
  readFile,
  tail,
  writeFile,
 )
import qualified Data.ByteString.Lazy as L
import Data.Function ((.))
import Data.Functor (fmap, (<$))
import Data.Int (Int, Int64)
import Data.Maybe (Maybe)
import Data.Tuple (fst, snd, uncurry)
import Data.Word (Word8)
import Papa.Base.System.IO.Except (IOExcept, exceptIO)
import System.IO (FilePath, Handle)

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> :set -XOverloadedStrings
-- >>> import Papa
-- >>> import qualified Papa.Bytestring.Data.ByteString.Lazy as LazyByteString
-- >>> import qualified System.IO.Error as IO
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genBytes = LazyByteString.fromChunks <$> Gen.list (Range.linear 0 4) (Gen.bytes (Range.linear 0 6))

-- | Total 'L.head': the first byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.head "abc"
-- Just 97
--
-- >>> LazyByteString.head ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.head b === preview _head (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
head :: ByteString -> Maybe Word8
head =
  fmap fst . L.uncons
{-# INLINE head #-}

-- | Total 'L.last': the last byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.last "abc"
-- Just 99
--
-- >>> LazyByteString.last ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.last b === preview _last (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
last :: ByteString -> Maybe Word8
last =
  fmap snd . L.unsnoc
{-# INLINE last #-}

-- | Total 'L.tail': all but the first byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.tail "abc"
-- Just "bc"
--
-- >>> LazyByteString.tail ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap LazyByteString.unpack (LazyByteString.tail b) === preview _tail (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
tail :: ByteString -> Maybe ByteString
tail =
  fmap snd . L.uncons
{-# INLINE tail #-}

-- | Total 'L.init': all but the last byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.init "abc"
-- Just "ab"
--
-- >>> LazyByteString.init ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap LazyByteString.unpack (LazyByteString.init b) === preview _init (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
init :: ByteString -> Maybe ByteString
init =
  fmap fst . L.unsnoc
{-# INLINE init #-}

-- | Total 'L.foldl1': a left fold seeded with the first byte. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.foldl1 (-) "\10\3\2"
-- Just 5
--
-- >>> LazyByteString.foldl1 (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.foldl1 max b === maximumOf folded (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldl1 f =
  fmap (uncurry (L.foldl f)) . L.uncons
{-# INLINE foldl1 #-}

-- | Total 'L.foldl1'': a strict left fold seeded with the first byte.
-- 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.foldl1' (-) "\10\3\2"
-- Just 5
--
-- >>> LazyByteString.foldl1' (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.foldl1' (-) b === LazyByteString.foldl1 (-) b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldl1' f =
  fmap (uncurry (L.foldl' f)) . L.uncons
{-# INLINE foldl1' #-}

-- | Total 'L.foldr1': a right fold seeded with the last byte. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.foldr1 (-) "\10\3\2"
-- Just 9
--
-- >>> LazyByteString.foldr1 (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.foldr1 min b === minimumOf folded (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldr1 f =
  fmap (\(b, w) -> L.foldr f w b) . L.unsnoc
{-# INLINE foldr1 #-}

-- | Total 'L.foldr1'': a strict right fold seeded with the last byte.
-- 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.foldr1' (-) "\10\3\2"
-- Just 9
--
-- >>> LazyByteString.foldr1' (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.foldr1' (-) b === LazyByteString.foldr1 (-) b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldr1' f =
  fmap (\(b, w) -> L.foldr' f w b) . L.unsnoc
{-# INLINE foldr1' #-}

-- | Total 'L.maximum': the greatest byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.maximum "abca"
-- Just 99
--
-- >>> LazyByteString.maximum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.maximum b === maximumOf folded (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximum :: ByteString -> Maybe Word8
maximum b =
  L.maximum b <$ guard (not (L.null b))
{-# INLINE maximum #-}

-- | Total 'L.minimum': the least byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.minimum "bcab"
-- Just 97
--
-- >>> LazyByteString.minimum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; LazyByteString.minimum b === minimumOf folded (LazyByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimum :: ByteString -> Maybe Word8
minimum b =
  L.minimum b <$ guard (not (L.null b))
{-# INLINE minimum #-}

-- | Total 'L.cycle': the infinite repetition of a byte string. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> LazyByteString.take 7 <$> LazyByteString.cycle "abc"
-- Just "abcabca"
--
-- >>> LazyByteString.cycle ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap (LazyByteString.take (LazyByteString.length b)) (LazyByteString.cycle b) === (b <$ guard (not (LazyByteString.null b)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
cycle :: ByteString -> Maybe ByteString
cycle b =
  L.cycle b <$ guard (not (L.null b))
{-# INLINE cycle #-}

-- | Total 'L.index': the byte at a position, counting from 0. 'Data.Maybe.Nothing' for
-- a negative index or one past the end. This is 'X.indexMaybe'.
--
-- ==== __Examples__
--
-- >>> LazyByteString.index "abc" 1
-- Just 98
--
-- >>> LazyByteString.index "abc" 3
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; i <- forAll (Gen.int (Range.linear (-3) 25)); LazyByteString.index b (intCast i) === (LazyByteString.unpack b !! i)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
index :: ByteString -> Int64 -> Maybe Word8
index =
  L.indexMaybe
{-# INLINE index #-}

-- | Write to 'System.IO.stdout'.
--
-- @since 0.7.0
putStr :: ByteString -> IOExcept ()
putStr =
  exceptIO . L.putStr
{-# INLINE putStr #-}

-- | Write a file, replacing it.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException IO.isDoesNotExistError) (const False) <$> runExceptT (LazyByteString.writeFile "/nonexistent/papa" "x")
-- True
--
-- @since 0.7.0
writeFile :: FilePath -> ByteString -> IOExcept ()
writeFile p =
  exceptIO . L.writeFile p
{-# INLINE writeFile #-}

-- | Append to a file.
--
-- @since 0.7.0
appendFile :: FilePath -> ByteString -> IOExcept ()
appendFile p =
  exceptIO . L.appendFile p
{-# INLINE appendFile #-}

-- | Write to a handle.
--
-- @since 0.7.0
hPut :: Handle -> ByteString -> IOExcept ()
hPut h =
  exceptIO . L.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 . L.hPutNonBlocking h
{-# INLINE hPutNonBlocking #-}

-- | Write to a handle. This is 'hPut'.
--
-- @since 0.7.0
hPutStr :: Handle -> ByteString -> IOExcept ()
hPutStr h =
  exceptIO . L.hPutStr h
{-# INLINE hPutStr #-}

-- | 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 . L.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 . L.hGetNonBlocking h
{-# INLINE hGetNonBlocking #-}

-- $teardown
-- The doctests of the other modules do not take string literals as text.
--
-- >>> :set -XNoOverloadedStrings