on master help sign in
Implement lines
1b5eb28Bryan O'Sullivan
Data/Text.hs+15−1
@@ -110,7 +110,7 @@ module Data.Text
110
110
111 -- ** Breaking into lines and words
111 -- ** Breaking into lines and words
112 , words
112 , words
113− -- , lines
113+ , lines
114
114
115 -- * Predicates
115 -- * Predicates
116 -- , isPrefixOf
116 -- , isPrefixOf
@@ -626,6 +626,8 @@ elemIndex c t = S.elemIndex c (stream t)
626zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
626zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
627zipWith f t1 t2 = unstream (S.zipWith f (stream t1) (stream t2))
627zipWith f t1 t2 = unstream (S.zipWith f (stream t1) (stream t2))
628
628
629+-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
630+-- representing white space.
629words :: Text -> [Text]
631words :: Text -> [Text]
630words (Text arr off len) = loop0 off off
632words (Text arr off len) = loop0 off off
631 where
633 where
@@ -641,5 +643,17 @@ words (Text arr off len) = loop0 off off
641 c = arr `A.unsafeIndex` n
643 c = arr `A.unsafeIndex` n
642{-# INLINE words #-}
644{-# INLINE words #-}
643
645
646+-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
647+-- newline 'Char's. The resulting strings do not contain newlines.
648+--
649+lines :: Text -> [Text]
650+lines ps
651+ | null ps = []
652+ | otherwise = case search ps of
653+ Nothing -> [ps]
654+ Just n -> take n ps : lines (drop (n+1) ps)
655+ where search = elemIndex '\n'
656+{-# INLINE lines #-}
657+
644errorEmptyList :: String -> a
658errorEmptyList :: String -> a
645errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
659errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")