on master help sign in
Rename break and span back to list-like names
3b257dfBryan O'Sullivan
Data/Text.hs+27−26
@@ -117,10 +117,10 @@ module Data.Text
117 , stripStart
117 , stripStart
118 , stripEnd
118 , stripEnd
119 , splitAt
119 , splitAt
120− , spanBy
120+ , breakOn
121+ , breakOnEnd
121 , break
122 , break
122− , breakEnd
123+ , span
123− , breakBy
124 , group
124 , group
125 , groupBy
125 , groupBy
126 , inits
126 , inits
@@ -1009,23 +1009,23 @@ splitAt n t@(Text arr off len)
1009 where d = iter_ t i
1009 where d = iter_ t i
1010{-# INLINE splitAt #-}
1010{-# INLINE splitAt #-}
1011
1011
1012−-- | /O(n)/ 'spanBy', applied to a predicate @p@ and text @t@, returns
1012+-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1013-- a pair whose first element is the longest prefix (possibly empty)
1013-- a pair whose first element is the longest prefix (possibly empty)
1014-- of @t@ of elements that satisfy @p@, and whose second is the
1014-- of @t@ of elements that satisfy @p@, and whose second is the
1015-- remainder of the list.
1015-- remainder of the list.
1016−spanBy :: (Char -> Bool) -> Text -> (Text, Text)
1016+span :: (Char -> Bool) -> Text -> (Text, Text)
1017−spanBy p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
1017+span p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
1018 where k = loop 0
1018 where k = loop 0
1019 loop !i | i >= len || not (p c) = i
1019 loop !i | i >= len || not (p c) = i
1020 | otherwise = loop (i+d)
1020 | otherwise = loop (i+d)
1021 where Iter c d = iter t i
1021 where Iter c d = iter t i
1022−{-# INLINE spanBy #-}
1022+{-# INLINE span #-}
1023
1023
1024−-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is
1024+-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1025-- over elements that fail the predicate @p@.
1025-- over elements that fail the predicate @p@.
1026−breakBy :: (Char -> Bool) -> Text -> (Text, Text)
1026+break :: (Char -> Bool) -> Text -> (Text, Text)
1027−breakBy p = spanBy (not . p)
1027+break p = span (not . p)
1028−{-# INLINE breakBy #-}
1028+{-# INLINE break #-}
1029
1029
1030-- | /O(n)/ Group characters in a string according to a predicate.
1030-- | /O(n)/ Group characters in a string according to a predicate.
1031groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1031groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
@@ -1113,7 +1113,7 @@ splitBy _ t@(Text _off _arr 0) = [t]
1113splitBy p t = loop t
1113splitBy p t = loop t
1114 where loop s | null s' = [l]
1114 where loop s | null s' = [l]
1115 | otherwise = l : loop (unsafeTail s')
1115 | otherwise = l : loop (unsafeTail s')
1116− where (l, s') = breakBy p s
1116+ where (l, s') = break p s
1117{-# INLINE splitBy #-}
1117{-# INLINE splitBy #-}
1118
1118
1119-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1119-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
@@ -1166,13 +1166,13 @@ filter p t = unstream (S.filter p (stream t))
1166--
1166--
1167-- Examples:
1167-- Examples:
1168--
1168--
1169−-- > break "::" "a::b::c" ==> ("a", "::b::c")
1169+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1170−-- > break "/" "foobar" ==> ("foobar", "")
1170+-- > breakOn "/" "foobar" ==> ("foobar", "")
1171--
1171--
1172-- Laws:
1172-- Laws:
1173--
1173--
1174-- > append prefix match == haystack
1174-- > append prefix match == haystack
1175−-- > where (prefix, match) = break needle haystack
1175+-- > where (prefix, match) = breakOn needle haystack
1176--
1176--
1177-- If you need to break a string by a substring repeatedly (e.g. you
1177-- If you need to break a string by a substring repeatedly (e.g. you
1178-- want to break on every instance of a substring), use 'find'
1178-- want to break on every instance of a substring), use 'find'
@@ -1180,25 +1180,26 @@ filter p t = unstream (S.filter p (stream t))
1180--
1180--
1181-- In (unlikely) bad cases, this function's time complexity degrades
1181-- In (unlikely) bad cases, this function's time complexity degrades
1182-- towards /O(n*m)/.
1182-- towards /O(n*m)/.
1183−break :: Text -> Text -> (Text, Text)
1183+breakOn :: Text -> Text -> (Text, Text)
1184−break pat src@(Text arr off len)
1184+breakOn pat src@(Text arr off len)
1185− | null pat = emptyError "break"
1185+ | null pat = emptyError "breakOn"
1186 | otherwise = case indices pat src of
1186 | otherwise = case indices pat src of
1187 [] -> (src, empty)
1187 [] -> (src, empty)
1188 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1188 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1189−{-# INLINE break #-}
1189+{-# INLINE breakOn #-}
1190
1190
1191−-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
1191+-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
1192+-- string.
1192--
1193--
1193-- The first element of the returned tuple is the prefix of @haystack@
1194-- The first element of the returned tuple is the prefix of @haystack@
1194-- up to and including the last match of @needle@. The second is the
1195-- up to and including the last match of @needle@. The second is the
1195-- remainder of @haystack@, following the match.
1196-- remainder of @haystack@, following the match.
1196--
1197--
1197−-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
1198+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1198−breakEnd :: Text -> Text -> (Text, Text)
1199+breakOnEnd :: Text -> Text -> (Text, Text)
1200+breakOnEnd pat src = (reverse b, reverse a)
1199−breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
1201+ where (a,b) = breakOn (reverse pat) (reverse src)
1200− in (reverse b, reverse a)
1201−{-# INLINE breakEnd #-}
1202+{-# INLINE breakOnEnd #-}
1202
1203
1203-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1204-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1204-- @haystack@. Each element of the returned list consists of a pair:
1205-- @haystack@. Each element of the returned list consists of a pair:
@@ -1325,7 +1326,7 @@ lines ps | null ps = []
1325 | otherwise = h : if null t
1326 | otherwise = h : if null t
1326 then []
1327 then []
1327 else lines (unsafeTail t)
1328 else lines (unsafeTail t)
1328− where (h,t) = spanBy (/= '\n') ps
1329+ where (h,t) = span (/= '\n') ps
1329{-# INLINE lines #-}
1330{-# INLINE lines #-}
1330
1331
1331{-
1332{-
Data/Text/Lazy.hs+23−23
@@ -122,10 +122,10 @@ module Data.Text.Lazy
122 , stripStart
122 , stripStart
123 , stripEnd
123 , stripEnd
124 , splitAt
124 , splitAt
125− , spanBy
125+ , span
126+ , breakOn
127+ , breakOnEnd
126 , break
128 , break
127− , breakEnd
128− , breakBy
129 , group
129 , group
130 , groupBy
130 , groupBy
131 , inits
131 , inits
@@ -964,13 +964,13 @@ splitAtWord x (Chunk c@(T.Text arr off len) cs)
964--
964--
965-- Examples:
965-- Examples:
966--
966--
967−-- > break "::" "a::b::c" ==> ("a", "::b::c")
967+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
968−-- > break "/" "foobar" ==> ("foobar", "")
968+-- > breakOn "/" "foobar" ==> ("foobar", "")
969--
969--
970-- Laws:
970-- Laws:
971--
971--
972-- > append prefix match == haystack
972-- > append prefix match == haystack
973−-- > where (prefix, match) = break needle haystack
973+-- > where (prefix, match) = breakOn needle haystack
974--
974--
975-- If you need to break a string by a substring repeatedly (e.g. you
975-- If you need to break a string by a substring repeatedly (e.g. you
976-- want to break on every instance of a substring), use 'find'
976-- want to break on every instance of a substring), use 'find'
@@ -981,25 +981,25 @@ splitAtWord x (Chunk c@(T.Text arr off len) cs)
981--
981--
982-- In (unlikely) bad cases, this function's time complexity degrades
982-- In (unlikely) bad cases, this function's time complexity degrades
983-- towards /O(n*m)/.
983-- towards /O(n*m)/.
984−break :: Text -> Text -> (Text, Text)
984+breakOn :: Text -> Text -> (Text, Text)
985−break pat src
985+breakOn pat src
986− | null pat = emptyError "break"
986+ | null pat = emptyError "breakOn"
987 | otherwise = case indices pat src of
987 | otherwise = case indices pat src of
988 [] -> (src, empty)
988 [] -> (src, empty)
989 (x:_) -> let h :*: t = splitAtWord x src
989 (x:_) -> let h :*: t = splitAtWord x src
990 in (h, t)
990 in (h, t)
991
991
992−-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
992+-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the string.
993--
993--
994-- The first element of the returned tuple is the prefix of @haystack@
994-- The first element of the returned tuple is the prefix of @haystack@
995-- up to and including the last match of @needle@. The second is the
995-- up to and including the last match of @needle@. The second is the
996-- remainder of @haystack@, following the match.
996-- remainder of @haystack@, following the match.
997--
997--
998−-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
998+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
999−breakEnd :: Text -> Text -> (Text, Text)
999+breakOnEnd :: Text -> Text -> (Text, Text)
1000−breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
1000+breakOnEnd pat src = let (a,b) = breakOn (reverse pat) (reverse src)
1001 in (reverse b, reverse a)
1001 in (reverse b, reverse a)
1002−{-# INLINE breakEnd #-}
1002+{-# INLINE breakOnEnd #-}
1003
1003
1004-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1004-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1005-- @haystack@. Each element of the returned list consists of a pair:
1005-- @haystack@. Each element of the returned list consists of a pair:
@@ -1034,10 +1034,10 @@ find pat src
1034 in (h',t) : go x h' t xs
1034 in (h',t) : go x h' t xs
1035 go _ _ _ _ = []
1035 go _ _ _ _ = []
1036
1036
1037−-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is over
1037+-- | /O(n)/ 'break' is like 'span', but the prefix returned is over
1038-- elements that fail the predicate @p@.
1038-- elements that fail the predicate @p@.
1039−breakBy :: (Char -> Bool) -> Text -> (Text, Text)
1039+break :: (Char -> Bool) -> Text -> (Text, Text)
1040−breakBy p t0 = break' t0
1040+break p t0 = break' t0
1041 where break' Empty = (empty, empty)
1041 where break' Empty = (empty, empty)
1042 break' c@(Chunk t ts) =
1042 break' c@(Chunk t ts) =
1043 case T.findIndex p t of
1043 case T.findIndex p t of
@@ -1047,13 +1047,13 @@ breakBy p t0 = break' t0
1047 | otherwise -> let (a,b) = T.splitAt n t
1047 | otherwise -> let (a,b) = T.splitAt n t
1048 in (Chunk a Empty, Chunk b ts)
1048 in (Chunk a Empty, Chunk b ts)
1049
1049
1050−-- | /O(n)/ 'spanBy', applied to a predicate @p@ and text @t@, returns
1050+-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1051-- a pair whose first element is the longest prefix (possibly empty)
1051-- a pair whose first element is the longest prefix (possibly empty)
1052-- of @t@ of elements that satisfy @p@, and whose second is the
1052-- of @t@ of elements that satisfy @p@, and whose second is the
1053-- remainder of the list.
1053-- remainder of the list.
1054−spanBy :: (Char -> Bool) -> Text -> (Text, Text)
1054+span :: (Char -> Bool) -> Text -> (Text, Text)
1055−spanBy p = breakBy (not . p)
1055+span p = break (not . p)
1056−{-# INLINE spanBy #-}
1056+{-# INLINE span #-}
1057
1057
1058-- | The 'group' function takes a 'Text' and returns a list of 'Text's
1058-- | The 'group' function takes a 'Text' and returns a list of 'Text's
1059-- such that the concatenation of the result is equal to the argument.
1059-- such that the concatenation of the result is equal to the argument.
@@ -1072,7 +1072,7 @@ group = groupBy (==)
1072groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1072groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1073groupBy _ Empty = []
1073groupBy _ Empty = []
1074groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
1074groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
1075− where (ys,zs) = spanBy (eq x) xs
1075+ where (ys,zs) = span (eq x) xs
1076 x = T.unsafeHead t
1076 x = T.unsafeHead t
1077 xs = chunk (T.unsafeTail t) ts
1077 xs = chunk (T.unsafeTail t) ts
1078
1078
@@ -1171,7 +1171,7 @@ chunksOf k = go
1171-- newline 'Char's. The resulting strings do not contain newlines.
1171-- newline 'Char's. The resulting strings do not contain newlines.
1172lines :: Text -> [Text]
1172lines :: Text -> [Text]
1173lines Empty = []
1173lines Empty = []
1174−lines t = let (l,t') = breakBy ((==) '\n') t
1174+lines t = let (l,t') = break ((==) '\n') t
1175 in l : if null t' then []
1175 in l : if null t' then []
1176 else lines (tail t')
1176 else lines (tail t')
1177
1177
Data/Text/Lazy/Read.hs+2−2
@@ -46,7 +46,7 @@ decimal :: Integral a => Reader a
46decimal txt
46decimal txt
47 | T.null h = Left "input does not start with a digit"
47 | T.null h = Left "input does not start with a digit"
48 | otherwise = Right (T.foldl' go 0 h, t)
48 | otherwise = Right (T.foldl' go 0 h, t)
49− where (h,t) = T.spanBy isDigit txt
49+ where (h,t) = T.span isDigit txt
50 go n d = (n * 10 + fromIntegral (digitToInt d))
50 go n d = (n * 10 + fromIntegral (digitToInt d))
51
51
52-- | Read a hexadecimal integer, consisting of an optional leading
52-- | Read a hexadecimal integer, consisting of an optional leading
@@ -74,7 +74,7 @@ hex :: Integral a => Reader a
74hex txt
74hex txt
75 | T.null h = Left "input does not start with a hexadecimal digit"
75 | T.null h = Left "input does not start with a hexadecimal digit"
76 | otherwise = Right (T.foldl' go 0 h, t)
76 | otherwise = Right (T.foldl' go 0 h, t)
77− where (h,t) = T.spanBy isHexDigit txt
77+ where (h,t) = T.span isHexDigit txt
78 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
78 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
79
79
80hexDigitToInt :: Char -> Int
80hexDigitToInt :: Char -> Int
Data/Text/Read.hs+2−2
@@ -46,7 +46,7 @@ decimal :: Integral a => Reader a
46decimal txt
46decimal txt
47 | T.null h = Left "input does not start with a digit"
47 | T.null h = Left "input does not start with a digit"
48 | otherwise = Right (T.foldl' go 0 h, t)
48 | otherwise = Right (T.foldl' go 0 h, t)
49− where (h,t) = T.spanBy isDigit txt
49+ where (h,t) = T.span isDigit txt
50 go n d = (n * 10 + fromIntegral (digitToInt d))
50 go n d = (n * 10 + fromIntegral (digitToInt d))
51
51
52-- | Read a hexadecimal integer, consisting of an optional leading
52-- | Read a hexadecimal integer, consisting of an optional leading
@@ -74,7 +74,7 @@ hex :: Integral a => Reader a
74hex txt
74hex txt
75 | T.null h = Left "input does not start with a hexadecimal digit"
75 | T.null h = Left "input does not start with a hexadecimal digit"
76 | otherwise = Right (T.foldl' go 0 h, t)
76 | otherwise = Right (T.foldl' go 0 h, t)
77− where (h,t) = T.spanBy isHexDigit txt
77+ where (h,t) = T.span isHexDigit txt
78 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
78 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
79
79
80hexDigitToInt :: Char -> Int
80hexDigitToInt :: Char -> Int
tests/Properties.hs+20−20
@@ -482,25 +482,25 @@ t_strip = T.dropAround isSpace `eq` T.strip
482tl_strip = TL.dropAround isSpace `eq` TL.strip
482tl_strip = TL.dropAround isSpace `eq` TL.strip
483t_splitAt n = L.splitAt n `eqP` (unpack2 . T.splitAt n)
483t_splitAt n = L.splitAt n `eqP` (unpack2 . T.splitAt n)
484tl_splitAt n = L.splitAt n `eqP` (unpack2 . TL.splitAt (fromIntegral n))
484tl_splitAt n = L.splitAt n `eqP` (unpack2 . TL.splitAt (fromIntegral n))
485−t_spanBy p = L.span p `eqP` (unpack2 . T.spanBy p)
485+t_span p = L.span p `eqP` (unpack2 . T.span p)
486−tl_spanBy p = L.span p `eqP` (unpack2 . TL.spanBy p)
486+tl_span p = L.span p `eqP` (unpack2 . TL.span p)
487
487
488−t_break_id s = squid `eq` (uncurry T.append . T.break s)
488+t_breakOn_id s = squid `eq` (uncurry T.append . T.breakOn s)
489 where squid t | T.null s = error "empty"
489 where squid t | T.null s = error "empty"
490 | otherwise = t
490 | otherwise = t
491−tl_break_id s = squid `eq` (uncurry TL.append . TL.break s)
491+tl_breakOn_id s = squid `eq` (uncurry TL.append . TL.breakOn s)
492 where squid t | TL.null s = error "empty"
492 where squid t | TL.null s = error "empty"
493 | otherwise = t
493 | otherwise = t
494−t_break_start (NotEmpty s) t = let (_,m) = T.break s t
494+t_breakOn_start (NotEmpty s) t = let (_,m) = T.breakOn s t
495 in T.null m || s `T.isPrefixOf` m
495 in T.null m || s `T.isPrefixOf` m
496−tl_break_start (NotEmpty s) t = let (_,m) = TL.break s t
496+tl_breakOn_start (NotEmpty s) t = let (_,m) = TL.breakOn s t
497 in TL.null m || s `TL.isPrefixOf` m
497 in TL.null m || s `TL.isPrefixOf` m
498−t_breakEnd_end (NotEmpty s) t = let (m,_) = T.breakEnd s t
498+t_breakOnEnd_end (NotEmpty s) t = let (m,_) = T.breakOnEnd s t
499 in T.null m || s `T.isSuffixOf` m
499 in T.null m || s `T.isSuffixOf` m
500−tl_breakEnd_end (NotEmpty s) t = let (m,_) = TL.breakEnd s t
500+tl_breakOnEnd_end (NotEmpty s) t = let (m,_) = TL.breakOnEnd s t
501 in TL.null m || s `TL.isSuffixOf` m
501 in TL.null m || s `TL.isSuffixOf` m
502−t_breakBy p = L.break p `eqP` (unpack2 . T.breakBy p)
502+t_break p = L.break p `eqP` (unpack2 . T.break p)
503−tl_breakBy p = L.break p `eqP` (unpack2 . TL.breakBy p)
503+tl_break p = L.break p `eqP` (unpack2 . TL.break p)
504t_group = L.group `eqP` (map unpackS . T.group)
504t_group = L.group `eqP` (map unpackS . T.group)
505tl_group = L.group `eqP` (map unpackS . TL.group)
505tl_group = L.group `eqP` (map unpackS . TL.group)
506t_groupBy p = L.groupBy p `eqP` (map unpackS . T.groupBy p)
506t_groupBy p = L.groupBy p `eqP` (map unpackS . T.groupBy p)
@@ -1052,16 +1052,16 @@ tests = [
1052 testProperty "tl_strip" tl_strip,
1052 testProperty "tl_strip" tl_strip,
1053 testProperty "t_splitAt" t_splitAt,
1053 testProperty "t_splitAt" t_splitAt,
1054 testProperty "tl_splitAt" tl_splitAt,
1054 testProperty "tl_splitAt" tl_splitAt,
1055− testProperty "t_spanBy" t_spanBy,
1055+ testProperty "t_span" t_span,
1056− testProperty "tl_spanBy" tl_spanBy,
1056+ testProperty "tl_span" tl_span,
1057− testProperty "t_break_id" t_break_id,
1057+ testProperty "t_breakOn_id" t_breakOn_id,
1058− testProperty "tl_break_id" tl_break_id,
1058+ testProperty "tl_breakOn_id" tl_breakOn_id,
1059− testProperty "t_break_start" t_break_start,
1059+ testProperty "t_breakOn_start" t_breakOn_start,
1060− testProperty "tl_break_start" tl_break_start,
1060+ testProperty "tl_breakOn_start" tl_breakOn_start,
1061− testProperty "t_breakEnd_end" t_breakEnd_end,
1061+ testProperty "t_breakOnEnd_end" t_breakOnEnd_end,
1062− testProperty "tl_breakEnd_end" tl_breakEnd_end,
1062+ testProperty "tl_breakOnEnd_end" tl_breakOnEnd_end,
1063− testProperty "t_breakBy" t_breakBy,
1063+ testProperty "t_break" t_break,
1064− testProperty "tl_breakBy" tl_breakBy,
1064+ testProperty "tl_break" tl_break,
1065 testProperty "t_group" t_group,
1065 testProperty "t_group" t_group,
1066 testProperty "tl_group" tl_group,
1066 testProperty "tl_group" tl_group,
1067 testProperty "t_groupBy" t_groupBy,
1067 testProperty "t_groupBy" t_groupBy,