Commits

Bryan O'Sullivan committed dab2cef

Start improving the API, and move the list-like API to new .Compat module.

  • Participants
  • Parent commits 9c6d123

Comments (0)

Files changed (3)

 
     -- ** Breaking into many substrings
     , split
+    , splitChar
     , splitWith
     , breakSubstring
 
 -- | /O(n)/ Break a 'Text' into pieces separated by the 'Char'
 -- argument, consuming the delimiter. I.e.
 --
--- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
--- > split 'a'  "aXaXaXa"    == ["","X","X","X",""]
--- > split 'x'  "x"          == ["",""]
+-- > splitChar '\n' "a\nb\nd\ne" == ["a","b","d","e"]
+-- > splitChar 'a'  "aXaXaXa"    == ["","X","X","X",""]
+-- > splitChar 'x'  "x"          == ["",""]
 -- 
 -- and
 --
--- > intercalate (singleton c) . split c == id
--- > split == splitWith . (==)
+-- > intercalate (singleton c) . splitChar c == id
+-- > splitChar == splitWith . (==)
 -- 
 -- As for all splitting functions in this library, this function does
 -- not copy the substrings, it just constructs new 'Text's that are
 -- slices of the original.
-split :: Char -> Text -> [Text]
-split c = splitWith (==c)
+splitChar :: Char -> Text -> [Text]
+splitChar c = splitWith (==c)
+{-# INLINE splitChar #-}
+
+-- | /O(m)*O(n)/ Break a 'Text' into pieces separated by the 'Text'
+-- argument, consuming the delimiter. I.e.
+--
+-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
+-- > split "aaa"  "aaaXaaaXaaaXaaa"  == ["","X","X","X",""]
+-- > split "x"    "x"                == ["",""]
+-- 
+-- and
+--
+-- > intercalate s . split s == id
+-- 
+-- As for all splitting functions in this library, this function does
+-- not copy the substrings, it just constructs new 'Text's that are
+-- slices of the original.
+split :: Text -> Text -> [Text]
+split pat = go
+  where
+    go src = search 0 src
+      where
+        search !n !s
+            | null s             = [src]      -- not found
+            | pat `isPrefixOf` s = take n src : go (drop l s)
+            | otherwise          = search (n+1) (unsafeTail s)
+        l = length pat
 {-# INLINE split #-}
 
 -- | /O(n)/ Splits a 'Text' into components delimited by separators,

Data/Text/Compat.hs

+{-# OPTIONS_GHC -fno-warn-unused-imports #-}
+-- |
+-- Module      : Data.Text.Compat
+-- Copyright   : (c) Bryan O'Sullivan 2009
+--
+-- License     : BSD-style
+-- Maintainer  : bos@serpentine.com, rtharper@aftereternity.co.uk,
+--               duncan@haskell.org
+-- Stability   : experimental
+-- Portability : GHC
+--
+-- A text API for which function names closely resemble those of the
+-- list and 'ByteString' types.
+--
+-- This module is intended to be imported @qualified@, to avoid name
+-- clashes with "Prelude" functions, e.g.
+--
+-- > import qualified Data.Text.Compat as T
+
+module Data.Text.Compat
+    (
+    -- * Types
+      Text
+
+    -- * Creation and elimination
+    , pack
+    , unpack
+    , singleton
+    , empty
+
+    -- * Basic interface
+    , cons
+    , snoc
+    , append
+    , uncons
+    , head
+    , last
+    , tail
+    , init
+    , null
+    , length
+
+    -- * Transformations
+    , map
+    , intercalate
+    , intersperse
+    , transpose
+    , reverse
+
+    -- * Case conversion
+    -- $case
+    , toCaseFold
+    , toLower
+    , toUpper
+
+    -- * Folds
+    , foldl
+    , foldl'
+    , foldl1
+    , foldl1'
+    , foldr
+    , foldr1
+
+    -- ** Special folds
+    , concat
+    , concatMap
+    , any
+    , all
+    , maximum
+    , minimum
+
+    -- * Construction
+
+    -- ** Scans
+    , scanl
+    , scanl1
+    , scanr
+    , scanr1
+
+    -- ** Accumulating maps
+    , mapAccumL
+    , mapAccumR
+
+    -- ** Generation and unfolding
+    , replicate
+    , unfoldr
+    , unfoldrN
+
+    -- * Substrings
+
+    -- ** Breaking strings
+    , take
+    , drop
+    , takeWhile
+    , dropWhile
+    , splitAt
+    , span
+    , break
+    , group
+    , groupBy
+    , inits
+    , tails
+
+    -- ** Breaking into many substrings
+    , split
+    , splitWith
+    , breakSubstring
+
+    -- ** Breaking into lines and words
+    , lines
+    --, lines'
+    , words
+    , unlines
+    , unwords
+
+    -- * Predicates
+    , isPrefixOf
+    , isSuffixOf
+    , isInfixOf
+
+    -- * Searching
+    , elem
+    , filter
+    , find
+    , partition
+
+    -- , findSubstring
+    
+    -- * Indexing
+    , index
+    , findIndex
+    , findIndices
+    , elemIndex
+    , elemIndices
+    , count
+
+    -- * Zipping and unzipping
+    , zip
+    , zipWith
+
+    -- -* Ordered text
+    -- , sort
+    ) where
+
+import Data.ByteString (ByteString)
+import Data.Text hiding (split)
+import Prelude (Char)
+
+split :: Char -> Text -> [Text]
+split = splitChar
+{-# INLINE split #-}
 library
   exposed-modules:
     Data.Text
+    Data.Text.Compat
     Data.Text.Encoding
     Data.Text.Encoding.Error
     Data.Text.Encoding.Fusion