Skip to content

Commit

Permalink
Fix filter/filter rules for Text and lazy Text
Browse files Browse the repository at this point in the history
Predicates should be applied in the right order.
  • Loading branch information
meooow25 authored and Bodigrim committed Feb 14, 2024
1 parent 456783a commit 9c7a352
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,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
]

0 comments on commit 9c7a352

Please sign in to comment.