@@ -6,7 +6,7 @@
66#if __GLASGOW_HASKELL__ >= 708
77{-# LANGUAGE TypeFamilies #-}
88#endif
9−
9+{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI, UnliftedFFITypes #-}
1010-- |
1111-- Module : Data.Text
1212-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
@@ -15,7 +15,6 @@
1515--
1616-- License : BSD-style
1717-- Maintainer : bos@serpentine.com
18−-- Stability : experimental
1918-- Portability : GHC
2019--
2120-- A time and space-efficient implementation of Unicode text.
@@ -32,8 +31,9 @@
3231--
3332-- To use an extended and very rich family of functions for working
3433-- with Unicode text (including normalization, regular expressions,
35−-- non-standard encodings, text breaking, and locales), see
36−-- <http://hackage.haskell.org/package/text-icu the text-icu package >.
34+-- non-standard encodings, text breaking, and locales), see the
35+-- <http://hackage.haskell.org/package/text-icu text-icu package >.
36+--
3737
3838module Data.Text
3939 (
@@ -43,6 +43,9 @@ module Data.Text
4343 -- * Acceptable data
4444 -- $replacement
4545
46+ -- * Definition of character
47+ -- $character_definition
48+
4649 -- * Fusion
4750 -- $fusion
4851
@@ -60,6 +63,7 @@ module Data.Text
6063 , snoc
6164 , append
6265 , uncons
66+ , unsnoc
6367 , head
6468 , last
6569 , tail
@@ -223,16 +227,17 @@ import qualified Data.Text.Internal.Fusion as S
223227import qualified Data.Text.Internal.Fusion.Common as S
224228import Data.Text.Encoding (decodeUtf8', encodeUtf8)
225229import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
226−import Data.Text.Internal.Private (span_)
227−import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
228−import Data.Text.Show (singleton, unpack, unpackCString#)
230+-- import Data.Text.Internal.Private (span_)
231+import Data.Text.Internal (Text(..), empty, empty_, firstf, mul, safe, text)
232+-- import Data.Text.Show (singleton, unpack, unpackCString#)
229233import qualified Prelude as P
230234import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
231− reverseIter_, unsafeHead, unsafeTail)
235+ reverseIter_, unsafeHead, unsafeTail,
236+ unsafeDupablePerformIO)
232237import Data.Text.Internal.Unsafe.Char (unsafeChr)
233238import qualified Data.Text.Internal.Functions as F
234239import qualified Data.Text.Internal.Encoding.Utf16 as U16
235−import Data.Text.Internal.Search (indices)
240+-- import Data.Text.Internal.Search (indices)
236241#if defined(__HADDOCK__)
237242import Data.ByteString (ByteString)
238243import qualified Data.Text.Lazy as L
@@ -246,6 +251,76 @@ import qualified GHC.Exts as Exts
246251import Text.Printf (PrintfArg, formatArg, formatString)
247252#endif
248253
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
259+import Data.Coerce
260+import GHC.Base (build)
261+import qualified GHC.CString as GHC
262+import GHC.Exts (Addr#, ByteArray#)
263+import GHCJS.Prim (unsafeUnpackJSString#)
264+
265+unpack :: Text -> String
266+unpack = coerce JSS.unpack
267+{-# INLINE [1] unpack #-}
268+
269+singleton :: Char -> Text
270+singleton = coerce JSS.singleton
271+{-# INLINE [1] singleton #-}
272+
273+{-# RULES
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
278+ #-}
279+
280+{-# RULES "inlineJSString" forall s.
281+ pack (GHC.Base.build (GHC.unpackFoldrCString# s)) =
282+ Text (JSString (unsafeUnpackJSString# s)) #-}
283+
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# #-}
288+
289+{-# RULES "TEXT literal" forall a.
290+ unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
291+ = unpackCString# a #-}
292+
293+{-# RULES "TEXT literal UTF8" [1] forall a.
294+ unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
295+ = unpackCString# a #-}
296+
297+{-# RULES "TEXT empty literal" [1]
298+ unstream (S.map safe (S.streamList []))
299+ = empty_ #-}
300+
301+{-# RULES "TEXT singleton literal" [1] forall a.
302+ unstream (S.map safe (S.streamList [a]))
303+ = singleton a #-}
304+
305+#if MIN_VERSION_ghcjs_prim(0,1,1)
306+{-# RULES "TEXT literal prim" [0] forall a.
307+ unpackCString# a = Text (JSString (unsafeUnpackJSString# a))
308+ #-}
309+#endif
310+
311+---------------------------------------------------------------------
312+-- $character_definition
313+--
314+-- This package uses the term /character/ to denote Unicode /code points/.
315+--
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).
323+
249324-- $strict
250325--
251326-- This package provides both strict and lazy 'Text' types. The
@@ -321,22 +396,31 @@ import Text.Printf (PrintfArg, formatArg, formatString)
321396-- Functions that can be fused by the compiler are documented with the
322397-- phrase \"Subject to fusion\".
323398
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 (==) #-}
324404instance Eq Text where
325− Text arrA offA lenA == Text arrB offB lenB
326− | lenA == lenB = A.equal arrA offA arrB offB lenA
327− | otherwise = False
328− {-# INLINE (==) #-}
405+ Text x == Text y = x == y
406+
407+-- instance Ord Text where
408+-- compare = compareText
329409
330410instance Ord Text where
331− compare = compareText
411+ compare (Text x) (Text y) = compare x y
332412
333413instance Read Text where
334414 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
335415
336416#if MIN_VERSION_base(4,9,0)
337−-- Semigroup orphan instances for older GHCs are provided by
338−-- 'semigroups` package
339−
417+-- | Non-orphan 'Semigroup' instance only defined for
418+-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
419+-- provided by
420+-- the [semigroups](http://hackage.haskell.org/package/semigroups)
421+-- package
422+--
423+-- @since 1.2.2.0
340424instance Semigroup Text where
341425 (<>) = append
342426#endif
@@ -354,6 +438,7 @@ instance IsString Text where
354438 fromString = pack
355439
356440#if __GLASGOW_HASKELL__ >= 708
441+-- | @since 1.2.0.0
357442instance Exts.IsList Text where
358443 type Item Text = Char
359444 fromList = pack
@@ -364,6 +449,7 @@ instance Exts.IsList Text where
364449instance NFData Text where rnf !_ = ()
365450#endif
366451
452+-- | @since 1.2.1.0
367453instance Binary Text where
368454 put t = put (encodeUtf8 t)
369455 get = do
@@ -397,8 +483,10 @@ instance Data Text where
397483
398484#if MIN_VERSION_base(4,7,0)
399485-- | Only defined for @base-4.7.0.0@ and later
486+--
487+-- @since 1.2.2.0
400488instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
489+ formatArg t = formatString $ unpack t
402490#endif
403491
404492packConstr :: Constr
@@ -407,19 +495,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
407495textDataType :: DataType
408496textDataType = mkDataType "Data.Text.Text" [packConstr]
409497
410−-- | /O(n)/ Compare two 'Text' values lexicographically.
411−compareText :: Text -> Text -> Ordering
412−compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
413− | lenA == 0 && lenB == 0 = EQ
414− | otherwise = go 0 0
415− where
416− go !i !j
417− | i >= lenA || j >= lenB = compare lenA lenB
418− | a < b = LT
419− | a > b = GT
420− | otherwise = go (i+di) (j+dj)
421− where Iter a di = iter ta i
422− Iter b dj = iter tb j
498+-- -- | /O(n)/ Compare two 'Text' values lexicographically.
499+-- compareText :: Text -> Text -> Ordering
500+-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
501+-- | lenA == 0 && lenB == 0 = EQ
502+-- | otherwise = go 0 0
503+-- where
504+-- go !i !j
505+-- | i >= lenA || j >= lenB = compare lenA lenB
506+-- | a < b = LT
507+-- | a > b = GT
508+-- | otherwise = go (i+di) (j+dj)
509+-- where Iter a di = iter ta i
510+-- Iter b dj = iter tb j
423511
424512-- -----------------------------------------------------------------------------
425513-- * Conversion to/from 'Text'
@@ -427,7 +515,8 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
427515-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
428516-- fusion. Performs replacement on invalid scalar values.
429517pack :: String -> Text
430−pack = unstream . S.map safe . S.streamList
518+pack = coerce JSS.pack
519+-- pack = unstream . S.map safe . S.streamList
431520{-# INLINE [1] pack #-}
432521
433522-- -----------------------------------------------------------------------------
@@ -438,7 +527,8 @@ pack = unstream . S.map safe . S.streamList
438527-- copying a new array. Subject to fusion. Performs replacement on
439528-- invalid scalar values.
440529cons :: Char -> Text -> Text
441−cons c t = unstream (S.cons (safe c) (stream t))
530+cons = coerce JSS.cons
531+-- cons c t = unstream (S.cons (safe c) (stream t))
442532{-# INLINE cons #-}
443533
444534infixr 5 `cons`
@@ -447,25 +537,27 @@ infixr 5 `cons`
447537-- entire array in the process, unless fused. Subject to fusion.
448538-- Performs replacement on invalid scalar values.
449539snoc :: Text -> Char -> Text
450−snoc t c = unstream (S.snoc (stream t) (safe c))
540+snoc = coerce JSS.snoc
541+-- snoc t c = unstream (S.snoc (stream t) (safe c))
451542{-# INLINE snoc #-}
452543
453544-- | /O(n)/ Appends one 'Text' to the other by copying both of them
454545-- into a new 'Text'. Subject to fusion.
455546append :: Text -> Text -> Text
456−append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
457− | len1 == 0 = b
458− | len2 == 0 = a
459− | len > 0 = Text (A.run x) 0 len
460− | otherwise = overflowError "append"
461− where
462− len = len1+len2
463− x :: ST s (A.MArray s)
464− x = do
465− arr <- A.new len
466− A.copyI arr 0 arr1 off1 len1
467− A.copyI arr len1 arr2 off2 len
468− return arr
547+append = coerce JSS.append
548+-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
549+-- | len1 == 0 = b
550+-- | len2 == 0 = a
551+-- | len > 0 = Text (A.run x) 0 len
552+-- | otherwise = overflowError "append"
553+-- where
554+-- len = len1+len2
555+-- x :: ST s (A.MArray s)
556+-- x = do
557+-- arr <- A.new len
558+-- A.copyI arr 0 arr1 off1 len1
559+-- A.copyI arr len1 arr2 off2 len
560+-- return arr
469561{-# NOINLINE append #-}
470562
471563{-# RULES
@@ -478,16 +570,18 @@ append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
478570-- | /O(1)/ Returns the first character of a 'Text', which must be
479571-- non-empty. Subject to fusion.
480572head :: Text -> Char
481−head t = S.head (stream t)
573+head = coerce JSS.head
574+-- head t = S.head (stream t)
482575{-# INLINE head #-}
483576
484577-- | /O(1)/ Returns the first character and rest of a 'Text', or
485578-- 'Nothing' if empty. Subject to fusion.
486579uncons :: Text -> Maybe (Char, Text)
487−uncons t@(Text arr off len)
488− | len <= 0 = Nothing
489− | otherwise = Just $ let !(Iter c d) = iter t 0
490− in (c, text arr (off+d) (len-d))
580+uncons = coerce JSS.uncons
581+-- uncons t@(Text arr off len)
582+-- | len <= 0 = Nothing
583+-- | otherwise = Just $ let !(Iter c d) = iter t 0
584+-- in (c, text arr (off+d) (len-d))
491585{-# INLINE [1] uncons #-}
492586
493587-- | Lifted from Control.Arrow and specialized.
@@ -497,12 +591,13 @@ second f (a, b) = (a, f b)
497591-- | /O(1)/ Returns the last character of a 'Text', which must be
498592-- non-empty. Subject to fusion.
499593last :: Text -> Char
500−last (Text arr off len)
501− | len <= 0 = emptyError "last"
502− | n < 0xDC00 || n > 0xDFFF = unsafeChr n
503− | otherwise = U16.chr2 n0 n
504− where n = A.unsafeIndex arr (off+len-1)
505− n0 = A.unsafeIndex arr (off+len-2)
594+last = coerce JSS.last
595+-- last (Text arr off len)
596+-- | len <= 0 = emptyError "last"
597+-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
598+-- | otherwise = U16.chr2 n0 n
599+-- where n = A.unsafeIndex arr (off+len-1)
600+-- n0 = A.unsafeIndex arr (off+len-2)
506601{-# INLINE [1] last #-}
507602
508603{-# RULES
@@ -515,10 +610,11 @@ last (Text arr off len)
515610-- | /O(1)/ Returns all characters after the head of a 'Text', which
516611-- must be non-empty. Subject to fusion.
517612tail :: Text -> Text
518−tail t@(Text arr off len)
519− | len <= 0 = emptyError "tail"
520− | otherwise = text arr (off+d) (len-d)
521− where d = iter_ t 0
613+tail = coerce JSS.tail
614+-- tail t@(Text arr off len)
615+-- | len <= 0 = emptyError "tail"
616+-- | otherwise = text arr (off+d) (len-d)
617+-- where d = iter_ t 0
522618{-# INLINE [1] tail #-}
523619
524620{-# RULES
@@ -531,11 +627,12 @@ tail t@(Text arr off len)
531627-- | /O(1)/ Returns all but the last character of a 'Text', which must
532628-- be non-empty. Subject to fusion.
533629init :: Text -> Text
534−init (Text arr off len) | len <= 0 = emptyError "init"
535− | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
536− | otherwise = text arr off (len-1)
537− where
538− n = A.unsafeIndex arr (off+len-1)
630+init = coerce JSS.init
631+-- init (Text arr off len) | len <= 0 = emptyError "init"
632+-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
633+-- | otherwise = text arr off (len-1)
634+-- where
635+-- n = A.unsafeIndex arr (off+len-1)
539636{-# INLINE [1] init #-}
540637
541638{-# RULES
@@ -545,14 +642,29 @@ init (Text arr off len) | len <= 0 = emptyError "init"
545642 unstream (S.init (stream t)) = init t
546643 #-}
547644
645+-- | /O(1)/ Returns all but the last character and the last character of a
646+-- 'Text', or 'Nothing' if empty.
647+--
648+-- @since 1.2.3.0
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 #-}
658+
548659-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
549660-- fusion.
550661null :: Text -> Bool
551−null (Text _arr _off len) =
552−#if defined(ASSERTS)
553− assert (len >= 0) $
554−#endif
555− len <= 0
662+null = coerce JSS.null
663+-- null (Text _arr _off len) =
664+-- #if defined(ASSERTS)
665+-- assert (len >= 0) $
666+-- #endif
667+-- len <= 0
556668{-# INLINE [1] null #-}
557669
558670{-# RULES
@@ -564,14 +676,15 @@ null (Text _arr _off len) =
564676
565677-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
566678-- Subject to fusion.
567−isSingleton :: Text -> Bool
568−isSingleton = S.isSingleton . stream
569−{-# INLINE isSingleton #-}
679+-- isSingleton :: Text -> Bool
680+-- isSingleton = S.isSingleton . stream
681+-- {-# INLINE isSingleton #-}
570682
571683-- | /O(n)/ Returns the number of characters in a 'Text'.
572684-- Subject to fusion.
573685length :: Text -> Int
574−length t = S.length (stream t)
686+length = coerce JSS.length
687+-- length t = S.length (stream t)
575688{-# INLINE [0] length #-}
576689-- length needs to be phased after the compareN/length rules otherwise
577690-- it may inline before the rules have an opportunity to fire.
@@ -624,29 +737,58 @@ compareLength t n = S.compareLengthI (stream t) n
624737-- -----------------------------------------------------------------------------
625738-- * Transformations
626739-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
627−-- each element of @t@. Subject to fusion. Performs replacement on
628−-- invalid scalar values.
740+-- each element of @t@.
741+--
742+-- Example:
743+--
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!"
747+--
748+-- Subject to fusion. Performs replacement on invalid scalar values.
629749map :: (Char -> Char) -> Text -> Text
630−map f t = unstream (S.map (safe . f) (stream t))
750+map = coerce JSS.map
751+-- map f t = unstream (S.map (safe . f) (stream t))
631752{-# INLINE [1] map #-}
632753
633754-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
634755-- 'Text's and concatenates the list after interspersing the first
635756-- argument between each element of the list.
757+--
758+-- Example:
759+--
760+-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
761+-- "WeNI!seekNI!theNI!HolyNI!Grail"
636762intercalate :: Text -> [Text] -> Text
637−intercalate t = concat . (F.intersperse t)
763+intercalate = coerce JSS.intercalate
764+-- intercalate t = concat . (F.intersperse t)
638765{-# INLINE intercalate #-}
639766
640767-- | /O(n)/ The 'intersperse' function takes a character and places it
641−-- between the characters of a 'Text'. Subject to fusion. Performs
642−-- replacement on invalid scalar values.
768+-- between the characters of a 'Text'.
769+--
770+-- Example:
771+--
772+-- >>> T.intersperse '.' "SHIELD"
773+-- "S.H.I.E.L.D"
774+--
775+-- Subject to fusion. Performs replacement on invalid scalar values.
643776intersperse :: Char -> Text -> Text
644−intersperse c t = unstream (S.intersperse (safe c) (stream t))
777+intersperse = coerce JSS.intersperse
778+-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
645779{-# INLINE intersperse #-}
646780
647−-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
781+-- | /O(n)/ Reverse the characters of a string.
782+--
783+-- Example:
784+--
785+-- >>> T.reverse "desrever"
786+-- "reversed"
787+--
788+-- Subject to fusion.
648789reverse :: Text -> Text
649−reverse t = S.reverse (stream t)
790+reverse = coerce JSS.reverse
791+-- reverse t = S.reverse (stream t)
650792{-# INLINE reverse #-}
651793
652794-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -663,12 +805,14 @@ reverse t = S.reverse (stream t)
663805-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
664806-- be replaced recursively:
665807--
666−-- > replace "oo" "foo" "oo" == "foo"
808+-- >>> replace "oo" "foo" "oo"
809+-- "foo"
667810--
668811-- In cases where several instances of @needle@ overlap, only the
669812-- first one will be replaced:
670813--
671−-- > replace "ofo" "bar" "ofofo" == "barfo"
814+-- >>> replace "ofo" "bar" "ofofo"
815+-- "barfo"
672816--
673817-- In (unlikely) bad cases, this function's time complexity degrades
674818-- towards /O(n*m)/.
@@ -680,28 +824,29 @@ replace :: Text
680824 -> Text
681825 -- ^ @haystack@ in which to search.
682826 -> Text
683−replace needle@(Text _ _ neeLen)
684− (Text repArr repOff repLen)
685− haystack@(Text hayArr hayOff hayLen)
686− | neeLen == 0 = emptyError "replace"
687− | L.null ixs = haystack
688− | len > 0 = Text (A.run x) 0 len
689− | otherwise = empty
690− where
691− ixs = indices needle haystack
692− len = hayLen - (neeLen - repLen) `mul` L.length ixs
693− x :: ST s (A.MArray s)
694− x = do
695− marr <- A.new len
696− let loop (i:is) o d = do
697− let d0 = d + i - o
698− d1 = d0 + repLen
699− A.copyI marr d hayArr (hayOff+o) d0
700− A.copyI marr d0 repArr repOff d1
701− loop is (i + neeLen) d1
702− loop [] o d = A.copyI marr d hayArr (hayOff+o) len
703− loop ixs 0 0
704− return marr
827+replace = coerce JSS.replace
828+-- replace needle@(Text _ _ neeLen)
829+-- (Text repArr repOff repLen)
830+-- haystack@(Text hayArr hayOff hayLen)
831+-- | neeLen == 0 = emptyError "replace"
832+-- | L.null ixs = haystack
833+-- | len > 0 = Text (A.run x) 0 len
834+-- | otherwise = empty
835+-- where
836+-- ixs = indices needle haystack
837+-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
838+-- x :: ST s (A.MArray s)
839+-- x = do
840+-- marr <- A.new len
841+-- let loop (i:is) o d = do
842+-- let d0 = d + i - o
843+-- d1 = d0 + repLen
844+-- A.copyI marr d hayArr (hayOff+o) d0
845+-- A.copyI marr d0 repArr repOff d1
846+-- loop is (i + neeLen) d1
847+-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
848+-- loop ixs 0 0
849+-- return marr
705850
706851-- ----------------------------------------------------------------------------
707852-- ** Case conversions (folds)
@@ -740,7 +885,8 @@ replace needle@(Text _ _ neeLen)
740885-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
741886-- instead of itself.
742887toCaseFold :: Text -> Text
743−toCaseFold t = unstream (S.toCaseFold (stream t))
888+-- toCaseFold t = unstream (S.toCaseFold (stream t))
889+toCaseFold = coerce JSS.toCaseFold
744890{-# INLINE toCaseFold #-}
745891
746892-- | /O(n)/ Convert a string to lower case, using simple case
@@ -751,7 +897,8 @@ toCaseFold t = unstream (S.toCaseFold (stream t))
751897-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
752898-- followed by \" ̇\" (combining dot above, U+0307).
753899toLower :: Text -> Text
754−toLower t = unstream (S.toLower (stream t))
900+-- toLower t = unstream (S.toLower (stream t))
901+toLower = coerce JSS.toLower
755902{-# INLINE toLower #-}
756903
757904-- | /O(n)/ Convert a string to upper case, using simple case
@@ -761,7 +908,8 @@ toLower t = unstream (S.toLower (stream t))
761908-- instance, the German \"ß\" (eszett, U+00DF) maps to the
762909-- two-letter sequence \"SS\".
763910toUpper :: Text -> Text
764−toUpper t = unstream (S.toUpper (stream t))
911+-- toUpper t = unstream (S.toUpper (stream t))
912+toUpper = coerce JSS.toUpper
765913{-# INLINE toUpper #-}
766914
767915-- | /O(n)/ Convert a string to title case, using simple case
@@ -782,8 +930,11 @@ toUpper t = unstream (S.toUpper (stream t))
782930-- guides disagree on whether the book name \"The Hill of the Red
783931-- Fox\" is correctly title cased—but this function will
784932-- capitalize /every/ word.
933+--
934+-- @since 1.0.0.0
785935toTitle :: Text -> Text
786−toTitle t = unstream (S.toTitle (stream t))
936+toTitle = coerce JSS.toTitle
937+-- toTitle t = unstream (S.toTitle (stream t))
787938{-# INLINE toTitle #-}
788939
789940-- | /O(n)/ Left-justify a string to the given length, using the
@@ -792,13 +943,17 @@ toTitle t = unstream (S.toTitle (stream t))
792943--
793944-- Examples:
794945--
795−-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796−-- > justifyLeft 3 'x' "foobar" == "foobar"
946+-- >>> justifyLeft 7 'x' "foo"
947+-- "fooxxxx"
948+--
949+-- >>> justifyLeft 3 'x' "foobar"
950+-- "foobar"
797951justifyLeft :: Int -> Char -> Text -> Text
798−justifyLeft k c t
799− | len >= k = t
800− | otherwise = t `append` replicateChar (k-len) c
801− where len = length t
952+justifyLeft = coerce JSS.justifyLeft
953+-- justifyLeft k c t
954+-- | len >= k = t
955+-- | otherwise = t `append` replicateChar (k-len) c
956+-- where len = length t
802957{-# INLINE [1] justifyLeft #-}
803958
804959{-# RULES
@@ -814,13 +969,17 @@ justifyLeft k c t
814969--
815970-- Examples:
816971--
817−-- > justifyRight 7 'x' "bar" == "xxxxbar"
818−-- > justifyRight 3 'x' "foobar" == "foobar"
972+-- >>> justifyRight 7 'x' "bar"
973+-- "xxxxbar"
974+--
975+-- >>> justifyRight 3 'x' "foobar"
976+-- "foobar"
819977justifyRight :: Int -> Char -> Text -> Text
820−justifyRight k c t
821− | len >= k = t
822− | otherwise = replicateChar (k-len) c `append` t
823− where len = length t
978+justifyRight = coerce JSS.justifyRight
979+-- justifyRight k c t
980+-- | len >= k = t
981+-- | otherwise = replicateChar (k-len) c `append` t
982+-- where len = length t
824983{-# INLINE justifyRight #-}
825984
826985-- | /O(n)/ Center a string to the given length, using the specified
@@ -829,23 +988,34 @@ justifyRight k c t
829988--
830989-- Examples:
831990--
832−-- > center 8 'x' "HS" = "xxxHSxxx"
991+-- >>> center 8 'x' "HS"
992+-- "xxxHSxxx"
833993center :: Int -> Char -> Text -> Text
834−center k c t
835− | len >= k = t
836− | otherwise = replicateChar l c `append` t `append` replicateChar r c
837− where len = length t
838− d = k - len
839− r = d `quot` 2
840− l = d - r
994+center = coerce JSS.center
995+-- center k c t
996+-- | len >= k = t
997+-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
998+-- where len = length t
999+-- d = k - len
1000+-- r = d `quot` 2
1001+-- l = d - r
8411002{-# INLINE center #-}
8421003
8431004-- | /O(n)/ The 'transpose' function transposes the rows and columns
8441005-- of its 'Text' argument. Note that this function uses 'pack',
8451006-- 'unpack', and the list version of transpose, and is thus not very
8461007-- efficient.
1008+--
1009+-- Examples:
1010+--
1011+-- >>> transpose ["green","orange"]
1012+-- ["go","rr","ea","en","ng","e"]
1013+--
1014+-- >>> transpose ["blue","red"]
1015+-- ["br","le","ud","e"]
8471016transpose :: [Text] -> [Text]
848−transpose ts = P.map pack (L.transpose (P.map unpack ts))
1017+transpose = coerce JSS.transpose
1018+-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
8491019
8501020-- -----------------------------------------------------------------------------
8511021-- * Reducing 'Text's (folds)
@@ -855,23 +1025,27 @@ transpose ts = P.map pack (L.transpose (P.map unpack ts))
8551025-- reduces the 'Text' using the binary operator, from left to right.
8561026-- Subject to fusion.
8571027foldl :: (a -> Char -> a) -> a -> Text -> a
858−foldl f z t = S.foldl f z (stream t)
1028+foldl f a0 (Text t) = JSS.foldl f a0 t
1029+-- foldl f z t = S.foldl f z (stream t)
8591030{-# INLINE foldl #-}
8601031
8611032-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
8621033foldl' :: (a -> Char -> a) -> a -> Text -> a
863−foldl' f z t = S.foldl' f z (stream t)
1034+foldl' f a0 (Text t) = JSS.foldl' f a0 t
1035+-- foldl' f z t = S.foldl' f z (stream t)
8641036{-# INLINE foldl' #-}
8651037
8661038-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
8671039-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
8681040foldl1 :: (Char -> Char -> Char) -> Text -> Char
869−foldl1 f t = S.foldl1 f (stream t)
1041+foldl1 = coerce JSS.foldl1
1042+-- foldl1 f t = S.foldl1 f (stream t)
8701043{-# INLINE foldl1 #-}
8711044
8721045-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
8731046foldl1' :: (Char -> Char -> Char) -> Text -> Char
874−foldl1' f t = S.foldl1' f (stream t)
1047+foldl1' = coerce JSS.foldl1'
1048+-- foldl1' f t = S.foldl1' f (stream t)
8751049{-# INLINE foldl1' #-}
8761050
8771051-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -879,14 +1053,16 @@ foldl1' f t = S.foldl1' f (stream t)
8791053-- reduces the 'Text' using the binary operator, from right to left.
8801054-- Subject to fusion.
8811055foldr :: (Char -> a -> a) -> a -> Text -> a
882−foldr f z t = S.foldr f z (stream t)
1056+foldr f a0 (Text t) = JSS.foldr f a0 t
1057+-- foldr f z t = S.foldr f z (stream t)
8831058{-# INLINE foldr #-}
8841059
8851060-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
8861061-- and thus must be applied to a non-empty 'Text'. Subject to
8871062-- fusion.
8881063foldr1 :: (Char -> Char -> Char) -> Text -> Char
889−foldr1 f t = S.foldr1 f (stream t)
1064+foldr1 = coerce JSS.foldr1
1065+-- foldr1 f t = S.foldr1 f (stream t)
8901066{-# INLINE foldr1 #-}
8911067
8921068-- -----------------------------------------------------------------------------
@@ -894,48 +1070,54 @@ foldr1 f t = S.foldr1 f (stream t)
8941070
8951071-- | /O(n)/ Concatenate a list of 'Text's.
8961072concat :: [Text] -> Text
897−concat ts = case ts' of
898− [] -> empty
899− [t] -> t
900− _ -> Text (A.run go) 0 len
901− where
902− ts' = L.filter (not . null) ts
903− len = sumP "concat" $ L.map lengthWord16 ts'
904− go :: ST s (A.MArray s)
905− go = do
906− arr <- A.new len
907− let step i (Text a o l) =
908− let !j = i + l in A.copyI arr i a o j >> return j
909− foldM step 0 ts' >> return arr
1073+concat = coerce JSS.concat
1074+-- concat ts = case ts' of
1075+-- [] -> empty
1076+-- [t] -> t
1077+-- _ -> Text (A.run go) 0 len
1078+-- where
1079+-- ts' = L.filter (not . null) ts
1080+-- len = sumP "concat" $ L.map lengthWord16 ts'
1081+-- go :: ST s (A.MArray s)
1082+-- go = do
1083+-- arr <- A.new len
1084+-- let step i (Text a o l) =
1085+-- let !j = i + l in A.copyI arr i a o j >> return j
1086+-- foldM step 0 ts' >> return arr
9101087
9111088-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
9121089-- concatenate the results.
9131090concatMap :: (Char -> Text) -> Text -> Text
914−concatMap f = concat . foldr ((:) . f) []
1091+concatMap = coerce JSS.concatMap
1092+-- concatMap f = concat . foldr ((:) . f) []
9151093{-# INLINE concatMap #-}
9161094
9171095-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
9181096-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
9191097any :: (Char -> Bool) -> Text -> Bool
920−any p t = S.any p (stream t)
1098+any = coerce JSS.any
1099+-- any p t = S.any p (stream t)
9211100{-# INLINE any #-}
9221101
9231102-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
9241103-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
9251104all :: (Char -> Bool) -> Text -> Bool
926−all p t = S.all p (stream t)
1105+all = coerce JSS.all
1106+-- all p t = S.all p (stream t)
9271107{-# INLINE all #-}
9281108
9291109-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
9301110-- must be non-empty. Subject to fusion.
9311111maximum :: Text -> Char
932−maximum t = S.maximum (stream t)
1112+maximum = coerce JSS.maximum
1113+-- maximum t = S.maximum (stream t)
9331114{-# INLINE maximum #-}
9341115
9351116-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
9361117-- must be non-empty. Subject to fusion.
9371118minimum :: Text -> Char
938−minimum t = S.minimum (stream t)
1119+minimum = coerce JSS.minimum
1120+-- minimum t = S.minimum (stream t)
9391121{-# INLINE minimum #-}
9401122
9411123-- -----------------------------------------------------------------------------
@@ -951,8 +1133,9 @@ minimum t = S.minimum (stream t)
9511133--
9521134-- > last (scanl f z xs) == foldl f z xs.
9531135scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
954−scanl f z t = unstream (S.scanl g z (stream t))
955− where g a b = safe (f a b)
1136+scanl = coerce JSS.scanl
1137+-- scanl f z t = unstream (S.scanl g z (stream t))
1138+-- where g a b = safe (f a b)
9561139{-# INLINE scanl #-}
9571140
9581141-- | /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))
9611144--
9621145-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
9631146scanl1 :: (Char -> Char -> Char) -> Text -> Text
964−scanl1 f t | null t = empty
965− | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1147+scanl1 = coerce JSS.scanl1
1148+-- scanl1 f t | null t = empty
1149+-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
9661150{-# INLINE scanl1 #-}
9671151
9681152-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -970,16 +1154,18 @@ scanl1 f t | null t = empty
9701154--
9711155-- > scanr f v == reverse . scanl (flip f) v . reverse
9721156scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
973−scanr f z = S.reverse . S.reverseScanr g z . reverseStream
974− where g a b = safe (f a b)
1157+scanr = coerce JSS.scanr
1158+-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1159+-- where g a b = safe (f a b)
9751160{-# INLINE scanr #-}
9761161
9771162-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
9781163-- value argument. Subject to fusion. Performs replacement on
9791164-- invalid scalar values.
9801165scanr1 :: (Char -> Char -> Char) -> Text -> Text
981−scanr1 f t | null t = empty
982− | otherwise = scanr f (last t) (init t)
1166+scanr1 = coerce JSS.scanr1
1167+-- scanr1 f t | null t = empty
1168+-- | otherwise = scanr f (last t) (init t)
9831169{-# INLINE scanr1 #-}
9841170
9851171-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -987,8 +1173,11 @@ scanr1 f t | null t = empty
9871173-- parameter from left to right, and returns a final 'Text'. Performs
9881174-- replacement on invalid scalar values.
9891175mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
990−mapAccumL f z0 = S.mapAccumL g z0 . stream
991− where g a b = second safe (f a b)
1176+mapAccumL f z0 (Text a) =
1177+ let (a', t) = JSS.mapAccumL f z0 a
1178+ in (a', Text t)
1179+-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1180+-- where g a b = second safe (f a b)
9921181{-# INLINE mapAccumL #-}
9931182
9941183-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -998,8 +1187,11 @@ mapAccumL f z0 = S.mapAccumL g z0 . stream
9981187-- 'Text'.
9991188-- Performs replacement on invalid scalar values.
10001189mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1001−mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1002− where g a b = second safe (f a b)
1190+mapAccumR f z0 (Text a) =
1191+ let (a', t) = JSS.mapAccumR f z0 a
1192+ in (a', Text t)
1193+-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1194+-- where g a b = second safe (f a b)
10031195{-# INLINE mapAccumR #-}
10041196
10051197-- -----------------------------------------------------------------------------
@@ -1008,20 +1200,21 @@ mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
10081200-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
10091201-- @t@ repeated @n@ times.
10101202replicate :: Int -> Text -> Text
1011−replicate n t@(Text a o l)
1012− | n <= 0 || l <= 0 = empty
1013− | n == 1 = t
1014− | isSingleton t = replicateChar n (unsafeHead t)
1015− | otherwise = Text (A.run x) 0 len
1016− where
1017− len = l `mul` n
1018− x :: ST s (A.MArray s)
1019− x = do
1020− arr <- A.new len
1021− let loop !d !i | i >= n = return arr
1022− | otherwise = let m = d + l
1023− in A.copyI arr d a o m >> loop m (i+1)
1024− loop 0 0
1203+replicate = coerce JSS.replicate
1204+-- replicate n t@(Text a o l)
1205+-- | n <= 0 || l <= 0 = empty
1206+-- | n == 1 = t
1207+-- | isSingleton t = replicateChar n (unsafeHead t)
1208+-- | otherwise = Text (A.run x) 0 len
1209+-- where
1210+-- len = l `mul` n
1211+-- x :: ST s (A.MArray s)
1212+-- x = do
1213+-- arr <- A.new len
1214+-- let loop !d !i | i >= n = return arr
1215+-- | otherwise = let m = d + l
1216+-- in A.copyI arr d a o m >> loop m (i+1)
1217+-- loop 0 0
10251218{-# INLINE [1] replicate #-}
10261219
10271220{-# RULES
@@ -1032,8 +1225,19 @@ replicate n t@(Text a o l)
10321225-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
10331226-- value of every element. Subject to fusion.
10341227replicateChar :: Int -> Char -> Text
1228+#ifndef __GHCJS__
10351229replicateChar n c = unstream (S.replicateCharI n (safe c))
10361230{-# INLINE replicateChar #-}
1231+#else
1232+replicateChar n c = Text $ js_replicateChar n c
1233+{-# INLINE [1] replicateChar #-}
1234+{-# RULES
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
1239+ #-}
1240+#endif
10371241
10381242-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
10391243-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1043,7 +1247,8 @@ replicateChar n c = unstream (S.replicateCharI n (safe c))
10431247-- string, and @b@ is the seed value for further production. Subject
10441248-- to fusion. Performs replacement on invalid scalar values.
10451249unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1046−unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1250+unfoldr f a = Text $ JSS.unfoldr f a
1251+-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10471252{-# INLINE unfoldr #-}
10481253
10491254-- | /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)
10531258-- correct, otherwise its performance is similar to 'unfoldr'. Subject
10541259-- to fusion. Performs replacement on invalid scalar values.
10551260unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1056−unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1261+unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1262+-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10571263{-# INLINE unfoldrN #-}
10581264
10591265-- -----------------------------------------------------------------------------
@@ -1063,18 +1269,19 @@ unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10631269-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
10641270-- the length of the Text. Subject to fusion.
10651271take :: Int -> Text -> Text
1066−take n t@(Text arr off len)
1067− | n <= 0 = empty
1068− | n >= len = t
1069− | otherwise = text arr off (iterN n t)
1272+take = coerce JSS.take
1273+-- take n t@(Text arr off len)
1274+-- | n <= 0 = empty
1275+-- | n >= len = t
1276+-- | otherwise = text arr off (iterN n t)
10701277{-# INLINE [1] take #-}
10711278
1072−iterN :: Int -> Text -> Int
1073−iterN n t@(Text _arr _off len) = loop 0 0
1074− where loop !i !cnt
1075− | i >= len || cnt >= n = i
1076− | otherwise = loop (i+d) (cnt+1)
1077− where d = iter_ t i
1279+-- iterN :: Int -> Text -> Int
1280+-- iterN n t@(Text _arr _off len) = loop 0 0
1281+-- where loop !i !cnt
1282+-- | i >= len || cnt >= n = i
1283+-- | otherwise = loop (i+d) (cnt+1)
1284+-- where d = iter_ t i
10781285
10791286{-# RULES
10801287"TEXT take -> fused" [~1] forall n t.
@@ -1088,31 +1295,36 @@ iterN n t@(Text _arr _off len) = loop 0 0
10881295--
10891296-- Examples:
10901297--
1091−-- > takeEnd 3 "foobar" == "bar"
1298+-- >>> takeEnd 3 "foobar"
1299+-- "bar"
1300+--
1301+-- @since 1.1.1.0
10921302takeEnd :: Int -> Text -> Text
1093−takeEnd n t@(Text arr off len)
1094− | n <= 0 = empty
1095− | n >= len = t
1096− | otherwise = text arr (off+i) (len-i)
1097− where i = iterNEnd n t
1098−
1099−iterNEnd :: Int -> Text -> Int
1100−iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1101− where loop i !m
1102− | i <= 0 = 0
1103− | m <= 1 = i
1104− | otherwise = loop (i+d) (m-1)
1105− where d = reverseIter_ t i
1303+takeEnd = coerce JSS.takeEnd
1304+-- takeEnd n t@(Text arr off len)
1305+-- | n <= 0 = empty
1306+-- | n >= len = t
1307+-- | otherwise = text arr (off+i) (len-i)
1308+-- where i = iterNEnd n t
1309+
1310+-- iterNEnd :: Int -> Text -> Int
1311+-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1312+-- where loop i !m
1313+-- | i <= 0 = 0
1314+-- | m <= 1 = i
1315+-- | otherwise = loop (i+d) (m-1)
1316+-- where d = reverseIter_ t i
11061317
11071318-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
11081319-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
11091320-- is greater than the length of the 'Text'. Subject to fusion.
11101321drop :: Int -> Text -> Text
1111−drop n t@(Text arr off len)
1112− | n <= 0 = t
1113− | n >= len = empty
1114− | otherwise = text arr (off+i) (len-i)
1115− where i = iterN n t
1322+drop = coerce JSS.drop
1323+-- drop n t@(Text arr off len)
1324+-- | n <= 0 = t
1325+-- | n >= len = empty
1326+-- | otherwise = text arr (off+i) (len-i)
1327+-- where i = iterN n t
11161328{-# INLINE [1] drop #-}
11171329
11181330{-# RULES
@@ -1127,22 +1339,27 @@ drop n t@(Text arr off len)
11271339--
11281340-- Examples:
11291341--
1130−-- > dropEnd 3 "foobar" == "foo"
1342+-- >>> dropEnd 3 "foobar"
1343+-- "foo"
1344+--
1345+-- @since 1.1.1.0
11311346dropEnd :: Int -> Text -> Text
1132−dropEnd n t@(Text arr off len)
1133− | n <= 0 = t
1134− | n >= len = empty
1135− | otherwise = text arr off (iterNEnd n t)
1347+dropEnd = coerce JSS.dropEnd
1348+-- dropEnd n t@(Text arr off len)
1349+-- | n <= 0 = t
1350+-- | n >= len = empty
1351+-- | otherwise = text arr off (iterNEnd n t)
11361352
11371353-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
11381354-- returns the longest prefix (possibly empty) of elements that
11391355-- satisfy @p@. Subject to fusion.
11401356takeWhile :: (Char -> Bool) -> Text -> Text
1141−takeWhile p t@(Text arr off len) = loop 0
1142− where loop !i | i >= len = t
1143− | p c = loop (i+d)
1144− | otherwise = text arr off i
1145− where Iter c d = iter t i
1357+takeWhile = coerce JSS.takeWhile
1358+-- takeWhile p t@(Text arr off len) = loop 0
1359+-- where loop !i | i >= len = t
1360+-- | p c = loop (i+d)
1361+-- | otherwise = text arr off i
1362+-- where Iter c d = iter t i
11461363{-# INLINE [1] takeWhile #-}
11471364
11481365{-# RULES
@@ -1157,13 +1374,25 @@ takeWhile p t@(Text arr off len) = loop 0
11571374-- satisfy @p@. Subject to fusion.
11581375-- Examples:
11591376--
1160−-- > takeWhileEnd (=='o') "foo" == "oo"
1377+-- >>> takeWhileEnd (=='o') "foo"
1378+-- "oo"
1379+--
1380+-- @since 1.2.2.0
11611381takeWhileEnd :: (Char -> Bool) -> Text -> Text
1382+#ifndef __GHCJS__
11621383takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11631384 where loop !i !l | l <= 0 = t
11641385 | p c = loop (i+d) (l+d)
11651386 | otherwise = text arr (off+l) (len-l)
11661387 where (c,d) = reverseIter t i
1388+#else
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
1394+ len = length t
1395+#endif
11671396{-# INLINE [1] takeWhileEnd #-}
11681397
11691398{-# RULES
@@ -1176,11 +1405,12 @@ takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11761405-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
11771406-- 'takeWhile' @p@ @t@. Subject to fusion.
11781407dropWhile :: (Char -> Bool) -> Text -> Text
1179−dropWhile p t@(Text arr off len) = loop 0 0
1180− where loop !i !l | l >= len = empty
1181− | p c = loop (i+d) (l+d)
1182− | otherwise = Text arr (off+i) (len-l)
1183− where Iter c d = iter t i
1408+dropWhile = coerce JSS.dropWhile
1409+-- dropWhile p t@(Text arr off len) = loop 0 0
1410+-- where loop !i !l | l >= len = empty
1411+-- | p c = loop (i+d) (l+d)
1412+-- | otherwise = Text arr (off+i) (len-l)
1413+
11841414{-# INLINE [1] dropWhile #-}
11851415
11861416{-# RULES
@@ -1196,13 +1426,15 @@ dropWhile p t@(Text arr off len) = loop 0 0
11961426--
11971427-- Examples:
11981428--
1199−-- > dropWhileEnd (=='.') "foo..." == "foo"
1429+-- >>> dropWhileEnd (=='.') "foo..."
1430+-- "foo"
12001431dropWhileEnd :: (Char -> Bool) -> Text -> Text
1201−dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1202− where loop !i !l | l <= 0 = empty
1203− | p c = loop (i+d) (l+d)
1204− | otherwise = Text arr off l
1205− where (c,d) = reverseIter t i
1432+dropWhileEnd = coerce JSS.dropWhileEnd
1433+-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1434+-- where loop !i !l | l <= 0 = empty
1435+-- | p c = loop (i+d) (l+d)
1436+-- | otherwise = Text arr off l
1437+-- where (c,d) = reverseIter t i
12061438{-# INLINE [1] dropWhileEnd #-}
12071439
12081440{-# RULES
@@ -1245,45 +1477,49 @@ strip = dropAround isSpace
12451477-- prefix of @t@ of length @n@, and whose second is the remainder of
12461478-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
12471479splitAt :: Int -> Text -> (Text, Text)
1248−splitAt n t@(Text arr off len)
1249− | n <= 0 = (empty, t)
1250− | n >= len = (t, empty)
1251− | otherwise = let k = iterN n t
1252− in (text arr off k, text arr (off+k) (len-k))
1480+splitAt = coerce JSS.splitAt
1481+-- splitAt n t@(Text arr off len)
1482+-- | n <= 0 = (empty, t)
1483+-- | n >= len = (t, empty)
1484+-- | otherwise = let k = iterN n t
1485+-- in (text arr off k, text arr (off+k) (len-k))
12531486
12541487-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
12551488-- a pair whose first element is the longest prefix (possibly empty)
12561489-- of @t@ of elements that satisfy @p@, and whose second is the
12571490-- remainder of the list.
12581491span :: (Char -> Bool) -> Text -> (Text, Text)
1259−span p t = case span_ p t of
1260− (# hd,tl #) -> (hd,tl)
1492+span = coerce JSS.span
1493+-- span p t = case span_ p t of
1494+-- (# hd,tl #) -> (hd,tl)
12611495{-# INLINE span #-}
12621496
12631497-- | /O(n)/ 'break' is like 'span', but the prefix returned is
12641498-- over elements that fail the predicate @p@.
12651499break :: (Char -> Bool) -> Text -> (Text, Text)
1266−break p = span (not . p)
1500+break = coerce JSS.break
1501+-- break p = span (not . p)
12671502{-# INLINE break #-}
12681503
12691504-- | /O(n)/ Group characters in a string according to a predicate.
12701505groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1271−groupBy p = loop
1272− where
1273− loop t@(Text arr off len)
1274− | null t = []
1275− | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1276− where Iter c d = iter t 0
1277− n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1506+groupBy = coerce JSS.groupBy
1507+-- groupBy p = loop
1508+-- where
1509+-- loop t@(Text arr off len)
1510+-- | null t = []
1511+-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1512+-- where Iter c d = iter t 0
1513+-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
12781514
12791515-- | Returns the /array/ index (in units of 'Word16') at which a
12801516-- character may be found. This is /not/ the same as the logical
12811517-- index returned by e.g. 'findIndex'.
1282−findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1283−findAIndexOrEnd q t@(Text _arr _off len) = go 0
1284− where go !i | i >= len || q c = i
1285− | otherwise = go (i+d)
1286− where Iter c d = iter t i
1518+-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1519+-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1520+-- where go !i | i >= len || q c = i
1521+-- | otherwise = go (i+d)
1522+-- where Iter c d = iter t i
12871523
12881524-- | /O(n)/ Group characters in a string by equality.
12891525group :: Text -> [Text]
@@ -1292,15 +1528,17 @@ group = groupBy (==)
12921528-- | /O(n)/ Return all initial segments of the given 'Text', shortest
12931529-- first.
12941530inits :: Text -> [Text]
1295−inits t@(Text arr off len) = loop 0
1296− where loop i | i >= len = [t]
1297− | otherwise = Text arr off i : loop (i + iter_ t i)
1531+inits = coerce JSS.inits
1532+-- inits t@(Text arr off len) = loop 0
1533+-- where loop i | i >= len = [t]
1534+-- | otherwise = Text arr off i : loop (i + iter_ t i)
12981535
12991536-- | /O(n)/ Return all final segments of the given 'Text', longest
13001537-- first.
13011538tails :: Text -> [Text]
1302−tails t | null t = [empty]
1303− | otherwise = t : tails (unsafeTail t)
1539+tails = coerce JSS.tails
1540+-- tails t | null t = [empty]
1541+-- | otherwise = t : tails (unsafeTail t)
13041542
13051543-- $split
13061544--
@@ -1314,9 +1552,14 @@ tails t | null t = [empty]
13141552--
13151553-- Examples:
13161554--
1317−-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1318−-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1319−-- > splitOn "x" "x" == ["",""]
1555+-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1556+-- ["a","b","d","e"]
1557+--
1558+-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1559+-- ["","X","X","X",""]
1560+--
1561+-- >>> splitOn "x" "x"
1562+-- ["",""]
13201563--
13211564-- and
13221565--
@@ -1333,13 +1576,14 @@ splitOn :: Text
13331576 -> Text
13341577 -- ^ Input text.
13351578 -> [Text]
1336−splitOn pat@(Text _ _ l) src@(Text arr off len)
1337− | l <= 0 = emptyError "splitOn"
1338− | isSingleton pat = split (== unsafeHead pat) src
1339− | otherwise = go 0 (indices pat src)
1340− where
1341− go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1342− go s _ = [text arr (s+off) (len-s)]
1579+splitOn = coerce JSS.splitOn
1580+-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1581+-- | l <= 0 = emptyError "splitOn"
1582+-- | isSingleton pat = split (== unsafeHead pat) src
1583+-- | otherwise = go 0 (indices pat src)
1584+-- where
1585+-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1586+-- go s _ = [text arr (s+off) (len-s)]
13431587{-# INLINE [1] splitOn #-}
13441588
13451589{-# RULES
@@ -1352,28 +1596,36 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
13521596-- resulting components do not contain the separators. Two adjacent
13531597-- separators result in an empty component in the output. eg.
13541598--
1355−-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356−-- > split (=='a') "" == [""]
1599+-- >>> split (=='a') "aabbaca"
1600+-- ["","","bb","c",""]
1601+--
1602+-- >>> split (=='a') ""
1603+-- [""]
13571604split :: (Char -> Bool) -> Text -> [Text]
1358−split _ t@(Text _off _arr 0) = [t]
1359−split p t = loop t
1360− where loop s | null s' = [l]
1361− | otherwise = l : loop (unsafeTail s')
1362− where (# l, s' #) = span_ (not . p) s
1605+split = coerce JSS.split
1606+-- split _ t@(Text _off _arr 0) = [t]
1607+-- split p t = loop t
1608+-- where loop s | null s' = [l]
1609+-- | otherwise = l : loop (unsafeTail s')
1610+-- where (# l, s' #) = span_ (not . p) s
13631611{-# INLINE split #-}
13641612
13651613-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
13661614-- element may be shorter than the other chunks, depending on the
13671615-- length of the input. Examples:
13681616--
1369−-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370−-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1617+-- >>> chunksOf 3 "foobarbaz"
1618+-- ["foo","bar","baz"]
1619+--
1620+-- >>> chunksOf 4 "haskell.org"
1621+-- ["hask","ell.","org"]
13711622chunksOf :: Int -> Text -> [Text]
1372−chunksOf k = go
1373− where
1374− go t = case splitAt k t of
1375− (a,b) | null a -> []
1376− | otherwise -> a : go b
1623+chunksOf = coerce JSS.chunksOf
1624+-- chunksOf k = go
1625+-- where
1626+-- go t = case splitAt k t of
1627+-- (a,b) | null a -> []
1628+-- | otherwise -> a : go b
13771629{-# INLINE chunksOf #-}
13781630
13791631-- ----------------------------------------------------------------------------
@@ -1386,7 +1638,8 @@ chunksOf k = go
13861638-- returns the first element matching the predicate, or 'Nothing' if
13871639-- there is no such element.
13881640find :: (Char -> Bool) -> Text -> Maybe Char
1389−find p t = S.findBy p (stream t)
1641+find = coerce JSS.find
1642+-- find p t = S.findBy p (stream t)
13901643{-# INLINE find #-}
13911644
13921645-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1395,14 +1648,16 @@ find p t = S.findBy p (stream t)
13951648--
13961649-- > partition p t == (filter p t, filter (not . p) t)
13971650partition :: (Char -> Bool) -> Text -> (Text, Text)
1398−partition p t = (filter p t, filter (not . p) t)
1651+partition = coerce JSS.partition
1652+-- partition p t = (filter p t, filter (not . p) t)
13991653{-# INLINE partition #-}
14001654
14011655-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
14021656-- returns a 'Text' containing those characters that satisfy the
14031657-- predicate.
14041658filter :: (Char -> Bool) -> Text -> Text
1405−filter p t = unstream (S.filter p (stream t))
1659+filter = coerce JSS.filter
1660+-- filter p t = unstream (S.filter p (stream t))
14061661{-# INLINE filter #-}
14071662
14081663-- | /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))
14121667--
14131668-- Examples:
14141669--
1415−-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416−-- > breakOn "/" "foobar" ==> ("foobar", "")
1670+-- >>> breakOn "::" "a::b::c"
1671+-- ("a","::b::c")
1672+--
1673+-- >>> breakOn "/" "foobar"
1674+-- ("foobar","")
14171675--
14181676-- Laws:
14191677--
@@ -1427,11 +1685,12 @@ filter p t = unstream (S.filter p (stream t))
14271685-- In (unlikely) bad cases, this function's time complexity degrades
14281686-- towards /O(n*m)/.
14291687breakOn :: Text -> Text -> (Text, Text)
1430−breakOn pat src@(Text arr off len)
1431− | null pat = emptyError "breakOn"
1432− | otherwise = case indices pat src of
1433− [] -> (src, empty)
1434− (x:_) -> (text arr off x, text arr (off+x) (len-x))
1688+breakOn = coerce JSS.breakOn
1689+-- breakOn pat src@(Text arr off len)
1690+-- | null pat = emptyError "breakOn"
1691+-- | otherwise = case indices pat src of
1692+-- [] -> (src, empty)
1693+-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
14351694{-# INLINE breakOn #-}
14361695
14371696-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1441,10 +1700,12 @@ breakOn pat src@(Text arr off len)
14411700-- up to and including the last match of @needle@. The second is the
14421701-- remainder of @haystack@, following the match.
14431702--
1444−-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1703+-- >>> breakOnEnd "::" "a::b::c"
1704+-- ("a::b::","c")
14451705breakOnEnd :: Text -> Text -> (Text, Text)
1446−breakOnEnd pat src = (reverse b, reverse a)
1447− where (a,b) = breakOn (reverse pat) (reverse src)
1706+breakOnEnd = coerce JSS.breakOnEnd
1707+-- breakOnEnd pat src = (reverse b, reverse a)
1708+-- where (a,b) = breakOn (reverse pat) (reverse src)
14481709{-# INLINE breakOnEnd #-}
14491710
14501711-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1456,10 +1717,11 @@ breakOnEnd pat src = (reverse b, reverse a)
14561717--
14571718-- Examples:
14581719--
1459−-- > breakOnAll "::" ""
1460−-- > ==> []
1461−-- > breakOnAll "/" "a/b/c/"
1462−-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
1720+-- >>> breakOnAll "::" ""
1721+-- []
1722+--
1723+-- >>> breakOnAll "/" "a/b/c/"
1724+-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
14631725--
14641726-- In (unlikely) bad cases, this function's time complexity degrades
14651727-- towards /O(n*m)/.
@@ -1468,12 +1730,13 @@ breakOnEnd pat src = (reverse b, reverse a)
14681730breakOnAll :: Text -- ^ @needle@ to search for
14691731 -> Text -- ^ @haystack@ in which to search
14701732 -> [(Text, Text)]
1471−breakOnAll pat src@(Text arr off slen)
1472− | null pat = emptyError "breakOnAll"
1473− | otherwise = L.map step (indices pat src)
1474− where
1475− step x = (chunk 0 x, chunk x (slen-x))
1476− chunk !n !l = text arr (n+off) l
1733+breakOnAll = coerce JSS.breakOnAll
1734+-- breakOnAll pat src@(Text arr off slen)
1735+-- | null pat = emptyError "breakOnAll"
1736+-- | otherwise = L.map step (indices pat src)
1737+-- where
1738+-- step x = (chunk 0 x, chunk x (slen-x))
1739+-- chunk !n !l = text arr (n+off) l
14771740{-# INLINE breakOnAll #-}
14781741
14791742-------------------------------------------------------------------------------
@@ -1497,14 +1760,16 @@ breakOnAll pat src@(Text arr off slen)
14971760
14981761-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
14991762index :: Text -> Int -> Char
1500−index t n = S.index (stream t) n
1763+index = coerce JSS.index
1764+-- index t n = S.index (stream t) n
15011765{-# INLINE index #-}
15021766
15031767-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
15041768-- and returns the index of the first element in the 'Text' satisfying
15051769-- the predicate. Subject to fusion.
15061770findIndex :: (Char -> Bool) -> Text -> Maybe Int
1507−findIndex p t = S.findIndex p (stream t)
1771+findIndex = coerce JSS.findIndex
1772+-- findIndex p t = S.findIndex p (stream t)
15081773{-# INLINE findIndex #-}
15091774
15101775-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1514,10 +1779,11 @@ findIndex p t = S.findIndex p (stream t)
15141779-- In (unlikely) bad cases, this function's time complexity degrades
15151780-- towards /O(n*m)/.
15161781count :: Text -> Text -> Int
1517−count pat src
1518− | null pat = emptyError "count"
1519− | isSingleton pat = countChar (unsafeHead pat) src
1520− | otherwise = L.length (indices pat src)
1782+count = coerce JSS.count
1783+-- count pat src
1784+-- | null pat = emptyError "count"
1785+-- | isSingleton pat = countChar (unsafeHead pat) src
1786+-- | otherwise = L.length (indices pat src)
15211787{-# INLINE [1] count #-}
15221788
15231789{-# RULES
@@ -1539,42 +1805,46 @@ countChar c t = S.countChar c (stream t)
15391805-- excess elements of the longer 'Text' are discarded. This is
15401806-- equivalent to a pair of 'unpack' operations.
15411807zip :: Text -> Text -> [(Char,Char)]
1542−zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1808+zip = coerce JSS.zip
1809+-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
15431810{-# INLINE zip #-}
15441811
15451812-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
15461813-- given as the first argument, instead of a tupling function.
15471814-- Performs replacement on invalid scalar values.
15481815zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1549−zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550− where g a b = safe (f a b)
1816+zipWith = coerce JSS.zipWith
1817+-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1818+-- where g a b = safe (f a b)
15511819{-# INLINE zipWith #-}
15521820
15531821-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
15541822-- representing white space.
15551823words :: Text -> [Text]
1556−words t@(Text arr off len) = loop 0 0
1557− where
1558− loop !start !n
1559− | n >= len = if start == n
1560− then []
1561− else [Text arr (start+off) (n-start)]
1562− | isSpace c =
1563− if start == n
1564− then loop (start+1) (start+1)
1565− else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1566− | otherwise = loop start (n+d)
1567− where Iter c d = iter t n
1824+words = coerce JSS.words
1825+-- words t@(Text arr off len) = loop 0 0
1826+-- where
1827+-- loop !start !n
1828+-- | n >= len = if start == n
1829+-- then []
1830+-- else [Text arr (start+off) (n-start)]
1831+-- | isSpace c =
1832+-- if start == n
1833+-- then loop (start+1) (start+1)
1834+-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1835+-- | otherwise = loop start (n+d)
1836+-- where Iter c d = iter t n
15681837{-# INLINE words #-}
15691838
15701839-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
15711840-- newline 'Char's. The resulting strings do not contain newlines.
15721841lines :: Text -> [Text]
1573−lines ps | null ps = []
1574− | otherwise = h : if null t
1575− then []
1576− else lines (unsafeTail t)
1577− where (# h,t #) = span_ (/= '\n') ps
1842+lines = coerce JSS.lines
1843+-- lines ps | null ps = []
1844+-- | otherwise = h : if null t
1845+-- then []
1846+-- else lines (unsafeTail t)
1847+-- where (# h,t #) = span_ (/= '\n') ps
15781848{-# INLINE lines #-}
15791849
15801850{-
@@ -1602,34 +1872,40 @@ lines' ps | null ps = []
16021872-- | /O(n)/ Joins lines, after appending a terminating newline to
16031873-- each.
16041874unlines :: [Text] -> Text
1605−unlines = concat . L.map (`snoc` '\n')
1875+unlines = coerce JSS.unlines
1876+-- unlines = concat . L.map (`snoc` '\n')
16061877{-# INLINE unlines #-}
16071878
16081879-- | /O(n)/ Joins words using single space characters.
16091880unwords :: [Text] -> Text
1610−unwords = intercalate (singleton ' ')
1881+unwords = coerce JSS.unwords
1882+-- unwords = intercalate (singleton ' ')
16111883{-# INLINE unwords #-}
16121884
16131885-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
16141886-- 'True' iff the first is a prefix of the second. Subject to fusion.
16151887isPrefixOf :: Text -> Text -> Bool
1616−isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617− alen <= blen && S.isPrefixOf (stream a) (stream b)
1888+isPrefixOf = coerce JSS.isPrefixOf
1889+-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1890+-- alen <= blen && S.isPrefixOf (stream a) (stream b)
16181891{-# INLINE [1] isPrefixOf #-}
16191892
16201893{-# RULES
16211894"TEXT isPrefixOf -> fused" [~1] forall s t.
16221895 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
16231898 #-}
16241899
16251900-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
16261901-- 'True' iff the first is a suffix of the second.
16271902isSuffixOf :: Text -> Text -> Bool
1628−isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1629− d >= 0 && a == b'
1630− where d = blen - alen
1631− b' | d == 0 = b
1632− | otherwise = Text barr (boff+d) alen
1903+isSuffixOf = coerce JSS.isSuffixOf
1904+-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1905+-- d >= 0 && a == b'
1906+-- where d = blen - alen
1907+-- b' | d == 0 = b
1908+-- | otherwise = Text barr (boff+d) alen
16331909{-# INLINE isSuffixOf #-}
16341910
16351911-- | /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) =
16391915-- In (unlikely) bad cases, this function's time complexity degrades
16401916-- towards /O(n*m)/.
16411917isInfixOf :: Text -> Text -> Bool
1642−isInfixOf needle haystack
1643− | null needle = True
1644− | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1645− | otherwise = not . L.null . indices needle $ haystack
1918+isInfixOf = coerce JSS.isInfixOf
1919+-- isInfixOf needle haystack
1920+-- | null needle = True
1921+-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1922+-- | otherwise = not . L.null . indices needle $ haystack
16461923{-# INLINE [1] isInfixOf #-}
16471924
16481925{-# RULES
@@ -1658,9 +1935,14 @@ isInfixOf needle haystack
16581935--
16591936-- Examples:
16601937--
1661−-- > stripPrefix "foo" "foobar" == Just "bar"
1662−-- > stripPrefix "" "baz" == Just "baz"
1663−-- > stripPrefix "foo" "quux" == Nothing
1938+-- >>> stripPrefix "foo" "foobar"
1939+-- Just "bar"
1940+--
1941+-- >>> stripPrefix "" "baz"
1942+-- Just "baz"
1943+--
1944+-- >>> stripPrefix "foo" "quux"
1945+-- Nothing
16641946--
16651947-- This is particularly useful with the @ViewPatterns@ extension to
16661948-- GHC, as follows:
@@ -1672,9 +1954,10 @@ isInfixOf needle haystack
16721954-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
16731955-- > fnordLength _ = -1
16741956stripPrefix :: Text -> Text -> Maybe Text
1675−stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1676− | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1677− | otherwise = Nothing
1957+stripPrefix = coerce JSS.stripPrefix
1958+-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1959+-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1960+-- | otherwise = Nothing
16781961
16791962-- | /O(n)/ Find the longest non-empty common prefix of two strings
16801963-- 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)
16851968--
16861969-- Examples:
16871970--
1688−-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689−-- > commonPrefixes "veeble" "fetzer" == Nothing
1690−-- > commonPrefixes "" "baz" == Nothing
1971+-- >>> commonPrefixes "foobar" "fooquux"
1972+-- Just ("foo","bar","quux")
1973+--
1974+-- >>> commonPrefixes "veeble" "fetzer"
1975+-- Nothing
1976+--
1977+-- >>> commonPrefixes "" "baz"
1978+-- Nothing
16911979commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1692−commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1693− where
1694− go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1695− | i > 0 = Just (Text arr0 off0 i,
1696− text arr0 (off0+i) (len0-i),
1697− text arr1 (off1+j) (len1-j))
1698− | otherwise = Nothing
1699− where Iter a d0 = iter t0 i
1700− Iter b d1 = iter t1 j
1980+commonPrefixes = coerce JSS.commonPrefixes
1981+-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1982+-- where
1983+-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1984+-- | i > 0 = Just (Text arr0 off0 i,
1985+-- text arr0 (off0+i) (len0-i),
1986+-- text arr1 (off1+j) (len1-j))
1987+-- | otherwise = Nothing
1988+-- where Iter a d0 = iter t0 i
1989+-- Iter b d1 = iter t1 j
17011990
17021991-- | /O(n)/ Return the prefix of the second string if its suffix
17031992-- matches the entire first string.
17041993--
17051994-- Examples:
17061995--
1707−-- > stripSuffix "bar" "foobar" == Just "foo"
1708−-- > stripSuffix "" "baz" == Just "baz"
1709−-- > stripSuffix "foo" "quux" == Nothing
1996+-- >>> stripSuffix "bar" "foobar"
1997+-- Just "foo"
1998+--
1999+-- >>> stripSuffix "" "baz"
2000+-- Just "baz"
2001+--
2002+-- >>> stripSuffix "foo" "quux"
2003+-- Nothing
17102004--
17112005-- This is particularly useful with the @ViewPatterns@ extension to
17122006-- GHC, as follows:
@@ -1718,21 +2012,22 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
17182012-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
17192013-- > quuxLength _ = -1
17202014stripSuffix :: Text -> Text -> Maybe Text
1721−stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1722− | p `isSuffixOf` t = Just $! text arr off (len-plen)
1723− | otherwise = Nothing
1724−
1725−-- | Add a list of non-negative numbers. Errors out on overflow.
1726−sumP :: String -> [Int] -> Int
1727−sumP fun = go 0
1728− where go !a (x:xs)
1729− | ax >= 0 = go ax xs
1730− | otherwise = overflowError fun
1731− where ax = a + x
1732− go a _ = a
1733−
1734−emptyError :: String -> a
1735−emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
2015+stripSuffix = coerce JSS.stripSuffix
2016+-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2017+-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
2018+-- | otherwise = Nothing
2019+
2020+-- -- | Add a list of non-negative numbers. Errors out on overflow.
2021+-- sumP :: String -> [Int] -> Int
2022+-- sumP fun = go 0
2023+-- where go !a (x:xs)
2024+-- | ax >= 0 = go ax xs
2025+-- | otherwise = overflowError fun
2026+-- where ax = a + x
2027+-- go a _ = a
2028+
2029+-- emptyError :: String -> a
2030+-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
17362031
17372032overflowError :: String -> a
17382033overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1746,10 +2041,22 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
17462041-- copy \"breaks the link\" to the original array, allowing it to be
17472042-- garbage collected if there are no other live references to it.
17482043copy :: Text -> Text
1749−copy (Text arr off len) = Text (A.run go) 0 len
1750− where
1751− go :: ST s (A.MArray s)
1752− go = do
1753− marr <- A.new len
1754− A.copyI marr 0 arr off len
1755− return marr
2044+copy = P.id -- TODO
2045+-- copy (Text arr off len) = Text (A.run go) 0 len
2046+-- where
2047+-- go :: ST s (A.MArray s)
2048+-- go = do
2049+-- marr <- A.new len
2050+
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
2055+
2056+-------------------------------------------------
2057+-- NOTE: the named chunk below used by doctest;
2058+-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2059+
2060+-- $setup
2061+-- >>> :set -XOverloadedStrings
2062+-- >>> import qualified Data.Text as T