on master help sign in
Start improving the API, and move the list-like API to new .Compat module.

--HG-- extra : convert_revision : 613d06e8dc0f7b73d48edb6a33884be4073c5f41

commit9e6d807ae1f2c4fd141e70bad94b1af80233284c authorBryan O'Sullivan <bos@serpentine.com> dateSat 4 Jul 2009, 05:04 (17 years ago) parent1689fcb
3 files changed+186−7
Data/Text.hs+34−7 Data/Text/Compat.hs+151 text.cabal+1
Data/Text.hs+34−7
@@ -111,6 +111,7 @@ module Data.Text
111111
112112 -- ** Breaking into many substrings
113113 , split
114+ , splitChar
114115 , splitWith
115116 , breakSubstring
116117
@@ -770,20 +771,46 @@ tails t | null t = [empty]
770771-- | /O(n)/ Break a 'Text' into pieces separated by the 'Char'
771772-- argument, consuming the delimiter. I.e.
772773--
773−-- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
774−-- > split 'a' "aXaXaXa" == ["","X","X","X",""]
775−-- > split 'x' "x" == ["",""]
774+-- > splitChar '\n' "a\nb\nd\ne" == ["a","b","d","e"]
775+-- > splitChar 'a' "aXaXaXa" == ["","X","X","X",""]
776+-- > splitChar 'x' "x" == ["",""]
776777--
777778-- and
778779--
779−-- > intercalate (singleton c) . split c == id
780−-- > split == splitWith . (==)
780+-- > intercalate (singleton c) . splitChar c == id
781+-- > splitChar == splitWith . (==)
781782--
782783-- As for all splitting functions in this library, this function does
783784-- not copy the substrings, it just constructs new 'Text's that are
784785-- slices of the original.
785−split :: Char -> Text -> [Text]
786−split c = splitWith (==c)
786+splitChar :: Char -> Text -> [Text]
787+splitChar c = splitWith (==c)
788+{-# INLINE splitChar #-}
789+
790+-- | /O(m)*O(n)/ Break a 'Text' into pieces separated by the 'Text'
791+-- argument, consuming the delimiter. I.e.
792+--
793+-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
794+-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
795+-- > split "x" "x" == ["",""]
796+--
797+-- and
798+--
799+-- > intercalate s . split s == id
800+--
801+-- As for all splitting functions in this library, this function does
802+-- not copy the substrings, it just constructs new 'Text's that are
803+-- slices of the original.
804+split :: Text -> Text -> [Text]
805+split pat = go
806+ where
807+ go src = search 0 src
808+ where
809+ search !n !s
810+ | null s = [src] -- not found
811+ | pat `isPrefixOf` s = take n src : go (drop l s)
812+ | otherwise = search (n+1) (unsafeTail s)
813+ l = length pat
787814{-# INLINE split #-}
788815
789816-- | /O(n)/ Splits a 'Text' into components delimited by separators,
Data/Text/Compat.hsnew file+151
@@ -0,0 +1,151 @@
1+{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2+-- |
3+-- Module : Data.Text.Compat
4+-- Copyright : (c) Bryan O'Sullivan 2009
5+--
6+-- License : BSD-style
7+-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
8+-- duncan@haskell.org
9+-- Stability : experimental
10+-- Portability : GHC
11+--
12+-- A text API for which function names closely resemble those of the
13+-- list and 'ByteString' types.
14+--
15+-- This module is intended to be imported @qualified@, to avoid name
16+-- clashes with "Prelude" functions, e.g.
17+--
18+-- > import qualified Data.Text.Compat as T
19+
20+module Data.Text.Compat
21+ (
22+ -- * Types
23+ Text
24+
25+ -- * Creation and elimination
26+ , pack
27+ , unpack
28+ , singleton
29+ , empty
30+
31+ -- * Basic interface
32+ , cons
33+ , snoc
34+ , append
35+ , uncons
36+ , head
37+ , last
38+ , tail
39+ , init
40+ , null
41+ , length
42+
43+ -- * Transformations
44+ , map
45+ , intercalate
46+ , intersperse
47+ , transpose
48+ , reverse
49+
50+ -- * Case conversion
51+ -- $case
52+ , toCaseFold
53+ , toLower
54+ , toUpper
55+
56+ -- * Folds
57+ , foldl
58+ , foldl'
59+ , foldl1
60+ , foldl1'
61+ , foldr
62+ , foldr1
63+
64+ -- ** Special folds
65+ , concat
66+ , concatMap
67+ , any
68+ , all
69+ , maximum
70+ , minimum
71+
72+ -- * Construction
73+
74+ -- ** Scans
75+ , scanl
76+ , scanl1
77+ , scanr
78+ , scanr1
79+
80+ -- ** Accumulating maps
81+ , mapAccumL
82+ , mapAccumR
83+
84+ -- ** Generation and unfolding
85+ , replicate
86+ , unfoldr
87+ , unfoldrN
88+
89+ -- * Substrings
90+
91+ -- ** Breaking strings
92+ , take
93+ , drop
94+ , takeWhile
95+ , dropWhile
96+ , splitAt
97+ , span
98+ , break
99+ , group
100+ , groupBy
101+ , inits
102+ , tails
103+
104+ -- ** Breaking into many substrings
105+ , split
106+ , splitWith
107+ , breakSubstring
108+
109+ -- ** Breaking into lines and words
110+ , lines
111+ --, lines'
112+ , words
113+ , unlines
114+ , unwords
115+
116+ -- * Predicates
117+ , isPrefixOf
118+ , isSuffixOf
119+ , isInfixOf
120+
121+ -- * Searching
122+ , elem
123+ , filter
124+ , find
125+ , partition
126+
127+ -- , findSubstring
128+
129+ -- * Indexing
130+ , index
131+ , findIndex
132+ , findIndices
133+ , elemIndex
134+ , elemIndices
135+ , count
136+
137+ -- * Zipping and unzipping
138+ , zip
139+ , zipWith
140+
141+ -- -* Ordered text
142+ -- , sort
143+ ) where
144+
145+import Data.ByteString (ByteString)
146+import Data.Text hiding (split)
147+import Prelude (Char)
148+
149+split :: Char -> Text -> [Text]
150+split = splitChar
151+{-# INLINE split #-}
text.cabal+1
@@ -17,6 +17,7 @@ extra-source-files: README
1717library
1818 exposed-modules:
1919 Data.Text
20+ Data.Text.Compat
2021 Data.Text.Encoding
2122 Data.Text.Encoding.Error
2223 Data.Text.Encoding.Fusion