Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filter/filter rules for Text and lazy Text #560

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,7 +1686,7 @@ filter p = filter_ text p

{-# RULES
"TEXT filter/filter -> filter" forall p q t.
filter p (filter q t) = filter (\c -> p c && q c) t
filter p (filter q t) = filter (\c -> q c && p c) t
#-}

-- | /O(n+m)/ Find the first instance of @needle@ (which must be
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Text/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ filter p = foldrChunks (chunk . filter_ T.Text p) Empty

{-# RULES
"TEXT filter/filter -> filter" forall p q t.
filter p (filter q t) = filter (\c -> p c && q c) t
filter p (filter q t) = filter (\c -> q c && p c) t
#-}

-- | /O(n)/ The 'find' function takes a predicate and a 'Text', and
Expand Down
8 changes: 8 additions & 0 deletions tests/Tests/Regressions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ t529 = do
(T.pack (chr 33 : replicate 31 (chr 0) ++ [chr 65533, chr 0]))
(decode (B.pack (33 : replicate 31 0 ++ [128, 0])))

-- See Github #559
-- filter/filter fusion rules should apply predicates in the right order.
t559 :: IO ()
t559 = do
T.filter undefined (T.filter (const False) "a") @?= ""
LT.filter undefined (LT.filter (const False) "a") @?= ""

tests :: F.TestTree
tests = F.testGroup "Regressions"
[ F.testCase "hGetContents_crash" hGetContents_crash
Expand All @@ -193,4 +200,5 @@ tests = F.testGroup "Regressions"
, F.testCase "t525" t525
, F.testCase "t528" t528
, F.testCase "t529" t529
, F.testCase "t559" t559
]
Loading