Commits

Bryan O'Sullivan committed 86ef261

Move breakSubstring into Compat module.

  • Participants
  • Parent commits fd8b79c

Comments (0)

Files changed (2)

File Data/Text.hs

     , splitTimes
     , splitChar
     , splitWith
-    , breakSubstring
 
     -- ** Breaking into lines and words
     , lines
 partition p t = (filter p t, filter (not . p) t)
 {-# INLINE partition #-}
 
--- | /O(n)/ Break a string on a substring, returning a pair of the
--- part of the string prior to the match, and the rest of the string.
---
--- The following relationship holds:
---
--- > break (==c) l == breakSubstring (singleton c) l
---
--- For example, to tokenise a string, dropping delimiters:
---
--- > tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)
--- >     where (h,t) = breakSubstring x y
---
--- To skip to the first occurence of a string:
---
--- > snd (breakSubstring x y)
---
--- To take the parts of a string before a delimiter:
---
--- > fst (breakSubstring x y)
---
-breakSubstring :: Text -- ^ String to search for
-               -> Text -- ^ String to search in
-               -> (Text,Text) -- ^ Head and tail of string broken at substring
-
-breakSubstring pat src = search 0 src
-  where
-    search !n !s
-        | null s             = (src,empty)      -- not found
-        | pat `isPrefixOf` s = (take n src,s)
-        | otherwise          = search (n+1) (unsafeTail s)
-{-# INLINE breakSubstring #-}
-
 -- | /O(n)/ 'filter', applied to a predicate and a 'Text',
 -- returns a 'Text' containing those characters that satisfy the
 -- predicate.

File Data/Text/Compat.hs

+{-# LANGUAGE BangPatterns #-}
 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
 -- |
 -- Module      : Data.Text.Compat
 
 import Data.ByteString (ByteString)
 import Data.Text hiding (split)
-import Prelude (Char)
+import Data.Text.Unsafe (unsafeTail)
+import Prelude (Char, (+), otherwise)
 
 split :: Char -> Text -> [Text]
 split = splitChar
 {-# INLINE split #-}
+
+-- | /O(n)/ Break a string on a substring, returning a pair of the
+-- part of the string prior to the match, and the rest of the string.
+--
+-- The following relationship holds:
+--
+-- > break (==c) l == breakSubstring (singleton c) l
+--
+-- For example, to tokenise a string, dropping delimiters:
+--
+-- > tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)
+-- >     where (h,t) = breakSubstring x y
+--
+-- To skip to the first occurence of a string:
+--
+-- > snd (breakSubstring x y)
+--
+-- To take the parts of a string before a delimiter:
+--
+-- > fst (breakSubstring x y)
+--
+breakSubstring :: Text -- ^ String to search for
+               -> Text -- ^ String to search in
+               -> (Text,Text) -- ^ Head and tail of string broken at substring
+
+breakSubstring pat src = search 0 src
+  where
+    search !n !s
+        | null s             = (src,empty)      -- not found
+        | pat `isPrefixOf` s = (take n src,s)
+        | otherwise          = search (n+1) (unsafeTail s)
+{-# INLINE breakSubstring #-}