@@ -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,68 @@ 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 (JSVal (..), unpackJSString#)
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. pack (GHC.Base.build (GHC.unpackFoldrCString# s)) = Text (JSString (unsafeDupablePerformIO (unpackJSString# s))) #-}
281+
282+-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
283+unpackCString# :: Addr# -> Text
284+unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
285+{-# NOINLINE unpackCString# #-}
286+
287+{-# RULES "TEXT literal" forall a.
288+ unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
289+ = unpackCString# a #-}
290+
291+{-# RULES "TEXT literal UTF8" [1] forall a.
292+ unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
293+ = unpackCString# a #-}
294+
295+{-# RULES "TEXT empty literal" [1]
296+ unstream (S.map safe (S.streamList []))
297+ = empty_ #-}
298+
299+{-# RULES "TEXT singleton literal" [1] forall a.
300+ unstream (S.map safe (S.streamList [a]))
301+ = singleton a #-}
302+
303+---------------------------------------------------------------------
304+-- $character_definition
305+--
306+-- This package uses the term /character/ to denote Unicode /code points/.
307+--
308+-- Note that this is not the same thing as a grapheme (e.g. a
309+-- composition of code points that form one visual symbol). For
310+-- instance, consider the grapheme \"ä\". This symbol has two
311+-- Unicode representations: a single code-point representation
312+-- @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code point),
313+-- and a two code point representation @U+0061@ (the \"@A@\" code
314+-- point) and @U+0308@ (the @COMBINING DIAERESIS@ code point).
315+
249316-- $strict
250317--
251318-- This package provides both strict and lazy 'Text' types. The
@@ -321,22 +388,31 @@ import Text.Printf (PrintfArg, formatArg, formatString)
321388-- Functions that can be fused by the compiler are documented with the
322389-- phrase \"Subject to fusion\".
323390
391+-- instance Eq Text where
392+-- Text arrA offA lenA == Text arrB offB lenB
393+-- | lenA == lenB = A.equal arrA offA arrB offB lenA
394+-- | otherwise = False
395+-- {-# INLINE (==) #-}
324396instance 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 (==) #-}
397+ Text x == Text y = x == y
398+
399+-- instance Ord Text where
400+-- compare = compareText
329401
330402instance Ord Text where
331− compare = compareText
403+ compare (Text x) (Text y) = compare x y
332404
333405instance Read Text where
334406 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
335407
336408#if MIN_VERSION_base(4,9,0)
337−-- Semigroup orphan instances for older GHCs are provided by
338−-- 'semigroups` package
339−
409+-- | Non-orphan 'Semigroup' instance only defined for
410+-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
411+-- provided by
412+-- the [semigroups](http://hackage.haskell.org/package/semigroups)
413+-- package
414+--
415+-- @since 1.2.2.0
340416instance Semigroup Text where
341417 (<>) = append
342418#endif
@@ -354,6 +430,7 @@ instance IsString Text where
354430 fromString = pack
355431
356432#if __GLASGOW_HASKELL__ >= 708
433+-- | @since 1.2.0.0
357434instance Exts.IsList Text where
358435 type Item Text = Char
359436 fromList = pack
@@ -364,6 +441,7 @@ instance Exts.IsList Text where
364441instance NFData Text where rnf !_ = ()
365442#endif
366443
444+-- | @since 1.2.1.0
367445instance Binary Text where
368446 put t = put (encodeUtf8 t)
369447 get = do
@@ -397,8 +475,10 @@ instance Data Text where
397475
398476#if MIN_VERSION_base(4,7,0)
399477-- | Only defined for @base-4.7.0.0@ and later
478+--
479+-- @since 1.2.2.0
400480instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
481+ formatArg t = formatString $ unpack t
402482#endif
403483
404484packConstr :: Constr
@@ -407,19 +487,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
407487textDataType :: DataType
408488textDataType = mkDataType "Data.Text.Text" [packConstr]
409489
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
490+-- -- | /O(n)/ Compare two 'Text' values lexicographically.
491+-- compareText :: Text -> Text -> Ordering
492+-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
493+-- | lenA == 0 && lenB == 0 = EQ
494+-- | otherwise = go 0 0
495+-- where
496+-- go !i !j
497+-- | i >= lenA || j >= lenB = compare lenA lenB
498+-- | a < b = LT
499+-- | a > b = GT
500+-- | otherwise = go (i+di) (j+dj)
501+-- where Iter a di = iter ta i
502+-- Iter b dj = iter tb j
423503
424504-- -----------------------------------------------------------------------------
425505-- * Conversion to/from 'Text'
@@ -427,7 +507,8 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
427507-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
428508-- fusion. Performs replacement on invalid scalar values.
429509pack :: String -> Text
430−pack = unstream . S.map safe . S.streamList
510+pack = coerce JSS.pack
511+-- pack = unstream . S.map safe . S.streamList
431512{-# INLINE [1] pack #-}
432513
433514-- -----------------------------------------------------------------------------
@@ -438,7 +519,8 @@ pack = unstream . S.map safe . S.streamList
438519-- copying a new array. Subject to fusion. Performs replacement on
439520-- invalid scalar values.
440521cons :: Char -> Text -> Text
441−cons c t = unstream (S.cons (safe c) (stream t))
522+cons = coerce JSS.cons
523+-- cons c t = unstream (S.cons (safe c) (stream t))
442524{-# INLINE cons #-}
443525
444526infixr 5 `cons`
@@ -447,25 +529,27 @@ infixr 5 `cons`
447529-- entire array in the process, unless fused. Subject to fusion.
448530-- Performs replacement on invalid scalar values.
449531snoc :: Text -> Char -> Text
450−snoc t c = unstream (S.snoc (stream t) (safe c))
532+snoc = coerce JSS.snoc
533+-- snoc t c = unstream (S.snoc (stream t) (safe c))
451534{-# INLINE snoc #-}
452535
453536-- | /O(n)/ Appends one 'Text' to the other by copying both of them
454537-- into a new 'Text'. Subject to fusion.
455538append :: 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
539+append = coerce JSS.append
540+-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
541+-- | len1 == 0 = b
542+-- | len2 == 0 = a
543+-- | len > 0 = Text (A.run x) 0 len
544+-- | otherwise = overflowError "append"
545+-- where
546+-- len = len1+len2
547+-- x :: ST s (A.MArray s)
548+-- x = do
549+-- arr <- A.new len
550+-- A.copyI arr 0 arr1 off1 len1
551+-- A.copyI arr len1 arr2 off2 len
552+-- return arr
469553{-# NOINLINE append #-}
470554
471555{-# RULES
@@ -478,16 +562,18 @@ append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
478562-- | /O(1)/ Returns the first character of a 'Text', which must be
479563-- non-empty. Subject to fusion.
480564head :: Text -> Char
481−head t = S.head (stream t)
565+head = coerce JSS.head
566+-- head t = S.head (stream t)
482567{-# INLINE head #-}
483568
484569-- | /O(1)/ Returns the first character and rest of a 'Text', or
485570-- 'Nothing' if empty. Subject to fusion.
486571uncons :: 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))
572+uncons = coerce JSS.uncons
573+-- uncons t@(Text arr off len)
574+-- | len <= 0 = Nothing
575+-- | otherwise = Just $ let !(Iter c d) = iter t 0
576+-- in (c, text arr (off+d) (len-d))
491577{-# INLINE [1] uncons #-}
492578
493579-- | Lifted from Control.Arrow and specialized.
@@ -497,12 +583,13 @@ second f (a, b) = (a, f b)
497583-- | /O(1)/ Returns the last character of a 'Text', which must be
498584-- non-empty. Subject to fusion.
499585last :: 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)
586+last = coerce JSS.last
587+-- last (Text arr off len)
588+-- | len <= 0 = emptyError "last"
589+-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
590+-- | otherwise = U16.chr2 n0 n
591+-- where n = A.unsafeIndex arr (off+len-1)
592+-- n0 = A.unsafeIndex arr (off+len-2)
506593{-# INLINE [1] last #-}
507594
508595{-# RULES
@@ -515,10 +602,11 @@ last (Text arr off len)
515602-- | /O(1)/ Returns all characters after the head of a 'Text', which
516603-- must be non-empty. Subject to fusion.
517604tail :: 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
605+tail = coerce JSS.tail
606+-- tail t@(Text arr off len)
607+-- | len <= 0 = emptyError "tail"
608+-- | otherwise = text arr (off+d) (len-d)
609+-- where d = iter_ t 0
522610{-# INLINE [1] tail #-}
523611
524612{-# RULES
@@ -531,11 +619,12 @@ tail t@(Text arr off len)
531619-- | /O(1)/ Returns all but the last character of a 'Text', which must
532620-- be non-empty. Subject to fusion.
533621init :: 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)
622+init = coerce JSS.init
623+-- init (Text arr off len) | len <= 0 = emptyError "init"
624+-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
625+-- | otherwise = text arr off (len-1)
626+-- where
627+-- n = A.unsafeIndex arr (off+len-1)
539628{-# INLINE [1] init #-}
540629
541630{-# RULES
@@ -545,14 +634,29 @@ init (Text arr off len) | len <= 0 = emptyError "init"
545634 unstream (S.init (stream t)) = init t
546635 #-}
547636
637+-- | /O(1)/ Returns all but the last character and the last character of a
638+-- 'Text', or 'Nothing' if empty.
639+--
640+-- @since 1.2.3.0
641+unsnoc :: Text -> Maybe (Text, Char)
642+unsnoc = coerce JSS.unsnoc
643+-- unsnoc (Text arr off len)
644+-- | len <= 0 = Nothing
645+-- | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
646+-- | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
647+-- where n = A.unsafeIndex arr (off+len-1)
648+-- n0 = A.unsafeIndex arr (off+len-2)
649+{-# INLINE [1] unsnoc #-}
650+
548651-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
549652-- fusion.
550653null :: Text -> Bool
551−null (Text _arr _off len) =
552−#if defined(ASSERTS)
553− assert (len >= 0) $
554−#endif
555− len <= 0
654+null = coerce JSS.null
655+-- null (Text _arr _off len) =
656+-- #if defined(ASSERTS)
657+-- assert (len >= 0) $
658+-- #endif
659+-- len <= 0
556660{-# INLINE [1] null #-}
557661
558662{-# RULES
@@ -564,14 +668,15 @@ null (Text _arr _off len) =
564668
565669-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
566670-- Subject to fusion.
567−isSingleton :: Text -> Bool
568−isSingleton = S.isSingleton . stream
569−{-# INLINE isSingleton #-}
671+-- isSingleton :: Text -> Bool
672+-- isSingleton = S.isSingleton . stream
673+-- {-# INLINE isSingleton #-}
570674
571675-- | /O(n)/ Returns the number of characters in a 'Text'.
572676-- Subject to fusion.
573677length :: Text -> Int
574−length t = S.length (stream t)
678+length = coerce JSS.length
679+-- length t = S.length (stream t)
575680{-# INLINE [0] length #-}
576681-- length needs to be phased after the compareN/length rules otherwise
577682-- it may inline before the rules have an opportunity to fire.
@@ -624,29 +729,58 @@ compareLength t n = S.compareLengthI (stream t) n
624729-- -----------------------------------------------------------------------------
625730-- * Transformations
626731-- | /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.
732+-- each element of @t@.
733+--
734+-- Example:
735+--
736+-- >>> let message = pack "I am not angry. Not at all."
737+-- >>> T.map (\c -> if c == '.' then '!' else c) message
738+-- "I am not angry! Not at all!"
739+--
740+-- Subject to fusion. Performs replacement on invalid scalar values.
629741map :: (Char -> Char) -> Text -> Text
630−map f t = unstream (S.map (safe . f) (stream t))
742+map = coerce JSS.map
743+-- map f t = unstream (S.map (safe . f) (stream t))
631744{-# INLINE [1] map #-}
632745
633746-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
634747-- 'Text's and concatenates the list after interspersing the first
635748-- argument between each element of the list.
749+--
750+-- Example:
751+--
752+-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
753+-- "WeNI!seekNI!theNI!HolyNI!Grail"
636754intercalate :: Text -> [Text] -> Text
637−intercalate t = concat . (F.intersperse t)
755+intercalate = coerce JSS.intercalate
756+-- intercalate t = concat . (F.intersperse t)
638757{-# INLINE intercalate #-}
639758
640759-- | /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.
760+-- between the characters of a 'Text'.
761+--
762+-- Example:
763+--
764+-- >>> T.intersperse '.' "SHIELD"
765+-- "S.H.I.E.L.D"
766+--
767+-- Subject to fusion. Performs replacement on invalid scalar values.
643768intersperse :: Char -> Text -> Text
644−intersperse c t = unstream (S.intersperse (safe c) (stream t))
769+intersperse = coerce JSS.intersperse
770+-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
645771{-# INLINE intersperse #-}
646772
647−-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
773+-- | /O(n)/ Reverse the characters of a string.
774+--
775+-- Example:
776+--
777+-- >>> T.reverse "desrever"
778+-- "reversed"
779+--
780+-- Subject to fusion.
648781reverse :: Text -> Text
649−reverse t = S.reverse (stream t)
782+reverse = coerce JSS.reverse
783+-- reverse t = S.reverse (stream t)
650784{-# INLINE reverse #-}
651785
652786-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -663,12 +797,14 @@ reverse t = S.reverse (stream t)
663797-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
664798-- be replaced recursively:
665799--
666−-- > replace "oo" "foo" "oo" == "foo"
800+-- >>> replace "oo" "foo" "oo"
801+-- "foo"
667802--
668803-- In cases where several instances of @needle@ overlap, only the
669804-- first one will be replaced:
670805--
671−-- > replace "ofo" "bar" "ofofo" == "barfo"
806+-- >>> replace "ofo" "bar" "ofofo"
807+-- "barfo"
672808--
673809-- In (unlikely) bad cases, this function's time complexity degrades
674810-- towards /O(n*m)/.
@@ -680,28 +816,29 @@ replace :: Text
680816 -> Text
681817 -- ^ @haystack@ in which to search.
682818 -> 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
819+replace = coerce JSS.replace
820+-- replace needle@(Text _ _ neeLen)
821+-- (Text repArr repOff repLen)
822+-- haystack@(Text hayArr hayOff hayLen)
823+-- | neeLen == 0 = emptyError "replace"
824+-- | L.null ixs = haystack
825+-- | len > 0 = Text (A.run x) 0 len
826+-- | otherwise = empty
827+-- where
828+-- ixs = indices needle haystack
829+-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
830+-- x :: ST s (A.MArray s)
831+-- x = do
832+-- marr <- A.new len
833+-- let loop (i:is) o d = do
834+-- let d0 = d + i - o
835+-- d1 = d0 + repLen
836+-- A.copyI marr d hayArr (hayOff+o) d0
837+-- A.copyI marr d0 repArr repOff d1
838+-- loop is (i + neeLen) d1
839+-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
840+-- loop ixs 0 0
841+-- return marr
705842
706843-- ----------------------------------------------------------------------------
707844-- ** Case conversions (folds)
@@ -740,7 +877,8 @@ replace needle@(Text _ _ neeLen)
740877-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
741878-- instead of itself.
742879toCaseFold :: Text -> Text
743−toCaseFold t = unstream (S.toCaseFold (stream t))
880+-- toCaseFold t = unstream (S.toCaseFold (stream t))
881+toCaseFold = coerce JSS.toCaseFold
744882{-# INLINE toCaseFold #-}
745883
746884-- | /O(n)/ Convert a string to lower case, using simple case
@@ -751,7 +889,8 @@ toCaseFold t = unstream (S.toCaseFold (stream t))
751889-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
752890-- followed by \" ̇\" (combining dot above, U+0307).
753891toLower :: Text -> Text
754−toLower t = unstream (S.toLower (stream t))
892+-- toLower t = unstream (S.toLower (stream t))
893+toLower = coerce JSS.toLower
755894{-# INLINE toLower #-}
756895
757896-- | /O(n)/ Convert a string to upper case, using simple case
@@ -761,7 +900,8 @@ toLower t = unstream (S.toLower (stream t))
761900-- instance, the German \"ß\" (eszett, U+00DF) maps to the
762901-- two-letter sequence \"SS\".
763902toUpper :: Text -> Text
764−toUpper t = unstream (S.toUpper (stream t))
903+-- toUpper t = unstream (S.toUpper (stream t))
904+toUpper = coerce JSS.toUpper
765905{-# INLINE toUpper #-}
766906
767907-- | /O(n)/ Convert a string to title case, using simple case
@@ -782,8 +922,11 @@ toUpper t = unstream (S.toUpper (stream t))
782922-- guides disagree on whether the book name \"The Hill of the Red
783923-- Fox\" is correctly title cased—but this function will
784924-- capitalize /every/ word.
925+--
926+-- @since 1.0.0.0
785927toTitle :: Text -> Text
786−toTitle t = unstream (S.toTitle (stream t))
928+toTitle = coerce JSS.toTitle
929+-- toTitle t = unstream (S.toTitle (stream t))
787930{-# INLINE toTitle #-}
788931
789932-- | /O(n)/ Left-justify a string to the given length, using the
@@ -792,13 +935,17 @@ toTitle t = unstream (S.toTitle (stream t))
792935--
793936-- Examples:
794937--
795−-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796−-- > justifyLeft 3 'x' "foobar" == "foobar"
938+-- >>> justifyLeft 7 'x' "foo"
939+-- "fooxxxx"
940+--
941+-- >>> justifyLeft 3 'x' "foobar"
942+-- "foobar"
797943justifyLeft :: 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
944+justifyLeft = coerce JSS.justifyLeft
945+-- justifyLeft k c t
946+-- | len >= k = t
947+-- | otherwise = t `append` replicateChar (k-len) c
948+-- where len = length t
802949{-# INLINE [1] justifyLeft #-}
803950
804951{-# RULES
@@ -814,13 +961,17 @@ justifyLeft k c t
814961--
815962-- Examples:
816963--
817−-- > justifyRight 7 'x' "bar" == "xxxxbar"
818−-- > justifyRight 3 'x' "foobar" == "foobar"
964+-- >>> justifyRight 7 'x' "bar"
965+-- "xxxxbar"
966+--
967+-- >>> justifyRight 3 'x' "foobar"
968+-- "foobar"
819969justifyRight :: 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
970+justifyRight = coerce JSS.justifyRight
971+-- justifyRight k c t
972+-- | len >= k = t
973+-- | otherwise = replicateChar (k-len) c `append` t
974+-- where len = length t
824975{-# INLINE justifyRight #-}
825976
826977-- | /O(n)/ Center a string to the given length, using the specified
@@ -829,23 +980,34 @@ justifyRight k c t
829980--
830981-- Examples:
831982--
832−-- > center 8 'x' "HS" = "xxxHSxxx"
983+-- >>> center 8 'x' "HS"
984+-- "xxxHSxxx"
833985center :: 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
986+center = coerce JSS.center
987+-- center k c t
988+-- | len >= k = t
989+-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
990+-- where len = length t
991+-- d = k - len
992+-- r = d `quot` 2
993+-- l = d - r
841994{-# INLINE center #-}
842995
843996-- | /O(n)/ The 'transpose' function transposes the rows and columns
844997-- of its 'Text' argument. Note that this function uses 'pack',
845998-- 'unpack', and the list version of transpose, and is thus not very
846999-- efficient.
1000+--
1001+-- Examples:
1002+--
1003+-- >>> transpose ["green","orange"]
1004+-- ["go","rr","ea","en","ng","e"]
1005+--
1006+-- >>> transpose ["blue","red"]
1007+-- ["br","le","ud","e"]
8471008transpose :: [Text] -> [Text]
848−transpose ts = P.map pack (L.transpose (P.map unpack ts))
1009+transpose = coerce JSS.transpose
1010+-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
8491011
8501012-- -----------------------------------------------------------------------------
8511013-- * Reducing 'Text's (folds)
@@ -855,23 +1017,27 @@ transpose ts = P.map pack (L.transpose (P.map unpack ts))
8551017-- reduces the 'Text' using the binary operator, from left to right.
8561018-- Subject to fusion.
8571019foldl :: (a -> Char -> a) -> a -> Text -> a
858−foldl f z t = S.foldl f z (stream t)
1020+foldl f a0 (Text t) = JSS.foldl f a0 t
1021+-- foldl f z t = S.foldl f z (stream t)
8591022{-# INLINE foldl #-}
8601023
8611024-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
8621025foldl' :: (a -> Char -> a) -> a -> Text -> a
863−foldl' f z t = S.foldl' f z (stream t)
1026+foldl' f a0 (Text t) = JSS.foldl' f a0 t
1027+-- foldl' f z t = S.foldl' f z (stream t)
8641028{-# INLINE foldl' #-}
8651029
8661030-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
8671031-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
8681032foldl1 :: (Char -> Char -> Char) -> Text -> Char
869−foldl1 f t = S.foldl1 f (stream t)
1033+foldl1 = coerce JSS.foldl1
1034+-- foldl1 f t = S.foldl1 f (stream t)
8701035{-# INLINE foldl1 #-}
8711036
8721037-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
8731038foldl1' :: (Char -> Char -> Char) -> Text -> Char
874−foldl1' f t = S.foldl1' f (stream t)
1039+foldl1' = coerce JSS.foldl1'
1040+-- foldl1' f t = S.foldl1' f (stream t)
8751041{-# INLINE foldl1' #-}
8761042
8771043-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -879,14 +1045,16 @@ foldl1' f t = S.foldl1' f (stream t)
8791045-- reduces the 'Text' using the binary operator, from right to left.
8801046-- Subject to fusion.
8811047foldr :: (Char -> a -> a) -> a -> Text -> a
882−foldr f z t = S.foldr f z (stream t)
1048+foldr f a0 (Text t) = JSS.foldr f a0 t
1049+-- foldr f z t = S.foldr f z (stream t)
8831050{-# INLINE foldr #-}
8841051
8851052-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
8861053-- and thus must be applied to a non-empty 'Text'. Subject to
8871054-- fusion.
8881055foldr1 :: (Char -> Char -> Char) -> Text -> Char
889−foldr1 f t = S.foldr1 f (stream t)
1056+foldr1 = coerce JSS.foldr1
1057+-- foldr1 f t = S.foldr1 f (stream t)
8901058{-# INLINE foldr1 #-}
8911059
8921060-- -----------------------------------------------------------------------------
@@ -894,48 +1062,54 @@ foldr1 f t = S.foldr1 f (stream t)
8941062
8951063-- | /O(n)/ Concatenate a list of 'Text's.
8961064concat :: [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
1065+concat = coerce JSS.concat
1066+-- concat ts = case ts' of
1067+-- [] -> empty
1068+-- [t] -> t
1069+-- _ -> Text (A.run go) 0 len
1070+-- where
1071+-- ts' = L.filter (not . null) ts
1072+-- len = sumP "concat" $ L.map lengthWord16 ts'
1073+-- go :: ST s (A.MArray s)
1074+-- go = do
1075+-- arr <- A.new len
1076+-- let step i (Text a o l) =
1077+-- let !j = i + l in A.copyI arr i a o j >> return j
1078+-- foldM step 0 ts' >> return arr
9101079
9111080-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
9121081-- concatenate the results.
9131082concatMap :: (Char -> Text) -> Text -> Text
914−concatMap f = concat . foldr ((:) . f) []
1083+concatMap = coerce JSS.concatMap
1084+-- concatMap f = concat . foldr ((:) . f) []
9151085{-# INLINE concatMap #-}
9161086
9171087-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
9181088-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
9191089any :: (Char -> Bool) -> Text -> Bool
920−any p t = S.any p (stream t)
1090+any = coerce JSS.any
1091+-- any p t = S.any p (stream t)
9211092{-# INLINE any #-}
9221093
9231094-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
9241095-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
9251096all :: (Char -> Bool) -> Text -> Bool
926−all p t = S.all p (stream t)
1097+all = coerce JSS.all
1098+-- all p t = S.all p (stream t)
9271099{-# INLINE all #-}
9281100
9291101-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
9301102-- must be non-empty. Subject to fusion.
9311103maximum :: Text -> Char
932−maximum t = S.maximum (stream t)
1104+maximum = coerce JSS.maximum
1105+-- maximum t = S.maximum (stream t)
9331106{-# INLINE maximum #-}
9341107
9351108-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
9361109-- must be non-empty. Subject to fusion.
9371110minimum :: Text -> Char
938−minimum t = S.minimum (stream t)
1111+minimum = coerce JSS.minimum
1112+-- minimum t = S.minimum (stream t)
9391113{-# INLINE minimum #-}
9401114
9411115-- -----------------------------------------------------------------------------
@@ -951,8 +1125,9 @@ minimum t = S.minimum (stream t)
9511125--
9521126-- > last (scanl f z xs) == foldl f z xs.
9531127scanl :: (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)
1128+scanl = coerce JSS.scanl
1129+-- scanl f z t = unstream (S.scanl g z (stream t))
1130+-- where g a b = safe (f a b)
9561131{-# INLINE scanl #-}
9571132
9581133-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -961,8 +1136,9 @@ scanl f z t = unstream (S.scanl g z (stream t))
9611136--
9621137-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
9631138scanl1 :: (Char -> Char -> Char) -> Text -> Text
964−scanl1 f t | null t = empty
965− | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1139+scanl1 = coerce JSS.scanl1
1140+-- scanl1 f t | null t = empty
1141+-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
9661142{-# INLINE scanl1 #-}
9671143
9681144-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -970,16 +1146,18 @@ scanl1 f t | null t = empty
9701146--
9711147-- > scanr f v == reverse . scanl (flip f) v . reverse
9721148scanr :: (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)
1149+scanr = coerce JSS.scanr
1150+-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1151+-- where g a b = safe (f a b)
9751152{-# INLINE scanr #-}
9761153
9771154-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
9781155-- value argument. Subject to fusion. Performs replacement on
9791156-- invalid scalar values.
9801157scanr1 :: (Char -> Char -> Char) -> Text -> Text
981−scanr1 f t | null t = empty
982− | otherwise = scanr f (last t) (init t)
1158+scanr1 = coerce JSS.scanr1
1159+-- scanr1 f t | null t = empty
1160+-- | otherwise = scanr f (last t) (init t)
9831161{-# INLINE scanr1 #-}
9841162
9851163-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -987,8 +1165,11 @@ scanr1 f t | null t = empty
9871165-- parameter from left to right, and returns a final 'Text'. Performs
9881166-- replacement on invalid scalar values.
9891167mapAccumL :: (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)
1168+mapAccumL f z0 (Text a) =
1169+ let (a', t) = JSS.mapAccumL f z0 a
1170+ in (a', Text t)
1171+-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1172+-- where g a b = second safe (f a b)
9921173{-# INLINE mapAccumL #-}
9931174
9941175-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -998,8 +1179,11 @@ mapAccumL f z0 = S.mapAccumL g z0 . stream
9981179-- 'Text'.
9991180-- Performs replacement on invalid scalar values.
10001181mapAccumR :: (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)
1182+mapAccumR f z0 (Text a) =
1183+ let (a', t) = JSS.mapAccumR f z0 a
1184+ in (a', Text t)
1185+-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1186+-- where g a b = second safe (f a b)
10031187{-# INLINE mapAccumR #-}
10041188
10051189-- -----------------------------------------------------------------------------
@@ -1008,20 +1192,21 @@ mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
10081192-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
10091193-- @t@ repeated @n@ times.
10101194replicate :: 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
1195+replicate = coerce JSS.replicate
1196+-- replicate n t@(Text a o l)
1197+-- | n <= 0 || l <= 0 = empty
1198+-- | n == 1 = t
1199+-- | isSingleton t = replicateChar n (unsafeHead t)
1200+-- | otherwise = Text (A.run x) 0 len
1201+-- where
1202+-- len = l `mul` n
1203+-- x :: ST s (A.MArray s)
1204+-- x = do
1205+-- arr <- A.new len
1206+-- let loop !d !i | i >= n = return arr
1207+-- | otherwise = let m = d + l
1208+-- in A.copyI arr d a o m >> loop m (i+1)
1209+-- loop 0 0
10251210{-# INLINE [1] replicate #-}
10261211
10271212{-# RULES
@@ -1032,8 +1217,19 @@ replicate n t@(Text a o l)
10321217-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
10331218-- value of every element. Subject to fusion.
10341219replicateChar :: Int -> Char -> Text
1220+#ifndef __GHCJS__
10351221replicateChar n c = unstream (S.replicateCharI n (safe c))
10361222{-# INLINE replicateChar #-}
1223+#else
1224+replicateChar n c = Text $ js_replicateChar n c
1225+{-# INLINE [1] replicateChar #-}
1226+{-# RULES
1227+"JSSTRING replicateChar -> fused" [~1] forall n c.
1228+ replicateChar n c = unstream (S.replicateCharI n (safe c))
1229+"JSSTRING replicateChar -> unfused" [1] forall n c.
1230+ unstream (S.replicateCharI n (safe c)) = replicateChar n c
1231+ #-}
1232+#endif
10371233
10381234-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
10391235-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1043,7 +1239,8 @@ replicateChar n c = unstream (S.replicateCharI n (safe c))
10431239-- string, and @b@ is the seed value for further production. Subject
10441240-- to fusion. Performs replacement on invalid scalar values.
10451241unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1046−unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1242+unfoldr f a = Text $ JSS.unfoldr f a
1243+-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10471244{-# INLINE unfoldr #-}
10481245
10491246-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1053,7 +1250,8 @@ unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10531250-- correct, otherwise its performance is similar to 'unfoldr'. Subject
10541251-- to fusion. Performs replacement on invalid scalar values.
10551252unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1056−unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1253+unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1254+-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10571255{-# INLINE unfoldrN #-}
10581256
10591257-- -----------------------------------------------------------------------------
@@ -1063,18 +1261,19 @@ unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10631261-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
10641262-- the length of the Text. Subject to fusion.
10651263take :: 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)
1264+take = coerce JSS.take
1265+-- take n t@(Text arr off len)
1266+-- | n <= 0 = empty
1267+-- | n >= len = t
1268+-- | otherwise = text arr off (iterN n t)
10701269{-# INLINE [1] take #-}
10711270
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
1271+-- iterN :: Int -> Text -> Int
1272+-- iterN n t@(Text _arr _off len) = loop 0 0
1273+-- where loop !i !cnt
1274+-- | i >= len || cnt >= n = i
1275+-- | otherwise = loop (i+d) (cnt+1)
1276+-- where d = iter_ t i
10781277
10791278{-# RULES
10801279"TEXT take -> fused" [~1] forall n t.
@@ -1088,31 +1287,36 @@ iterN n t@(Text _arr _off len) = loop 0 0
10881287--
10891288-- Examples:
10901289--
1091−-- > takeEnd 3 "foobar" == "bar"
1290+-- >>> takeEnd 3 "foobar"
1291+-- "bar"
1292+--
1293+-- @since 1.1.1.0
10921294takeEnd :: 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
1295+takeEnd = coerce JSS.takeEnd
1296+-- takeEnd n t@(Text arr off len)
1297+-- | n <= 0 = empty
1298+-- | n >= len = t
1299+-- | otherwise = text arr (off+i) (len-i)
1300+-- where i = iterNEnd n t
1301+
1302+-- iterNEnd :: Int -> Text -> Int
1303+-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1304+-- where loop i !m
1305+-- | i <= 0 = 0
1306+-- | m <= 1 = i
1307+-- | otherwise = loop (i+d) (m-1)
1308+-- where d = reverseIter_ t i
11061309
11071310-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
11081311-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
11091312-- is greater than the length of the 'Text'. Subject to fusion.
11101313drop :: 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
1314+drop = coerce JSS.drop
1315+-- drop n t@(Text arr off len)
1316+-- | n <= 0 = t
1317+-- | n >= len = empty
1318+-- | otherwise = text arr (off+i) (len-i)
1319+-- where i = iterN n t
11161320{-# INLINE [1] drop #-}
11171321
11181322{-# RULES
@@ -1127,22 +1331,27 @@ drop n t@(Text arr off len)
11271331--
11281332-- Examples:
11291333--
1130−-- > dropEnd 3 "foobar" == "foo"
1334+-- >>> dropEnd 3 "foobar"
1335+-- "foo"
1336+--
1337+-- @since 1.1.1.0
11311338dropEnd :: 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)
1339+dropEnd = coerce JSS.dropEnd
1340+-- dropEnd n t@(Text arr off len)
1341+-- | n <= 0 = t
1342+-- | n >= len = empty
1343+-- | otherwise = text arr off (iterNEnd n t)
11361344
11371345-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
11381346-- returns the longest prefix (possibly empty) of elements that
11391347-- satisfy @p@. Subject to fusion.
11401348takeWhile :: (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
1349+takeWhile = coerce JSS.takeWhile
1350+-- takeWhile p t@(Text arr off len) = loop 0
1351+-- where loop !i | i >= len = t
1352+-- | p c = loop (i+d)
1353+-- | otherwise = text arr off i
1354+-- where Iter c d = iter t i
11461355{-# INLINE [1] takeWhile #-}
11471356
11481357{-# RULES
@@ -1157,13 +1366,25 @@ takeWhile p t@(Text arr off len) = loop 0
11571366-- satisfy @p@. Subject to fusion.
11581367-- Examples:
11591368--
1160−-- > takeWhileEnd (=='o') "foo" == "oo"
1369+-- >>> takeWhileEnd (=='o') "foo"
1370+-- "oo"
1371+--
1372+-- @since 1.2.2.0
11611373takeWhileEnd :: (Char -> Bool) -> Text -> Text
1374+#ifndef __GHCJS__
11621375takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11631376 where loop !i !l | l <= 0 = t
11641377 | p c = loop (i+d) (l+d)
11651378 | otherwise = text arr (off+l) (len-l)
11661379 where (c,d) = reverseIter t i
1380+#else
1381+takeWhileEnd p t@(Text jst) = loop (len-1) len
1382+ where loop !i !l | l <= 0 = t
1383+ | p c = loop (i+d) (l+d)
1384+ | otherwise = Text $ js_substr1 l jst
1385+ where (c,d) = reverseIter t i
1386+ len = length t
1387+#endif
11671388{-# INLINE [1] takeWhileEnd #-}
11681389
11691390{-# RULES
@@ -1176,11 +1397,12 @@ takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11761397-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
11771398-- 'takeWhile' @p@ @t@. Subject to fusion.
11781399dropWhile :: (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
1400+dropWhile = coerce JSS.dropWhile
1401+-- dropWhile p t@(Text arr off len) = loop 0 0
1402+-- where loop !i !l | l >= len = empty
1403+-- | p c = loop (i+d) (l+d)
1404+-- | otherwise = Text arr (off+i) (len-l)
1405+
11841406{-# INLINE [1] dropWhile #-}
11851407
11861408{-# RULES
@@ -1196,13 +1418,15 @@ dropWhile p t@(Text arr off len) = loop 0 0
11961418--
11971419-- Examples:
11981420--
1199−-- > dropWhileEnd (=='.') "foo..." == "foo"
1421+-- >>> dropWhileEnd (=='.') "foo..."
1422+-- "foo"
12001423dropWhileEnd :: (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
1424+dropWhileEnd = coerce JSS.dropWhileEnd
1425+-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1426+-- where loop !i !l | l <= 0 = empty
1427+-- | p c = loop (i+d) (l+d)
1428+-- | otherwise = Text arr off l
1429+-- where (c,d) = reverseIter t i
12061430{-# INLINE [1] dropWhileEnd #-}
12071431
12081432{-# RULES
@@ -1245,45 +1469,49 @@ strip = dropAround isSpace
12451469-- prefix of @t@ of length @n@, and whose second is the remainder of
12461470-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
12471471splitAt :: 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))
1472+splitAt = coerce JSS.splitAt
1473+-- splitAt n t@(Text arr off len)
1474+-- | n <= 0 = (empty, t)
1475+-- | n >= len = (t, empty)
1476+-- | otherwise = let k = iterN n t
1477+-- in (text arr off k, text arr (off+k) (len-k))
12531478
12541479-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
12551480-- a pair whose first element is the longest prefix (possibly empty)
12561481-- of @t@ of elements that satisfy @p@, and whose second is the
12571482-- remainder of the list.
12581483span :: (Char -> Bool) -> Text -> (Text, Text)
1259−span p t = case span_ p t of
1260− (# hd,tl #) -> (hd,tl)
1484+span = coerce JSS.span
1485+-- span p t = case span_ p t of
1486+-- (# hd,tl #) -> (hd,tl)
12611487{-# INLINE span #-}
12621488
12631489-- | /O(n)/ 'break' is like 'span', but the prefix returned is
12641490-- over elements that fail the predicate @p@.
12651491break :: (Char -> Bool) -> Text -> (Text, Text)
1266−break p = span (not . p)
1492+break = coerce JSS.break
1493+-- break p = span (not . p)
12671494{-# INLINE break #-}
12681495
12691496-- | /O(n)/ Group characters in a string according to a predicate.
12701497groupBy :: (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))
1498+groupBy = coerce JSS.groupBy
1499+-- groupBy p = loop
1500+-- where
1501+-- loop t@(Text arr off len)
1502+-- | null t = []
1503+-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1504+-- where Iter c d = iter t 0
1505+-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
12781506
12791507-- | Returns the /array/ index (in units of 'Word16') at which a
12801508-- character may be found. This is /not/ the same as the logical
12811509-- 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
1510+-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1511+-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1512+-- where go !i | i >= len || q c = i
1513+-- | otherwise = go (i+d)
1514+-- where Iter c d = iter t i
12871515
12881516-- | /O(n)/ Group characters in a string by equality.
12891517group :: Text -> [Text]
@@ -1292,15 +1520,17 @@ group = groupBy (==)
12921520-- | /O(n)/ Return all initial segments of the given 'Text', shortest
12931521-- first.
12941522inits :: 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)
1523+inits = coerce JSS.inits
1524+-- inits t@(Text arr off len) = loop 0
1525+-- where loop i | i >= len = [t]
1526+-- | otherwise = Text arr off i : loop (i + iter_ t i)
12981527
12991528-- | /O(n)/ Return all final segments of the given 'Text', longest
13001529-- first.
13011530tails :: Text -> [Text]
1302−tails t | null t = [empty]
1303− | otherwise = t : tails (unsafeTail t)
1531+tails = coerce JSS.tails
1532+-- tails t | null t = [empty]
1533+-- | otherwise = t : tails (unsafeTail t)
13041534
13051535-- $split
13061536--
@@ -1314,9 +1544,14 @@ tails t | null t = [empty]
13141544--
13151545-- Examples:
13161546--
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" == ["",""]
1547+-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1548+-- ["a","b","d","e"]
1549+--
1550+-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1551+-- ["","X","X","X",""]
1552+--
1553+-- >>> splitOn "x" "x"
1554+-- ["",""]
13201555--
13211556-- and
13221557--
@@ -1333,13 +1568,14 @@ splitOn :: Text
13331568 -> Text
13341569 -- ^ Input text.
13351570 -> [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)]
1571+splitOn = coerce JSS.splitOn
1572+-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1573+-- | l <= 0 = emptyError "splitOn"
1574+-- | isSingleton pat = split (== unsafeHead pat) src
1575+-- | otherwise = go 0 (indices pat src)
1576+-- where
1577+-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1578+-- go s _ = [text arr (s+off) (len-s)]
13431579{-# INLINE [1] splitOn #-}
13441580
13451581{-# RULES
@@ -1352,28 +1588,36 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
13521588-- resulting components do not contain the separators. Two adjacent
13531589-- separators result in an empty component in the output. eg.
13541590--
1355−-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356−-- > split (=='a') "" == [""]
1591+-- >>> split (=='a') "aabbaca"
1592+-- ["","","bb","c",""]
1593+--
1594+-- >>> split (=='a') ""
1595+-- [""]
13571596split :: (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
1597+split = coerce JSS.split
1598+-- split _ t@(Text _off _arr 0) = [t]
1599+-- split p t = loop t
1600+-- where loop s | null s' = [l]
1601+-- | otherwise = l : loop (unsafeTail s')
1602+-- where (# l, s' #) = span_ (not . p) s
13631603{-# INLINE split #-}
13641604
13651605-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
13661606-- element may be shorter than the other chunks, depending on the
13671607-- length of the input. Examples:
13681608--
1369−-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370−-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1609+-- >>> chunksOf 3 "foobarbaz"
1610+-- ["foo","bar","baz"]
1611+--
1612+-- >>> chunksOf 4 "haskell.org"
1613+-- ["hask","ell.","org"]
13711614chunksOf :: 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
1615+chunksOf = coerce JSS.chunksOf
1616+-- chunksOf k = go
1617+-- where
1618+-- go t = case splitAt k t of
1619+-- (a,b) | null a -> []
1620+-- | otherwise -> a : go b
13771621{-# INLINE chunksOf #-}
13781622
13791623-- ----------------------------------------------------------------------------
@@ -1386,7 +1630,8 @@ chunksOf k = go
13861630-- returns the first element matching the predicate, or 'Nothing' if
13871631-- there is no such element.
13881632find :: (Char -> Bool) -> Text -> Maybe Char
1389−find p t = S.findBy p (stream t)
1633+find = coerce JSS.find
1634+-- find p t = S.findBy p (stream t)
13901635{-# INLINE find #-}
13911636
13921637-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1395,14 +1640,16 @@ find p t = S.findBy p (stream t)
13951640--
13961641-- > partition p t == (filter p t, filter (not . p) t)
13971642partition :: (Char -> Bool) -> Text -> (Text, Text)
1398−partition p t = (filter p t, filter (not . p) t)
1643+partition = coerce JSS.partition
1644+-- partition p t = (filter p t, filter (not . p) t)
13991645{-# INLINE partition #-}
14001646
14011647-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
14021648-- returns a 'Text' containing those characters that satisfy the
14031649-- predicate.
14041650filter :: (Char -> Bool) -> Text -> Text
1405−filter p t = unstream (S.filter p (stream t))
1651+filter = coerce JSS.filter
1652+-- filter p t = unstream (S.filter p (stream t))
14061653{-# INLINE filter #-}
14071654
14081655-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1412,8 +1659,11 @@ filter p t = unstream (S.filter p (stream t))
14121659--
14131660-- Examples:
14141661--
1415−-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416−-- > breakOn "/" "foobar" ==> ("foobar", "")
1662+-- >>> breakOn "::" "a::b::c"
1663+-- ("a","::b::c")
1664+--
1665+-- >>> breakOn "/" "foobar"
1666+-- ("foobar","")
14171667--
14181668-- Laws:
14191669--
@@ -1427,11 +1677,12 @@ filter p t = unstream (S.filter p (stream t))
14271677-- In (unlikely) bad cases, this function's time complexity degrades
14281678-- towards /O(n*m)/.
14291679breakOn :: 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))
1680+breakOn = coerce JSS.breakOn
1681+-- breakOn pat src@(Text arr off len)
1682+-- | null pat = emptyError "breakOn"
1683+-- | otherwise = case indices pat src of
1684+-- [] -> (src, empty)
1685+-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
14351686{-# INLINE breakOn #-}
14361687
14371688-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1441,10 +1692,12 @@ breakOn pat src@(Text arr off len)
14411692-- up to and including the last match of @needle@. The second is the
14421693-- remainder of @haystack@, following the match.
14431694--
1444−-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1695+-- >>> breakOnEnd "::" "a::b::c"
1696+-- ("a::b::","c")
14451697breakOnEnd :: Text -> Text -> (Text, Text)
1446−breakOnEnd pat src = (reverse b, reverse a)
1447− where (a,b) = breakOn (reverse pat) (reverse src)
1698+breakOnEnd = coerce JSS.breakOnEnd
1699+-- breakOnEnd pat src = (reverse b, reverse a)
1700+-- where (a,b) = breakOn (reverse pat) (reverse src)
14481701{-# INLINE breakOnEnd #-}
14491702
14501703-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1456,10 +1709,11 @@ breakOnEnd pat src = (reverse b, reverse a)
14561709--
14571710-- Examples:
14581711--
1459−-- > breakOnAll "::" ""
1460−-- > ==> []
1461−-- > breakOnAll "/" "a/b/c/"
1462−-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
1712+-- >>> breakOnAll "::" ""
1713+-- []
1714+--
1715+-- >>> breakOnAll "/" "a/b/c/"
1716+-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
14631717--
14641718-- In (unlikely) bad cases, this function's time complexity degrades
14651719-- towards /O(n*m)/.
@@ -1468,12 +1722,13 @@ breakOnEnd pat src = (reverse b, reverse a)
14681722breakOnAll :: Text -- ^ @needle@ to search for
14691723 -> Text -- ^ @haystack@ in which to search
14701724 -> [(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
1725+breakOnAll = coerce JSS.breakOnAll
1726+-- breakOnAll pat src@(Text arr off slen)
1727+-- | null pat = emptyError "breakOnAll"
1728+-- | otherwise = L.map step (indices pat src)
1729+-- where
1730+-- step x = (chunk 0 x, chunk x (slen-x))
1731+-- chunk !n !l = text arr (n+off) l
14771732{-# INLINE breakOnAll #-}
14781733
14791734-------------------------------------------------------------------------------
@@ -1497,14 +1752,16 @@ breakOnAll pat src@(Text arr off slen)
14971752
14981753-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
14991754index :: Text -> Int -> Char
1500−index t n = S.index (stream t) n
1755+index = coerce JSS.index
1756+-- index t n = S.index (stream t) n
15011757{-# INLINE index #-}
15021758
15031759-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
15041760-- and returns the index of the first element in the 'Text' satisfying
15051761-- the predicate. Subject to fusion.
15061762findIndex :: (Char -> Bool) -> Text -> Maybe Int
1507−findIndex p t = S.findIndex p (stream t)
1763+findIndex = coerce JSS.findIndex
1764+-- findIndex p t = S.findIndex p (stream t)
15081765{-# INLINE findIndex #-}
15091766
15101767-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1514,10 +1771,11 @@ findIndex p t = S.findIndex p (stream t)
15141771-- In (unlikely) bad cases, this function's time complexity degrades
15151772-- towards /O(n*m)/.
15161773count :: 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)
1774+count = coerce JSS.count
1775+-- count pat src
1776+-- | null pat = emptyError "count"
1777+-- | isSingleton pat = countChar (unsafeHead pat) src
1778+-- | otherwise = L.length (indices pat src)
15211779{-# INLINE [1] count #-}
15221780
15231781{-# RULES
@@ -1539,42 +1797,46 @@ countChar c t = S.countChar c (stream t)
15391797-- excess elements of the longer 'Text' are discarded. This is
15401798-- equivalent to a pair of 'unpack' operations.
15411799zip :: Text -> Text -> [(Char,Char)]
1542−zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1800+zip = coerce JSS.zip
1801+-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
15431802{-# INLINE zip #-}
15441803
15451804-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
15461805-- given as the first argument, instead of a tupling function.
15471806-- Performs replacement on invalid scalar values.
15481807zipWith :: (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)
1808+zipWith = coerce JSS.zipWith
1809+-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1810+-- where g a b = safe (f a b)
15511811{-# INLINE zipWith #-}
15521812
15531813-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
15541814-- representing white space.
15551815words :: 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
1816+words = coerce JSS.words
1817+-- words t@(Text arr off len) = loop 0 0
1818+-- where
1819+-- loop !start !n
1820+-- | n >= len = if start == n
1821+-- then []
1822+-- else [Text arr (start+off) (n-start)]
1823+-- | isSpace c =
1824+-- if start == n
1825+-- then loop (start+1) (start+1)
1826+-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1827+-- | otherwise = loop start (n+d)
1828+-- where Iter c d = iter t n
15681829{-# INLINE words #-}
15691830
15701831-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
15711832-- newline 'Char's. The resulting strings do not contain newlines.
15721833lines :: 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
1834+lines = coerce JSS.lines
1835+-- lines ps | null ps = []
1836+-- | otherwise = h : if null t
1837+-- then []
1838+-- else lines (unsafeTail t)
1839+-- where (# h,t #) = span_ (/= '\n') ps
15781840{-# INLINE lines #-}
15791841
15801842{-
@@ -1602,34 +1864,40 @@ lines' ps | null ps = []
16021864-- | /O(n)/ Joins lines, after appending a terminating newline to
16031865-- each.
16041866unlines :: [Text] -> Text
1605−unlines = concat . L.map (`snoc` '\n')
1867+unlines = coerce JSS.unlines
1868+-- unlines = concat . L.map (`snoc` '\n')
16061869{-# INLINE unlines #-}
16071870
16081871-- | /O(n)/ Joins words using single space characters.
16091872unwords :: [Text] -> Text
1610−unwords = intercalate (singleton ' ')
1873+unwords = coerce JSS.unwords
1874+-- unwords = intercalate (singleton ' ')
16111875{-# INLINE unwords #-}
16121876
16131877-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
16141878-- 'True' iff the first is a prefix of the second. Subject to fusion.
16151879isPrefixOf :: Text -> Text -> Bool
1616−isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617− alen <= blen && S.isPrefixOf (stream a) (stream b)
1880+isPrefixOf = coerce JSS.isPrefixOf
1881+-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1882+-- alen <= blen && S.isPrefixOf (stream a) (stream b)
16181883{-# INLINE [1] isPrefixOf #-}
16191884
16201885{-# RULES
16211886"TEXT isPrefixOf -> fused" [~1] forall s t.
16221887 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1888+"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1889+ S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
16231890 #-}
16241891
16251892-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
16261893-- 'True' iff the first is a suffix of the second.
16271894isSuffixOf :: 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
1895+isSuffixOf = coerce JSS.isSuffixOf
1896+-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1897+-- d >= 0 && a == b'
1898+-- where d = blen - alen
1899+-- b' | d == 0 = b
1900+-- | otherwise = Text barr (boff+d) alen
16331901{-# INLINE isSuffixOf #-}
16341902
16351903-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1639,10 +1907,11 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
16391907-- In (unlikely) bad cases, this function's time complexity degrades
16401908-- towards /O(n*m)/.
16411909isInfixOf :: 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
1910+isInfixOf = coerce JSS.isInfixOf
1911+-- isInfixOf needle haystack
1912+-- | null needle = True
1913+-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1914+-- | otherwise = not . L.null . indices needle $ haystack
16461915{-# INLINE [1] isInfixOf #-}
16471916
16481917{-# RULES
@@ -1658,9 +1927,14 @@ isInfixOf needle haystack
16581927--
16591928-- Examples:
16601929--
1661−-- > stripPrefix "foo" "foobar" == Just "bar"
1662−-- > stripPrefix "" "baz" == Just "baz"
1663−-- > stripPrefix "foo" "quux" == Nothing
1930+-- >>> stripPrefix "foo" "foobar"
1931+-- Just "bar"
1932+--
1933+-- >>> stripPrefix "" "baz"
1934+-- Just "baz"
1935+--
1936+-- >>> stripPrefix "foo" "quux"
1937+-- Nothing
16641938--
16651939-- This is particularly useful with the @ViewPatterns@ extension to
16661940-- GHC, as follows:
@@ -1672,9 +1946,10 @@ isInfixOf needle haystack
16721946-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
16731947-- > fnordLength _ = -1
16741948stripPrefix :: 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
1949+stripPrefix = coerce JSS.stripPrefix
1950+-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1951+-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1952+-- | otherwise = Nothing
16781953
16791954-- | /O(n)/ Find the longest non-empty common prefix of two strings
16801955-- and return it, along with the suffixes of each string at which they
@@ -1685,28 +1960,39 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
16851960--
16861961-- Examples:
16871962--
1688−-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689−-- > commonPrefixes "veeble" "fetzer" == Nothing
1690−-- > commonPrefixes "" "baz" == Nothing
1963+-- >>> commonPrefixes "foobar" "fooquux"
1964+-- Just ("foo","bar","quux")
1965+--
1966+-- >>> commonPrefixes "veeble" "fetzer"
1967+-- Nothing
1968+--
1969+-- >>> commonPrefixes "" "baz"
1970+-- Nothing
16911971commonPrefixes :: 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
1972+commonPrefixes = coerce JSS.commonPrefixes
1973+-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1974+-- where
1975+-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1976+-- | i > 0 = Just (Text arr0 off0 i,
1977+-- text arr0 (off0+i) (len0-i),
1978+-- text arr1 (off1+j) (len1-j))
1979+-- | otherwise = Nothing
1980+-- where Iter a d0 = iter t0 i
1981+-- Iter b d1 = iter t1 j
17011982
17021983-- | /O(n)/ Return the prefix of the second string if its suffix
17031984-- matches the entire first string.
17041985--
17051986-- Examples:
17061987--
1707−-- > stripSuffix "bar" "foobar" == Just "foo"
1708−-- > stripSuffix "" "baz" == Just "baz"
1709−-- > stripSuffix "foo" "quux" == Nothing
1988+-- >>> stripSuffix "bar" "foobar"
1989+-- Just "foo"
1990+--
1991+-- >>> stripSuffix "" "baz"
1992+-- Just "baz"
1993+--
1994+-- >>> stripSuffix "foo" "quux"
1995+-- Nothing
17101996--
17111997-- This is particularly useful with the @ViewPatterns@ extension to
17121998-- GHC, as follows:
@@ -1718,21 +2004,22 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
17182004-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
17192005-- > quuxLength _ = -1
17202006stripSuffix :: 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"
2007+stripSuffix = coerce JSS.stripSuffix
2008+-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2009+-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
2010+-- | otherwise = Nothing
2011+
2012+-- -- | Add a list of non-negative numbers. Errors out on overflow.
2013+-- sumP :: String -> [Int] -> Int
2014+-- sumP fun = go 0
2015+-- where go !a (x:xs)
2016+-- | ax >= 0 = go ax xs
2017+-- | otherwise = overflowError fun
2018+-- where ax = a + x
2019+-- go a _ = a
2020+
2021+-- emptyError :: String -> a
2022+-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
17362023
17372024overflowError :: String -> a
17382025overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1746,10 +2033,22 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
17462033-- copy \"breaks the link\" to the original array, allowing it to be
17472034-- garbage collected if there are no other live references to it.
17482035copy :: 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
2036+copy = P.id -- TODO
2037+-- copy (Text arr off len) = Text (A.run go) 0 len
2038+-- where
2039+-- go :: ST s (A.MArray s)
2040+-- go = do
2041+-- marr <- A.new len
2042+
2043+foreign import javascript unsafe
2044+ "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
2045+foreign import javascript unsafe
2046+ "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString
2047+
2048+-------------------------------------------------
2049+-- NOTE: the named chunk below used by doctest;
2050+-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2051+
2052+-- $setup
2053+-- >>> :set -XOverloadedStrings
2054+-- >>> import qualified Data.Text as T