Skip to content

Commit

Permalink
Drop outdated comment (#328)
Browse files Browse the repository at this point in the history
* Drop outdated comment

The Lazy `lines` function has not been `too` strict for some
time now.  The issue was resolved a day or so after the comment
was added, but the comment was inadvertently left behind.

* Add lazy `lines` non-strictness test

Co-authored-by: Viktor Dukhovni <[email protected]>
  • Loading branch information
vdukhovni and hs-viktor authored Nov 26, 2020
1 parent 54133b3 commit 101566e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
24 changes: 0 additions & 24 deletions Data/ByteString/Lazy/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -758,30 +758,6 @@ lines (Chunk c0 cs0) = loop0 c0 cs0
let c' = revChunks (B.unsafeTake n c : line)
in c' `seq` (c' : loop0 (B.unsafeDrop (n+1) c) cs)

{-
This function is too strict! Consider,
> prop_lazy =
(L.unpack . head . lazylines $ L.append (L.pack "a\nb\n") (error "failed"))
==
"a"
fails. Here's a properly lazy version of 'lines' for lazy bytestrings
lazylines :: L.ByteString -> [L.ByteString]
lazylines s
| L.null s = []
| otherwise =
let (l,s') = L.break ((==) '\n') s
in l : if L.null s' then []
else lazylines (L.tail s')
we need a similarly lazy, but efficient version.
-}


-- | 'unlines' is an inverse operation to 'lines'. It joins lines,
-- after appending a terminating newline to each.
unlines :: [ByteString] -> ByteString
Expand Down
7 changes: 7 additions & 0 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,11 @@ prop_unlinesBB (String8 xs) =
prop_unlinesLC (String8 xs) =
(unlines.lines) xs == (LC.unpack. LC.unlines . LC.lines .LC.pack) xs

prop_lines_lazy1 =
head (LC.lines (LC.append (LC.pack "a\nb\n") undefined)) == LC.pack "a"
prop_lines_lazy2 =
head (tail (LC.lines (LC.append (LC.pack "a\nb\n") undefined))) == LC.pack "b"

prop_wordsBB (String8 xs) =
(words xs) == ((map C.unpack) . C.words . C.pack) xs
-- prop_wordstokensBB xs = C.words xs == C.tokens isSpace xs
Expand Down Expand Up @@ -2334,6 +2339,8 @@ bb_tests =
, testProperty "lines" prop_linesBB
, testProperty "unlines" prop_unlinesBB
, testProperty "unlines" prop_unlinesLC
, testProperty "lines_lazy1" prop_lines_lazy1
, testProperty "lines_lazy2" prop_lines_lazy2
, testProperty "words" prop_wordsBB
, testProperty "words" prop_wordsLC
, testProperty "unwords" prop_unwordsBB
Expand Down

0 comments on commit 101566e

Please sign in to comment.