@@ -13,20 +13,31 @@
13-- Stability : experimental
13-- Stability : experimental
14-- Portability : GHC
14-- Portability : GHC
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.
20+-- /Note/: Read below the synopsis for important notes on the use of
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.
23-- > import qualified Data.Text as T
26-- > import qualified Data.Text as T
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>
25module Data.Text
33module Data.Text
27 -- * Strict vs lazy types
35 -- * Strict vs lazy types
28 -- $strict
36 -- $strict
30 -- * Fusion
41 -- * Fusion
31 -- $fusion
42 -- $fusion
@@ -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.
246+-- A 'Text' value is a sequence of Unicode scalar values, as defined
247+-- in §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+-- (§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.
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 \"�\" (U+FFFD). Functions that perform this
260+-- inspection and replacement are documented with the phrase
261+-- \"Performs replacement on invalid scalar values\".
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, §3.5:
269+-- <http://unicode.org/reports/tr36/#Deletion_of_Noncharacters>)
233-- $fusion
271-- $fusion
235-- Most of the functions in this module are subject to /fusion/,
273-- Most of the functions in this module are subject to /fusion/,
@@ -320,9 +358,10 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
320-- -----------------------------------------------------------------------------
358-- -----------------------------------------------------------------------------
321-- * Conversion to/from 'Text'
359-- * Conversion to/from 'Text'
323−-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to fusion.
361+-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
362+-- fusion. Performs replacement on invalid scalar values.
324pack :: String -> Text
363pack :: String -> Text
325−pack = unstream . S.streamList
364+pack = unstream . S.streamList . L.map safe
326{-# INLINE [1] pack #-}
365{-# INLINE [1] pack #-}
328-- | /O(n)/ Convert a Text into a String. Subject to fusion.
367-- | /O(n)/ Convert a Text into a String. Subject to fusion.
@@ -330,10 +369,10 @@ unpack :: Text -> String
330unpack = S.unstreamList . stream
369unpack = S.unstreamList . stream
331{-# INLINE [1] unpack #-}
370{-# INLINE [1] unpack #-}
333−-- | /O(1)/ Convert a character into a Text.
372+-- | /O(1)/ Convert a character into a Text. Subject to fusion.
334−-- Subject to fusion.
373+-- Performs replacement on invalid scalar values.
335singleton :: Char -> Text
374singleton :: Char -> Text
336−singleton = unstream . S.singleton
375+singleton = unstream . S.singleton . safe
337{-# INLINE [1] singleton #-}
376{-# INLINE [1] singleton #-}
339-- -----------------------------------------------------------------------------
378-- -----------------------------------------------------------------------------
@@ -341,15 +380,17 @@ singleton = unstream . S.singleton
342-- | /O(n)/ Adds a character to the front of a 'Text'. This function
381-- | /O(n)/ Adds a character to the front of a 'Text'. This function
343-- is more costly than its 'List' counterpart because it requires
382-- is more costly than its 'List' counterpart because it requires
344−-- copying a new array. Subject to fusion.
383+-- copying a new array. Subject to fusion. Performs replacement on
384+-- invalid scalar values.
345cons :: Char -> Text -> Text
385cons :: Char -> Text -> Text
346−cons c t = unstream (S.cons c (stream t))
386+cons c t = unstream (S.cons (safe c) (stream t))
347{-# INLINE cons #-}
387{-# INLINE cons #-}
349-- | /O(n)/ Adds a character to the end of a 'Text'. This copies the
389-- | /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.
390-- entire array in the process, unless fused. Subject to fusion.
391+-- Performs replacement on invalid scalar values.
351snoc :: Text -> Char -> Text
392snoc :: Text -> Char -> Text
352−snoc t c = unstream (S.snoc (stream t) c)
393+snoc t c = unstream (S.snoc (stream t) (safe c))
353{-# INLINE snoc #-}
394{-# INLINE snoc #-}
355-- | /O(n)/ Appends one 'Text' to the other by copying both of them
396-- | /O(n)/ Appends one 'Text' to the other by copying both of them
@@ -523,9 +564,10 @@ compareLength t n = S.compareLengthI (stream t) n
523-- -----------------------------------------------------------------------------
564-- -----------------------------------------------------------------------------
524-- * Transformations
565-- * Transformations
525-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
566-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
526−-- each element of @t@. Subject to fusion.
567+-- each element of @t@. Subject to fusion. Performs replacement on
568+-- invalid scalar values.
527map :: (Char -> Char) -> Text -> Text
569map :: (Char -> Char) -> Text -> Text
528−map f t = unstream (S.map f (stream t))
570+map f t = unstream (S.map (safe . f) (stream t))
529{-# INLINE [1] map #-}
571{-# INLINE [1] map #-}
531-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
573-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
@@ -536,9 +578,10 @@ intercalate t = concat . (U.intersperse t)
536{-# INLINE intercalate #-}
578{-# INLINE intercalate #-}
538-- | /O(n)/ The 'intersperse' function takes a character and places it
580-- | /O(n)/ The 'intersperse' function takes a character and places it
539−-- between the characters of a 'Text'. Subject to fusion.
581+-- between the characters of a 'Text'. Subject to fusion. Performs
582+-- replacement on invalid scalar values.
540intersperse :: Char -> Text -> Text
583intersperse :: Char -> Text -> Text
541−intersperse c t = unstream (S.intersperse c (stream t))
584+intersperse c t = unstream (S.intersperse (safe c) (stream t))
542{-# INLINE intersperse #-}
585{-# INLINE intersperse #-}
544-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
587-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
@@ -615,7 +658,10 @@ toUpper t = unstream (S.toUpper (stream t))
615{-# INLINE toUpper #-}
658{-# INLINE toUpper #-}
617-- | /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
618−-- 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.
620-- > justifyLeft 7 'x' "foo" == "fooxxxx"
666-- > justifyLeft 7 'x' "foo" == "fooxxxx"
621-- > justifyLeft 3 'x' "foobar" == "foobar"
667-- > justifyLeft 3 'x' "foobar" == "foobar"
@@ -634,7 +680,10 @@ justifyLeft k c t
636-- | /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
637−-- specified fill character on the left. Examples:
683+-- specified fill character on the left. Performs replacement on
684+-- invalid scalar values.
639-- > justifyRight 7 'x' "bar" == "xxxxbar"
688-- > justifyRight 7 'x' "bar" == "xxxxbar"
640-- > justifyRight 3 'x' "foobar" == "foobar"
689-- > justifyRight 3 'x' "foobar" == "foobar"
@@ -645,8 +694,11 @@ justifyRight k c t
645 where len = length t
694 where len = length t
646{-# INLINE justifyRight #-}
695{-# INLINE justifyRight #-}
648−-- | /O(n)/ Center a string to the given length, using the
697+-- | /O(n)/ Center a string to the given length, using the specified
649−-- specified fill character on either side. Examples:
698+-- fill character on either side. Performs replacement on invalid
651-- > center 8 'x' "HS" = "xxxHSxxx"
703-- > center 8 'x' "HS" = "xxxHSxxx"
652center :: Int -> Char -> Text -> Text
704center :: Int -> Char -> Text -> Text
@@ -761,6 +813,7 @@ minimum t = S.minimum (stream t)
762-- | /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
763-- 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.
765-- > 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, ...]
@@ -768,11 +821,13 @@ minimum t = S.minimum (stream t)
769-- > last (scanl f z xs) == foldl f z xs.
822-- > last (scanl f z xs) == foldl f z xs.
770scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
823scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
771−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)
772{-# INLINE scanl #-}
826{-# INLINE scanl #-}
774-- | /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
775−-- value argument. Subject to fusion.
829+-- value argument. Subject to fusion. Performs replacement on
830+-- invalid scalar values.
777-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
832-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
778scanl1 :: (Char -> Char -> Char) -> Text -> Text
833scanl1 :: (Char -> Char -> Char) -> Text -> Text
@@ -780,15 +835,18 @@ scanl1 f t | null t = empty
780 | otherwise = scanl f (unsafeHead t) (unsafeTail t)
835 | otherwise = scanl f (unsafeHead t) (unsafeTail t)
781{-# INLINE scanl1 #-}
836{-# INLINE scanl1 #-}
783−-- | /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.
785-- > scanr f v == reverse . scanl (flip f) v . reverse
841-- > scanr f v == reverse . scanl (flip f) v . reverse
786scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
842scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
787−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)
788{-# INLINE scanr #-}
845{-# INLINE scanr #-}
790-- | /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
791−-- value argument. Subject to fusion.
848+-- value argument. Subject to fusion. Performs replacement on
849+-- invalid scalar values.
792scanr1 :: (Char -> Char -> Char) -> Text -> Text
850scanr1 :: (Char -> Char -> Char) -> Text -> Text
793scanr1 f t | null t = empty
851scanr1 f t | null t = empty
794 | otherwise = scanr f (last t) (init t)
852 | otherwise = scanr f (last t) (init t)
@@ -796,9 +854,11 @@ scanr1 f t | null t = empty
797-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
855-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
798-- function to each element of a 'Text', passing an accumulating
856-- function to each element of a 'Text', passing an accumulating
799−-- 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.
800mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
859mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
801−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)
802{-# INLINE mapAccumL #-}
862{-# INLINE mapAccumL #-}
804-- | The 'mapAccumR' function behaves like a combination of 'map' and
864-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -806,8 +866,10 @@ mapAccumL f z0 = S.mapAccumL f z0 . stream
806-- 'Text', passing an accumulating parameter from right to left, and
866-- 'Text', passing an accumulating parameter from right to left, and
807-- returning a final value of this accumulator together with the new
867-- returning a final value of this accumulator together with the new
808-- 'Text'.
868-- 'Text'.
869+-- Performs replacement on invalid scalar values.
809mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
870mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
810−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)
811{-# INLINE mapAccumR #-}
873{-# INLINE mapAccumR #-}
813-- -----------------------------------------------------------------------------
875-- -----------------------------------------------------------------------------
@@ -840,7 +902,7 @@ replicate n t@(Text a o l)
840-- | /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
841-- value of every element. Subject to fusion.
903-- value of every element. Subject to fusion.
842replicateChar :: Int -> Char -> Text
904replicateChar :: Int -> Char -> Text
843−replicateChar n c = unstream (S.replicateCharI n c)
905+replicateChar n c = unstream (S.replicateCharI n (safe c))
844{-# INLINE replicateChar #-}
906{-# INLINE replicateChar #-}
846-- | /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'
@@ -849,9 +911,9 @@ replicateChar n c = unstream (S.replicateCharI n c)
849-- returns 'Nothing' if it is done producing the 'Text', otherwise
911-- returns 'Nothing' if it is done producing the 'Text', otherwise
850-- '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
851-- string, and @b@ is the seed value for further production. Subject
913-- string, and @b@ is the seed value for further production. Subject
852−-- to fusion.
914+-- to fusion. Performs replacement on invalid scalar values.
853unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
915unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
854−unfoldr f s = unstream (S.unfoldr f s)
916+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
855{-# INLINE unfoldr #-}
917{-# INLINE unfoldr #-}
857-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
919-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -859,9 +921,9 @@ unfoldr f s = unstream (S.unfoldr f s)
859-- first argument to 'unfoldrN'. This function is more efficient than
921-- first argument to 'unfoldrN'. This function is more efficient than
860-- 'unfoldr' when the maximum length of the result is known and
922-- 'unfoldr' when the maximum length of the result is known and
861-- correct, otherwise its performance is similar to 'unfoldr'. Subject
923-- correct, otherwise its performance is similar to 'unfoldr'. Subject
862−-- to fusion.
924+-- to fusion. Performs replacement on invalid scalar values.
863unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
925unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
864−unfoldrN n f s = unstream (S.unfoldrN n f s)
926+unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
865{-# INLINE unfoldrN #-}
927{-# INLINE unfoldrN #-}
867-- -----------------------------------------------------------------------------
929-- -----------------------------------------------------------------------------
@@ -1299,8 +1361,10 @@ zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1300-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1362-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1301-- given as the first argument, instead of a tupling function.
1363-- given as the first argument, instead of a tupling function.
1364+-- Performs replacement on invalid scalar values.
1302zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1365zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1303−zipWith f t1 t2 = unstream (S.zipWith f (stream t1) (stream t2))
1366+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1367+ where g a b = safe (f a b)
1304{-# INLINE [0] zipWith #-}
1368{-# INLINE [0] zipWith #-}
1306-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1370-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's