@@ -110,6 +110,7 @@ module Data.Text
111 -- ** Breaking into lines and words
111 -- ** Breaking into lines and words
114 , unlines
115 , unlines
115 , unwords
116 , unwords
@@ -143,7 +144,7 @@ module Data.Text
145import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
146import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
146− Eq, (==), (++),
147+ Eq(..), (++),
147 Read(..), Show(..),
148 Read(..), Show(..),
148 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
149 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
149 not, return, otherwise)
150 not, return, otherwise)
@@ -904,16 +905,34 @@ words t@(Text arr off len) = loop 0 0
905-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
906-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
906-- newline 'Char's. The resulting strings do not contain newlines.
907-- newline 'Char's. The resulting strings do not contain newlines.
908lines :: Text -> [Text]
908lines :: Text -> [Text]
909−lines ps
909+lines ps | null ps = []
911− | otherwise = case search ps of
910+ | otherwise = h : if null t
912− Nothing -> [ps]
911+ then []
913− Just n -> take n ps : lines (drop (n+1) ps)
912+ else lines (unsafeTail t)
914− where search = elemIndex '\n'
913+ where (h,t) = span (/= '\n') ps
915{-# INLINE lines #-}
914{-# INLINE lines #-}
916+-- | /O(n)/ Portably breaks a 'Text' up into a list of 'Text's at line
919+-- A line boundary is considered to be either a line feed, a carriage
920+-- return immediately followed by a line feed, or a carriage return.
921+-- This accounts for both Unix and Windows line ending conventions,
922+-- and for the old convention used on Mac OS 9 and earlier.
923+lines' :: Text -> [Text]
924+lines' ps | null ps = []
925+ | otherwise = h : case uncons t of
928+ | c == '\n' -> lines t'
929+ | c == '\r' -> case uncons t' of
930+ Just ('\n',t'') -> lines t''
932+ where (h,t) = span notEOL ps
933+ notEOL c = c /= '\n' && c /= '\r'
934+{-# INLINE lines' #-}
917-- | /O(n)/ Joins lines, after appending a terminating newline to
936-- | /O(n)/ Joins lines, after appending a terminating newline to
919unlines :: [Text] -> Text
938unlines :: [Text] -> Text