Skip to content

Commit b4d7ce5

Browse files
BodigrimLysxia
authored andcommitted
Implement pattern synonyms for StrictText and LazyText
1 parent d67f2aa commit b4d7ce5

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/Data/Text.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{-# LANGUAGE UnliftedFFITypes #-}
55
{-# LANGUAGE ScopedTypeVariables #-}
66
{-# LANGUAGE PartialTypeSignatures #-}
7+
{-# LANGUAGE PatternSynonyms #-}
8+
{-# LANGUAGE ViewPatterns #-}
79

810
{-# OPTIONS_GHC -fno-warn-orphans #-}
911
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
@@ -61,6 +63,11 @@ module Data.Text
6163
, singleton
6264
, empty
6365

66+
-- * Pattern matching
67+
, pattern Empty
68+
, pattern (:<)
69+
, pattern (:>)
70+
6471
-- * Basic interface
6572
, cons
6673
, snoc
@@ -565,6 +572,34 @@ null (Text _arr _off len) =
565572
"TEXT null/empty -> True" null empty = True
566573
#-}
567574

575+
-- | Bidirectional pattern synonym for 'empty' and 'null' (both /O(1)/),
576+
-- to be used together with '(:<)' or '(:>)'.
577+
--
578+
-- @since 2.1.2
579+
pattern Empty :: Text
580+
pattern Empty <- (null -> True) where
581+
Empty = empty
582+
583+
-- | Bidirectional pattern synonym for 'cons' (/O(n)/) and 'uncons' (/O(1)/),
584+
-- to be used together with 'Empty'.
585+
--
586+
-- @since 2.1.2
587+
pattern (:<) :: Char -> Text -> Text
588+
pattern x :< xs <- (uncons -> Just (x, xs)) where
589+
(:<) = cons
590+
infixr 5 :<
591+
{-# COMPLETE Empty, (:<) #-}
592+
593+
-- | Bidirectional pattern synonym for 'snoc' (/O(n)/) and 'unsnoc' (/O(1)/)
594+
-- to be used together with 'Empty'.
595+
--
596+
-- @since 2.1.2
597+
pattern (:>) :: Text -> Char -> Text
598+
pattern xs :> x <- (unsnoc -> Just (xs, x)) where
599+
(:>) = snoc
600+
infixl 5 :>
601+
{-# COMPLETE Empty, (:>) #-}
602+
568603
-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
569604
isSingleton :: Text -> Bool
570605
isSingleton = S.isSingleton . stream

src/Data/Text/Internal/Lazy.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ import qualified Data.Text.Internal as T
5050
import qualified Data.Text as T
5151

5252
data Text = Empty
53+
-- ^ Empty text.
5354
| Chunk {-# UNPACK #-} !T.Text Text
55+
-- ^ Chunks must be non-empty, this invariant is not checked.
5456
deriving (Typeable)
5557

5658
-- | Type synonym for the lazy flavour of 'Text'.

src/Data/Text/Lazy.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{-# LANGUAGE Trustworthy #-}
44
{-# LANGUAGE TemplateHaskellQuotes #-}
55
{-# LANGUAGE LambdaCase #-}
6+
{-# LANGUAGE PatternSynonyms #-}
7+
{-# LANGUAGE ViewPatterns #-}
68

79
-- |
810
-- Module : Data.Text.Lazy
@@ -60,6 +62,11 @@ module Data.Text.Lazy
6062
, foldrChunks
6163
, foldlChunks
6264

65+
-- * Pattern matching
66+
, pattern Empty
67+
, pattern (:<)
68+
, pattern (:>)
69+
6370
-- * Basic interface
6471
, cons
6572
, snoc
@@ -533,6 +540,26 @@ null Empty = True
533540
null _ = False
534541
{-# INLINE [1] null #-}
535542

543+
-- | Bidirectional pattern synonym for 'cons' (/O(n)/) and 'uncons' (/O(1)/),
544+
-- to be used together with 'Empty'.
545+
--
546+
-- @since 2.1.2
547+
pattern (:<) :: Char -> Text -> Text
548+
pattern x :< xs <- (uncons -> Just (x, xs)) where
549+
(:<) = cons
550+
infixr 5 :<
551+
{-# COMPLETE Empty, (:<) #-}
552+
553+
-- | Bidirectional pattern synonym for 'snoc' (/O(n)/) and 'unsnoc' (/O(1)/)
554+
-- to be used together with 'Empty'.
555+
--
556+
-- @since 2.1.2
557+
pattern (:>) :: Text -> Char -> Text
558+
pattern xs :> x <- (unsnoc -> Just (xs, x)) where
559+
(:>) = snoc
560+
infixl 5 :>
561+
{-# COMPLETE Empty, (:>) #-}
562+
536563
-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
537564
isSingleton :: Text -> Bool
538565
isSingleton = S.isSingleton . stream

0 commit comments

Comments
 (0)