on master help sign in
Rename break and span back to list-like names
commit3b257df4ab2b776d3611151d093e8846d59b994d authorBryan O'Sullivan <bos@serpentine.com> dateMon 8 Nov 2010, 13:47 (16 years ago) parentd904602
5 files changed+74−73
Data/Text.hs+27−26 Data/Text/Lazy.hs+23−23 Data/Text/Lazy/Read.hs+2−2 Data/Text/Read.hs+2−2 tests/Properties.hs+20−20
Data/Text.hs+27−26
@@ -117,10 +117,10 @@ module Data.Text
117117 , stripStart
118118 , stripEnd
119119 , splitAt
120− , spanBy
120+ , breakOn
121+ , breakOnEnd
121122 , break
122− , breakEnd
123− , breakBy
123+ , span
124124 , group
125125 , groupBy
126126 , inits
@@ -1009,23 +1009,23 @@ splitAt n t@(Text arr off len)
10091009 where d = iter_ t i
10101010{-# INLINE splitAt #-}
10111011
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
10131013-- a pair whose first element is the longest prefix (possibly empty)
10141014-- of @t@ of elements that satisfy @p@, and whose second is the
10151015-- remainder of the list.
1016−spanBy :: (Char -> Bool) -> Text -> (Text, Text)
1017−spanBy p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
1016+span :: (Char -> Bool) -> Text -> (Text, Text)
1017+span p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
10181018 where k = loop 0
10191019 loop !i | i >= len || not (p c) = i
10201020 | otherwise = loop (i+d)
10211021 where Iter c d = iter t i
1022−{-# INLINE spanBy #-}
1022+{-# INLINE span #-}
10231023
1024−-- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is
1024+-- | /O(n)/ 'break' is like 'span', but the prefix returned is
10251025-- over elements that fail the predicate @p@.
1026−breakBy :: (Char -> Bool) -> Text -> (Text, Text)
1027−breakBy p = spanBy (not . p)
1028−{-# INLINE breakBy #-}
1026+break :: (Char -> Bool) -> Text -> (Text, Text)
1027+break p = span (not . p)
1028+{-# INLINE break #-}
10291029
10301030-- | /O(n)/ Group characters in a string according to a predicate.
10311031groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
@@ -1113,7 +1113,7 @@ splitBy _ t@(Text _off _arr 0) = [t]
11131113splitBy p t = loop t
11141114 where loop s | null s' = [l]
11151115 | otherwise = l : loop (unsafeTail s')
1116− where (l, s') = breakBy p s
1116+ where (l, s') = break p s
11171117{-# INLINE splitBy #-}
11181118
11191119-- | /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))
11661166--
11671167-- Examples:
11681168--
1169−-- > break "::" "a::b::c" ==> ("a", "::b::c")
1170−-- > break "/" "foobar" ==> ("foobar", "")
1169+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1170+-- > breakOn "/" "foobar" ==> ("foobar", "")
11711171--
11721172-- Laws:
11731173--
11741174-- > append prefix match == haystack
1175−-- > where (prefix, match) = break needle haystack
1175+-- > where (prefix, match) = breakOn needle haystack
11761176--
11771177-- If you need to break a string by a substring repeatedly (e.g. you
11781178-- want to break on every instance of a substring), use 'find'
@@ -1180,25 +1180,26 @@ filter p t = unstream (S.filter p (stream t))
11801180--
11811181-- In (unlikely) bad cases, this function's time complexity degrades
11821182-- towards /O(n*m)/.
1183−break :: Text -> Text -> (Text, Text)
1184−break pat src@(Text arr off len)
1185− | null pat = emptyError "break"
1183+breakOn :: Text -> Text -> (Text, Text)
1184+breakOn pat src@(Text arr off len)
1185+ | null pat = emptyError "breakOn"
11861186 | otherwise = case indices pat src of
11871187 [] -> (src, empty)
11881188 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1189−{-# INLINE break #-}
1189+{-# INLINE breakOn #-}
11901190
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.
11921193--
11931194-- The first element of the returned tuple is the prefix of @haystack@
11941195-- up to and including the last match of @needle@. The second is the
11951196-- remainder of @haystack@, following the match.
11961197--
1197−-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
1198−breakEnd :: Text -> Text -> (Text, Text)
1199−breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
1200− in (reverse b, reverse a)
1201−{-# INLINE breakEnd #-}
1198+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1199+breakOnEnd :: Text -> Text -> (Text, Text)
1200+breakOnEnd pat src = (reverse b, reverse a)
1201+ where (a,b) = breakOn (reverse pat) (reverse src)
1202+{-# INLINE breakOnEnd #-}
12021203
12031204-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
12041205-- @haystack@. Each element of the returned list consists of a pair:
@@ -1325,7 +1326,7 @@ lines ps | null ps = []
13251326 | otherwise = h : if null t
13261327 then []
13271328 else lines (unsafeTail t)
1328− where (h,t) = spanBy (/= '\n') ps
1329+ where (h,t) = span (/= '\n') ps
13291330{-# INLINE lines #-}
13301331
13311332{-
Data/Text/Lazy.hs+23−23
@@ -122,10 +122,10 @@ module Data.Text.Lazy
122122 , stripStart
123123 , stripEnd
124124 , splitAt
125− , spanBy
125+ , span
126+ , breakOn
127+ , breakOnEnd
126128 , break
127− , breakEnd
128− , breakBy
129129 , group
130130 , groupBy
131131 , inits
@@ -964,13 +964,13 @@ splitAtWord x (Chunk c@(T.Text arr off len) cs)
964964--
965965-- Examples:
966966--
967−-- > break "::" "a::b::c" ==> ("a", "::b::c")
968−-- > break "/" "foobar" ==> ("foobar", "")
967+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
968+-- > breakOn "/" "foobar" ==> ("foobar", "")
969969--
970970-- Laws:
971971--
972972-- > append prefix match == haystack
973−-- > where (prefix, match) = break needle haystack
973+-- > where (prefix, match) = breakOn needle haystack
974974--
975975-- If you need to break a string by a substring repeatedly (e.g. you
976976-- 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)
981981--
982982-- In (unlikely) bad cases, this function's time complexity degrades
983983-- towards /O(n*m)/.
984−break :: Text -> Text -> (Text, Text)
985−break pat src
986− | null pat = emptyError "break"
984+breakOn :: Text -> Text -> (Text, Text)
985+breakOn pat src
986+ | null pat = emptyError "breakOn"
987987 | otherwise = case indices pat src of
988988 [] -> (src, empty)
989989 (x:_) -> let h :*: t = splitAtWord x src
990990 in (h, t)
991991
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.
993993--
994994-- The first element of the returned tuple is the prefix of @haystack@
995995-- up to and including the last match of @needle@. The second is the
996996-- remainder of @haystack@, following the match.
997997--
998−-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
999−breakEnd :: Text -> Text -> (Text, Text)
1000−breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
998+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
999+breakOnEnd :: Text -> Text -> (Text, Text)
1000+breakOnEnd pat src = let (a,b) = breakOn (reverse pat) (reverse src)
10011001 in (reverse b, reverse a)
1002−{-# INLINE breakEnd #-}
1002+{-# INLINE breakOnEnd #-}
10031003
10041004-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
10051005-- @haystack@. Each element of the returned list consists of a pair:
@@ -1034,10 +1034,10 @@ find pat src
10341034 in (h',t) : go x h' t xs
10351035 go _ _ _ _ = []
10361036
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
10381038-- elements that fail the predicate @p@.
1039−breakBy :: (Char -> Bool) -> Text -> (Text, Text)
1040−breakBy p t0 = break' t0
1039+break :: (Char -> Bool) -> Text -> (Text, Text)
1040+break p t0 = break' t0
10411041 where break' Empty = (empty, empty)
10421042 break' c@(Chunk t ts) =
10431043 case T.findIndex p t of
@@ -1047,13 +1047,13 @@ breakBy p t0 = break' t0
10471047 | otherwise -> let (a,b) = T.splitAt n t
10481048 in (Chunk a Empty, Chunk b ts)
10491049
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
10511051-- a pair whose first element is the longest prefix (possibly empty)
10521052-- of @t@ of elements that satisfy @p@, and whose second is the
10531053-- remainder of the list.
1054−spanBy :: (Char -> Bool) -> Text -> (Text, Text)
1055−spanBy p = breakBy (not . p)
1056−{-# INLINE spanBy #-}
1054+span :: (Char -> Bool) -> Text -> (Text, Text)
1055+span p = break (not . p)
1056+{-# INLINE span #-}
10571057
10581058-- | The 'group' function takes a 'Text' and returns a list of 'Text's
10591059-- such that the concatenation of the result is equal to the argument.
@@ -1072,7 +1072,7 @@ group = groupBy (==)
10721072groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
10731073groupBy _ Empty = []
10741074groupBy 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
10761076 x = T.unsafeHead t
10771077 xs = chunk (T.unsafeTail t) ts
10781078
@@ -1171,7 +1171,7 @@ chunksOf k = go
11711171-- newline 'Char's. The resulting strings do not contain newlines.
11721172lines :: Text -> [Text]
11731173lines Empty = []
1174−lines t = let (l,t') = breakBy ((==) '\n') t
1174+lines t = let (l,t') = break ((==) '\n') t
11751175 in l : if null t' then []
11761176 else lines (tail t')
11771177
Data/Text/Lazy/Read.hs+2−2
@@ -46,7 +46,7 @@ decimal :: Integral a => Reader a
4646decimal txt
4747 | T.null h = Left "input does not start with a digit"
4848 | otherwise = Right (T.foldl' go 0 h, t)
49− where (h,t) = T.spanBy isDigit txt
49+ where (h,t) = T.span isDigit txt
5050 go n d = (n * 10 + fromIntegral (digitToInt d))
5151
5252-- | Read a hexadecimal integer, consisting of an optional leading
@@ -74,7 +74,7 @@ hex :: Integral a => Reader a
7474hex txt
7575 | T.null h = Left "input does not start with a hexadecimal digit"
7676 | otherwise = Right (T.foldl' go 0 h, t)
77− where (h,t) = T.spanBy isHexDigit txt
77+ where (h,t) = T.span isHexDigit txt
7878 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
7979
8080hexDigitToInt :: Char -> Int
Data/Text/Read.hs+2−2
@@ -46,7 +46,7 @@ decimal :: Integral a => Reader a
4646decimal txt
4747 | T.null h = Left "input does not start with a digit"
4848 | otherwise = Right (T.foldl' go 0 h, t)
49− where (h,t) = T.spanBy isDigit txt
49+ where (h,t) = T.span isDigit txt
5050 go n d = (n * 10 + fromIntegral (digitToInt d))
5151
5252-- | Read a hexadecimal integer, consisting of an optional leading
@@ -74,7 +74,7 @@ hex :: Integral a => Reader a
7474hex txt
7575 | T.null h = Left "input does not start with a hexadecimal digit"
7676 | otherwise = Right (T.foldl' go 0 h, t)
77− where (h,t) = T.spanBy isHexDigit txt
77+ where (h,t) = T.span isHexDigit txt
7878 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
7979
8080hexDigitToInt :: Char -> Int
tests/Properties.hs+20−20
@@ -482,25 +482,25 @@ t_strip = T.dropAround isSpace `eq` T.strip
482482tl_strip = TL.dropAround isSpace `eq` TL.strip
483483t_splitAt n = L.splitAt n `eqP` (unpack2 . T.splitAt n)
484484tl_splitAt n = L.splitAt n `eqP` (unpack2 . TL.splitAt (fromIntegral n))
485−t_spanBy p = L.span p `eqP` (unpack2 . T.spanBy p)
486−tl_spanBy p = L.span p `eqP` (unpack2 . TL.spanBy p)
485+t_span p = L.span p `eqP` (unpack2 . T.span p)
486+tl_span p = L.span p `eqP` (unpack2 . TL.span p)
487487
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)
489489 where squid t | T.null s = error "empty"
490490 | 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)
492492 where squid t | TL.null s = error "empty"
493493 | 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
495495 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
497497 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
499499 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
501501 in TL.null m || s `TL.isSuffixOf` m
502−t_breakBy p = L.break p `eqP` (unpack2 . T.breakBy p)
503−tl_breakBy p = L.break p `eqP` (unpack2 . TL.breakBy p)
502+t_break p = L.break p `eqP` (unpack2 . T.break p)
503+tl_break p = L.break p `eqP` (unpack2 . TL.break p)
504504t_group = L.group `eqP` (map unpackS . T.group)
505505tl_group = L.group `eqP` (map unpackS . TL.group)
506506t_groupBy p = L.groupBy p `eqP` (map unpackS . T.groupBy p)
@@ -1052,16 +1052,16 @@ tests = [
10521052 testProperty "tl_strip" tl_strip,
10531053 testProperty "t_splitAt" t_splitAt,
10541054 testProperty "tl_splitAt" tl_splitAt,
1055− testProperty "t_spanBy" t_spanBy,
1056− testProperty "tl_spanBy" tl_spanBy,
1057− testProperty "t_break_id" t_break_id,
1058− testProperty "tl_break_id" tl_break_id,
1059− testProperty "t_break_start" t_break_start,
1060− testProperty "tl_break_start" tl_break_start,
1061− testProperty "t_breakEnd_end" t_breakEnd_end,
1062− testProperty "tl_breakEnd_end" tl_breakEnd_end,
1063− testProperty "t_breakBy" t_breakBy,
1064− testProperty "tl_breakBy" tl_breakBy,
1055+ testProperty "t_span" t_span,
1056+ testProperty "tl_span" tl_span,
1057+ testProperty "t_breakOn_id" t_breakOn_id,
1058+ testProperty "tl_breakOn_id" tl_breakOn_id,
1059+ testProperty "t_breakOn_start" t_breakOn_start,
1060+ testProperty "tl_breakOn_start" tl_breakOn_start,
1061+ testProperty "t_breakOnEnd_end" t_breakOnEnd_end,
1062+ testProperty "tl_breakOnEnd_end" tl_breakOnEnd_end,
1063+ testProperty "t_break" t_break,
1064+ testProperty "tl_break" tl_break,
10651065 testProperty "t_group" t_group,
10661066 testProperty "tl_group" tl_group,
10671067 testProperty "t_groupBy" t_groupBy,