@@ -6,7 +6,7 @@
6#if __GLASGOW_HASKELL__ >= 708
6#if __GLASGOW_HASKELL__ >= 708
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
9−{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI, UnliftedFFITypes #-}
9+
11-- Module : Data.Text
11-- Module : Data.Text
12-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
12-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
@@ -15,6 +15,7 @@
16-- License : BSD-style
16-- License : BSD-style
17-- Maintainer : bos@serpentine.com
17-- Maintainer : bos@serpentine.com
18+-- Stability : experimental
18-- Portability : GHC
19-- Portability : GHC
20-- A time and space-efficient implementation of Unicode text.
21-- A time and space-efficient implementation of Unicode text.
@@ -31,9 +32,8 @@
32-- To use an extended and very rich family of functions for working
33-- To use an extended and very rich family of functions for working
33-- with Unicode text (including normalization, regular expressions,
34-- with Unicode text (including normalization, regular expressions,
34−-- non-standard encodings, text breaking, and locales), see the
35+-- non-standard encodings, text breaking, and locales), see
35−-- <http://hackage.haskell.org/package/text-icu text-icu package >.
36+-- <http://hackage.haskell.org/package/text-icu the text-icu package >.
38module Data.Text
38module Data.Text
@@ -43,9 +43,6 @@ module Data.Text
43 -- * Acceptable data
43 -- * Acceptable data
44 -- $replacement
44 -- $replacement
46− -- * Definition of character
47− -- $character_definition
49 -- * Fusion
46 -- * Fusion
50 -- $fusion
47 -- $fusion
@@ -63,7 +60,6 @@ module Data.Text
@@ -227,17 +223,16 @@ import qualified Data.Text.Internal.Fusion as S
227import qualified Data.Text.Internal.Fusion.Common as S
223import qualified Data.Text.Internal.Fusion.Common as S
228import Data.Text.Encoding (decodeUtf8', encodeUtf8)
224import Data.Text.Encoding (decodeUtf8', encodeUtf8)
229import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
230−-- import Data.Text.Internal.Private (span_)
226+import Data.Text.Internal.Private (span_)
231−import Data.Text.Internal (Text(..), empty, empty_, firstf, mul, safe, text)
227+import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
232−-- import Data.Text.Show (singleton, unpack, unpackCString#)
228+import Data.Text.Show (singleton, unpack, unpackCString#)
233import qualified Prelude as P
229import qualified Prelude as P
234import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
235− reverseIter_, unsafeHead, unsafeTail,
231+ reverseIter_, unsafeHead, unsafeTail)
236− unsafeDupablePerformIO)
237import Data.Text.Internal.Unsafe.Char (unsafeChr)
232import Data.Text.Internal.Unsafe.Char (unsafeChr)
238import qualified Data.Text.Internal.Functions as F
233import qualified Data.Text.Internal.Functions as F
239import qualified Data.Text.Internal.Encoding.Utf16 as U16
234import qualified Data.Text.Internal.Encoding.Utf16 as U16
240−-- import Data.Text.Internal.Search (indices)
235+import Data.Text.Internal.Search (indices)
241#if defined(__HADDOCK__)
236#if defined(__HADDOCK__)
242import Data.ByteString (ByteString)
237import Data.ByteString (ByteString)
243import qualified Data.Text.Lazy as L
238import qualified Data.Text.Lazy as L
@@ -251,80 +246,6 @@ import qualified GHC.Exts as Exts
251import Text.Printf (PrintfArg, formatArg, formatString)
246import Text.Printf (PrintfArg, formatArg, formatString)
254−-- GHCJS ------------------------------------------------------------
255−import Data.JSString.Internal.Type (JSString (..))
256−import qualified Data.JSString as JSS
257−import qualified Data.JSString.Internal.Fusion as SJS
258−import qualified Data.JSString.Internal.Fusion.Common as SJS
260−import GHC.Base (build)
261−import qualified GHC.CString as GHC
262−import GHC.Exts (Addr#, ByteArray#)
263−#if MIN_VERSION_ghcjs_prim(0,1,1)
264−import GHCJS.Prim (unsafeUnpackJSString#)
267−unpack :: Text -> String
268−unpack = coerce JSS.unpack
269−{-# INLINE [1] unpack #-}
271−singleton :: Char -> Text
272−singleton = coerce JSS.singleton
273−{-# INLINE [1] singleton #-}
276−"JSSTRING singleton -> fused" [~1] forall a.
277− singleton a = unstream (S.singleton (safe a))
278−"JSSTRING singleton -> unfused" [1] forall a.
279− unstream (S.singleton (safe a)) = singleton a
282−#if MIN_VERSION_ghcjs_prim(0,1,1)
283−{-# RULES "inlineJSString" forall s.
284− pack (GHC.Base.build (GHC.unpackFoldrCString# s)) =
285− Text (JSString (unsafeUnpackJSString# s)) #-}
288−-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
289−unpackCString# :: Addr# -> Text
290−unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
291−{-# NOINLINE unpackCString# #-}
293−{-# RULES "TEXT literal" forall a.
294− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
295− = unpackCString# a #-}
297−{-# RULES "TEXT literal UTF8" [1] forall a.
298− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
299− = unpackCString# a #-}
301−{-# RULES "TEXT empty literal" [1]
302− unstream (S.map safe (S.streamList []))
305−{-# RULES "TEXT singleton literal" [1] forall a.
306− unstream (S.map safe (S.streamList [a]))
309−#if MIN_VERSION_ghcjs_prim(0,1,1)
310−{-# RULES "TEXT literal prim" [0] forall a.
311− unpackCString# a = Text (JSString (unsafeUnpackJSString# a))
315−---------------------------------------------------------------------
316−-- $character_definition
318−-- This package uses the term /character/ to denote Unicode /code points/.
320−-- Note that this is not the same thing as a grapheme (e.g. a
321−-- composition of code points that form one visual symbol). For
322−-- instance, consider the grapheme \"ä\". This symbol has two
323−-- Unicode representations: a single code-point representation
324−-- @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code point),
325−-- and a two code point representation @U+0061@ (the \"@A@\" code
326−-- point) and @U+0308@ (the @COMBINING DIAERESIS@ code point).
328-- $strict
249-- $strict
330-- This package provides both strict and lazy 'Text' types. The
251-- This package provides both strict and lazy 'Text' types. The
@@ -400,31 +321,22 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
400-- Functions that can be fused by the compiler are documented with the
321-- Functions that can be fused by the compiler are documented with the
401-- phrase \"Subject to fusion\".
322-- phrase \"Subject to fusion\".
403−-- instance Eq Text where
404−-- Text arrA offA lenA == Text arrB offB lenB
405−-- | lenA == lenB = A.equal arrA offA arrB offB lenA
406−-- | otherwise = False
407−-- {-# INLINE (==) #-}
408instance Eq Text where
324instance Eq Text where
409− Text x == Text y = x == y
325+ Text arrA offA lenA == Text arrB offB lenB
410−
326+ | lenA == lenB = A.equal arrA offA arrB offB lenA
411−-- instance Ord Text where
327+ | otherwise = False
412−-- compare = compareText
328+ {-# INLINE (==) #-}
414instance Ord Text where
330instance Ord Text where
415− compare (Text x) (Text y) = compare x y
331+ compare = compareText
417instance Read Text where
333instance Read Text where
418 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
420#if MIN_VERSION_base(4,9,0)
336#if MIN_VERSION_base(4,9,0)
421−-- | Non-orphan 'Semigroup' instance only defined for
422−-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
337+-- Semigroup orphan instances for older GHCs are provided by
424−-- the [semigroups](http://hackage.haskell.org/package/semigroups)
425−-- package
338+-- 'semigroups` package
428instance Semigroup Text where
340instance Semigroup Text where
429 (<>) = append
341 (<>) = append
@@ -442,7 +354,6 @@ instance IsString Text where
442 fromString = pack
354 fromString = pack
444#if __GLASGOW_HASKELL__ >= 708
356#if __GLASGOW_HASKELL__ >= 708
446instance Exts.IsList Text where
357instance Exts.IsList Text where
447 type Item Text = Char
358 type Item Text = Char
448 fromList = pack
359 fromList = pack
@@ -453,7 +364,6 @@ instance Exts.IsList Text where
453instance NFData Text where rnf !_ = ()
364instance NFData Text where rnf !_ = ()
457instance Binary Text where
367instance Binary Text where
458 put t = put (encodeUtf8 t)
368 put t = put (encodeUtf8 t)
@@ -487,10 +397,8 @@ instance Data Text where
488#if MIN_VERSION_base(4,7,0)
398#if MIN_VERSION_base(4,7,0)
489-- | Only defined for @base-4.7.0.0@ and later
399-- | Only defined for @base-4.7.0.0@ and later
492instance PrintfArg Text where
400instance PrintfArg Text where
493− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
496packConstr :: Constr
404packConstr :: Constr
@@ -499,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
499textDataType :: DataType
407textDataType :: DataType
500textDataType = mkDataType "Data.Text.Text" [packConstr]
408textDataType = mkDataType "Data.Text.Text" [packConstr]
502−-- -- | /O(n)/ Compare two 'Text' values lexicographically.
410+-- | /O(n)/ Compare two 'Text' values lexicographically.
503−-- compareText :: Text -> Text -> Ordering
411+compareText :: Text -> Text -> Ordering
504−-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
412+compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
505−-- | lenA == 0 && lenB == 0 = EQ
413+ | lenA == 0 && lenB == 0 = EQ
506−-- | otherwise = go 0 0
414+ | otherwise = go 0 0
508−-- go !i !j
416+ go !i !j
509−-- | i >= lenA || j >= lenB = compare lenA lenB
417+ | i >= lenA || j >= lenB = compare lenA lenB
510−-- | a < b = LT
418+ | a < b = LT
511−-- | a > b = GT
419+ | a > b = GT
512−-- | otherwise = go (i+di) (j+dj)
420+ | otherwise = go (i+di) (j+dj)
513−-- where Iter a di = iter ta i
421+ where Iter a di = iter ta i
514−-- Iter b dj = iter tb j
422+ Iter b dj = iter tb j
516-- -----------------------------------------------------------------------------
424-- -----------------------------------------------------------------------------
517-- * Conversion to/from 'Text'
425-- * Conversion to/from 'Text'
@@ -519,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
519-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
520-- fusion. Performs replacement on invalid scalar values.
428-- fusion. Performs replacement on invalid scalar values.
521pack :: String -> Text
429pack :: String -> Text
522−pack = coerce JSS.pack
523−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
524{-# INLINE [1] pack #-}
431{-# INLINE [1] pack #-}
526-- -----------------------------------------------------------------------------
433-- -----------------------------------------------------------------------------
@@ -531,8 +438,7 @@ pack = coerce JSS.pack
531-- copying a new array. Subject to fusion. Performs replacement on
438-- copying a new array. Subject to fusion. Performs replacement on
532-- invalid scalar values.
439-- invalid scalar values.
533cons :: Char -> Text -> Text
440cons :: Char -> Text -> Text
534−cons = coerce JSS.cons
535−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
536{-# INLINE cons #-}
442{-# INLINE cons #-}
538infixr 5 `cons`
444infixr 5 `cons`
@@ -541,27 +447,25 @@ infixr 5 `cons`
541-- entire array in the process, unless fused. Subject to fusion.
447-- entire array in the process, unless fused. Subject to fusion.
542-- Performs replacement on invalid scalar values.
448-- Performs replacement on invalid scalar values.
543snoc :: Text -> Char -> Text
449snoc :: Text -> Char -> Text
544−snoc = coerce JSS.snoc
545−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
546{-# INLINE snoc #-}
451{-# INLINE snoc #-}
548-- | /O(n)/ Appends one 'Text' to the other by copying both of them
453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
549-- into a new 'Text'. Subject to fusion.
454-- into a new 'Text'. Subject to fusion.
550append :: Text -> Text -> Text
455append :: Text -> Text -> Text
551−append = coerce JSS.append
552−-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
456+append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
553−-- | len1 == 0 = b
457+ | len1 == 0 = b
554−-- | len2 == 0 = a
458+ | len2 == 0 = a
555−-- | len > 0 = Text (A.run x) 0 len
459+ | len > 0 = Text (A.run x) 0 len
556−-- | otherwise = overflowError "append"
460+ | otherwise = overflowError "append"
558−-- len = len1+len2
462+ len = len1+len2
559−-- x :: ST s (A.MArray s)
463+ x :: ST s (A.MArray s)
561−-- arr <- A.new len
465+ arr <- A.new len
562−-- A.copyI arr 0 arr1 off1 len1
466+ A.copyI arr 0 arr1 off1 len1
563−-- A.copyI arr len1 arr2 off2 len
467+ A.copyI arr len1 arr2 off2 len
564−-- return arr
468+ return arr
565{-# NOINLINE append #-}
469{-# NOINLINE append #-}
@@ -574,18 +478,16 @@ append = coerce JSS.append
574-- | /O(1)/ Returns the first character of a 'Text', which must be
478-- | /O(1)/ Returns the first character of a 'Text', which must be
575-- non-empty. Subject to fusion.
479-- non-empty. Subject to fusion.
576head :: Text -> Char
480head :: Text -> Char
577−head = coerce JSS.head
578−-- head t = S.head (stream t)
481+head t = S.head (stream t)
579{-# INLINE head #-}
482{-# INLINE head #-}
581-- | /O(1)/ Returns the first character and rest of a 'Text', or
484-- | /O(1)/ Returns the first character and rest of a 'Text', or
582-- 'Nothing' if empty. Subject to fusion.
485-- 'Nothing' if empty. Subject to fusion.
583uncons :: Text -> Maybe (Char, Text)
486uncons :: Text -> Maybe (Char, Text)
584−uncons = coerce JSS.uncons
585−-- uncons t@(Text arr off len)
487+uncons t@(Text arr off len)
586−-- | len <= 0 = Nothing
488+ | len <= 0 = Nothing
587−-- | otherwise = Just $ let !(Iter c d) = iter t 0
489+ | otherwise = Just $ let !(Iter c d) = iter t 0
588−-- in (c, text arr (off+d) (len-d))
490+ in (c, text arr (off+d) (len-d))
589{-# INLINE [1] uncons #-}
491{-# INLINE [1] uncons #-}
591-- | Lifted from Control.Arrow and specialized.
493-- | Lifted from Control.Arrow and specialized.
@@ -595,13 +497,12 @@ second f (a, b) = (a, f b)
595-- | /O(1)/ Returns the last character of a 'Text', which must be
497-- | /O(1)/ Returns the last character of a 'Text', which must be
596-- non-empty. Subject to fusion.
498-- non-empty. Subject to fusion.
597last :: Text -> Char
499last :: Text -> Char
598−last = coerce JSS.last
599−-- last (Text arr off len)
500+last (Text arr off len)
600−-- | len <= 0 = emptyError "last"
501+ | len <= 0 = emptyError "last"
601−-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
502+ | n < 0xDC00 || n > 0xDFFF = unsafeChr n
602−-- | otherwise = U16.chr2 n0 n
503+ | otherwise = U16.chr2 n0 n
603−-- where n = A.unsafeIndex arr (off+len-1)
504+ where n = A.unsafeIndex arr (off+len-1)
604−-- n0 = A.unsafeIndex arr (off+len-2)
505+ n0 = A.unsafeIndex arr (off+len-2)
605{-# INLINE [1] last #-}
506{-# INLINE [1] last #-}
@@ -614,11 +515,10 @@ last = coerce JSS.last
614-- | /O(1)/ Returns all characters after the head of a 'Text', which
515-- | /O(1)/ Returns all characters after the head of a 'Text', which
615-- must be non-empty. Subject to fusion.
516-- must be non-empty. Subject to fusion.
616tail :: Text -> Text
517tail :: Text -> Text
617−tail = coerce JSS.tail
618−-- tail t@(Text arr off len)
518+tail t@(Text arr off len)
619−-- | len <= 0 = emptyError "tail"
519+ | len <= 0 = emptyError "tail"
620−-- | otherwise = text arr (off+d) (len-d)
520+ | otherwise = text arr (off+d) (len-d)
621−-- where d = iter_ t 0
521+ where d = iter_ t 0
622{-# INLINE [1] tail #-}
522{-# INLINE [1] tail #-}
@@ -631,12 +531,11 @@ tail = coerce JSS.tail
631-- | /O(1)/ Returns all but the last character of a 'Text', which must
531-- | /O(1)/ Returns all but the last character of a 'Text', which must
632-- be non-empty. Subject to fusion.
532-- be non-empty. Subject to fusion.
633init :: Text -> Text
533init :: Text -> Text
634−init = coerce JSS.init
635−-- init (Text arr off len) | len <= 0 = emptyError "init"
534+init (Text arr off len) | len <= 0 = emptyError "init"
636−-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
535+ | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
637−-- | otherwise = text arr off (len-1)
536+ | otherwise = text arr off (len-1)
639−-- n = A.unsafeIndex arr (off+len-1)
538+ n = A.unsafeIndex arr (off+len-1)
640{-# INLINE [1] init #-}
539{-# INLINE [1] init #-}
@@ -646,29 +545,14 @@ init = coerce JSS.init
646 unstream (S.init (stream t)) = init t
545 unstream (S.init (stream t)) = init t
649−-- | /O(1)/ Returns all but the last character and the last character of a
650−-- 'Text', or 'Nothing' if empty.
653−unsnoc :: Text -> Maybe (Text, Char)
654−unsnoc = coerce JSS.unsnoc
655−-- unsnoc (Text arr off len)
656−-- | len <= 0 = Nothing
657−-- | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
658−-- | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
659−-- where n = A.unsafeIndex arr (off+len-1)
660−-- n0 = A.unsafeIndex arr (off+len-2)
661−{-# INLINE [1] unsnoc #-}
663-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
664-- fusion.
549-- fusion.
665null :: Text -> Bool
550null :: Text -> Bool
666−null = coerce JSS.null
667−-- null (Text _arr _off len) =
551+null (Text _arr _off len) =
668−-- #if defined(ASSERTS)
552+#if defined(ASSERTS)
669−-- assert (len >= 0) $
553+ assert (len >= 0) $
671−-- len <= 0
555+ len <= 0
672{-# INLINE [1] null #-}
556{-# INLINE [1] null #-}
@@ -680,15 +564,14 @@ null = coerce JSS.null
681-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
682-- Subject to fusion.
566-- Subject to fusion.
683−-- isSingleton :: Text -> Bool
567+isSingleton :: Text -> Bool
684−-- isSingleton = S.isSingleton . stream
568+isSingleton = S.isSingleton . stream
685−-- {-# INLINE isSingleton #-}
569+{-# INLINE isSingleton #-}
687-- | /O(n)/ Returns the number of characters in a 'Text'.
571-- | /O(n)/ Returns the number of characters in a 'Text'.
688-- Subject to fusion.
572-- Subject to fusion.
689length :: Text -> Int
573length :: Text -> Int
690−length = coerce JSS.length
691−-- length t = S.length (stream t)
574+length t = S.length (stream t)
692{-# INLINE [0] length #-}
575{-# INLINE [0] length #-}
693-- length needs to be phased after the compareN/length rules otherwise
576-- length needs to be phased after the compareN/length rules otherwise
694-- it may inline before the rules have an opportunity to fire.
577-- it may inline before the rules have an opportunity to fire.
@@ -741,58 +624,29 @@ compareLength t n = S.compareLengthI (stream t) n
741-- -----------------------------------------------------------------------------
624-- -----------------------------------------------------------------------------
742-- * Transformations
625-- * Transformations
743-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
626-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
744−-- each element of @t@.
748−-- >>> let message = pack "I am not angry. Not at all."
749−-- >>> T.map (\c -> if c == '.' then '!' else c) message
750−-- "I am not angry! Not at all!"
752−-- Subject to fusion. Performs replacement on invalid scalar values.
627+-- each element of @t@. Subject to fusion. Performs replacement on
628+-- invalid scalar values.
753map :: (Char -> Char) -> Text -> Text
629map :: (Char -> Char) -> Text -> Text
755−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
756{-# INLINE [1] map #-}
631{-# INLINE [1] map #-}
758-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
759-- 'Text's and concatenates the list after interspersing the first
634-- 'Text's and concatenates the list after interspersing the first
760-- argument between each element of the list.
635-- argument between each element of the list.
764−-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
765−-- "WeNI!seekNI!theNI!HolyNI!Grail"
766intercalate :: Text -> [Text] -> Text
636intercalate :: Text -> [Text] -> Text
767−intercalate = coerce JSS.intercalate
768−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
769{-# INLINE intercalate #-}
638{-# INLINE intercalate #-}
771-- | /O(n)/ The 'intersperse' function takes a character and places it
640-- | /O(n)/ The 'intersperse' function takes a character and places it
772−-- between the characters of a 'Text'.
641+-- between the characters of a 'Text'. Subject to fusion. Performs
776−-- >>> T.intersperse '.' "SHIELD"
779−-- Subject to fusion. Performs replacement on invalid scalar values.
642+-- replacement on invalid scalar values.
780intersperse :: Char -> Text -> Text
643intersperse :: Char -> Text -> Text
781−intersperse = coerce JSS.intersperse
782−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
783{-# INLINE intersperse #-}
645{-# INLINE intersperse #-}
785−-- | /O(n)/ Reverse the characters of a string.
647+-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
789−-- >>> T.reverse "desrever"
792−-- Subject to fusion.
793reverse :: Text -> Text
648reverse :: Text -> Text
794−reverse = coerce JSS.reverse
795−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
796{-# INLINE reverse #-}
650{-# INLINE reverse #-}
798-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -809,14 +663,12 @@ reverse = coerce JSS.reverse
809-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
663-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
810-- be replaced recursively:
664-- be replaced recursively:
812−-- >>> replace "oo" "foo" "oo"
666+-- > replace "oo" "foo" "oo" == "foo"
815-- In cases where several instances of @needle@ overlap, only the
668-- In cases where several instances of @needle@ overlap, only the
816-- first one will be replaced:
669-- first one will be replaced:
818−-- >>> replace "ofo" "bar" "ofofo"
671+-- > replace "ofo" "bar" "ofofo" == "barfo"
821-- In (unlikely) bad cases, this function's time complexity degrades
673-- In (unlikely) bad cases, this function's time complexity degrades
822-- towards /O(n*m)/.
674-- towards /O(n*m)/.
@@ -828,29 +680,28 @@ replace :: Text
829 -- ^ @haystack@ in which to search.
681 -- ^ @haystack@ in which to search.
831−replace = coerce JSS.replace
832−-- replace needle@(Text _ _ neeLen)
683+replace needle@(Text _ _ neeLen)
833−-- (Text repArr repOff repLen)
684+ (Text repArr repOff repLen)
834−-- haystack@(Text hayArr hayOff hayLen)
685+ haystack@(Text hayArr hayOff hayLen)
835−-- | neeLen == 0 = emptyError "replace"
686+ | neeLen == 0 = emptyError "replace"
836−-- | L.null ixs = haystack
687+ | L.null ixs = haystack
837−-- | len > 0 = Text (A.run x) 0 len
688+ | len > 0 = Text (A.run x) 0 len
838−-- | otherwise = empty
689+ | otherwise = empty
840−-- ixs = indices needle haystack
691+ ixs = indices needle haystack
841−-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
692+ len = hayLen - (neeLen - repLen) `mul` L.length ixs
842−-- x :: ST s (A.MArray s)
693+ x :: ST s (A.MArray s)
844−-- marr <- A.new len
695+ marr <- A.new len
845−-- let loop (i:is) o d = do
696+ let loop (i:is) o d = do
846−-- let d0 = d + i - o
697+ let d0 = d + i - o
847−-- d1 = d0 + repLen
698+ d1 = d0 + repLen
848−-- A.copyI marr d hayArr (hayOff+o) d0
699+ A.copyI marr d hayArr (hayOff+o) d0
849−-- A.copyI marr d0 repArr repOff d1
700+ A.copyI marr d0 repArr repOff d1
850−-- loop is (i + neeLen) d1
701+ loop is (i + neeLen) d1
851−-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
702+ loop [] o d = A.copyI marr d hayArr (hayOff+o) len
852−-- loop ixs 0 0
703+ loop ixs 0 0
853−-- return marr
704+ return marr
855-- ----------------------------------------------------------------------------
706-- ----------------------------------------------------------------------------
856-- ** Case conversions (folds)
707-- ** Case conversions (folds)
@@ -889,8 +740,7 @@ replace = coerce JSS.replace
889-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
890-- instead of itself.
741-- instead of itself.
891toCaseFold :: Text -> Text
742toCaseFold :: Text -> Text
892−-- toCaseFold t = unstream (S.toCaseFold (stream t))
743+toCaseFold t = unstream (S.toCaseFold (stream t))
893−toCaseFold = coerce JSS.toCaseFold
894{-# INLINE toCaseFold #-}
744{-# INLINE toCaseFold #-}
896-- | /O(n)/ Convert a string to lower case, using simple case
746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -901,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
901-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
902-- followed by \" ̇\" (combining dot above, U+0307).
752-- followed by \" ̇\" (combining dot above, U+0307).
903toLower :: Text -> Text
753toLower :: Text -> Text
904−-- toLower t = unstream (S.toLower (stream t))
754+toLower t = unstream (S.toLower (stream t))
905−toLower = coerce JSS.toLower
906{-# INLINE toLower #-}
755{-# INLINE toLower #-}
908-- | /O(n)/ Convert a string to upper case, using simple case
757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -912,8 +761,7 @@ toLower = coerce JSS.toLower
912-- instance, the German \"ß\" (eszett, U+00DF) maps to the
761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
913-- two-letter sequence \"SS\".
762-- two-letter sequence \"SS\".
914toUpper :: Text -> Text
763toUpper :: Text -> Text
915−-- toUpper t = unstream (S.toUpper (stream t))
764+toUpper t = unstream (S.toUpper (stream t))
916−toUpper = coerce JSS.toUpper
917{-# INLINE toUpper #-}
765{-# INLINE toUpper #-}
919-- | /O(n)/ Convert a string to title case, using simple case
767-- | /O(n)/ Convert a string to title case, using simple case
@@ -934,11 +782,8 @@ toUpper = coerce JSS.toUpper
934-- guides disagree on whether the book name \"The Hill of the Red
782-- guides disagree on whether the book name \"The Hill of the Red
935-- Fox\" is correctly title cased—but this function will
783-- Fox\" is correctly title cased—but this function will
936-- capitalize /every/ word.
784-- capitalize /every/ word.
939toTitle :: Text -> Text
785toTitle :: Text -> Text
940−toTitle = coerce JSS.toTitle
941−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
942{-# INLINE toTitle #-}
787{-# INLINE toTitle #-}
944-- | /O(n)/ Left-justify a string to the given length, using the
789-- | /O(n)/ Left-justify a string to the given length, using the
@@ -947,17 +792,13 @@ toTitle = coerce JSS.toTitle
948-- Examples:
793-- Examples:
950−-- >>> justifyLeft 7 'x' "foo"
795+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
953−-- >>> justifyLeft 3 'x' "foobar"
796+-- > justifyLeft 3 'x' "foobar" == "foobar"
955justifyLeft :: Int -> Char -> Text -> Text
797justifyLeft :: Int -> Char -> Text -> Text
956−justifyLeft = coerce JSS.justifyLeft
798+justifyLeft k c t
958−-- | len >= k = t
799+ | len >= k = t
959−-- | otherwise = t `append` replicateChar (k-len) c
800+ | otherwise = t `append` replicateChar (k-len) c
960−-- where len = length t
801+ where len = length t
961{-# INLINE [1] justifyLeft #-}
802{-# INLINE [1] justifyLeft #-}
@@ -973,17 +814,13 @@ justifyLeft = coerce JSS.justifyLeft
974-- Examples:
815-- Examples:
976−-- >>> justifyRight 7 'x' "bar"
817+-- > justifyRight 7 'x' "bar" == "xxxxbar"
979−-- >>> justifyRight 3 'x' "foobar"
818+-- > justifyRight 3 'x' "foobar" == "foobar"
981justifyRight :: Int -> Char -> Text -> Text
819justifyRight :: Int -> Char -> Text -> Text
982−justifyRight = coerce JSS.justifyRight
820+justifyRight k c t
983−-- justifyRight k c t
984−-- | len >= k = t
821+ | len >= k = t
985−-- | otherwise = replicateChar (k-len) c `append` t
822+ | otherwise = replicateChar (k-len) c `append` t
986−-- where len = length t
823+ where len = length t
987{-# INLINE justifyRight #-}
824{-# INLINE justifyRight #-}
989-- | /O(n)/ Center a string to the given length, using the specified
826-- | /O(n)/ Center a string to the given length, using the specified
@@ -992,34 +829,23 @@ justifyRight = coerce JSS.justifyRight
993-- Examples:
830-- Examples:
995−-- >>> center 8 'x' "HS"
832+-- > center 8 'x' "HS" = "xxxHSxxx"
997center :: Int -> Char -> Text -> Text
833center :: Int -> Char -> Text -> Text
998−center = coerce JSS.center
834+center k c t
1000−-- | len >= k = t
835+ | len >= k = t
1001−-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
836+ | otherwise = replicateChar l c `append` t `append` replicateChar r c
1002−-- where len = length t
837+ where len = length t
1003−-- d = k - len
838+ d = k - len
1004−-- r = d `quot` 2
839+ r = d `quot` 2
1005−-- l = d - r
840+ l = d - r
1006{-# INLINE center #-}
841{-# INLINE center #-}
1008-- | /O(n)/ The 'transpose' function transposes the rows and columns
843-- | /O(n)/ The 'transpose' function transposes the rows and columns
1009-- of its 'Text' argument. Note that this function uses 'pack',
844-- of its 'Text' argument. Note that this function uses 'pack',
1010-- 'unpack', and the list version of transpose, and is thus not very
845-- 'unpack', and the list version of transpose, and is thus not very
1011-- efficient.
846-- efficient.
1015−-- >>> transpose ["green","orange"]
1016−-- ["go","rr","ea","en","ng","e"]
1018−-- >>> transpose ["blue","red"]
1019−-- ["br","le","ud","e"]
1020transpose :: [Text] -> [Text]
847transpose :: [Text] -> [Text]
1021−transpose = coerce JSS.transpose
1022−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
1024-- -----------------------------------------------------------------------------
850-- -----------------------------------------------------------------------------
1025-- * Reducing 'Text's (folds)
851-- * Reducing 'Text's (folds)
@@ -1029,27 +855,23 @@ transpose = coerce JSS.transpose
1029-- reduces the 'Text' using the binary operator, from left to right.
855-- reduces the 'Text' using the binary operator, from left to right.
1030-- Subject to fusion.
856-- Subject to fusion.
1031foldl :: (a -> Char -> a) -> a -> Text -> a
857foldl :: (a -> Char -> a) -> a -> Text -> a
1032−foldl f a0 (Text t) = JSS.foldl f a0 t
1033−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
1034{-# INLINE foldl #-}
859{-# INLINE foldl #-}
1036-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
1037foldl' :: (a -> Char -> a) -> a -> Text -> a
862foldl' :: (a -> Char -> a) -> a -> Text -> a
1038−foldl' f a0 (Text t) = JSS.foldl' f a0 t
1039−-- foldl' f z t = S.foldl' f z (stream t)
863+foldl' f z t = S.foldl' f z (stream t)
1040{-# INLINE foldl' #-}
864{-# INLINE foldl' #-}
1042-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
1043-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
1044foldl1 :: (Char -> Char -> Char) -> Text -> Char
868foldl1 :: (Char -> Char -> Char) -> Text -> Char
1045−foldl1 = coerce JSS.foldl1
1046−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
1047{-# INLINE foldl1 #-}
870{-# INLINE foldl1 #-}
1049-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
1050foldl1' :: (Char -> Char -> Char) -> Text -> Char
873foldl1' :: (Char -> Char -> Char) -> Text -> Char
1051−foldl1' = coerce JSS.foldl1'
1052−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
1053{-# INLINE foldl1' #-}
875{-# INLINE foldl1' #-}
1055-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -1057,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
1057-- reduces the 'Text' using the binary operator, from right to left.
879-- reduces the 'Text' using the binary operator, from right to left.
1058-- Subject to fusion.
880-- Subject to fusion.
1059foldr :: (Char -> a -> a) -> a -> Text -> a
881foldr :: (Char -> a -> a) -> a -> Text -> a
1060−foldr f a0 (Text t) = JSS.foldr f a0 t
1061−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
1062{-# INLINE foldr #-}
883{-# INLINE foldr #-}
1064-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
1065-- and thus must be applied to a non-empty 'Text'. Subject to
886-- and thus must be applied to a non-empty 'Text'. Subject to
1066-- fusion.
887-- fusion.
1067foldr1 :: (Char -> Char -> Char) -> Text -> Char
888foldr1 :: (Char -> Char -> Char) -> Text -> Char
1068−foldr1 = coerce JSS.foldr1
1069−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
1070{-# INLINE foldr1 #-}
890{-# INLINE foldr1 #-}
1072-- -----------------------------------------------------------------------------
892-- -----------------------------------------------------------------------------
@@ -1074,54 +894,48 @@ foldr1 = coerce JSS.foldr1
1075-- | /O(n)/ Concatenate a list of 'Text's.
895-- | /O(n)/ Concatenate a list of 'Text's.
1076concat :: [Text] -> Text
896concat :: [Text] -> Text
1077−concat = coerce JSS.concat
1078−-- concat ts = case ts' of
897+concat ts = case ts' of
1079−-- [] -> empty
898+ [] -> empty
1080−-- [t] -> t
899+ [t] -> t
1081−-- _ -> Text (A.run go) 0 len
900+ _ -> Text (A.run go) 0 len
1083−-- ts' = L.filter (not . null) ts
902+ ts' = L.filter (not . null) ts
1084−-- len = sumP "concat" $ L.map lengthWord16 ts'
903+ len = sumP "concat" $ L.map lengthWord16 ts'
1085−-- go :: ST s (A.MArray s)
904+ go :: ST s (A.MArray s)
1086−-- go = do
905+ go = do
1087−-- arr <- A.new len
906+ arr <- A.new len
1088−-- let step i (Text a o l) =
907+ let step i (Text a o l) =
1089−-- let !j = i + l in A.copyI arr i a o j >> return j
908+ let !j = i + l in A.copyI arr i a o j >> return j
1090−-- foldM step 0 ts' >> return arr
909+ foldM step 0 ts' >> return arr
1092-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
1093-- concatenate the results.
912-- concatenate the results.
1094concatMap :: (Char -> Text) -> Text -> Text
913concatMap :: (Char -> Text) -> Text -> Text
1095−concatMap = coerce JSS.concatMap
1096−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
1097{-# INLINE concatMap #-}
915{-# INLINE concatMap #-}
1099-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1100-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1101any :: (Char -> Bool) -> Text -> Bool
919any :: (Char -> Bool) -> Text -> Bool
1102−any = coerce JSS.any
1103−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1104{-# INLINE any #-}
921{-# INLINE any #-}
1106-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1107-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1108all :: (Char -> Bool) -> Text -> Bool
925all :: (Char -> Bool) -> Text -> Bool
1109−all = coerce JSS.all
1110−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1111{-# INLINE all #-}
927{-# INLINE all #-}
1113-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1114-- must be non-empty. Subject to fusion.
930-- must be non-empty. Subject to fusion.
1115maximum :: Text -> Char
931maximum :: Text -> Char
1116−maximum = coerce JSS.maximum
1117−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1118{-# INLINE maximum #-}
933{-# INLINE maximum #-}
1120-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1121-- must be non-empty. Subject to fusion.
936-- must be non-empty. Subject to fusion.
1122minimum :: Text -> Char
937minimum :: Text -> Char
1123−minimum = coerce JSS.minimum
1124−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1125{-# INLINE minimum #-}
939{-# INLINE minimum #-}
1127-- -----------------------------------------------------------------------------
941-- -----------------------------------------------------------------------------
@@ -1137,9 +951,8 @@ minimum = coerce JSS.minimum
1138-- > last (scanl f z xs) == foldl f z xs.
952-- > last (scanl f z xs) == foldl f z xs.
1139scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1140−scanl = coerce JSS.scanl
1141−-- scanl f z t = unstream (S.scanl g z (stream t))
954+scanl f z t = unstream (S.scanl g z (stream t))
1142−-- where g a b = safe (f a b)
955+ where g a b = safe (f a b)
1143{-# INLINE scanl #-}
956{-# INLINE scanl #-}
1145-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -1148,9 +961,8 @@ scanl = coerce JSS.scanl
1149-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1150scanl1 :: (Char -> Char -> Char) -> Text -> Text
963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1151−scanl1 = coerce JSS.scanl1
1152−-- scanl1 f t | null t = empty
964+scanl1 f t | null t = empty
1153−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1154{-# INLINE scanl1 #-}
966{-# INLINE scanl1 #-}
1156-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -1158,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1159-- > scanr f v == reverse . scanl (flip f) v . reverse
971-- > scanr f v == reverse . scanl (flip f) v . reverse
1160scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1161−scanr = coerce JSS.scanr
1162−-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
973+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1163−-- where g a b = safe (f a b)
974+ where g a b = safe (f a b)
1164{-# INLINE scanr #-}
975{-# INLINE scanr #-}
1166-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1167-- value argument. Subject to fusion. Performs replacement on
978-- value argument. Subject to fusion. Performs replacement on
1168-- invalid scalar values.
979-- invalid scalar values.
1169scanr1 :: (Char -> Char -> Char) -> Text -> Text
980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1170−scanr1 = coerce JSS.scanr1
1171−-- scanr1 f t | null t = empty
981+scanr1 f t | null t = empty
1172−-- | otherwise = scanr f (last t) (init t)
982+ | otherwise = scanr f (last t) (init t)
1173{-# INLINE scanr1 #-}
983{-# INLINE scanr1 #-}
1175-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1177,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1177-- parameter from left to right, and returns a final 'Text'. Performs
987-- parameter from left to right, and returns a final 'Text'. Performs
1178-- replacement on invalid scalar values.
988-- replacement on invalid scalar values.
1179mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1180−mapAccumL f z0 (Text a) =
1181− let (a', t) = JSS.mapAccumL f z0 a
1183−-- mapAccumL f z0 = S.mapAccumL g z0 . stream
990+mapAccumL f z0 = S.mapAccumL g z0 . stream
1184−-- where g a b = second safe (f a b)
991+ where g a b = second safe (f a b)
1185{-# INLINE mapAccumL #-}
992{-# INLINE mapAccumL #-}
1187-- | The 'mapAccumR' function behaves like a combination of 'map' and
994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1191,11 +998,8 @@ mapAccumL f z0 (Text a) =
1191-- 'Text'.
998-- 'Text'.
1192-- Performs replacement on invalid scalar values.
999-- Performs replacement on invalid scalar values.
1193mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1194−mapAccumR f z0 (Text a) =
1195− let (a', t) = JSS.mapAccumR f z0 a
1197−-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1001+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1198−-- where g a b = second safe (f a b)
1002+ where g a b = second safe (f a b)
1199{-# INLINE mapAccumR #-}
1003{-# INLINE mapAccumR #-}
1201-- -----------------------------------------------------------------------------
1005-- -----------------------------------------------------------------------------
@@ -1204,21 +1008,20 @@ mapAccumR f z0 (Text a) =
1204-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1205-- @t@ repeated @n@ times.
1009-- @t@ repeated @n@ times.
1206replicate :: Int -> Text -> Text
1010replicate :: Int -> Text -> Text
1207−replicate = coerce JSS.replicate
1208−-- replicate n t@(Text a o l)
1011+replicate n t@(Text a o l)
1209−-- | n <= 0 || l <= 0 = empty
1012+ | n <= 0 || l <= 0 = empty
1210−-- | n == 1 = t
1013+ | n == 1 = t
1211−-- | isSingleton t = replicateChar n (unsafeHead t)
1014+ | isSingleton t = replicateChar n (unsafeHead t)
1212−-- | otherwise = Text (A.run x) 0 len
1015+ | otherwise = Text (A.run x) 0 len
1214−-- len = l `mul` n
1017+ len = l `mul` n
1215−-- x :: ST s (A.MArray s)
1018+ x :: ST s (A.MArray s)
1216−-- x = do
1019+ x = do
1217−-- arr <- A.new len
1020+ arr <- A.new len
1218−-- let loop !d !i | i >= n = return arr
1021+ let loop !d !i | i >= n = return arr
1219−-- | otherwise = let m = d + l
1022+ | otherwise = let m = d + l
1220−-- in A.copyI arr d a o m >> loop m (i+1)
1023+ in A.copyI arr d a o m >> loop m (i+1)
1221−-- loop 0 0
1024+ loop 0 0
1222{-# INLINE [1] replicate #-}
1025{-# INLINE [1] replicate #-}
1224{-# RULES
1027{-# RULES
@@ -1229,19 +1032,8 @@ replicate = coerce JSS.replicate
1229-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1230-- value of every element. Subject to fusion.
1033-- value of every element. Subject to fusion.
1231replicateChar :: Int -> Char -> Text
1034replicateChar :: Int -> Char -> Text
1233replicateChar n c = unstream (S.replicateCharI n (safe c))
1035replicateChar n c = unstream (S.replicateCharI n (safe c))
1234{-# INLINE replicateChar #-}
1036{-# INLINE replicateChar #-}
1236−replicateChar n c = Text $ js_replicateChar n c
1237−{-# INLINE [1] replicateChar #-}
1239−"JSSTRING replicateChar -> fused" [~1] forall n c.
1240− replicateChar n c = unstream (S.replicateCharI n (safe c))
1241−"JSSTRING replicateChar -> unfused" [1] forall n c.
1242− unstream (S.replicateCharI n (safe c)) = replicateChar n c
1246-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1247-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
1039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1251,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
1251-- string, and @b@ is the seed value for further production. Subject
1043-- string, and @b@ is the seed value for further production. Subject
1252-- to fusion. Performs replacement on invalid scalar values.
1044-- to fusion. Performs replacement on invalid scalar values.
1253unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1254−unfoldr f a = Text $ JSS.unfoldr f a
1255−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1256{-# INLINE unfoldr #-}
1047{-# INLINE unfoldr #-}
1258-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
1049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1262,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
1262-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1263-- to fusion. Performs replacement on invalid scalar values.
1054-- to fusion. Performs replacement on invalid scalar values.
1264unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1265−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1266−-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1056+unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1267{-# INLINE unfoldrN #-}
1057{-# INLINE unfoldrN #-}
1269-- -----------------------------------------------------------------------------
1059-- -----------------------------------------------------------------------------
@@ -1273,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1273-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1274-- the length of the Text. Subject to fusion.
1064-- the length of the Text. Subject to fusion.
1275take :: Int -> Text -> Text
1065take :: Int -> Text -> Text
1276−take = coerce JSS.take
1277−-- take n t@(Text arr off len)
1066+take n t@(Text arr off len)
1278−-- | n <= 0 = empty
1067+ | n <= 0 = empty
1279−-- | n >= len = t
1068+ | n >= len = t
1280−-- | otherwise = text arr off (iterN n t)
1069+ | otherwise = text arr off (iterN n t)
1281{-# INLINE [1] take #-}
1070{-# INLINE [1] take #-}
1283−-- iterN :: Int -> Text -> Int
1072+iterN :: Int -> Text -> Int
1284−-- iterN n t@(Text _arr _off len) = loop 0 0
1073+iterN n t@(Text _arr _off len) = loop 0 0
1285−-- where loop !i !cnt
1074+ where loop !i !cnt
1286−-- | i >= len || cnt >= n = i
1075+ | i >= len || cnt >= n = i
1287−-- | otherwise = loop (i+d) (cnt+1)
1076+ | otherwise = loop (i+d) (cnt+1)
1288−-- where d = iter_ t i
1077+ where d = iter_ t i
1290{-# RULES
1079{-# RULES
1291"TEXT take -> fused" [~1] forall n t.
1080"TEXT take -> fused" [~1] forall n t.
@@ -1299,36 +1088,31 @@ take = coerce JSS.take
1300-- Examples:
1089-- Examples:
1302−-- >>> takeEnd 3 "foobar"
1091+-- > takeEnd 3 "foobar" == "bar"
1306takeEnd :: Int -> Text -> Text
1092takeEnd :: Int -> Text -> Text
1307−takeEnd = coerce JSS.takeEnd
1308−-- takeEnd n t@(Text arr off len)
1093+takeEnd n t@(Text arr off len)
1309−-- | n <= 0 = empty
1094+ | n <= 0 = empty
1310−-- | n >= len = t
1095+ | n >= len = t
1311−-- | otherwise = text arr (off+i) (len-i)
1096+ | otherwise = text arr (off+i) (len-i)
1312−-- where i = iterNEnd n t
1097+ where i = iterNEnd n t
1314−-- iterNEnd :: Int -> Text -> Int
1099+iterNEnd :: Int -> Text -> Int
1315−-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1100+iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1316−-- where loop i !m
1101+ where loop i !m
1317−-- | i <= 0 = 0
1102+ | i <= 0 = 0
1318−-- | m <= 1 = i
1103+ | m <= 1 = i
1319−-- | otherwise = loop (i+d) (m-1)
1104+ | otherwise = loop (i+d) (m-1)
1320−-- where d = reverseIter_ t i
1105+ where d = reverseIter_ t i
1322-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1323-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1324-- is greater than the length of the 'Text'. Subject to fusion.
1109-- is greater than the length of the 'Text'. Subject to fusion.
1325drop :: Int -> Text -> Text
1110drop :: Int -> Text -> Text
1326−drop = coerce JSS.drop
1327−-- drop n t@(Text arr off len)
1111+drop n t@(Text arr off len)
1328−-- | n <= 0 = t
1112+ | n <= 0 = t
1329−-- | n >= len = empty
1113+ | n >= len = empty
1330−-- | otherwise = text arr (off+i) (len-i)
1114+ | otherwise = text arr (off+i) (len-i)
1331−-- where i = iterN n t
1115+ where i = iterN n t
1332{-# INLINE [1] drop #-}
1116{-# INLINE [1] drop #-}
1334{-# RULES
1118{-# RULES
@@ -1343,27 +1127,22 @@ drop = coerce JSS.drop
1344-- Examples:
1128-- Examples:
1346−-- >>> dropEnd 3 "foobar"
1130+-- > dropEnd 3 "foobar" == "foo"
1350dropEnd :: Int -> Text -> Text
1131dropEnd :: Int -> Text -> Text
1351−dropEnd = coerce JSS.dropEnd
1352−-- dropEnd n t@(Text arr off len)
1132+dropEnd n t@(Text arr off len)
1353−-- | n <= 0 = t
1133+ | n <= 0 = t
1354−-- | n >= len = empty
1134+ | n >= len = empty
1355−-- | otherwise = text arr off (iterNEnd n t)
1135+ | otherwise = text arr off (iterNEnd n t)
1357-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1358-- returns the longest prefix (possibly empty) of elements that
1138-- returns the longest prefix (possibly empty) of elements that
1359-- satisfy @p@. Subject to fusion.
1139-- satisfy @p@. Subject to fusion.
1360takeWhile :: (Char -> Bool) -> Text -> Text
1140takeWhile :: (Char -> Bool) -> Text -> Text
1361−takeWhile = coerce JSS.takeWhile
1362−-- takeWhile p t@(Text arr off len) = loop 0
1141+takeWhile p t@(Text arr off len) = loop 0
1363−-- where loop !i | i >= len = t
1142+ where loop !i | i >= len = t
1364−-- | p c = loop (i+d)
1143+ | p c = loop (i+d)
1365−-- | otherwise = text arr off i
1144+ | otherwise = text arr off i
1366−-- where Iter c d = iter t i
1145+ where Iter c d = iter t i
1367{-# INLINE [1] takeWhile #-}
1146{-# INLINE [1] takeWhile #-}
1369{-# RULES
1148{-# RULES
@@ -1378,25 +1157,13 @@ takeWhile = coerce JSS.takeWhile
1378-- satisfy @p@. Subject to fusion.
1157-- satisfy @p@. Subject to fusion.
1379-- Examples:
1158-- Examples:
1381−-- >>> takeWhileEnd (=='o') "foo"
1160+-- > takeWhileEnd (=='o') "foo" == "oo"
1385takeWhileEnd :: (Char -> Bool) -> Text -> Text
1161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1387takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1388 where loop !i !l | l <= 0 = t
1163 where loop !i !l | l <= 0 = t
1389 | p c = loop (i+d) (l+d)
1164 | p c = loop (i+d) (l+d)
1390 | otherwise = text arr (off+l) (len-l)
1165 | otherwise = text arr (off+l) (len-l)
1391 where (c,d) = reverseIter t i
1166 where (c,d) = reverseIter t i
1393−takeWhileEnd p t@(Text jst) = loop (len-1) len
1394− where loop !i !l | l <= 0 = t
1395− | p c = loop (i+d) (l+d)
1396− | otherwise = Text $ js_substr1 l jst
1397− where (c,d) = reverseIter t i
1398− len = js_length jst
1400{-# INLINE [1] takeWhileEnd #-}
1167{-# INLINE [1] takeWhileEnd #-}
1402{-# RULES
1169{-# RULES
@@ -1409,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
1409-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1410-- 'takeWhile' @p@ @t@. Subject to fusion.
1177-- 'takeWhile' @p@ @t@. Subject to fusion.
1411dropWhile :: (Char -> Bool) -> Text -> Text
1178dropWhile :: (Char -> Bool) -> Text -> Text
1412−dropWhile = coerce JSS.dropWhile
1413−-- dropWhile p t@(Text arr off len) = loop 0 0
1179+dropWhile p t@(Text arr off len) = loop 0 0
1414−-- where loop !i !l | l >= len = empty
1180+ where loop !i !l | l >= len = empty
1415−-- | p c = loop (i+d) (l+d)
1181+ | p c = loop (i+d) (l+d)
1416−-- | otherwise = Text arr (off+i) (len-l)
1182+ | otherwise = Text arr (off+i) (len-l)
1417−
1183+ where Iter c d = iter t i
1418{-# INLINE [1] dropWhile #-}
1184{-# INLINE [1] dropWhile #-}
1420{-# RULES
1186{-# RULES
@@ -1430,15 +1196,13 @@ dropWhile = coerce JSS.dropWhile
1431-- Examples:
1197-- Examples:
1433−-- >>> dropWhileEnd (=='.') "foo..."
1199+-- > dropWhileEnd (=='.') "foo..." == "foo"
1435dropWhileEnd :: (Char -> Bool) -> Text -> Text
1200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1436−dropWhileEnd = coerce JSS.dropWhileEnd
1437−-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1201+dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1438−-- where loop !i !l | l <= 0 = empty
1202+ where loop !i !l | l <= 0 = empty
1439−-- | p c = loop (i+d) (l+d)
1203+ | p c = loop (i+d) (l+d)
1440−-- | otherwise = Text arr off l
1204+ | otherwise = Text arr off l
1441−-- where (c,d) = reverseIter t i
1205+ where (c,d) = reverseIter t i
1442{-# INLINE [1] dropWhileEnd #-}
1206{-# INLINE [1] dropWhileEnd #-}
1444{-# RULES
1208{-# RULES
@@ -1481,49 +1245,45 @@ strip = dropAround isSpace
1481-- prefix of @t@ of length @n@, and whose second is the remainder of
1245-- prefix of @t@ of length @n@, and whose second is the remainder of
1482-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1483splitAt :: Int -> Text -> (Text, Text)
1247splitAt :: Int -> Text -> (Text, Text)
1484−splitAt = coerce JSS.splitAt
1485−-- splitAt n t@(Text arr off len)
1248+splitAt n t@(Text arr off len)
1486−-- | n <= 0 = (empty, t)
1249+ | n <= 0 = (empty, t)
1487−-- | n >= len = (t, empty)
1250+ | n >= len = (t, empty)
1488−-- | otherwise = let k = iterN n t
1251+ | otherwise = let k = iterN n t
1489−-- in (text arr off k, text arr (off+k) (len-k))
1252+ in (text arr off k, text arr (off+k) (len-k))
1491-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1492-- a pair whose first element is the longest prefix (possibly empty)
1255-- a pair whose first element is the longest prefix (possibly empty)
1493-- of @t@ of elements that satisfy @p@, and whose second is the
1256-- of @t@ of elements that satisfy @p@, and whose second is the
1494-- remainder of the list.
1257-- remainder of the list.
1495span :: (Char -> Bool) -> Text -> (Text, Text)
1258span :: (Char -> Bool) -> Text -> (Text, Text)
1496−span = coerce JSS.span
1497−-- span p t = case span_ p t of
1259+span p t = case span_ p t of
1498−-- (# hd,tl #) -> (hd,tl)
1260+ (# hd,tl #) -> (hd,tl)
1499{-# INLINE span #-}
1261{-# INLINE span #-}
1501-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1502-- over elements that fail the predicate @p@.
1264-- over elements that fail the predicate @p@.
1503break :: (Char -> Bool) -> Text -> (Text, Text)
1265break :: (Char -> Bool) -> Text -> (Text, Text)
1504−break = coerce JSS.break
1505−-- break p = span (not . p)
1266+break p = span (not . p)
1506{-# INLINE break #-}
1267{-# INLINE break #-}
1508-- | /O(n)/ Group characters in a string according to a predicate.
1269-- | /O(n)/ Group characters in a string according to a predicate.
1509groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1510−groupBy = coerce JSS.groupBy
1511−-- groupBy p = loop
1271+groupBy p = loop
1513−-- loop t@(Text arr off len)
1273+ loop t@(Text arr off len)
1514−-- | null t = []
1274+ | null t = []
1515−-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1275+ | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1516−-- where Iter c d = iter t 0
1276+ where Iter c d = iter t 0
1517−-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1277+ n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1519-- | Returns the /array/ index (in units of 'Word16') at which a
1279-- | Returns the /array/ index (in units of 'Word16') at which a
1520-- character may be found. This is /not/ the same as the logical
1280-- character may be found. This is /not/ the same as the logical
1521-- index returned by e.g. 'findIndex'.
1281-- index returned by e.g. 'findIndex'.
1522−-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1282+findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1523−-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1283+findAIndexOrEnd q t@(Text _arr _off len) = go 0
1524−-- where go !i | i >= len || q c = i
1284+ where go !i | i >= len || q c = i
1525−-- | otherwise = go (i+d)
1285+ | otherwise = go (i+d)
1526−-- where Iter c d = iter t i
1286+ where Iter c d = iter t i
1528-- | /O(n)/ Group characters in a string by equality.
1288-- | /O(n)/ Group characters in a string by equality.
1529group :: Text -> [Text]
1289group :: Text -> [Text]
@@ -1532,17 +1292,15 @@ group = groupBy (==)
1532-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1533-- first.
1293-- first.
1534inits :: Text -> [Text]
1294inits :: Text -> [Text]
1535−inits = coerce JSS.inits
1536−-- inits t@(Text arr off len) = loop 0
1295+inits t@(Text arr off len) = loop 0
1537−-- where loop i | i >= len = [t]
1296+ where loop i | i >= len = [t]
1538−-- | otherwise = Text arr off i : loop (i + iter_ t i)
1297+ | otherwise = Text arr off i : loop (i + iter_ t i)
1540-- | /O(n)/ Return all final segments of the given 'Text', longest
1299-- | /O(n)/ Return all final segments of the given 'Text', longest
1541-- first.
1300-- first.
1542tails :: Text -> [Text]
1301tails :: Text -> [Text]
1543−tails = coerce JSS.tails
1544−-- tails t | null t = [empty]
1302+tails t | null t = [empty]
1545−-- | otherwise = t : tails (unsafeTail t)
1303+ | otherwise = t : tails (unsafeTail t)
1547-- $split
1305-- $split
@@ -1556,14 +1314,9 @@ tails = coerce JSS.tails
1557-- Examples:
1315-- Examples:
1559−-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1317+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1560−-- ["a","b","d","e"]
1562−-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1318+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1563−-- ["","X","X","X",""]
1565−-- >>> splitOn "x" "x"
1319+-- > splitOn "x" "x" == ["",""]
@@ -1580,14 +1333,13 @@ splitOn :: Text
1581 -- ^ Input text.
1334 -- ^ Input text.
1582 -> [Text]
1335 -> [Text]
1583−splitOn = coerce JSS.splitOn
1584−-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1336+splitOn pat@(Text _ _ l) src@(Text arr off len)
1585−-- | l <= 0 = emptyError "splitOn"
1337+ | l <= 0 = emptyError "splitOn"
1586−-- | isSingleton pat = split (== unsafeHead pat) src
1338+ | isSingleton pat = split (== unsafeHead pat) src
1587−-- | otherwise = go 0 (indices pat src)
1339+ | otherwise = go 0 (indices pat src)
1589−-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1341+ go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1590−-- go s _ = [text arr (s+off) (len-s)]
1342+ go s _ = [text arr (s+off) (len-s)]
1591{-# INLINE [1] splitOn #-}
1343{-# INLINE [1] splitOn #-}
1593{-# RULES
1345{-# RULES
@@ -1600,36 +1352,28 @@ splitOn = coerce JSS.splitOn
1600-- resulting components do not contain the separators. Two adjacent
1352-- resulting components do not contain the separators. Two adjacent
1601-- separators result in an empty component in the output. eg.
1353-- separators result in an empty component in the output. eg.
1603−-- >>> split (=='a') "aabbaca"
1355+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1604−-- ["","","bb","c",""]
1606−-- >>> split (=='a') ""
1356+-- > split (=='a') "" == [""]
1608split :: (Char -> Bool) -> Text -> [Text]
1357split :: (Char -> Bool) -> Text -> [Text]
1609−split = coerce JSS.split
1610−-- split _ t@(Text _off _arr 0) = [t]
1358+split _ t@(Text _off _arr 0) = [t]
1611−-- split p t = loop t
1359+split p t = loop t
1612−-- where loop s | null s' = [l]
1360+ where loop s | null s' = [l]
1613−-- | otherwise = l : loop (unsafeTail s')
1361+ | otherwise = l : loop (unsafeTail s')
1614−-- where (# l, s' #) = span_ (not . p) s
1362+ where (# l, s' #) = span_ (not . p) s
1615{-# INLINE split #-}
1363{-# INLINE split #-}
1617-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1618-- element may be shorter than the other chunks, depending on the
1366-- element may be shorter than the other chunks, depending on the
1619-- length of the input. Examples:
1367-- length of the input. Examples:
1621−-- >>> chunksOf 3 "foobarbaz"
1369+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1622−-- ["foo","bar","baz"]
1624−-- >>> chunksOf 4 "haskell.org"
1370+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1625−-- ["hask","ell.","org"]
1626chunksOf :: Int -> Text -> [Text]
1371chunksOf :: Int -> Text -> [Text]
1627−chunksOf = coerce JSS.chunksOf
1372+chunksOf k = go
1630−-- go t = case splitAt k t of
1374+ go t = case splitAt k t of
1631−-- (a,b) | null a -> []
1375+ (a,b) | null a -> []
1632−-- | otherwise -> a : go b
1376+ | otherwise -> a : go b
1633{-# INLINE chunksOf #-}
1377{-# INLINE chunksOf #-}
1635-- ----------------------------------------------------------------------------
1379-- ----------------------------------------------------------------------------
@@ -1642,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
1642-- returns the first element matching the predicate, or 'Nothing' if
1386-- returns the first element matching the predicate, or 'Nothing' if
1643-- there is no such element.
1387-- there is no such element.
1644find :: (Char -> Bool) -> Text -> Maybe Char
1388find :: (Char -> Bool) -> Text -> Maybe Char
1645−find = coerce JSS.find
1646−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
1647{-# INLINE find #-}
1390{-# INLINE find #-}
1649-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
1392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1652,16 +1395,14 @@ find = coerce JSS.find
1653-- > partition p t == (filter p t, filter (not . p) t)
1396-- > partition p t == (filter p t, filter (not . p) t)
1654partition :: (Char -> Bool) -> Text -> (Text, Text)
1397partition :: (Char -> Bool) -> Text -> (Text, Text)
1655−partition = coerce JSS.partition
1656−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
1657{-# INLINE partition #-}
1399{-# INLINE partition #-}
1659-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1660-- returns a 'Text' containing those characters that satisfy the
1402-- returns a 'Text' containing those characters that satisfy the
1661-- predicate.
1403-- predicate.
1662filter :: (Char -> Bool) -> Text -> Text
1404filter :: (Char -> Bool) -> Text -> Text
1663−filter = coerce JSS.filter
1664−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
1665{-# INLINE filter #-}
1406{-# INLINE filter #-}
1667-- | /O(n+m)/ Find the first instance of @needle@ (which must be
1408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1671,11 +1412,8 @@ filter = coerce JSS.filter
1672-- Examples:
1413-- Examples:
1674−-- >>> breakOn "::" "a::b::c"
1415+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1677−-- >>> breakOn "/" "foobar"
1416+-- > breakOn "/" "foobar" ==> ("foobar", "")
@@ -1689,12 +1427,11 @@ filter = coerce JSS.filter
1689-- In (unlikely) bad cases, this function's time complexity degrades
1427-- In (unlikely) bad cases, this function's time complexity degrades
1690-- towards /O(n*m)/.
1428-- towards /O(n*m)/.
1691breakOn :: Text -> Text -> (Text, Text)
1429breakOn :: Text -> Text -> (Text, Text)
1692−breakOn = coerce JSS.breakOn
1693−-- breakOn pat src@(Text arr off len)
1430+breakOn pat src@(Text arr off len)
1694−-- | null pat = emptyError "breakOn"
1431+ | null pat = emptyError "breakOn"
1695−-- | otherwise = case indices pat src of
1432+ | otherwise = case indices pat src of
1696−-- [] -> (src, empty)
1433+ [] -> (src, empty)
1697−-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
1434+ (x:_) -> (text arr off x, text arr (off+x) (len-x))
1698{-# INLINE breakOn #-}
1435{-# INLINE breakOn #-}
1700-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
1437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1704,12 +1441,10 @@ breakOn = coerce JSS.breakOn
1704-- up to and including the last match of @needle@. The second is the
1441-- up to and including the last match of @needle@. The second is the
1705-- remainder of @haystack@, following the match.
1442-- remainder of @haystack@, following the match.
1707−-- >>> breakOnEnd "::" "a::b::c"
1444+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1709breakOnEnd :: Text -> Text -> (Text, Text)
1445breakOnEnd :: Text -> Text -> (Text, Text)
1710−breakOnEnd = coerce JSS.breakOnEnd
1711−-- breakOnEnd pat src = (reverse b, reverse a)
1446+breakOnEnd pat src = (reverse b, reverse a)
1712−-- where (a,b) = breakOn (reverse pat) (reverse src)
1447+ where (a,b) = breakOn (reverse pat) (reverse src)
1713{-# INLINE breakOnEnd #-}
1448{-# INLINE breakOnEnd #-}
1715-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1721,11 +1456,10 @@ breakOnEnd = coerce JSS.breakOnEnd
1722-- Examples:
1457-- Examples:
1724−-- >>> breakOnAll "::" ""
1459+-- > breakOnAll "::" ""
1725−-- []
1460+-- > ==> []
1727−-- >>> breakOnAll "/" "a/b/c/"
1461+-- > breakOnAll "/" "a/b/c/"
1728−-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
1462+-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
1730-- In (unlikely) bad cases, this function's time complexity degrades
1464-- In (unlikely) bad cases, this function's time complexity degrades
1731-- towards /O(n*m)/.
1465-- towards /O(n*m)/.
@@ -1734,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
1734breakOnAll :: Text -- ^ @needle@ to search for
1468breakOnAll :: Text -- ^ @needle@ to search for
1735 -> Text -- ^ @haystack@ in which to search
1469 -> Text -- ^ @haystack@ in which to search
1736 -> [(Text, Text)]
1470 -> [(Text, Text)]
1737−breakOnAll = coerce JSS.breakOnAll
1738−-- breakOnAll pat src@(Text arr off slen)
1471+breakOnAll pat src@(Text arr off slen)
1739−-- | null pat = emptyError "breakOnAll"
1472+ | null pat = emptyError "breakOnAll"
1740−-- | otherwise = L.map step (indices pat src)
1473+ | otherwise = L.map step (indices pat src)
1742−-- step x = (chunk 0 x, chunk x (slen-x))
1475+ step x = (chunk 0 x, chunk x (slen-x))
1743−-- chunk !n !l = text arr (n+off) l
1476+ chunk !n !l = text arr (n+off) l
1744{-# INLINE breakOnAll #-}
1477{-# INLINE breakOnAll #-}
1746-------------------------------------------------------------------------------
1479-------------------------------------------------------------------------------
@@ -1764,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
1765-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1766index :: Text -> Int -> Char
1499index :: Text -> Int -> Char
1767−index = coerce JSS.index
1768−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
1769{-# INLINE index #-}
1501{-# INLINE index #-}
1771-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1772-- and returns the index of the first element in the 'Text' satisfying
1504-- and returns the index of the first element in the 'Text' satisfying
1773-- the predicate. Subject to fusion.
1505-- the predicate. Subject to fusion.
1774findIndex :: (Char -> Bool) -> Text -> Maybe Int
1506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1775−findIndex = coerce JSS.findIndex
1776−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
1777{-# INLINE findIndex #-}
1508{-# INLINE findIndex #-}
1779-- | /O(n+m)/ The 'count' function returns the number of times the
1510-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1783,11 +1514,10 @@ findIndex = coerce JSS.findIndex
1783-- In (unlikely) bad cases, this function's time complexity degrades
1514-- In (unlikely) bad cases, this function's time complexity degrades
1784-- towards /O(n*m)/.
1515-- towards /O(n*m)/.
1785count :: Text -> Text -> Int
1516count :: Text -> Text -> Int
1786−count = coerce JSS.count
1787−-- count pat src
1517+count pat src
1788−-- | null pat = emptyError "count"
1518+ | null pat = emptyError "count"
1789−-- | isSingleton pat = countChar (unsafeHead pat) src
1519+ | isSingleton pat = countChar (unsafeHead pat) src
1790−-- | otherwise = L.length (indices pat src)
1520+ | otherwise = L.length (indices pat src)
1791{-# INLINE [1] count #-}
1521{-# INLINE [1] count #-}
1793{-# RULES
1523{-# RULES
@@ -1809,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
1809-- excess elements of the longer 'Text' are discarded. This is
1539-- excess elements of the longer 'Text' are discarded. This is
1810-- equivalent to a pair of 'unpack' operations.
1540-- equivalent to a pair of 'unpack' operations.
1811zip :: Text -> Text -> [(Char,Char)]
1541zip :: Text -> Text -> [(Char,Char)]
1812−zip = coerce JSS.zip
1813−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1814{-# INLINE zip #-}
1543{-# INLINE zip #-}
1816-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1817-- given as the first argument, instead of a tupling function.
1546-- given as the first argument, instead of a tupling function.
1818-- Performs replacement on invalid scalar values.
1547-- Performs replacement on invalid scalar values.
1819zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1820−zipWith = coerce JSS.zipWith
1821−-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1549+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1822−-- where g a b = safe (f a b)
1550+ where g a b = safe (f a b)
1823{-# INLINE zipWith #-}
1551{-# INLINE zipWith #-}
1825-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1826-- representing white space.
1554-- representing white space.
1827words :: Text -> [Text]
1555words :: Text -> [Text]
1828−words = coerce JSS.words
1829−-- words t@(Text arr off len) = loop 0 0
1556+words t@(Text arr off len) = loop 0 0
1831−-- loop !start !n
1558+ loop !start !n
1832−-- | n >= len = if start == n
1559+ | n >= len = if start == n
1833−-- then []
1560+ then []
1834−-- else [Text arr (start+off) (n-start)]
1561+ else [Text arr (start+off) (n-start)]
1835−-- | isSpace c =
1562+ | isSpace c =
1836−-- if start == n
1563+ if start == n
1837−-- then loop (start+1) (start+1)
1564+ then loop (start+1) (start+1)
1838−-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1565+ else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1839−-- | otherwise = loop start (n+d)
1566+ | otherwise = loop start (n+d)
1840−-- where Iter c d = iter t n
1567+ where Iter c d = iter t n
1841{-# INLINE words #-}
1568{-# INLINE words #-}
1843-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1844-- newline 'Char's. The resulting strings do not contain newlines.
1571-- newline 'Char's. The resulting strings do not contain newlines.
1845lines :: Text -> [Text]
1572lines :: Text -> [Text]
1846−lines = coerce JSS.lines
1847−-- lines ps | null ps = []
1573+lines ps | null ps = []
1848−-- | otherwise = h : if null t
1574+ | otherwise = h : if null t
1849−-- then []
1575+ then []
1850−-- else lines (unsafeTail t)
1576+ else lines (unsafeTail t)
1851−-- where (# h,t #) = span_ (/= '\n') ps
1577+ where (# h,t #) = span_ (/= '\n') ps
1852{-# INLINE lines #-}
1578{-# INLINE lines #-}
@@ -1876,40 +1602,34 @@ lines' ps | null ps = []
1876-- | /O(n)/ Joins lines, after appending a terminating newline to
1602-- | /O(n)/ Joins lines, after appending a terminating newline to
1878unlines :: [Text] -> Text
1604unlines :: [Text] -> Text
1879−unlines = coerce JSS.unlines
1880−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
1881{-# INLINE unlines #-}
1606{-# INLINE unlines #-}
1883-- | /O(n)/ Joins words using single space characters.
1608-- | /O(n)/ Joins words using single space characters.
1884unwords :: [Text] -> Text
1609unwords :: [Text] -> Text
1885−unwords = coerce JSS.unwords
1886−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
1887{-# INLINE unwords #-}
1611{-# INLINE unwords #-}
1889-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1890-- 'True' iff the first is a prefix of the second. Subject to fusion.
1614-- 'True' iff the first is a prefix of the second. Subject to fusion.
1891isPrefixOf :: Text -> Text -> Bool
1615isPrefixOf :: Text -> Text -> Bool
1892−isPrefixOf = coerce JSS.isPrefixOf
1893−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1616+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1894−-- alen <= blen && S.isPrefixOf (stream a) (stream b)
1617+ alen <= blen && S.isPrefixOf (stream a) (stream b)
1895{-# INLINE [1] isPrefixOf #-}
1618{-# INLINE [1] isPrefixOf #-}
1897{-# RULES
1620{-# RULES
1898"TEXT isPrefixOf -> fused" [~1] forall s t.
1621"TEXT isPrefixOf -> fused" [~1] forall s t.
1899 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1900−"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1901− S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
1904-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1905-- 'True' iff the first is a suffix of the second.
1626-- 'True' iff the first is a suffix of the second.
1906isSuffixOf :: Text -> Text -> Bool
1627isSuffixOf :: Text -> Text -> Bool
1907−isSuffixOf = coerce JSS.isSuffixOf
1908−-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1628+isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1909−-- d >= 0 && a == b'
1629+ d >= 0 && a == b'
1910−-- where d = blen - alen
1630+ where d = blen - alen
1911−-- b' | d == 0 = b
1631+ b' | d == 0 = b
1912−-- | otherwise = Text barr (boff+d) alen
1632+ | otherwise = Text barr (boff+d) alen
1913{-# INLINE isSuffixOf #-}
1633{-# INLINE isSuffixOf #-}
1915-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
1635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1919,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
1919-- In (unlikely) bad cases, this function's time complexity degrades
1639-- In (unlikely) bad cases, this function's time complexity degrades
1920-- towards /O(n*m)/.
1640-- towards /O(n*m)/.
1921isInfixOf :: Text -> Text -> Bool
1641isInfixOf :: Text -> Text -> Bool
1922−isInfixOf = coerce JSS.isInfixOf
1923−-- isInfixOf needle haystack
1642+isInfixOf needle haystack
1924−-- | null needle = True
1643+ | null needle = True
1925−-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1644+ | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1926−-- | otherwise = not . L.null . indices needle $ haystack
1645+ | otherwise = not . L.null . indices needle $ haystack
1927{-# INLINE [1] isInfixOf #-}
1646{-# INLINE [1] isInfixOf #-}
1929{-# RULES
1648{-# RULES
@@ -1939,14 +1658,9 @@ isInfixOf = coerce JSS.isInfixOf
1940-- Examples:
1659-- Examples:
1942−-- >>> stripPrefix "foo" "foobar"
1661+-- > stripPrefix "foo" "foobar" == Just "bar"
1945−-- >>> stripPrefix "" "baz"
1662+-- > stripPrefix "" "baz" == Just "baz"
1948−-- >>> stripPrefix "foo" "quux"
1663+-- > stripPrefix "foo" "quux" == Nothing
1951-- This is particularly useful with the @ViewPatterns@ extension to
1665-- This is particularly useful with the @ViewPatterns@ extension to
1952-- GHC, as follows:
1666-- GHC, as follows:
@@ -1958,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
1958-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1959-- > fnordLength _ = -1
1673-- > fnordLength _ = -1
1960stripPrefix :: Text -> Text -> Maybe Text
1674stripPrefix :: Text -> Text -> Maybe Text
1961−stripPrefix = coerce JSS.stripPrefix
1962−-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1675+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1963−-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1676+ | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1964−-- | otherwise = Nothing
1677+ | otherwise = Nothing
1966-- | /O(n)/ Find the longest non-empty common prefix of two strings
1679-- | /O(n)/ Find the longest non-empty common prefix of two strings
1967-- and return it, along with the suffixes of each string at which they
1680-- and return it, along with the suffixes of each string at which they
@@ -1972,39 +1685,28 @@ stripPrefix = coerce JSS.stripPrefix
1973-- Examples:
1686-- Examples:
1975−-- >>> commonPrefixes "foobar" "fooquux"
1688+-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1976−-- Just ("foo","bar","quux")
1978−-- >>> commonPrefixes "veeble" "fetzer"
1689+-- > commonPrefixes "veeble" "fetzer" == Nothing
1981−-- >>> commonPrefixes "" "baz"
1690+-- > commonPrefixes "" "baz" == Nothing
1983commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1984−commonPrefixes = coerce JSS.commonPrefixes
1985−-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1692+commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1987−-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1694+ go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1988−-- | i > 0 = Just (Text arr0 off0 i,
1695+ | i > 0 = Just (Text arr0 off0 i,
1989−-- text arr0 (off0+i) (len0-i),
1696+ text arr0 (off0+i) (len0-i),
1990−-- text arr1 (off1+j) (len1-j))
1697+ text arr1 (off1+j) (len1-j))
1991−-- | otherwise = Nothing
1698+ | otherwise = Nothing
1992−-- where Iter a d0 = iter t0 i
1699+ where Iter a d0 = iter t0 i
1993−-- Iter b d1 = iter t1 j
1700+ Iter b d1 = iter t1 j
1995-- | /O(n)/ Return the prefix of the second string if its suffix
1702-- | /O(n)/ Return the prefix of the second string if its suffix
1996-- matches the entire first string.
1703-- matches the entire first string.
1998-- Examples:
1705-- Examples:
2000−-- >>> stripSuffix "bar" "foobar"
1707+-- > stripSuffix "bar" "foobar" == Just "foo"
2003−-- >>> stripSuffix "" "baz"
1708+-- > stripSuffix "" "baz" == Just "baz"
2006−-- >>> stripSuffix "foo" "quux"
1709+-- > stripSuffix "foo" "quux" == Nothing
2009-- This is particularly useful with the @ViewPatterns@ extension to
1711-- This is particularly useful with the @ViewPatterns@ extension to
2010-- GHC, as follows:
1712-- GHC, as follows:
@@ -2016,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
2016-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
2017-- > quuxLength _ = -1
1719-- > quuxLength _ = -1
2018stripSuffix :: Text -> Text -> Maybe Text
1720stripSuffix :: Text -> Text -> Maybe Text
2019−stripSuffix = coerce JSS.stripSuffix
2020−-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1721+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2021−-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
1722+ | p `isSuffixOf` t = Just $! text arr off (len-plen)
2022−-- | otherwise = Nothing
1723+ | otherwise = Nothing
2024−-- -- | Add a list of non-negative numbers. Errors out on overflow.
1725+-- | Add a list of non-negative numbers. Errors out on overflow.
2025−-- sumP :: String -> [Int] -> Int
1726+sumP :: String -> [Int] -> Int
2026−-- sumP fun = go 0
1727+sumP fun = go 0
2027−-- where go !a (x:xs)
1728+ where go !a (x:xs)
2028−-- | ax >= 0 = go ax xs
1729+ | ax >= 0 = go ax xs
2029−-- | otherwise = overflowError fun
1730+ | otherwise = overflowError fun
2030−-- where ax = a + x
1731+ where ax = a + x
2031−-- go a _ = a
1732+ go a _ = a
2033−-- emptyError :: String -> a
1734+emptyError :: String -> a
2034−-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1735+emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
2036overflowError :: String -> a
1737overflowError :: String -> a
2037overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -2045,24 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
2045-- copy \"breaks the link\" to the original array, allowing it to be
1746-- copy \"breaks the link\" to the original array, allowing it to be
2046-- garbage collected if there are no other live references to it.
1747-- garbage collected if there are no other live references to it.
2047copy :: Text -> Text
1748copy :: Text -> Text
2049−-- copy (Text arr off len) = Text (A.run go) 0 len
1749+copy (Text arr off len) = Text (A.run go) 0 len
2051−-- go :: ST s (A.MArray s)
1751+ go :: ST s (A.MArray s)
2052−-- go = do
1752+ go = do
2053−-- marr <- A.new len
1753+ marr <- A.new len
2054−
1754+ A.copyI marr 0 arr off len
2055−foreign import javascript unsafe
1755+ return marr
2056− "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
2057−foreign import javascript unsafe
2058− "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString
2059−foreign import javascript unsafe
2060− "$1.length" js_length :: JSString -> Int
2062−-------------------------------------------------
2063−-- NOTE: the named chunk below used by doctest;
2064−-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2067−-- >>> :set -XOverloadedStrings
2068−-- >>> import qualified Data.Text as T