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