on master help sign in
Move breakSubstring into Compat module.
c7b94a9Bryan O'Sullivan
Data/Text.hs−33
@@ -118,7 +118,6 @@ module Data.Text
118 , splitTimes
118 , splitTimes
119 , splitChar
119 , splitChar
120 , splitWith
120 , splitWith
121− , breakSubstring
122
121
123 -- ** Breaking into lines and words
122 -- ** Breaking into lines and words
124 , lines
123 , lines
@@ -941,38 +940,6 @@ partition :: (Char -> Bool) -> Text -> (Text, Text)
941partition p t = (filter p t, filter (not . p) t)
940partition p t = (filter p t, filter (not . p) t)
942{-# INLINE partition #-}
941{-# INLINE partition #-}
943
942
944−-- | /O(n)/ Break a string on a substring, returning a pair of the
945−-- part of the string prior to the match, and the rest of the string.
946−--
947−-- The following relationship holds:
948−--
949−-- > break (==c) l == breakSubstring (singleton c) l
950−--
951−-- For example, to tokenise a string, dropping delimiters:
952−--
953−-- > tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)
954−-- > where (h,t) = breakSubstring x y
955−--
956−-- To skip to the first occurence of a string:
957−--
958−-- > snd (breakSubstring x y)
959−--
960−-- To take the parts of a string before a delimiter:
961−--
962−-- > fst (breakSubstring x y)
963−--
964−breakSubstring :: Text -- ^ String to search for
965− -> Text -- ^ String to search in
966− -> (Text,Text) -- ^ Head and tail of string broken at substring
967−
968−breakSubstring pat src = search 0 src
969− where
970− search !n !s
971− | null s = (src,empty) -- not found
972− | pat `isPrefixOf` s = (take n src,s)
973− | otherwise = search (n+1) (unsafeTail s)
974−{-# INLINE breakSubstring #-}
975−
976-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
943-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
977-- returns a 'Text' containing those characters that satisfy the
944-- returns a 'Text' containing those characters that satisfy the
978-- predicate.
945-- predicate.
Data/Text/Compat.hs+35−1
@@ -1,3 +1,4 @@
1+{-# LANGUAGE BangPatterns #-}
1{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2-- |
3-- |
3-- Module : Data.Text.Compat
4-- Module : Data.Text.Compat
@@ -144,8 +145,41 @@ module Data.Text.Compat
144
145
145import Data.ByteString (ByteString)
146import Data.ByteString (ByteString)
146import Data.Text hiding (split)
147import Data.Text hiding (split)
148+import Data.Text.Unsafe (unsafeTail)
147−import Prelude (Char)
149+import Prelude (Char, (+), otherwise)
148
150
149split :: Char -> Text -> [Text]
151split :: Char -> Text -> [Text]
150split = splitChar
152split = splitChar
151{-# INLINE split #-}
153{-# INLINE split #-}
154+
155+-- | /O(n)/ Break a string on a substring, returning a pair of the
156+-- part of the string prior to the match, and the rest of the string.
157+--
158+-- The following relationship holds:
159+--
160+-- > break (==c) l == breakSubstring (singleton c) l
161+--
162+-- For example, to tokenise a string, dropping delimiters:
163+--
164+-- > tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)
165+-- > where (h,t) = breakSubstring x y
166+--
167+-- To skip to the first occurence of a string:
168+--
169+-- > snd (breakSubstring x y)
170+--
171+-- To take the parts of a string before a delimiter:
172+--
173+-- > fst (breakSubstring x y)
174+--
175+breakSubstring :: Text -- ^ String to search for
176+ -> Text -- ^ String to search in
177+ -> (Text,Text) -- ^ Head and tail of string broken at substring
178+
179+breakSubstring pat src = search 0 src
180+ where
181+ search !n !s
182+ | null s = (src,empty) -- not found
183+ | pat `isPrefixOf` s = (take n src,s)
184+ | otherwise = search (n+1) (unsafeTail s)
185+{-# INLINE breakSubstring #-}