diff --git a/src/Data/Text.hs b/src/Data/Text.hs index 39461322..9b1cc407 100644 --- a/src/Data/Text.hs +++ b/src/Data/Text.hs @@ -4,6 +4,8 @@ {-# LANGUAGE UnliftedFFITypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE PartialTypeSignatures #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -Wno-partial-type-signatures #-} @@ -61,6 +63,11 @@ module Data.Text , singleton , empty + -- * Pattern matching + , pattern Empty + , pattern (:<) + , pattern (:>) + -- * Basic interface , cons , snoc @@ -565,6 +572,34 @@ null (Text _arr _off len) = "TEXT null/empty -> True" null empty = True #-} +-- | Bidirectional pattern synonym for 'empty' and 'null' (both /O(1)/), +-- to be used together with '(:<)' or '(:>)'. +-- +-- @since 2.1.2 +pattern Empty :: Text +pattern Empty <- (null -> True) where + Empty = empty + +-- | Bidirectional pattern synonym for 'cons' (/O(n)/) and 'uncons' (/O(1)/), +-- to be used together with 'Empty'. +-- +-- @since 2.1.2 +pattern (:<) :: Char -> Text -> Text +pattern x :< xs <- (uncons -> Just (x, xs)) where + (:<) = cons +infixr 5 :< +{-# COMPLETE Empty, (:<) #-} + +-- | Bidirectional pattern synonym for 'snoc' (/O(n)/) and 'unsnoc' (/O(1)/) +-- to be used together with 'Empty'. +-- +-- @since 2.1.2 +pattern (:>) :: Text -> Char -> Text +pattern xs :> x <- (unsnoc -> Just (xs, x)) where + (:>) = snoc +infixl 5 :> +{-# COMPLETE Empty, (:>) #-} + -- | /O(1)/ Tests whether a 'Text' contains exactly one character. isSingleton :: Text -> Bool isSingleton = S.isSingleton . stream diff --git a/src/Data/Text/Internal/Lazy.hs b/src/Data/Text/Internal/Lazy.hs index 5a81cfe5..fbf6380e 100644 --- a/src/Data/Text/Internal/Lazy.hs +++ b/src/Data/Text/Internal/Lazy.hs @@ -50,7 +50,9 @@ import qualified Data.Text.Internal as T import qualified Data.Text as T data Text = Empty + -- ^ Empty text. | Chunk {-# UNPACK #-} !T.Text Text + -- ^ Chunks must be non-empty, this invariant is not checked. deriving (Typeable) -- | Type synonym for the lazy flavour of 'Text'. diff --git a/src/Data/Text/Lazy.hs b/src/Data/Text/Lazy.hs index 938bd436..59bbc9d4 100644 --- a/src/Data/Text/Lazy.hs +++ b/src/Data/Text/Lazy.hs @@ -3,6 +3,8 @@ {-# LANGUAGE Trustworthy #-} {-# LANGUAGE TemplateHaskellQuotes #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ViewPatterns #-} -- | -- Module : Data.Text.Lazy @@ -60,6 +62,11 @@ module Data.Text.Lazy , foldrChunks , foldlChunks + -- * Pattern matching + , pattern Empty + , pattern (:<) + , pattern (:>) + -- * Basic interface , cons , snoc @@ -533,6 +540,26 @@ null Empty = True null _ = False {-# INLINE [1] null #-} +-- | Bidirectional pattern synonym for 'cons' (/O(n)/) and 'uncons' (/O(1)/), +-- to be used together with 'Empty'. +-- +-- @since 2.1.2 +pattern (:<) :: Char -> Text -> Text +pattern x :< xs <- (uncons -> Just (x, xs)) where + (:<) = cons +infixr 5 :< +{-# COMPLETE Empty, (:<) #-} + +-- | Bidirectional pattern synonym for 'snoc' (/O(n)/) and 'unsnoc' (/O(1)/) +-- to be used together with 'Empty'. +-- +-- @since 2.1.2 +pattern (:>) :: Text -> Char -> Text +pattern xs :> x <- (unsnoc -> Just (xs, x)) where + (:>) = snoc +infixl 5 :> +{-# COMPLETE Empty, (:>) #-} + -- | /O(1)/ Tests whether a 'Text' contains exactly one character. isSingleton :: Text -> Bool isSingleton = S.isSingleton . stream