on master help sign in
Move breakSubstring into Compat module.

--HG-- extra : convert_revision : 4e3dadbee683f8925eed464ac8fee1dd1670cdf5

commitc7b94a9a8b32d8c782f794a9fbdcef7c526b3e3d authorBryan O'Sullivan <bos@serpentine.com> dateSun 5 Jul 2009, 06:15 (17 years ago) parentf584cbc
2 files changed+35−34
Data/Text.hs−33 Data/Text/Compat.hs+35−1
Data/Text.hs−33
@@ -118,7 +118,6 @@ module Data.Text
118118 , splitTimes
119119 , splitChar
120120 , splitWith
121− , breakSubstring
122121
123122 -- ** Breaking into lines and words
124123 , lines
@@ -941,38 +940,6 @@ partition :: (Char -> Bool) -> Text -> (Text, Text)
941940partition p t = (filter p t, filter (not . p) t)
942941{-# INLINE partition #-}
943942
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−
976943-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
977944-- returns a 'Text' containing those characters that satisfy the
978945-- predicate.
Data/Text/Compat.hs+35−1
@@ -1,3 +1,4 @@
1+{-# LANGUAGE BangPatterns #-}
12{-# OPTIONS_GHC -fno-warn-unused-imports #-}
23-- |
34-- Module : Data.Text.Compat
@@ -144,8 +145,41 @@ module Data.Text.Compat
144145
145146import Data.ByteString (ByteString)
146147import Data.Text hiding (split)
147−import Prelude (Char)
148+import Data.Text.Unsafe (unsafeTail)
149+import Prelude (Char, (+), otherwise)
148150
149151split :: Char -> Text -> [Text]
150152split = splitChar
151153{-# 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 #-}