Skip to content

Commit fc86f1c

Browse files
dolioBodigrim
authored andcommitted
Avoid calling length on chunks in lazy splitAt
This instead uses the `measureOff` function used in the strict `splitAt` to count only as many characters as are needed.
1 parent 1be3f68 commit fc86f1c

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/Data/Text/Lazy.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,12 +1306,18 @@ splitAt = loop
13061306
loop :: Int64 -> Text -> (Text, Text)
13071307
loop !_ Empty = (empty, empty)
13081308
loop n t | n <= 0 = (empty, t)
1309-
loop n (Chunk t ts)
1310-
| n < len = let (t',t'') = T.splitAt (int64ToInt n) t
1311-
in (Chunk t' Empty, Chunk t'' ts)
1312-
| otherwise = let (ts',ts'') = loop (n - len) ts
1309+
loop n (Chunk t@(T.Text arr off len) ts)
1310+
| m > 0, m >= len = (Chunk t Empty, ts)
1311+
| m > 0 = let t' = T.Text arr off m
1312+
t'' = T.Text arr (off+m) (len-m)
1313+
in (Chunk t' Empty, Chunk t'' ts)
1314+
| otherwise = let (ts', ts'') = loop (n + intToInt64 m) ts
13131315
in (Chunk t ts', ts'')
1314-
where len = intToInt64 (T.length t)
1316+
where
1317+
k | n > intToInt64 len = len+1
1318+
| otherwise = int64ToInt n
1319+
m = T.measureOff k t
1320+
13151321

13161322
-- | /O(n)/ 'splitAtWord' @n t@ returns a strict pair whose first
13171323
-- element is a prefix of @t@ whose chunks contain @n@ 'Word8'

0 commit comments

Comments
 (0)