Skip to content

Commit 7d3130b

Browse files
authored
Merge pull request #301 from channable/feat-unfuse-take-drop
Add rewrite rule to unfuse (text . drop)
2 parents e07c149 + 8c0a8da commit 7d3130b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Data/Text.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ drop n t@(Text arr off len)
12241224
drop n t = unstream (S.drop n (stream t))
12251225
"TEXT drop -> unfused" [1] forall n t.
12261226
unstream (S.drop n (stream t)) = drop n t
1227+
"TEXT take . drop -> unfused" [1] forall len off t.
1228+
unstream (S.take len (S.drop off (stream t))) = take len (drop off t)
12271229
#-}
12281230

12291231
-- | /O(n)/ 'dropEnd' @n@ @t@ returns the prefix remaining after

tests/Tests/Regressions.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import qualified Data.ByteString as B
1414
import Data.ByteString.Char8 ()
1515
import qualified Data.ByteString.Lazy as LB
1616
import qualified Data.Text as T
17+
import qualified Data.Text.Array as TA
1718
import qualified Data.Text.Encoding as TE
19+
import qualified Data.Text.Internal as T
1820
import qualified Data.Text.IO as T
1921
import qualified Data.Text.Lazy as LT
2022
import qualified Data.Text.Lazy.Encoding as LE
@@ -95,6 +97,24 @@ t227 =
9597
(T.length $ T.filter isLetter $ T.take (-3) "Hello! How are you doing today?")
9698
0
9799

100+
-- See GitHub issue #301
101+
-- This tests whether the "TEXT take . drop -> unfused" rule is applied to the
102+
-- slice function. When the slice function is fused, a new array will be
103+
-- constructed that is shorter than the original array. Without fusion the
104+
-- array remains unmodified.
105+
t301 :: IO ()
106+
t301 = do
107+
assertEqual "The length of the array remains the same despite slicing"
108+
(TA.length originalArr)
109+
(TA.length newArr)
110+
111+
assertEqual "The new array still contains the original value"
112+
(T.Text newArr originalOff originalLen)
113+
original
114+
where
115+
original@(T.Text originalArr originalOff originalLen) = T.pack "1234567890"
116+
T.Text newArr _off _len = T.take 1 $ T.drop 1 original
117+
98118
tests :: F.Test
99119
tests = F.testGroup "Regressions"
100120
[ F.testCase "hGetContents_crash" hGetContents_crash
@@ -105,4 +125,5 @@ tests = F.testGroup "Regressions"
105125
, F.testCase "t197" t197
106126
, F.testCase "t221" t221
107127
, F.testCase "t227" t227
128+
, F.testCase "t301" t301
108129
]

0 commit comments

Comments
 (0)