papa-0.7.0: src/Papa/Bytestring/Data/ByteString.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"'s maximum and minimum only on a byte string that is
-- not empty, for their speed.
-- | Strict byte strings, with total versions of the partial functions of
-- "Data.ByteString", and I/O that returns its errors. Import qualified:
--
-- > import qualified Papa.Bytestring.Data.ByteString as ByteString
--
-- The functions of "Data.ByteString" 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';
-- * for an index out of range: 'index' (which is 'X.indexMaybe').
--
-- Its I/O actions are replaced by actions of the same names that return
-- their 'Control.Exception.IOException' ('Papa.Base.System.IO.Except.IOExcept'):
-- 'getLine', 'getContents', 'putStr', 'interact', 'readFile', 'writeFile',
-- 'appendFile', 'hGetLine', 'hGetContents', 'hGet', 'hGetSome',
-- 'hGetNonBlocking', 'hPut', 'hPutNonBlocking', 'hPutStr', 'packCString',
-- 'packCStringLen', 'fromFilePath', 'toFilePath'. All of them read strictly.
--
-- The optics of @lens@ work on 'ByteString' too: @'Control.Lens.preview'
-- 'Control.Lens._head'@ and @'Control.Lens.preview' ('Control.Lens.ix' i)@
-- read a byte, and "Data.ByteString.Lens"'s @packedBytes@ and @bytes@ convert
-- and traverse.
--
-- @since 0.7.0
module Papa.Bytestring.Data.ByteString (
module X,
head,
last,
tail,
init,
foldl1,
foldl1',
foldr1,
foldr1',
maximum,
minimum,
index,
getLine,
getContents,
putStr,
interact,
readFile,
writeFile,
appendFile,
hGetLine,
hGetContents,
hGet,
hGetSome,
hGetNonBlocking,
hPut,
hPutNonBlocking,
hPutStr,
packCString,
packCStringLen,
fromFilePath,
toFilePath,
) where
import Control.Monad (guard)
import Data.Bool (not)
import Data.ByteString as X hiding (
appendFile,
foldl1,
foldl1',
foldr1,
foldr1',
fromFilePath,
getContents,
getLine,
hGet,
hGetContents,
hGetLine,
hGetNonBlocking,
hGetSome,
hPut,
hPutNonBlocking,
hPutStr,
head,
index,
init,
interact,
last,
maximum,
minimum,
packCString,
packCStringLen,
putStr,
readFile,
tail,
toFilePath,
writeFile,
)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Data.Function ((.))
import Data.Functor (fmap, (<$))
import Data.Int (Int)
import Data.Maybe (Maybe)
import Data.Tuple (fst, snd, uncurry)
import Data.Word (Word8)
import Foreign.C.String (CString, CStringLen)
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 as ByteString
-- >>> import qualified System.IO.Error as IO
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genBytes = Gen.bytes (Range.linear 0 20)
-- | Total 'B.head': the first byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.head "abc"
-- Just 97
--
-- >>> ByteString.head ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.head b === preview _head (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
head :: ByteString -> Maybe Word8
head =
fmap fst . B.uncons
{-# INLINE head #-}
-- | Total 'B.last': the last byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.last "abc"
-- Just 99
--
-- >>> ByteString.last ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.last b === preview _last (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
last :: ByteString -> Maybe Word8
last =
fmap snd . B.unsnoc
{-# INLINE last #-}
-- | Total 'B.tail': all but the first byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> ByteString.tail "abc"
-- Just "bc"
--
-- >>> ByteString.tail ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap ByteString.unpack (ByteString.tail b) === preview _tail (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
tail :: ByteString -> Maybe ByteString
tail =
fmap snd . B.uncons
{-# INLINE tail #-}
-- | Total 'B.init': all but the last byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> ByteString.init "abc"
-- Just "ab"
--
-- >>> ByteString.init ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; fmap ByteString.unpack (ByteString.init b) === preview _init (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
init :: ByteString -> Maybe ByteString
init =
fmap fst . B.unsnoc
{-# INLINE init #-}
-- | Total 'B.foldl1': a left fold seeded with the first byte. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.foldl1 (-) "\10\3\2"
-- Just 5
--
-- >>> ByteString.foldl1 (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.foldl1 max b === maximumOf folded (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldl1 f =
fmap (uncurry (B.foldl f)) . B.uncons
{-# INLINE foldl1 #-}
-- | Total 'B.foldl1'': a strict left fold seeded with the first byte.
-- 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.foldl1' (-) "\10\3\2"
-- Just 5
--
-- >>> ByteString.foldl1' (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.foldl1' (-) b === ByteString.foldl1 (-) b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldl1' f =
fmap (uncurry (B.foldl' f)) . B.uncons
{-# INLINE foldl1' #-}
-- | Total 'B.foldr1': a right fold seeded with the last byte. 'Data.Maybe.Nothing' for
-- the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.foldr1 (-) "\10\3\2"
-- Just 9
--
-- >>> ByteString.foldr1 (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.foldr1 min b === minimumOf folded (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldr1 f =
fmap (\(b, w) -> B.foldr f w b) . B.unsnoc
{-# INLINE foldr1 #-}
-- | Total 'B.foldr1'': a strict right fold seeded with the last byte.
-- 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.foldr1' (-) "\10\3\2"
-- Just 9
--
-- >>> ByteString.foldr1' (-) ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.foldr1' (-) b === ByteString.foldr1 (-) b
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Maybe Word8
foldr1' f =
fmap (\(b, w) -> B.foldr' f w b) . B.unsnoc
{-# INLINE foldr1' #-}
-- | Total 'B.maximum': the greatest byte. 'Data.Maybe.Nothing' for the empty byte
-- string.
--
-- ==== __Examples__
--
-- >>> ByteString.maximum "abca"
-- Just 99
--
-- >>> ByteString.maximum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.maximum b === maximumOf folded (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximum :: ByteString -> Maybe Word8
maximum b =
B.maximum b <$ guard (not (B.null b))
{-# INLINE maximum #-}
-- | Total 'B.minimum': the least byte. 'Data.Maybe.Nothing' for the empty byte string.
--
-- ==== __Examples__
--
-- >>> ByteString.minimum "bcab"
-- Just 97
--
-- >>> ByteString.minimum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; ByteString.minimum b === minimumOf folded (ByteString.unpack b)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimum :: ByteString -> Maybe Word8
minimum b =
B.minimum b <$ guard (not (B.null b))
{-# INLINE minimum #-}
-- | Total 'B.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__
--
-- >>> ByteString.index "abc" 1
-- Just 98
--
-- >>> ByteString.index "abc" 3
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll genBytes; i <- forAll (Gen.int (Range.linear (-3) 25)); ByteString.index b i === (ByteString.unpack b !! i)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
index :: ByteString -> Int -> Maybe Word8
index =
B.indexMaybe
{-# INLINE index #-}
-- | Read a line from 'System.IO.stdin', up to a newline byte, which is
-- removed. See 'hGetLine'.
--
-- @since 0.7.0
getLine :: IOExcept ByteString
getLine =
exceptIO B8.getLine
{-# INLINE getLine #-}
-- | Read all of 'System.IO.stdin'.
--
-- @since 0.7.0
getContents :: IOExcept ByteString
getContents =
exceptIO B.getContents
{-# INLINE getContents #-}
-- | Write to 'System.IO.stdout'.
--
-- @since 0.7.0
putStr :: ByteString -> IOExcept ()
putStr =
exceptIO . B.putStr
{-# INLINE putStr #-}
-- | Read all of 'System.IO.stdin', and write a function of it to
-- 'System.IO.stdout'.
--
-- @since 0.7.0
interact :: (ByteString -> ByteString) -> IOExcept ()
interact =
exceptIO . B.interact
{-# INLINE interact #-}
-- | Read a file.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException IO.isDoesNotExistError) (const False) <$> runExceptT (ByteString.readFile "/nonexistent/papa")
-- True
--
-- @since 0.7.0
readFile :: FilePath -> IOExcept ByteString
readFile =
exceptIO . B.readFile
{-# INLINE readFile #-}
-- | Write a file, replacing it.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException IO.isDoesNotExistError) (const False) <$> runExceptT (ByteString.writeFile "/nonexistent/papa" "x")
-- True
--
-- @since 0.7.0
writeFile :: FilePath -> ByteString -> IOExcept ()
writeFile p =
exceptIO . B.writeFile p
{-# INLINE writeFile #-}
-- | Append to a file.
--
-- @since 0.7.0
appendFile :: FilePath -> ByteString -> IOExcept ()
appendFile p =
exceptIO . B.appendFile p
{-# INLINE appendFile #-}
-- | Read a line from a handle, up to a newline byte (@\\n@), which is
-- removed; a carriage return before it is kept. An end-of-file error at the
-- end of the handle.
--
-- @since 0.7.0
hGetLine :: Handle -> IOExcept ByteString
hGetLine =
exceptIO . B8.hGetLine
{-# INLINE hGetLine #-}
-- | Read the rest of a handle, and close it.
--
-- @since 0.7.0
hGetContents :: Handle -> IOExcept ByteString
hGetContents =
exceptIO . B.hGetContents
{-# INLINE hGetContents #-}
-- | 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 . B.hGet h
{-# INLINE hGet #-}
-- | Read up to the given number of bytes from a handle, waiting for at least
-- one unless the end is reached. An error for a negative number.
--
-- @since 0.7.0
hGetSome :: Handle -> Int -> IOExcept ByteString
hGetSome h =
exceptIO . B.hGetSome h
{-# INLINE hGetSome #-}
-- | 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 . B.hGetNonBlocking h
{-# INLINE hGetNonBlocking #-}
-- | Write to a handle.
--
-- @since 0.7.0
hPut :: Handle -> ByteString -> IOExcept ()
hPut h =
exceptIO . B.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 . B.hPutNonBlocking h
{-# INLINE hPutNonBlocking #-}
-- | Write to a handle. This is 'hPut'.
--
-- @since 0.7.0
hPutStr :: Handle -> ByteString -> IOExcept ()
hPutStr h =
exceptIO . B.hPutStr h
{-# INLINE hPutStr #-}
-- | Copy a NUL-terminated C string. The pointer must point to one.
--
-- @since 0.7.0
packCString :: CString -> IOExcept ByteString
packCString =
exceptIO . B.packCString
{-# INLINE packCString #-}
-- | Copy a C string of the given length. An error for a negative length; the
-- pointer must point to that many bytes.
--
-- @since 0.7.0
packCStringLen :: CStringLen -> IOExcept ByteString
packCStringLen =
exceptIO . B.packCStringLen
{-# INLINE packCStringLen #-}
-- | The bytes of a file path, in the file system's encoding. An error for a
-- path that the encoding cannot represent.
--
-- ==== __Examples__
--
-- >>> runExceptT (ByteString.fromFilePath "a/b")
-- Right "a/b"
--
-- @since 0.7.0
fromFilePath :: FilePath -> IOExcept ByteString
fromFilePath =
exceptIO . B.fromFilePath
{-# INLINE fromFilePath #-}
-- | The file path of some bytes, in the file system's encoding. An error for
-- bytes that the encoding cannot decode.
--
-- ==== __Examples__
--
-- >>> runExceptT (ByteString.toFilePath "a/b")
-- Right "a/b"
--
-- >>> check $ withTests 100 $ property $ do p <- forAll (Gen.string (Range.linear 0 10) Gen.alphaNum); r <- evalIO (runExceptT (ByteString.fromFilePath p >>= ByteString.toFilePath)); r === Right p
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
toFilePath :: ByteString -> IOExcept FilePath
toFilePath =
exceptIO . B.toFilePath
{-# INLINE toFilePath #-}
-- $teardown
-- The doctests of the other modules do not take string literals as text.
--
-- >>> :set -XNoOverloadedStrings