papa-0.7.0: src/Papa/Text/Data/Text.hs
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Redundant lambda" -}
-- Reason: a function whose text follows other arguments (@splitOn p@,
-- @replace p r@) takes the text 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.
-- | Strict text, with total versions of the partial functions of
-- "Data.Text". Import qualified:
--
-- > import qualified Papa.Text.Data.Text as Text
--
-- The functions of "Data.Text" that throw are replaced by functions of the
-- same names that return 'Data.Maybe.Nothing' there instead:
--
-- * for an empty text: 'head', 'last', 'tail', 'init', 'foldl1', 'foldl1'',
-- 'foldr1', 'maximum', 'minimum';
-- * for an index out of range: 'index';
-- * for an empty needle: 'splitOn', 'breakOn', 'breakOnEnd', 'breakOnAll',
-- 'count', 'replace'.
--
-- @unpackCString#@ and @unpackCStringAscii#@, which read a C string from a
-- raw address, are not exported.
--
-- The optics of @lens@ work on 'Text' too: @'Control.Lens.preview'
-- 'Control.Lens._head'@, @'Control.Lens.preview' 'Control.Lens._last'@ and
-- @'Control.Lens.preview' ('Control.Lens.ix' i)@ read a character, and
-- "Data.Text.Lens"'s @packed@, @unpacked@ and @text@ convert and traverse.
--
-- A function whose result would be longer than @'Prelude.maxBound' :: 'Int'@ bytes
-- (@replicate@, @concat@, @intercalate@ and the like) throws a size overflow,
-- like an allocation that cannot fit in memory.
--
-- @since 0.7.0
module Papa.Text.Data.Text (
module X,
display,
displayFixed,
displayExponent,
readMaybe,
readEither,
head,
last,
tail,
init,
foldl1,
foldl1',
foldr1,
maximum,
minimum,
index,
splitOn,
breakOn,
breakOnEnd,
breakOnAll,
count,
replace,
) where
import Control.Applicative ((*>))
import Control.Monad (guard)
import Data.Bifunctor (first)
import Data.Bits (toIntegralSized)
import Data.Bool (not)
import Data.Char (Char)
import Data.Either (Either)
import Data.Function ((.))
import Data.Functor (fmap, (<$))
import Data.Int (Int)
import Data.Maybe (Maybe (Just), fromMaybe)
import Data.Ord (Ord (max, min, (>=)))
import Data.Text as X hiding (
breakOn,
breakOnAll,
breakOnEnd,
count,
foldl1,
foldl1',
foldr1,
head,
index,
init,
last,
maximum,
minimum,
replace,
splitOn,
tail,
unpackCString#,
unpackCStringAscii#,
)
import qualified Data.Text as T
import Data.Tuple (fst, snd, uncurry)
import Data.Word (Word)
import Numeric (showEFloat, showFFloat)
import Text.Read (Read)
import qualified Text.Read as Read
import Text.Show (Show)
import qualified Text.Show as Show
import Prelude (RealFloat)
import qualified Prelude as P
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> :set -XOverloadedStrings
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Prelude
-- >>> import Numeric (showFFloat)
-- >>> import qualified Papa.Text.Data.Text as Text
-- >>> import qualified Data.Text as DT
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genText = Gen.text (Range.linear 0 20) (Gen.element "abc")
-- >>> let genNeedle = Gen.text (Range.linear 0 3) (Gen.element "abc")
-- | The 'Text' of a value's 'Show.show'.
--
-- ==== __Examples__
--
-- >>> display (3 :: Int)
-- "3"
--
-- >>> display (Just 'x')
-- "Just 'x'"
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int (Range.linear (-100) 100)); Text.unpack (display n) === P.show n
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
display :: (Show a) => a -> Text
display =
T.pack . Show.show
{-# INLINE display #-}
-- | A floating-point number with the given number of digits after the
-- decimal point, as C's @%.2f@ and the like show it. It rounds the
-- shortest decimal digits that 'Show.show' gives, a half to the even digit
-- (@displayFixed 2 0.125@ is @0.12@). @NaN@ and the infinities are
-- @NaN@, @Infinity@ and @-Infinity@. "Numeric"'s @showFFloat@, returning
-- 'Text'; 'Papa.Unsafe.Format.printf' is in "Papa.Unsafe.Format".
--
-- ==== __Examples__
--
-- >>> displayFixed 2 (3.14159 :: Double)
-- "3.14"
--
-- >>> displayFixed 2 (2 :: Double)
-- "2.00"
--
-- >>> displayFixed 0 (2.7 :: Double)
-- "3"
--
-- >>> displayFixed 2 (-0.001 :: Double)
-- "-0.00"
--
-- >>> check $ withTests 200 $ property $ do x <- forAll (Gen.double (Range.linearFracFrom 0 (-1e6) 1e6)); n <- forAll (Gen.word (Range.linear 0 10)); Text.unpack (displayFixed n x) === showFFloat (Just (Prelude.fromIntegral n)) x ""
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
displayFixed :: (RealFloat a) => Word -> a -> Text
displayFixed n x =
T.pack (showFFloat (Just (digits n)) x "")
{-# INLINE displayFixed #-}
-- | A floating-point number in scientific notation, with the given number of
-- digits after the decimal point. "Numeric"'s @showEFloat@, returning
-- 'Text'.
--
-- ==== __Examples__
--
-- >>> displayExponent 2 (1234.5 :: Double)
-- "1.23e3"
--
-- >>> displayExponent 1 (0.00012 :: Double)
-- "1.2e-4"
--
-- @since 0.7.0
displayExponent :: (RealFloat a) => Word -> a -> Text
displayExponent n x =
T.pack (showEFloat (Just (digits n)) x "")
{-# INLINE displayExponent #-}
-- | A number of digits as an 'Int'. A 'Word' beyond the 'Int's is more digits
-- than any number has, so it is the largest 'Int'.
digits :: Word -> Int
digits n =
fromMaybe P.maxBound (toIntegralSized n)
{-# INLINE digits #-}
-- | Parse the 'Show.show' of a value: 'Data.Maybe.Nothing' where it does not
-- parse. "Text.Read"'s @readMaybe@, for 'Text'.
--
-- ==== __Examples__
--
-- >>> readMaybe "42" :: Maybe Int
-- Just 42
--
-- >>> readMaybe " 2.5 " :: Maybe Double
-- Just 2.5
--
-- >>> readMaybe "forty-two" :: Maybe Int
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int (Range.linear (-1000) 1000)); readMaybe (display n) === Just n
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
readMaybe :: (Read a) => Text -> Maybe a
readMaybe =
Read.readMaybe . T.unpack
{-# INLINE readMaybe #-}
-- | Parse the 'Show.show' of a value, or say why it does not parse.
-- "Text.Read"'s @readEither@, for 'Text'.
--
-- ==== __Examples__
--
-- >>> readEither "42" :: Either Text Int
-- Right 42
--
-- >>> readEither "x" :: Either Text Int
-- Left "Prelude.read: no parse"
--
-- @since 0.7.0
readEither :: (Read a) => Text -> Either Text a
readEither =
first T.pack . Read.readEither . T.unpack
{-# INLINE readEither #-}
-- | Total 'T.head': the first character. 'Data.Maybe.Nothing' for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.head "abc"
-- Just 'a'
--
-- >>> Text.head ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.head t === preview _head (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
head :: Text -> Maybe Char
head =
fmap fst . T.uncons
{-# INLINE head #-}
-- | Total 'T.last': the last character. 'Data.Maybe.Nothing' for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.last "abc"
-- Just 'c'
--
-- >>> Text.last ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.last t === preview _last (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
last :: Text -> Maybe Char
last =
fmap snd . T.unsnoc
{-# INLINE last #-}
-- | Total 'T.tail': all but the first character. 'Data.Maybe.Nothing' for the empty
-- text.
--
-- ==== __Examples__
--
-- >>> Text.tail "abc"
-- Just "bc"
--
-- >>> Text.tail ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; fmap Text.unpack (Text.tail t) === preview _tail (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
tail :: Text -> Maybe Text
tail =
fmap snd . T.uncons
{-# INLINE tail #-}
-- | Total 'T.init': all but the last character. 'Data.Maybe.Nothing' for the empty
-- text.
--
-- ==== __Examples__
--
-- >>> Text.init "abc"
-- Just "ab"
--
-- >>> Text.init ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; fmap Text.unpack (Text.init t) === preview _init (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
init :: Text -> Maybe Text
init =
fmap fst . T.unsnoc
{-# INLINE init #-}
-- | Total 'T.foldl1': a left fold seeded with the first character. 'Data.Maybe.Nothing'
-- for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.foldl1 max "abca"
-- Just 'c'
--
-- >>> Text.foldl1 max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.foldl1 min t === fmap (\(c, r) -> Text.foldl min c r) (Text.uncons t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1 :: (Char -> Char -> Char) -> Text -> Maybe Char
foldl1 f =
fmap (uncurry (T.foldl f)) . T.uncons
{-# INLINE foldl1 #-}
-- | Total 'T.foldl1'': a strict left fold seeded with the first character.
-- 'Data.Maybe.Nothing' for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.foldl1' max "abca"
-- Just 'c'
--
-- >>> Text.foldl1' max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.foldl1' min t === Text.foldl1 min t
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1' :: (Char -> Char -> Char) -> Text -> Maybe Char
foldl1' f =
fmap (uncurry (T.foldl' f)) . T.uncons
{-# INLINE foldl1' #-}
-- | Total 'T.foldr1': a right fold seeded with the last character. 'Data.Maybe.Nothing'
-- for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.foldr1 max "abca"
-- Just 'c'
--
-- >>> Text.foldr1 max ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.foldr1 min t === Text.foldl1 min t
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1 :: (Char -> Char -> Char) -> Text -> Maybe Char
foldr1 f =
fmap (\(t, c) -> T.foldr f c t) . T.unsnoc
{-# INLINE foldr1 #-}
-- | Total 'T.maximum': the greatest character. 'Data.Maybe.Nothing' for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.maximum "abca"
-- Just 'c'
--
-- >>> Text.maximum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.maximum t === maximumOf folded (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximum :: Text -> Maybe Char
maximum =
foldl1' max
{-# INLINE maximum #-}
-- | Total 'T.minimum': the least character. 'Data.Maybe.Nothing' for the empty text.
--
-- ==== __Examples__
--
-- >>> Text.minimum "bcab"
-- Just 'a'
--
-- >>> Text.minimum ""
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; Text.minimum t === minimumOf folded (Text.unpack t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimum :: Text -> Maybe Char
minimum =
foldl1' min
{-# INLINE minimum #-}
-- | Total 'T.index': the character at a position, counting from 0.
-- 'Data.Maybe.Nothing' for a negative index or one past the end. /O(n)/.
--
-- ==== __Examples__
--
-- >>> Text.index "abc" 1
-- Just 'b'
--
-- >>> Text.index "abc" 3
-- Nothing
--
-- >>> Text.index "abc" (-1)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do t <- forAll genText; i <- forAll (Gen.int (Range.linear (-3) 25)); Text.index t i === (Text.unpack t !! i)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
index :: Text -> Int -> Maybe Char
index t i =
guard (i >= 0) *> head (T.drop i t)
{-# INLINE index #-}
-- | Total 'T.splitOn': the pieces of a text between occurrences of a
-- needle. 'Data.Maybe.Nothing' for the empty needle.
--
-- ==== __Examples__
--
-- >>> Text.splitOn "," "a,b,,c"
-- Just ["a","b","","c"]
--
-- >>> Text.splitOn "" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; t <- forAll genText; fmap (Text.intercalate p) (Text.splitOn p t) === (t <$ guard (not (Text.null p)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
splitOn :: Text -> Text -> Maybe [Text]
splitOn p =
\t -> T.splitOn p t <$ needle p
{-# INLINE splitOn #-}
-- | Total 'T.breakOn': the text before the first occurrence of a needle, and
-- the rest. 'Data.Maybe.Nothing' for the empty needle.
--
-- ==== __Examples__
--
-- >>> Text.breakOn "::" "a::b::c"
-- Just ("a","::b::c")
--
-- >>> Text.breakOn "" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; t <- forAll genText; fmap (uncurry (<>)) (Text.breakOn p t) === (t <$ guard (not (Text.null p)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
breakOn :: Text -> Text -> Maybe (Text, Text)
breakOn p =
\t -> T.breakOn p t <$ needle p
{-# INLINE breakOn #-}
-- | Total 'T.breakOnEnd': the text up to and including the last occurrence
-- of a needle, and the rest. 'Data.Maybe.Nothing' for the empty needle.
--
-- ==== __Examples__
--
-- >>> Text.breakOnEnd "::" "a::b::c"
-- Just ("a::b::","c")
--
-- >>> Text.breakOnEnd "" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; t <- forAll genText; fmap (uncurry (<>)) (Text.breakOnEnd p t) === (t <$ guard (not (Text.null p)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
breakOnEnd :: Text -> Text -> Maybe (Text, Text)
breakOnEnd p =
\t -> T.breakOnEnd p t <$ needle p
{-# INLINE breakOnEnd #-}
-- | Total 'T.breakOnAll': each split of a text at an occurrence of a needle.
-- 'Data.Maybe.Nothing' for the empty needle.
--
-- ==== __Examples__
--
-- >>> Text.breakOnAll "/" "a/b/c"
-- Just [("a","/b/c"),("a/b","/c")]
--
-- >>> Text.breakOnAll "" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; t <- forAll genText; fmap (P.all (\(a, b) -> a <> b == t)) (Text.breakOnAll p t) === (True <$ guard (not (Text.null p)))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
breakOnAll :: Text -> Text -> Maybe [(Text, Text)]
breakOnAll p =
\t -> T.breakOnAll p t <$ needle p
{-# INLINE breakOnAll #-}
-- | Total 'T.count': the number of non-overlapping occurrences of a needle.
-- 'Data.Maybe.Nothing' for the empty needle.
--
-- ==== __Examples__
--
-- >>> Text.count "aa" "aaaaa"
-- Just 2
--
-- >>> Text.count "" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; t <- forAll genText; Text.count p t === fmap (\xs -> P.length xs - 1) (Text.splitOn p t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
count :: Text -> Text -> Maybe Int
count p =
\t -> T.count p t <$ needle p
{-# INLINE count #-}
-- | Total 'T.replace': replace every non-overlapping occurrence of a needle
-- (the first argument) with a replacement (the second). 'Data.Maybe.Nothing' for the
-- empty needle.
--
-- ==== __Examples__
--
-- >>> Text.replace "ofo" "bar" "ofofo"
-- Just "barfo"
--
-- >>> Text.replace "" "x" "abc"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do p <- forAll genNeedle; r <- forAll genNeedle; t <- forAll genText; Text.replace p r t === fmap (Text.intercalate r) (Text.splitOn p t)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
replace :: Text -> Text -> Text -> Maybe Text
replace p r =
\t -> T.replace p r t <$ needle p
{-# INLINE replace #-}
-- | 'Data.Maybe.Just' for a needle that is not empty.
needle :: Text -> Maybe ()
needle =
guard . not . T.null
{-# INLINE needle #-}
-- $teardown
-- The doctests of the other modules do not take string literals as text.
--
-- >>> :set -XNoOverloadedStrings