papa-0.7.0: src/Papa/Text/Data/Text/Lazy/Builder/Int.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Building the text of an integral number. Import qualified:
--
-- > import qualified Papa.Text.Data.Text.Lazy.Builder.Int as BuilderInt
--
-- "Data.Text.Lazy.Builder.Int"'s @hexadecimal@ throws for a negative number;
-- 'hexadecimal' returns 'Data.Maybe.Nothing' there instead. 'X.decimal' is
-- "Data.Text.Lazy.Builder.Int"'s.
--
-- @since 0.7.0
module Papa.Text.Data.Text.Lazy.Builder.Int (
module X,
hexadecimal,
) where
import Control.Monad (guard)
import Data.Functor ((<$))
import Data.Maybe (Maybe)
import Data.Ord ((>=))
import Data.Text.Lazy.Builder (Builder)
import Data.Text.Lazy.Builder.Int as X (
decimal,
)
import qualified Data.Text.Lazy.Builder.Int as BI
import Prelude (Integral)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.Text.Data.Text.Lazy.Builder.Int as BuilderInt
-- >>> import qualified Data.Text.Lazy.Builder as B
-- >>> import qualified Data.Text.Read as TR
-- >>> import qualified Data.Text.Lazy as L
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- | The lower-case hexadecimal digits of a number, without a @0x@.
-- 'Data.Maybe.Nothing' for a negative number.
--
-- ==== __Examples__
--
-- >>> B.toLazyText <$> BuilderInt.hexadecimal (255 :: Int)
-- Just "ff"
--
-- >>> B.toLazyText <$> BuilderInt.hexadecimal (-1 :: Int)
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do n <- forAll (Gen.int (Range.linear (-1000) 100000)); fmap (TR.hexadecimal . L.toStrict . B.toLazyText) (BuilderInt.hexadecimal n) === (Right (n, mempty) <$ guard (n >= 0))
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
hexadecimal :: (Integral a) => a -> Maybe Builder
hexadecimal n =
BI.hexadecimal n <$ guard (n >= 0)
{-# INLINE hexadecimal #-}