on master help sign in
Oops! I accidentally released 0.10.0.1 with backwards-incompatible API changes
b00c8a5Bryan O'Sullivan
Data/Text.hs+101−37
@@ -13,20 +13,31 @@
13-- Stability : experimental
13-- Stability : experimental
14-- Portability : GHC
14-- Portability : GHC
15--
15--
16−-- A time and space-efficient implementation of Unicode text using
16+-- A time and space-efficient implementation of Unicode text.
17−-- packed Word16 arrays. Suitable for performance critical use, both
17+-- Suitable for performance critical use, both in terms of large data
18−-- in terms of large data quantities and high speed.
18+-- quantities and high speed.
19+--
20+-- /Note/: Read below the synopsis for important notes on the use of
21+-- this module.
19--
22--
20-- This module is intended to be imported @qualified@, to avoid name
23-- This module is intended to be imported @qualified@, to avoid name
21-- clashes with "Prelude" functions, e.g.
24-- clashes with "Prelude" functions, e.g.
22--
25--
23-- > import qualified Data.Text as T
26-- > import qualified Data.Text as T
27+--
28+-- To use an extended and very rich family of functions for working
29+-- with Unicode text (including normalization, regular expressions,
30+-- non-standard encodings, text breaking, and locales), see the
31+-- @text-icu@ package: <http://hackage.haskell.org/package/text-icu>
24
32
25module Data.Text
33module Data.Text
26 (
34 (
27 -- * Strict vs lazy types
35 -- * Strict vs lazy types
28 -- $strict
36 -- $strict
29
37
38+ -- * Acceptable data
39+ -- $replacement
40+
30 -- * Fusion
41 -- * Fusion
31 -- $fusion
42 -- $fusion
32
43
@@ -196,7 +207,7 @@ import Data.String (IsString(..))
196import qualified Data.Text.Fusion as S
207import qualified Data.Text.Fusion as S
197import qualified Data.Text.Fusion.Common as S
208import qualified Data.Text.Fusion.Common as S
198import Data.Text.Fusion (stream, reverseStream, unstream)
209import Data.Text.Fusion (stream, reverseStream, unstream)
199−import Data.Text.Internal (Text(..), empty, text, textP)
210+import Data.Text.Internal (Text(..), empty, firstf, safe, text, textP)
200import qualified Prelude as P
211import qualified Prelude as P
201import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
212import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
202 unsafeHead, unsafeTail)
213 unsafeHead, unsafeTail)
@@ -230,6 +241,33 @@ import Data.Int (Int64)
230-- difference being that the strict module uses 'Int' values for
241-- difference being that the strict module uses 'Int' values for
231-- lengths and counts, while the lazy module uses 'Int64' lengths.
242-- lengths and counts, while the lazy module uses 'Int64' lengths.
232
243
244+-- $replacement
245+--
246+-- A 'Text' value is a sequence of Unicode scalar values, as defined
247+-- in &#xa7;3.9, definition D76 of the Unicode 5.2 standard:
248+-- <http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#page=35>. As
249+-- such, a 'Text' cannot contain values in the range U+D800 to U+DFFF
250+-- inclusive. Haskell implementations admit all Unicode code points
251+-- (&#xa7;3.4, definition D10) as 'Char' values, including code points
252+-- from this invalid range. This means that there are some 'Char'
253+-- values that are not valid Unicode scalar values, and the functions
254+-- in this module must handle those cases.
255+--
256+-- Within this module, many functions construct a 'Text' from one or
257+-- more 'Char' values. Those functions will substitute 'Char' values
258+-- that are not valid Unicode scalar values with the replacement
259+-- character \"&#xfffd;\" (U+FFFD). Functions that perform this
260+-- inspection and replacement are documented with the phrase
261+-- \"Performs replacement on invalid scalar values\".
262+--
263+-- (One reason for this policy of replacement is that internally, a
264+-- 'Text' value is represented as packed UTF-16 data. Values in the
265+-- range U+D800 through U+DFFF are used by UTF-16 to denote surrogate
266+-- code points, and so cannot be represented. The functions replace
267+-- invalid scalar values, instead of dropping them, as a security
268+-- measure. For details, see Unicode Technical Report 36, &#xa7;3.5:
269+-- <http://unicode.org/reports/tr36/#Deletion_of_Noncharacters>)
270+
233-- $fusion
271-- $fusion
234--
272--
235-- Most of the functions in this module are subject to /fusion/,
273-- Most of the functions in this module are subject to /fusion/,
@@ -240,6 +278,7 @@ import Data.Int (Int64)
240--
278--
241-- > import Data.Text as T
279-- > import Data.Text as T
242-- > import Data.Text.Encoding as E
280-- > import Data.Text.Encoding as E
281+-- > import Data.ByteString (ByteString)
243-- >
282-- >
244-- > countChars :: ByteString -> Int
283-- > countChars :: ByteString -> Int
245-- > countChars = T.length . T.toUpper . E.decodeUtf8
284-- > countChars = T.length . T.toUpper . E.decodeUtf8
@@ -251,7 +290,7 @@ import Data.Int (Int64)
251-- function will be compiled down to a single loop over the source
290-- function will be compiled down to a single loop over the source
252-- 'ByteString'.
291-- 'ByteString'.
253--
292--
254−-- Functions that can be fused by the compiler are marked with the
293+-- Functions that can be fused by the compiler are documented with the
255-- phrase \"Subject to fusion\".
294-- phrase \"Subject to fusion\".
256
295
257instance Eq Text where
296instance Eq Text where
@@ -320,9 +359,10 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
320-- -----------------------------------------------------------------------------
359-- -----------------------------------------------------------------------------
321-- * Conversion to/from 'Text'
360-- * Conversion to/from 'Text'
322
361
323−-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to fusion.
362+-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
363+-- fusion. Performs replacement on invalid scalar values.
324pack :: String -> Text
364pack :: String -> Text
325−pack = unstream . S.streamList
365+pack = unstream . S.streamList . L.map safe
326{-# INLINE [1] pack #-}
366{-# INLINE [1] pack #-}
327
367
328-- | /O(n)/ Convert a Text into a String. Subject to fusion.
368-- | /O(n)/ Convert a Text into a String. Subject to fusion.
@@ -330,10 +370,10 @@ unpack :: Text -> String
330unpack = S.unstreamList . stream
370unpack = S.unstreamList . stream
331{-# INLINE [1] unpack #-}
371{-# INLINE [1] unpack #-}
332
372
333−-- | /O(1)/ Convert a character into a Text.
373+-- | /O(1)/ Convert a character into a Text. Subject to fusion.
334−-- Subject to fusion.
374+-- Performs replacement on invalid scalar values.
335singleton :: Char -> Text
375singleton :: Char -> Text
336−singleton = unstream . S.singleton
376+singleton = unstream . S.singleton . safe
337{-# INLINE [1] singleton #-}
377{-# INLINE [1] singleton #-}
338
378
339-- -----------------------------------------------------------------------------
379-- -----------------------------------------------------------------------------
@@ -341,15 +381,17 @@ singleton = unstream . S.singleton
341
381
342-- | /O(n)/ Adds a character to the front of a 'Text'. This function
382-- | /O(n)/ Adds a character to the front of a 'Text'. This function
343-- is more costly than its 'List' counterpart because it requires
383-- is more costly than its 'List' counterpart because it requires
344−-- copying a new array. Subject to fusion.
384+-- copying a new array. Subject to fusion. Performs replacement on
385+-- invalid scalar values.
345cons :: Char -> Text -> Text
386cons :: Char -> Text -> Text
346−cons c t = unstream (S.cons c (stream t))
387+cons c t = unstream (S.cons (safe c) (stream t))
347{-# INLINE cons #-}
388{-# INLINE cons #-}
348
389
349-- | /O(n)/ Adds a character to the end of a 'Text'. This copies the
390-- | /O(n)/ Adds a character to the end of a 'Text'. This copies the
350-- entire array in the process, unless fused. Subject to fusion.
391-- entire array in the process, unless fused. Subject to fusion.
392+-- Performs replacement on invalid scalar values.
351snoc :: Text -> Char -> Text
393snoc :: Text -> Char -> Text
352−snoc t c = unstream (S.snoc (stream t) c)
394+snoc t c = unstream (S.snoc (stream t) (safe c))
353{-# INLINE snoc #-}
395{-# INLINE snoc #-}
354
396
355-- | /O(n)/ Appends one 'Text' to the other by copying both of them
397-- | /O(n)/ Appends one 'Text' to the other by copying both of them
@@ -523,9 +565,10 @@ compareLength t n = S.compareLengthI (stream t) n
523-- -----------------------------------------------------------------------------
565-- -----------------------------------------------------------------------------
524-- * Transformations
566-- * Transformations
525-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
567-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
526−-- each element of @t@. Subject to fusion.
568+-- each element of @t@. Subject to fusion. Performs replacement on
569+-- invalid scalar values.
527map :: (Char -> Char) -> Text -> Text
570map :: (Char -> Char) -> Text -> Text
528−map f t = unstream (S.map f (stream t))
571+map f t = unstream (S.map (safe . f) (stream t))
529{-# INLINE [1] map #-}
572{-# INLINE [1] map #-}
530
573
531-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
574-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
@@ -536,9 +579,10 @@ intercalate t = concat . (U.intersperse t)
536{-# INLINE intercalate #-}
579{-# INLINE intercalate #-}
537
580
538-- | /O(n)/ The 'intersperse' function takes a character and places it
581-- | /O(n)/ The 'intersperse' function takes a character and places it
539−-- between the characters of a 'Text'. Subject to fusion.
582+-- between the characters of a 'Text'. Subject to fusion. Performs
583+-- replacement on invalid scalar values.
540intersperse :: Char -> Text -> Text
584intersperse :: Char -> Text -> Text
541−intersperse c t = unstream (S.intersperse c (stream t))
585+intersperse c t = unstream (S.intersperse (safe c) (stream t))
542{-# INLINE intersperse #-}
586{-# INLINE intersperse #-}
543
587
544-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
588-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
@@ -614,7 +658,10 @@ toUpper t = unstream (S.toUpper (stream t))
614{-# INLINE toUpper #-}
658{-# INLINE toUpper #-}
615
659
616-- | /O(n)/ Left-justify a string to the given length, using the
660-- | /O(n)/ Left-justify a string to the given length, using the
617−-- specified fill character on the right. Subject to fusion. Examples:
661+-- specified fill character on the right. Subject to fusion.
662+-- Performs replacement on invalid scalar values.
663+--
664+-- Examples:
618--
665--
619-- > justifyLeft 7 'x' "foo" == "fooxxxx"
666-- > justifyLeft 7 'x' "foo" == "fooxxxx"
620-- > justifyLeft 3 'x' "foobar" == "foobar"
667-- > justifyLeft 3 'x' "foobar" == "foobar"
@@ -633,7 +680,10 @@ justifyLeft k c t
633 #-}
680 #-}
634
681
635-- | /O(n)/ Right-justify a string to the given length, using the
682-- | /O(n)/ Right-justify a string to the given length, using the
636−-- specified fill character on the left. Examples:
683+-- specified fill character on the left. Performs replacement on
684+-- invalid scalar values.
685+--
686+-- Examples:
637--
687--
638-- > justifyRight 7 'x' "bar" == "xxxxbar"
688-- > justifyRight 7 'x' "bar" == "xxxxbar"
639-- > justifyRight 3 'x' "foobar" == "foobar"
689-- > justifyRight 3 'x' "foobar" == "foobar"
@@ -644,8 +694,11 @@ justifyRight k c t
644 where len = length t
694 where len = length t
645{-# INLINE justifyRight #-}
695{-# INLINE justifyRight #-}
646
696
647−-- | /O(n)/ Center a string to the given length, using the
697+-- | /O(n)/ Center a string to the given length, using the specified
648−-- specified fill character on either side. Examples:
698+-- fill character on either side. Performs replacement on invalid
699+-- scalar values.
700+--
701+-- Examples:
649--
702--
650-- > center 8 'x' "HS" = "xxxHSxxx"
703-- > center 8 'x' "HS" = "xxxHSxxx"
651center :: Int -> Char -> Text -> Text
704center :: Int -> Char -> Text -> Text
@@ -760,6 +813,7 @@ minimum t = S.minimum (stream t)
760
813
761-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
814-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
762-- successive reduced values from the left. Subject to fusion.
815-- successive reduced values from the left. Subject to fusion.
816+-- Performs replacement on invalid scalar values.
763--
817--
764-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
818-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
765--
819--
@@ -767,11 +821,13 @@ minimum t = S.minimum (stream t)
767--
821--
768-- > last (scanl f z xs) == foldl f z xs.
822-- > last (scanl f z xs) == foldl f z xs.
769scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
823scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
770−scanl f z t = unstream (S.scanl f z (stream t))
824+scanl f z t = unstream (S.scanl g z (stream t))
825+ where g a b = safe (f a b)
771{-# INLINE scanl #-}
826{-# INLINE scanl #-}
772
827
773-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
828-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
774−-- value argument. Subject to fusion.
829+-- value argument. Subject to fusion. Performs replacement on
830+-- invalid scalar values.
775--
831--
776-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
832-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
777scanl1 :: (Char -> Char -> Char) -> Text -> Text
833scanl1 :: (Char -> Char -> Char) -> Text -> Text
@@ -779,15 +835,18 @@ scanl1 f t | null t = empty
779 | otherwise = scanl f (unsafeHead t) (unsafeTail t)
835 | otherwise = scanl f (unsafeHead t) (unsafeTail t)
780{-# INLINE scanl1 #-}
836{-# INLINE scanl1 #-}
781
837
782−-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'.
838+-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
839+-- replacement on invalid scalar values.
783--
840--
784-- > scanr f v == reverse . scanl (flip f) v . reverse
841-- > scanr f v == reverse . scanl (flip f) v . reverse
785scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
842scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
786−scanr f z = S.reverse . S.reverseScanr f z . reverseStream
843+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
844+ where g a b = safe (f a b)
787{-# INLINE scanr #-}
845{-# INLINE scanr #-}
788
846
789-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
847-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
790−-- value argument. Subject to fusion.
848+-- value argument. Subject to fusion. Performs replacement on
849+-- invalid scalar values.
791scanr1 :: (Char -> Char -> Char) -> Text -> Text
850scanr1 :: (Char -> Char -> Char) -> Text -> Text
792scanr1 f t | null t = empty
851scanr1 f t | null t = empty
793 | otherwise = scanr f (last t) (init t)
852 | otherwise = scanr f (last t) (init t)
@@ -795,9 +854,11 @@ scanr1 f t | null t = empty
795
854
796-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
855-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
797-- function to each element of a 'Text', passing an accumulating
856-- function to each element of a 'Text', passing an accumulating
798−-- parameter from left to right, and returns a final 'Text'.
857+-- parameter from left to right, and returns a final 'Text'. Performs
858+-- replacement on invalid scalar values.
799mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
859mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
800−mapAccumL f z0 = S.mapAccumL f z0 . stream
860+mapAccumL f z0 = S.mapAccumL g z0 . stream
861+ where g a b = second safe (f a b)
801{-# INLINE mapAccumL #-}
862{-# INLINE mapAccumL #-}
802
863
803-- | The 'mapAccumR' function behaves like a combination of 'map' and
864-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -805,8 +866,10 @@ mapAccumL f z0 = S.mapAccumL f z0 . stream
805-- 'Text', passing an accumulating parameter from right to left, and
866-- 'Text', passing an accumulating parameter from right to left, and
806-- returning a final value of this accumulator together with the new
867-- returning a final value of this accumulator together with the new
807-- 'Text'.
868-- 'Text'.
869+-- Performs replacement on invalid scalar values.
808mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
870mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
809−mapAccumR f z0 = second reverse . S.mapAccumL f z0 . reverseStream
871+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
872+ where g a b = second safe (f a b)
810{-# INLINE mapAccumR #-}
873{-# INLINE mapAccumR #-}
811
874
812-- -----------------------------------------------------------------------------
875-- -----------------------------------------------------------------------------
@@ -839,7 +902,7 @@ replicate n t@(Text a o l)
839-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
902-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
840-- value of every element. Subject to fusion.
903-- value of every element. Subject to fusion.
841replicateChar :: Int -> Char -> Text
904replicateChar :: Int -> Char -> Text
842−replicateChar n c = unstream (S.replicateCharI n c)
905+replicateChar n c = unstream (S.replicateCharI n (safe c))
843{-# INLINE replicateChar #-}
906{-# INLINE replicateChar #-}
844
907
845-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
908-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
@@ -848,9 +911,9 @@ replicateChar n c = unstream (S.replicateCharI n c)
848-- returns 'Nothing' if it is done producing the 'Text', otherwise
911-- returns 'Nothing' if it is done producing the 'Text', otherwise
849-- 'Just' @(a,b)@. In this case, @a@ is the next 'Char' in the
912-- 'Just' @(a,b)@. In this case, @a@ is the next 'Char' in the
850-- string, and @b@ is the seed value for further production. Subject
913-- string, and @b@ is the seed value for further production. Subject
851−-- to fusion.
914+-- to fusion. Performs replacement on invalid scalar values.
852unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
915unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
853−unfoldr f s = unstream (S.unfoldr f s)
916+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
854{-# INLINE unfoldr #-}
917{-# INLINE unfoldr #-}
855
918
856-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
919-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -858,9 +921,9 @@ unfoldr f s = unstream (S.unfoldr f s)
858-- first argument to 'unfoldrN'. This function is more efficient than
921-- first argument to 'unfoldrN'. This function is more efficient than
859-- 'unfoldr' when the maximum length of the result is known and
922-- 'unfoldr' when the maximum length of the result is known and
860-- correct, otherwise its performance is similar to 'unfoldr'. Subject
923-- correct, otherwise its performance is similar to 'unfoldr'. Subject
861−-- to fusion.
924+-- to fusion. Performs replacement on invalid scalar values.
862unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
925unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
863−unfoldrN n f s = unstream (S.unfoldrN n f s)
926+unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
864{-# INLINE unfoldrN #-}
927{-# INLINE unfoldrN #-}
865
928
866-- -----------------------------------------------------------------------------
929-- -----------------------------------------------------------------------------
@@ -1297,8 +1360,10 @@ zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1297
1360
1298-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1361-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1299-- given as the first argument, instead of a tupling function.
1362-- given as the first argument, instead of a tupling function.
1363+-- Performs replacement on invalid scalar values.
1300zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1364zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1301−zipWith f t1 t2 = unstream (S.zipWith f (stream t1) (stream t2))
1365+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1366+ where g a b = safe (f a b)
1302{-# INLINE [0] zipWith #-}
1367{-# INLINE [0] zipWith #-}
1303
1368
1304-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1369-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
@@ -1362,8 +1427,7 @@ unwords = intercalate (singleton ' ')
1362{-# INLINE unwords #-}
1427{-# INLINE unwords #-}
1363
1428
1364-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1429-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1365−-- 'True' iff the first is a prefix of the second. This function is
1430+-- 'True' iff the first is a prefix of the second. Subject to fusion.
1366−-- subject to fusion.
1367isPrefixOf :: Text -> Text -> Bool
1431isPrefixOf :: Text -> Text -> Bool
1368isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1432isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1369 alen <= blen && S.isPrefixOf (stream a) (stream b)
1433 alen <= blen && S.isPrefixOf (stream a) (stream b)
Data/Text/Internal.hs+24
@@ -22,8 +22,12 @@ module Data.Text.Internal
22 -- * Construction
22 -- * Construction
23 , text
23 , text
24 , textP
24 , textP
25+ -- * Safety
26+ , safe
25 -- * Code that must be here for accessibility
27 -- * Code that must be here for accessibility
26 , empty
28 , empty
29+ -- * Utilities
30+ , firstf
27 -- * Debugging
31 -- * Debugging
28 , showText
32 , showText
29 ) where
33 ) where
@@ -31,7 +35,9 @@ module Data.Text.Internal
31#if defined(ASSERTS)
35#if defined(ASSERTS)
32import Control.Exception (assert)
36import Control.Exception (assert)
33#endif
37#endif
38+import Data.Bits ((.&.))
34import qualified Data.Text.Array as A
39import qualified Data.Text.Array as A
40+import Data.Text.UnsafeChar (ord)
35import Data.Typeable (Typeable)
41import Data.Typeable (Typeable)
36
42
37-- | A space efficient, packed, unboxed Unicode text type.
43-- | A space efficient, packed, unboxed Unicode text type.
@@ -72,3 +78,21 @@ showText :: Text -> String
72showText (Text arr off len) =
78showText (Text arr off len) =
73 "Text " ++ show (A.toList arr off len) ++ ' ' :
79 "Text " ++ show (A.toList arr off len) ++ ' ' :
74 show off ++ ' ' : show len
80 show off ++ ' ' : show len
81+
82+-- | Map a 'Char' to a 'Text'-safe value.
83+--
84+-- UTF-16 surrogate code points are not included in the set of Unicode
85+-- scalar values, but are unfortunately admitted as valid 'Char'
86+-- values by Haskell. They cannot be represented in a 'Text'. This
87+-- function remaps those code points to the Unicode replacement
88+-- character \"&#xfffd;\", and leaves other code points unchanged.
89+safe :: Char -> Char
90+safe c
91+ | ord c .&. 0x1ff800 /= 0xd800 = c
92+ | otherwise = '\xfffd'
93+{-# INLINE safe #-}
94+
95+-- | Apply a function to the first element of an optional pair.
96+firstf :: (a -> c) -> Maybe (a,b) -> Maybe (c,b)
97+firstf f (Just (a, b)) = Just (f a, b)
98+firstf _ Nothing = Nothing
Data/Text/Lazy.hs+147−67
@@ -11,18 +11,23 @@
11-- Portability : GHC
11-- Portability : GHC
12--
12--
13-- A time and space-efficient implementation of Unicode text using
13-- A time and space-efficient implementation of Unicode text using
14+-- lists of packed arrays.
15+--
16+-- /Note/: Read below the synopsis for important notes on the use of
17+-- this module.
18+--
14−-- lists of packed arrays. This representation is suitable for high
19+-- The representation used by this module is suitable for high
15-- performance use and for streaming large quantities of data. It
20-- performance use and for streaming large quantities of data. It
16-- provides a means to manipulate a large body of text without
21-- provides a means to manipulate a large body of text without
17-- requiring that the entire content be resident in memory.
22-- requiring that the entire content be resident in memory.
18--
23--
19-- Some operations, such as 'concat', 'append', 'reverse' and 'cons',
24-- Some operations, such as 'concat', 'append', 'reverse' and 'cons',
20−-- have better complexity than their "Data.Text" equivalents, due to
25+-- have better time complexity than their "Data.Text" equivalents, due
21−-- optimisations resulting from the list spine structure. And for
26+-- to the underlying representation being a list of chunks. For other
22−-- other operations lazy 'Text's are usually within a few percent of
27+-- operations, lazy 'Text's are usually within a few percent of strict
23−-- strict ones, but with better heap usage. For data larger than
28+-- ones, but often with better heap usage if used in a streaming
24−-- available memory, or if you have tight memory constraints, this
29+-- fashion. For data larger than available memory, or if you have
25−-- module will be the only option.
30+-- tight memory constraints, this module will be the only option.
26--
31--
27-- This module is intended to be imported @qualified@, to avoid name
32-- This module is intended to be imported @qualified@, to avoid name
28-- clashes with "Prelude" functions. eg.
33-- clashes with "Prelude" functions. eg.
@@ -31,7 +36,15 @@
31
36
32module Data.Text.Lazy
37module Data.Text.Lazy
33 (
38 (
39+ -- * Fusion
40+ -- $fusion
41+
42+ -- * Acceptable data
43+ -- $replacement
44+
45+ -- * Types
34 Text
46 Text
47+
35 -- * Creation and elimination
48 -- * Creation and elimination
36 , pack
49 , pack
37 , unpack
50 , unpack
@@ -200,10 +213,62 @@ import qualified Data.Text.Lazy.Fusion as S
200import Data.Text.Fusion.Internal (PairS(..))
213import Data.Text.Fusion.Internal (PairS(..))
201import Data.Text.Lazy.Fusion (stream, unstream)
214import Data.Text.Lazy.Fusion (stream, unstream)
202import Data.Text.Lazy.Internal (Text(..), chunk, empty, foldlChunks, foldrChunks)
215import Data.Text.Lazy.Internal (Text(..), chunk, empty, foldlChunks, foldrChunks)
203−import Data.Text.Internal (textP)
216+import Data.Text.Internal (firstf, safe, textP)
204import qualified Data.Text.Util as U
217import qualified Data.Text.Util as U
205import Data.Text.Lazy.Search (indices)
218import Data.Text.Lazy.Search (indices)
206
219
220+-- $fusion
221+--
222+-- Most of the functions in this module are subject to /fusion/,
223+-- meaning that a pipeline of such functions will usually allocate at
224+-- most one 'Text' value.
225+--
226+-- As an example, consider the following pipeline:
227+--
228+-- > import Data.Text.Lazy as T
229+-- > import Data.Text.Lazy.Encoding as E
230+-- > import Data.ByteString.Lazy (ByteString)
231+-- >
232+-- > countChars :: ByteString -> Int
233+-- > countChars = T.length . T.toUpper . E.decodeUtf8
234+--
235+-- From the type signatures involved, this looks like it should
236+-- allocate one 'ByteString' value, and two 'Text' values. However,
237+-- when a module is compiled with optimisation enabled under GHC, the
238+-- two intermediate 'Text' values will be optimised away, and the
239+-- function will be compiled down to a single loop over the source
240+-- 'ByteString'.
241+--
242+-- Functions that can be fused by the compiler are documented with the
243+-- phrase \"Subject to fusion\".
244+
245+-- $replacement
246+--
247+-- A 'Text' value is a sequence of Unicode scalar values, as defined
248+-- in &#xa7;3.9, definition D76 of the Unicode 5.2 standard:
249+-- <http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#page=35>. As
250+-- such, a 'Text' cannot contain values in the range U+D800 to U+DFFF
251+-- inclusive. Haskell implementations admit all Unicode code points
252+-- (&#xa7;3.4, definition D10) as 'Char' values, including code points
253+-- from this invalid range. This means that there are some 'Char'
254+-- values that are not valid Unicode scalar values, and the functions
255+-- in this module must handle those cases.
256+--
257+-- Within this module, many functions construct a 'Text' from one or
258+-- more 'Char' values. Those functions will substitute 'Char' values
259+-- that are not valid Unicode scalar values with the replacement
260+-- character \"&#xfffd;\" (U+FFFD). Functions that perform this
261+-- inspection and replacement are documented with the phrase
262+-- \"Performs replacement on invalid scalar values\".
263+--
264+-- (One reason for this policy of replacement is that internally, a
265+-- 'Text' value is represented as packed UTF-16 data. Values in the
266+-- range U+D800 through U+DFFF are used by UTF-16 to denote surrogate
267+-- code points, and so cannot be represented. The functions replace
268+-- invalid scalar values, instead of dropping them, as a security
269+-- measure. For details, see Unicode Technical Report 36, &#xa7;3.5:
270+-- <http://unicode.org/reports/tr36#Deletion_of_Noncharacters>)
271+
207equal :: Text -> Text -> Bool
272equal :: Text -> Text -> Bool
208equal Empty Empty = True
273equal Empty Empty = True
209equal Empty _ = False
274equal Empty _ = False
@@ -274,19 +339,19 @@ instance Data Text where
274
339
275-- | /O(n)/ Convert a 'String' into a 'Text'.
340-- | /O(n)/ Convert a 'String' into a 'Text'.
276--
341--
277−-- This function is subject to array fusion.
342+-- Subject to fusion. Performs replacement on invalid scalar values.
278pack :: String -> Text
343pack :: String -> Text
279−pack s = unstream (S.streamList s)
344+pack = unstream . S.streamList . L.map safe
280{-# INLINE [1] pack #-}
345{-# INLINE [1] pack #-}
281
346
282-- | /O(n)/ Convert a 'Text' into a 'String'.
347-- | /O(n)/ Convert a 'Text' into a 'String'.
283−-- Subject to array fusion.
348+-- Subject to fusion.
284unpack :: Text -> String
349unpack :: Text -> String
285unpack t = S.unstreamList (stream t)
350unpack t = S.unstreamList (stream t)
286{-# INLINE [1] unpack #-}
351{-# INLINE [1] unpack #-}
287
352
288−-- | /O(1)/ Convert a character into a Text.
353+-- | /O(1)/ Convert a character into a Text. Subject to fusion.
289−-- Subject to fusion.
354+-- Performs replacement on invalid scalar values.
290singleton :: Char -> Text
355singleton :: Char -> Text
291singleton c = Chunk (T.singleton c) Empty
356singleton c = Chunk (T.singleton c) Empty
292{-# INLINE [1] singleton #-}
357{-# INLINE [1] singleton #-}
@@ -346,8 +411,7 @@ snoc t c = foldrChunks Chunk (singleton c) t
346 unstream (S.snoc (stream t) c) = snoc t c
411 unstream (S.snoc (stream t) c) = snoc t c
347 #-}
412 #-}
348
413
349−-- | /O(n\/c)/ Appends one 'Text' to another. Subject to array
414+-- | /O(n\/c)/ Appends one 'Text' to another. Subject to fusion.
350−-- fusion.
351append :: Text -> Text -> Text
415append :: Text -> Text -> Text
352append xs ys = foldrChunks Chunk ys xs
416append xs ys = foldrChunks Chunk ys xs
353{-# INLINE [1] append #-}
417{-# INLINE [1] append #-}
@@ -360,7 +424,7 @@ append xs ys = foldrChunks Chunk ys xs
360 #-}
424 #-}
361
425
362-- | /O(1)/ Returns the first character and rest of a 'Text', or
426-- | /O(1)/ Returns the first character and rest of a 'Text', or
363−-- 'Nothing' if empty. Subject to array fusion.
427+-- 'Nothing' if empty. Subject to fusion.
364uncons :: Text -> Maybe (Char, Text)
428uncons :: Text -> Maybe (Char, Text)
365uncons Empty = Nothing
429uncons Empty = Nothing
366uncons (Chunk t ts) = Just (T.unsafeHead t, ts')
430uncons (Chunk t ts) = Just (T.unsafeHead t, ts')
@@ -369,13 +433,13 @@ uncons (Chunk t ts) = Just (T.unsafeHead t, ts')
369{-# INLINE uncons #-}
433{-# INLINE uncons #-}
370
434
371-- | /O(1)/ Returns the first character of a 'Text', which must be
435-- | /O(1)/ Returns the first character of a 'Text', which must be
372−-- non-empty. Subject to array fusion.
436+-- non-empty. Subject to fusion.
373head :: Text -> Char
437head :: Text -> Char
374head t = S.head (stream t)
438head t = S.head (stream t)
375{-# INLINE head #-}
439{-# INLINE head #-}
376
440
377-- | /O(1)/ Returns all characters after the head of a 'Text', which
441-- | /O(1)/ Returns all characters after the head of a 'Text', which
378−-- must be non-empty. Subject to array fusion.
442+-- must be non-empty. Subject to fusion.
379tail :: Text -> Text
443tail :: Text -> Text
380tail (Chunk t ts) = chunk (T.tail t) ts
444tail (Chunk t ts) = chunk (T.tail t) ts
381tail Empty = emptyError "tail"
445tail Empty = emptyError "tail"
@@ -389,7 +453,7 @@ tail Empty = emptyError "tail"
389 #-}
453 #-}
390
454
391-- | /O(1)/ Returns all but the last character of a 'Text', which must
455-- | /O(1)/ Returns all but the last character of a 'Text', which must
392−-- be non-empty. Subject to array fusion.
456+-- be non-empty. Subject to fusion.
393init :: Text -> Text
457init :: Text -> Text
394init (Chunk t0 ts0) = go t0 ts0
458init (Chunk t0 ts0) = go t0 ts0
395 where go t (Chunk t' ts) = Chunk t (go t' ts)
459 where go t (Chunk t' ts) = Chunk t (go t' ts)
@@ -404,7 +468,7 @@ init Empty = emptyError "init"
404 unstream (S.init (stream t)) = init t
468 unstream (S.init (stream t)) = init t
405 #-}
469 #-}
406
470
407−-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to array
471+-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
408-- fusion.
472-- fusion.
409null :: Text -> Bool
473null :: Text -> Bool
410null Empty = True
474null Empty = True
@@ -425,7 +489,7 @@ isSingleton = S.isSingleton . stream
425{-# INLINE isSingleton #-}
489{-# INLINE isSingleton #-}
426
490
427-- | /O(1)/ Returns the last character of a 'Text', which must be
491-- | /O(1)/ Returns the last character of a 'Text', which must be
428−-- non-empty. Subject to array fusion.
492+-- non-empty. Subject to fusion.
429last :: Text -> Char
493last :: Text -> Char
430last Empty = emptyError "last"
494last Empty = emptyError "last"
431last (Chunk t ts) = go t ts
495last (Chunk t ts) = go t ts
@@ -469,9 +533,10 @@ compareLength t n = S.compareLengthI (stream t) n
469-- properties of code.
533-- properties of code.
470
534
471-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
535-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
472−-- each element of @t@. Subject to array fusion.
536+-- each element of @t@. Subject to fusion. Performs replacement on
537+-- invalid scalar values.
473map :: (Char -> Char) -> Text -> Text
538map :: (Char -> Char) -> Text -> Text
474−map f t = unstream (S.map f (stream t))
539+map f t = unstream (S.map (safe . f) (stream t))
475{-# INLINE [1] map #-}
540{-# INLINE [1] map #-}
476
541
477-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
542-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
@@ -482,13 +547,17 @@ intercalate t = concat . (U.intersperse t)
482{-# INLINE intercalate #-}
547{-# INLINE intercalate #-}
483
548
484-- | /O(n)/ The 'intersperse' function takes a character and places it
549-- | /O(n)/ The 'intersperse' function takes a character and places it
485−-- between the characters of a 'Text'. Subject to array fusion.
550+-- between the characters of a 'Text'. Subject to fusion. Performs
551+-- replacement on invalid scalar values.
486−intersperse :: Char -> Text -> Text
552+intersperse :: Char -> Text -> Text
487−intersperse c t = unstream (S.intersperse c (stream t))
553+intersperse c t = unstream (S.intersperse (safe c) (stream t))
488{-# INLINE intersperse #-}
554{-# INLINE intersperse #-}
489
555
490-- | /O(n)/ Left-justify a string to the given length, using the
556-- | /O(n)/ Left-justify a string to the given length, using the
491−-- specified fill character on the right. Subject to fusion. Examples:
557+-- specified fill character on the right. Subject to fusion. Performs
558+-- replacement on invalid scalar values.
559+--
560+-- Examples:
492--
561--
493-- > justifyLeft 7 'x' "foo" == "fooxxxx"
562-- > justifyLeft 7 'x' "foo" == "fooxxxx"
494-- > justifyLeft 3 'x' "foobar" == "foobar"
563-- > justifyLeft 3 'x' "foobar" == "foobar"
@@ -507,7 +576,10 @@ justifyLeft k c t
507 #-}
576 #-}
508
577
509-- | /O(n)/ Right-justify a string to the given length, using the
578-- | /O(n)/ Right-justify a string to the given length, using the
510−-- specified fill character on the left. Examples:
579+-- specified fill character on the left. Performs replacement on
580+-- invalid scalar values.
581+--
582+-- Examples:
511--
583--
512-- > justifyRight 7 'x' "bar" == "xxxxbar"
584-- > justifyRight 7 'x' "bar" == "xxxxbar"
513-- > justifyRight 3 'x' "foobar" == "foobar"
585-- > justifyRight 3 'x' "foobar" == "foobar"
@@ -518,8 +590,11 @@ justifyRight k c t
518 where len = length t
590 where len = length t
519{-# INLINE justifyRight #-}
591{-# INLINE justifyRight #-}
520
592
521−-- | /O(n)/ Center a string to the given length, using the
593+-- | /O(n)/ Center a string to the given length, using the specified
522−-- specified fill character on either side. Examples:
594+-- fill character on either side. Performs replacement on invalid
595+-- scalar values.
596+--
597+-- Examples:
523--
598--
524-- > center 8 'x' "HS" = "xxxHSxxx"
599-- > center 8 'x' "HS" = "xxxHSxxx"
525center :: Int64 -> Char -> Text -> Text
600center :: Int64 -> Char -> Text -> Text
@@ -608,26 +683,24 @@ toUpper t = unstream (S.toUpper (stream t))
608-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
683-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
609-- (typically the left-identity of the operator), and a 'Text',
684-- (typically the left-identity of the operator), and a 'Text',
610-- reduces the 'Text' using the binary operator, from left to right.
685-- reduces the 'Text' using the binary operator, from left to right.
611−-- Subject to array fusion.
686+-- Subject to fusion.
612foldl :: (a -> Char -> a) -> a -> Text -> a
687foldl :: (a -> Char -> a) -> a -> Text -> a
613foldl f z t = S.foldl f z (stream t)
688foldl f z t = S.foldl f z (stream t)
614{-# INLINE foldl #-}
689{-# INLINE foldl #-}
615
690
616-- | /O(n)/ A strict version of 'foldl'.
691-- | /O(n)/ A strict version of 'foldl'.
617−-- Subject to array fusion.
692+-- Subject to fusion.
618foldl' :: (a -> Char -> a) -> a -> Text -> a
693foldl' :: (a -> Char -> a) -> a -> Text -> a
619foldl' f z t = S.foldl' f z (stream t)
694foldl' f z t = S.foldl' f z (stream t)
620{-# INLINE foldl' #-}
695{-# INLINE foldl' #-}
621
696
622-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
697-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
623−-- and thus must be applied to a non-empty 'Text'. Subject to array
698+-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
624−-- fusion.
625foldl1 :: (Char -> Char -> Char) -> Text -> Char
699foldl1 :: (Char -> Char -> Char) -> Text -> Char
626foldl1 f t = S.foldl1 f (stream t)
700foldl1 f t = S.foldl1 f (stream t)
627{-# INLINE foldl1 #-}
701{-# INLINE foldl1 #-}
628
702
629−-- | /O(n)/ A strict version of 'foldl1'.
703+-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
630−-- Subject to array fusion.
631foldl1' :: (Char -> Char -> Char) -> Text -> Char
704foldl1' :: (Char -> Char -> Char) -> Text -> Char
632foldl1' f t = S.foldl1' f (stream t)
705foldl1' f t = S.foldl1' f (stream t)
633{-# INLINE foldl1' #-}
706{-# INLINE foldl1' #-}
@@ -635,13 +708,13 @@ foldl1' f t = S.foldl1' f (stream t)
635-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
708-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
636-- (typically the right-identity of the operator), and a 'Text',
709-- (typically the right-identity of the operator), and a 'Text',
637-- reduces the 'Text' using the binary operator, from right to left.
710-- reduces the 'Text' using the binary operator, from right to left.
638−-- Subject to array fusion.
711+-- Subject to fusion.
639foldr :: (Char -> a -> a) -> a -> Text -> a
712foldr :: (Char -> a -> a) -> a -> Text -> a
640foldr f z t = S.foldr f z (stream t)
713foldr f z t = S.foldr f z (stream t)
641{-# INLINE foldr #-}
714{-# INLINE foldr #-}
642
715
643−-- | /O(n)/ A variant of 'foldr' that has no starting value argument, and
716+-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
644−-- thust must be applied to a non-empty 'Text'. Subject to array
717+-- and thust must be applied to a non-empty 'Text'. Subject to
645-- fusion.
718-- fusion.
646foldr1 :: (Char -> Char -> Char) -> Text -> Char
719foldr1 :: (Char -> Char -> Char) -> Text -> Char
647foldr1 f t = S.foldr1 f (stream t)
720foldr1 f t = S.foldr1 f (stream t)
@@ -664,32 +737,32 @@ concatMap f = concat . foldr ((:) . f) []
664{-# INLINE concatMap #-}
737{-# INLINE concatMap #-}
665
738
666-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
739-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
667−-- 'Text' @t@ satisifes the predicate @p@. Subject to array fusion.
740+-- 'Text' @t@ satisifes the predicate @p@. Subject to fusion.
668any :: (Char -> Bool) -> Text -> Bool
741any :: (Char -> Bool) -> Text -> Bool
669any p t = S.any p (stream t)
742any p t = S.any p (stream t)
670{-# INLINE any #-}
743{-# INLINE any #-}
671
744
672-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
745-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
673−-- 'Text' @t@ satisify the predicate @p@. Subject to array fusion.
746+-- 'Text' @t@ satisify the predicate @p@. Subject to fusion.
674all :: (Char -> Bool) -> Text -> Bool
747all :: (Char -> Bool) -> Text -> Bool
675all p t = S.all p (stream t)
748all p t = S.all p (stream t)
676{-# INLINE all #-}
749{-# INLINE all #-}
677
750
678-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
751-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
679−-- must be non-empty. Subject to array fusion.
752+-- must be non-empty. Subject to fusion.
680maximum :: Text -> Char
753maximum :: Text -> Char
681maximum t = S.maximum (stream t)
754maximum t = S.maximum (stream t)
682{-# INLINE maximum #-}
755{-# INLINE maximum #-}
683
756
684-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
757-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
685−-- must be non-empty. Subject to array fusion.
758+-- must be non-empty. Subject to fusion.
686minimum :: Text -> Char
759minimum :: Text -> Char
687minimum t = S.minimum (stream t)
760minimum t = S.minimum (stream t)
688{-# INLINE minimum #-}
761{-# INLINE minimum #-}
689
762
690-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
763-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
691−-- successive reduced values from the left. This function is subject
764+-- successive reduced values from the left. Subject to fusion.
692−-- to array fusion.
765+-- Performs replacement on invalid scalar values.
693--
766--
694-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
767-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
695--
768--
@@ -697,11 +770,13 @@ minimum t = S.minimum (stream t)
697--
770--
698-- > last (scanl f z xs) == foldl f z xs.
771-- > last (scanl f z xs) == foldl f z xs.
699scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
772scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
700−scanl f z t = unstream (S.scanl f z (stream t))
773+scanl f z t = unstream (S.scanl g z (stream t))
774+ where g a b = safe (f a b)
701{-# INLINE scanl #-}
775{-# INLINE scanl #-}
702
776
703-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
777-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
704−-- value argument. This function is subject to array fusion.
778+-- value argument. Subject to fusion. Performs replacement on
779+-- invalid scalar values.
705--
780--
706-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
781-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
707scanl1 :: (Char -> Char -> Char) -> Text -> Text
782scanl1 :: (Char -> Char -> Char) -> Text -> Text
@@ -710,21 +785,24 @@ scanl1 f t0 = case uncons t0 of
710 Just (t,ts) -> scanl f t ts
785 Just (t,ts) -> scanl f t ts
711{-# INLINE scanl1 #-}
786{-# INLINE scanl1 #-}
712
787
713−-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'.
788+-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
789+-- replacement on invalid scalar values.
714--
790--
715-- > scanr f v == reverse . scanl (flip f) v . reverse
791-- > scanr f v == reverse . scanl (flip f) v . reverse
716scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
792scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
717−scanr f v = reverse . scanl (flip f) v . reverse
793+scanr f v = reverse . scanl g v . reverse
794+ where g a b = safe (f b a)
718
795
719-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
796-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
720−-- value argument.
797+-- value argument. Performs replacement on invalid scalar values.
721scanr1 :: (Char -> Char -> Char) -> Text -> Text
798scanr1 :: (Char -> Char -> Char) -> Text -> Text
722scanr1 f t | null t = empty
799scanr1 f t | null t = empty
723 | otherwise = scanr f (last t) (init t)
800 | otherwise = scanr f (last t) (init t)
724
801
725-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
802-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
726-- function to each element of a 'Text', passing an accumulating
803-- function to each element of a 'Text', passing an accumulating
727−-- parameter from left to right, and returns a final 'Text'.
804+-- parameter from left to right, and returns a final 'Text'. Performs
805+-- replacement on invalid scalar values.
728mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
806mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
729mapAccumL f = go
807mapAccumL f = go
730 where
808 where
@@ -738,7 +816,7 @@ mapAccumL f = go
738-- a strict 'foldr'; it applies a function to each element of a
816-- a strict 'foldr'; it applies a function to each element of a
739-- 'Text', passing an accumulating parameter from right to left, and
817-- 'Text', passing an accumulating parameter from right to left, and
740-- returning a final value of this accumulator together with the new
818-- returning a final value of this accumulator together with the new
741−-- 'Text'.
819+-- 'Text'. Performs replacement on invalid scalar values.
742mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
820mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
743mapAccumR f = go
821mapAccumR f = go
744 where
822 where
@@ -762,7 +840,7 @@ replicate n t
762-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
840-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
763-- value of every element. Subject to fusion.
841-- value of every element. Subject to fusion.
764replicateChar :: Int64 -> Char -> Text
842replicateChar :: Int64 -> Char -> Text
765−replicateChar n c = unstream (S.replicateCharI n c)
843+replicateChar n c = unstream (S.replicateCharI n (safe c))
766{-# INLINE replicateChar #-}
844{-# INLINE replicateChar #-}
767
845
768{-# RULES
846{-# RULES
@@ -775,9 +853,10 @@ replicateChar n c = unstream (S.replicateCharI n c)
775-- 'Text' from a seed value. The function takes the element and
853-- 'Text' from a seed value. The function takes the element and
776-- returns 'Nothing' if it is done producing the 'Text', otherwise
854-- returns 'Nothing' if it is done producing the 'Text', otherwise
777-- 'Just' @(a,b)@. In this case, @a@ is the next 'Char' in the
855-- 'Just' @(a,b)@. In this case, @a@ is the next 'Char' in the
778−-- string, and @b@ is the seed value for further production.
856+-- string, and @b@ is the seed value for further production. Performs
857+-- replacement on invalid scalar values.
779−unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
858+unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
780−unfoldr f s = unstream (S.unfoldr f s)
859+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
781{-# INLINE unfoldr #-}
860{-# INLINE unfoldr #-}
782
861
783-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
862-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -785,8 +864,9 @@ unfoldr f s = unstream (S.unfoldr f s)
785-- first argument to 'unfoldrN'. This function is more efficient than
864-- first argument to 'unfoldrN'. This function is more efficient than
786-- 'unfoldr' when the maximum length of the result is known and
865-- 'unfoldr' when the maximum length of the result is known and
787-- correct, otherwise its performance is similar to 'unfoldr'.
866-- correct, otherwise its performance is similar to 'unfoldr'.
867+-- Performs replacement on invalid scalar values.
788−unfoldrN :: Int64 -> (a -> Maybe (Char,a)) -> a -> Text
868+unfoldrN :: Int64 -> (a -> Maybe (Char,a)) -> a -> Text
789−unfoldrN n f s = unstream (S.unfoldrN n f s)
869+unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
790{-# INLINE unfoldrN #-}
870{-# INLINE unfoldrN #-}
791
871
792-- | /O(n)/ 'take' @n@, applied to a 'Text', returns the prefix of the
872-- | /O(n)/ 'take' @n@, applied to a 'Text', returns the prefix of the
@@ -847,9 +927,9 @@ dropWords i t0
847 where len' = fromIntegral len
927 where len' = fromIntegral len
848 n' = fromIntegral n
928 n' = fromIntegral n
849
929
850−-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text', returns
930+-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
851−-- the longest prefix (possibly empty) of elements that satisfy @p@.
931+-- returns the longest prefix (possibly empty) of elements that
852−-- This function is subject to array fusion.
932+-- satisfy @p@. Subject to fusion.
853takeWhile :: (Char -> Bool) -> Text -> Text
933takeWhile :: (Char -> Bool) -> Text -> Text
854takeWhile p t0 = takeWhile' t0
934takeWhile p t0 = takeWhile' t0
855 where takeWhile' Empty = Empty
935 where takeWhile' Empty = Empty
@@ -868,7 +948,7 @@ takeWhile p t0 = takeWhile' t0
868 #-}
948 #-}
869
949
870-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
950-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
871−-- 'takeWhile' @p@ @t@. This function is subject to array fusion.
951+-- 'takeWhile' @p@ @t@. Subject to fusion.
872dropWhile :: (Char -> Bool) -> Text -> Text
952dropWhile :: (Char -> Bool) -> Text -> Text
873dropWhile p t0 = dropWhile' t0
953dropWhile p t0 = dropWhile' t0
874 where dropWhile' Empty = Empty
954 where dropWhile' Empty = Empty
@@ -1193,8 +1273,7 @@ unwords = intercalate (singleton ' ')
1193{-# INLINE unwords #-}
1273{-# INLINE unwords #-}
1194
1274
1195-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1275-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1196−-- 'True' iff the first is a prefix of the second. This function is
1276+-- 'True' iff the first is a prefix of the second. Subject to fusion.
1197−-- subject to fusion.
1198isPrefixOf :: Text -> Text -> Bool
1277isPrefixOf :: Text -> Text -> Bool
1199isPrefixOf Empty _ = True
1278isPrefixOf Empty _ = True
1200isPrefixOf _ Empty = False
1279isPrefixOf _ Empty = False
@@ -1340,8 +1419,7 @@ count pat src
1340 #-}
1419 #-}
1341
1420
1342-- | /O(n)/ The 'countChar' function returns the number of times the
1421-- | /O(n)/ The 'countChar' function returns the number of times the
1343−-- query element appears in the given 'Text'. This function is subject
1422+-- query element appears in the given 'Text'. Subject to fusion.
1344−-- to fusion.
1345countChar :: Char -> Text -> Int64
1423countChar :: Char -> Text -> Int64
1346countChar c t = S.countChar c (stream t)
1424countChar c t = S.countChar c (stream t)
1347
1425
@@ -1355,8 +1433,10 @@ zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1355
1433
1356-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1434-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1357-- given as the first argument, instead of a tupling function.
1435-- given as the first argument, instead of a tupling function.
1436+-- Performs replacement on invalid scalar values.
1358zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1437zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1359−zipWith f t1 t2 = unstream (S.zipWith f (stream t1) (stream t2))
1438+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1439+ where g a b = safe (f a b)
1360{-# INLINE [0] zipWith #-}
1440{-# INLINE [0] zipWith #-}
1361
1441
1362revChunks :: [T.Text] -> Text
1442revChunks :: [T.Text] -> Text
text.cabal+10−5
@@ -1,5 +1,5 @@
1name: text
1name: text
2−version: 0.10.0.0
2+version: 0.10.0.2
3homepage: http://bitbucket.org/bos/text
3homepage: http://bitbucket.org/bos/text
4bug-reports: http://bitbucket.org/bos/text/issues
4bug-reports: http://bitbucket.org/bos/text/issues
5synopsis: An efficient packed Unicode text type.
5synopsis: An efficient packed Unicode text type.
@@ -15,17 +15,22 @@ description:
15 .
15 .
16 The 'Text' type provides character-encoding, type-safe case
16 The 'Text' type provides character-encoding, type-safe case
17 conversion via whole-string case conversion functions. It also
17 conversion via whole-string case conversion functions. It also
18− provides a range of functions for converting Text values to and from
18+ provides a range of functions for converting 'Text' values to and from
19− 'ByteStrings', using several standard encodings (see the 'text-icu'
19+ 'ByteStrings', using several standard encodings.
20− package for a much larger variety of encoding functions).
21 .
20 .
22 Efficient locale-sensitive support for text IO is also supported.
21 Efficient locale-sensitive support for text IO is also supported.
23 .
22 .
24− This module is intended to be imported qualified, to avoid name
23+ These modules are intended to be imported qualified, to avoid name
25 clashes with Prelude functions, e.g.
24 clashes with Prelude functions, e.g.
26 .
25 .
27 > import qualified Data.Text as T
26 > import qualified Data.Text as T
28 .
27 .
28+ To use an extended and very rich family of functions for working
29+ with Unicode text (including normalization, regular expressions,
30+ non-standard encodings, text breaking, and locales), see
31+ the @text-icu@ package:
32+ <http://hackage.haskell.org/package/text-icu>
33+ .
29license: BSD3
34license: BSD3
30license-file: LICENSE
35license-file: LICENSE
31author: Bryan O'Sullivan <bos@serpentine.com>
36author: Bryan O'Sullivan <bos@serpentine.com>