papa-0.7.0: src/Papa/Bytestring/Data/ByteString/Builder.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Building byte strings, with no silent truncation, and I/O that returns
-- its errors. Import qualified:
--
-- > import qualified Papa.Bytestring.Data.ByteString.Builder as Builder
--
-- "Data.ByteString.Builder"'s @char7@ and @string7@ store a character's code
-- point modulo 128, and @char8@ and @string8@ modulo 256. They are replaced by
-- functions of the same names that return 'Data.Maybe.Nothing' for a character that
-- does not fit. 'X.charUtf8' and 'X.stringUtf8' encode every character.
--
-- 'hPutBuilder' and 'writeFile' return their
-- 'Control.Exception.IOException' ('Papa.Base.System.IO.Except.IOExcept').
--
-- @since 0.7.0
module Papa.Bytestring.Data.ByteString.Builder (
module X,
char7,
string7,
char8,
string8,
hPutBuilder,
writeFile,
) where
import Control.Monad (guard)
import Data.Bool (Bool)
import Data.ByteString.Builder as X hiding (
char7,
char8,
hPutBuilder,
string7,
string8,
writeFile,
)
import qualified Data.ByteString.Builder as B
import Data.Char (Char)
import Data.Foldable (all)
import Data.Function ((.))
import Data.Functor ((<$))
import Data.Maybe (Maybe)
import Data.Ord ((<=))
import Data.String (String)
import Papa.Base.System.IO.Except (IOExcept, exceptIO)
import Papa.Internal.Char8 (latin1, latin1s)
import System.IO (FilePath, Handle)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.Bytestring.Data.ByteString.Builder as Builder
-- >>> import qualified Data.ByteString.Lazy as LB
-- >>> 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
-- | Checked 'B.char7': a character as a 7-bit byte. 'Data.Maybe.Nothing' for a
-- character above @\'\\127\'@.
--
-- ==== __Examples__
--
-- >>> Builder.toLazyByteString <$> Builder.char7 'a'
-- Just "a"
--
-- >>> Builder.toLazyByteString <$> Builder.char7 '\128'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; fmap Builder.toLazyByteString (Builder.char7 c) === (Builder.toLazyByteString (Builder.charUtf8 c) <$ guard (c <= '\127'))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
char7 :: Char -> Maybe Builder
char7 c =
B.char7 c <$ guard (ascii c)
{-# INLINE char7 #-}
-- | Checked 'B.string7': a string as 7-bit bytes. 'Data.Maybe.Nothing' if a character
-- is above @\'\\127\'@.
--
-- ==== __Examples__
--
-- >>> Builder.toLazyByteString <$> Builder.string7 "abc"
-- Just "abc"
--
-- >>> Builder.toLazyByteString <$> Builder.string7 "caf\233"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; fmap Builder.toLazyByteString (Builder.string7 s) === (Builder.toLazyByteString (Builder.stringUtf8 s) <$ guard (all (<= '\127') s))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
string7 :: String -> Maybe Builder
string7 s =
B.string7 s <$ guard (all ascii s)
{-# INLINE string7 #-}
-- | Checked 'B.char8': a character as a byte. 'Data.Maybe.Nothing' for a character
-- above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> Builder.toLazyByteString <$> Builder.char8 '\233'
-- Just "\233"
--
-- >>> Builder.toLazyByteString <$> Builder.char8 '\256'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll genChar; fmap Builder.toLazyByteString (Builder.char8 c) === fmap Builder.toLazyByteString (Builder.string8 [c])
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
char8 :: Char -> Maybe Builder
char8 c =
B.char8 c <$ guard (latin1 c)
{-# INLINE char8 #-}
-- | Checked 'B.string8': a string as bytes. 'Data.Maybe.Nothing' if a character is
-- above @\'\\255\'@.
--
-- ==== __Examples__
--
-- >>> Builder.toLazyByteString <$> Builder.string8 "caf\233"
-- Just "caf\233"
--
-- >>> Builder.toLazyByteString <$> Builder.string8 "\256"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; fmap (\b -> LB.length (Builder.toLazyByteString b)) (Builder.string8 s) === (intCast (length s) <$ guard (all (<= '\255') s))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
string8 :: String -> Maybe Builder
string8 s =
B.string8 s <$ latin1s s
{-# INLINE string8 #-}
-- | Write a builder to a handle.
--
-- @since 0.7.0
hPutBuilder :: Handle -> Builder -> IOExcept ()
hPutBuilder h =
exceptIO . B.hPutBuilder h
{-# INLINE hPutBuilder #-}
-- | Write a builder to a file, replacing it.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException IO.isDoesNotExistError) (const False) <$> runExceptT (Builder.writeFile "/nonexistent/papa" mempty)
-- True
--
-- @since 0.7.0
writeFile :: FilePath -> Builder -> IOExcept ()
writeFile p =
exceptIO . B.writeFile p
{-# INLINE writeFile #-}
-- | Whether a character is ASCII: @\'\\0\'@ to @\'\\127\'@.
ascii :: Char -> Bool
ascii c =
c <= '\127'
{-# INLINE ascii #-}