Skip to content

Commit

Permalink
Added Text.PortableLines.ByteString.Lazy
Browse files Browse the repository at this point in the history
I didn't implement a super-efficient algorithm, but copied the algorithm used in Text.PortableLines.ByteString.
  • Loading branch information
joeyadams committed Nov 14, 2011
1 parent 4d51027 commit c835dd5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Text/PortableLines/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Text.PortableLines.ByteString.Lazy
( lines8
) where

import Prelude as P hiding (lines)

import Data.ByteString.Lazy (ByteString)

import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as L8

-- | Like the 'L8.lines' function from Data.ByteString.Lazy.Char8, but treat the
-- @\"\\r\\n\"@ and @\"\\r\"@ sequences as newlines too, not just @\"\\n\"@.
--
-- Input is assumed to be in ASCII or an ASCII-compatible encoding (at least
-- with respect to newline characters). For example, UTF-8 is fine, but UTF-16
-- is not.
lines8 :: ByteString -> [ByteString]
lines8 str | L.null str = []
| otherwise = let (line, rest) = breakNewline8 str
in line : lines8 rest

breakNewline8 :: ByteString -> (ByteString, ByteString)
breakNewline8 str =
case L.break (\c -> c == 13 || c == 10) str of
(line, rest) | L.null rest -> (line, rest)
| L.length rest >= 2 &&
rest `L.index` 0 == 13 &&
rest `L.index` 1 == 10 -> (line, L.drop 2 rest)
| otherwise -> (line, L.tail rest)
1 change: 1 addition & 0 deletions portable-lines.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cabal-version: >=1.8
library
exposed-modules: Text.PortableLines
, Text.PortableLines.ByteString
, Text.PortableLines.ByteString.Lazy
build-depends: base == 4.*
, bytestring
ghc-options: -Wall -O2

0 comments on commit c835dd5

Please sign in to comment.