@@ -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,80 @@ 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+#if MIN_VERSION_ghcjs_prim(0,1,1)
264+import GHCJS.Prim (unsafeUnpackJSString#)
265+#endif
266+
267+unpack :: Text -> String
268+unpack = coerce JSS.unpack
269+{-# INLINE [1] unpack #-}
270+
271+singleton :: Char -> Text
272+singleton = coerce JSS.singleton
273+{-# INLINE [1] singleton #-}
274+
275+{-# RULES
276+"JSSTRING singleton -> fused" [~1] forall a.
277+ singleton a = unstream (S.singleton (safe a))
278+"JSSTRING singleton -> unfused" [1] forall a.
279+ unstream (S.singleton (safe a)) = singleton a
280+ #-}
281+
282+#if MIN_VERSION_ghcjs_prim(0,1,1)
283+{-# RULES "inlineJSString" forall s.
284+ pack (GHC.Base.build (GHC.unpackFoldrCString# s)) =
285+ Text (JSString (unsafeUnpackJSString# s)) #-}
286+#endif
287+
288+-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
289+unpackCString# :: Addr# -> Text
290+unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
291+{-# NOINLINE unpackCString# #-}
292+
293+{-# RULES "TEXT literal" forall a.
294+ unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
295+ = unpackCString# a #-}
296+
297+{-# RULES "TEXT literal UTF8" [1] forall a.
298+ unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
299+ = unpackCString# a #-}
300+
301+{-# RULES "TEXT empty literal" [1]
302+ unstream (S.map safe (S.streamList []))
303+ = empty_ #-}
304+
305+{-# RULES "TEXT singleton literal" [1] forall a.
306+ unstream (S.map safe (S.streamList [a]))
307+ = singleton a #-}
308+
309+#if MIN_VERSION_ghcjs_prim(0,1,1)
310+{-# RULES "TEXT literal prim" [0] forall a.
311+ unpackCString# a = Text (JSString (unsafeUnpackJSString# a))
312+ #-}
313+#endif
314+
315+---------------------------------------------------------------------
316+-- $character_definition
317+--
318+-- This package uses the term /character/ to denote Unicode /code points/.
319+--
320+-- Note that this is not the same thing as a grapheme (e.g. a
321+-- composition of code points that form one visual symbol). For
322+-- instance, consider the grapheme \"ä\". This symbol has two
323+-- Unicode representations: a single code-point representation
324+-- @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code point),
325+-- and a two code point representation @U+0061@ (the \"@A@\" code
326+-- point) and @U+0308@ (the @COMBINING DIAERESIS@ code point).
327+
249328-- $strict
250329--
251330-- This package provides both strict and lazy 'Text' types. The
@@ -321,22 +400,31 @@ import Text.Printf (PrintfArg, formatArg, formatString)
321400-- Functions that can be fused by the compiler are documented with the
322401-- phrase \"Subject to fusion\".
323402
403+-- instance Eq Text where
404+-- Text arrA offA lenA == Text arrB offB lenB
405+-- | lenA == lenB = A.equal arrA offA arrB offB lenA
406+-- | otherwise = False
407+-- {-# INLINE (==) #-}
324408instance 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 (==) #-}
409+ Text x == Text y = x == y
410+
411+-- instance Ord Text where
412+-- compare = compareText
329413
330414instance Ord Text where
331− compare = compareText
415+ compare (Text x) (Text y) = compare x y
332416
333417instance Read Text where
334418 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
335419
336420#if MIN_VERSION_base(4,9,0)
337−-- Semigroup orphan instances for older GHCs are provided by
338−-- 'semigroups` package
339−
421+-- | Non-orphan 'Semigroup' instance only defined for
422+-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
423+-- provided by
424+-- the [semigroups](http://hackage.haskell.org/package/semigroups)
425+-- package
426+--
427+-- @since 1.2.2.0
340428instance Semigroup Text where
341429 (<>) = append
342430#endif
@@ -354,6 +442,7 @@ instance IsString Text where
354442 fromString = pack
355443
356444#if __GLASGOW_HASKELL__ >= 708
445+-- | @since 1.2.0.0
357446instance Exts.IsList Text where
358447 type Item Text = Char
359448 fromList = pack
@@ -364,6 +453,7 @@ instance Exts.IsList Text where
364453instance NFData Text where rnf !_ = ()
365454#endif
366455
456+-- | @since 1.2.1.0
367457instance Binary Text where
368458 put t = put (encodeUtf8 t)
369459 get = do
@@ -397,8 +487,10 @@ instance Data Text where
397487
398488#if MIN_VERSION_base(4,7,0)
399489-- | Only defined for @base-4.7.0.0@ and later
490+--
491+-- @since 1.2.2.0
400492instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
493+ formatArg t = formatString $ unpack t
402494#endif
403495
404496packConstr :: Constr
@@ -407,19 +499,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
407499textDataType :: DataType
408500textDataType = mkDataType "Data.Text.Text" [packConstr]
409501
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
502+-- -- | /O(n)/ Compare two 'Text' values lexicographically.
503+-- compareText :: Text -> Text -> Ordering
504+-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
505+-- | lenA == 0 && lenB == 0 = EQ
506+-- | otherwise = go 0 0
507+-- where
508+-- go !i !j
509+-- | i >= lenA || j >= lenB = compare lenA lenB
510+-- | a < b = LT
511+-- | a > b = GT
512+-- | otherwise = go (i+di) (j+dj)
513+-- where Iter a di = iter ta i
514+-- Iter b dj = iter tb j
423515
424516-- -----------------------------------------------------------------------------
425517-- * Conversion to/from 'Text'
@@ -427,7 +519,8 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
427519-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
428520-- fusion. Performs replacement on invalid scalar values.
429521pack :: String -> Text
430−pack = unstream . S.map safe . S.streamList
522+pack = coerce JSS.pack
523+-- pack = unstream . S.map safe . S.streamList
431524{-# INLINE [1] pack #-}
432525
433526-- -----------------------------------------------------------------------------
@@ -438,7 +531,8 @@ pack = unstream . S.map safe . S.streamList
438531-- copying a new array. Subject to fusion. Performs replacement on
439532-- invalid scalar values.
440533cons :: Char -> Text -> Text
441−cons c t = unstream (S.cons (safe c) (stream t))
534+cons = coerce JSS.cons
535+-- cons c t = unstream (S.cons (safe c) (stream t))
442536{-# INLINE cons #-}
443537
444538infixr 5 `cons`
@@ -447,25 +541,27 @@ infixr 5 `cons`
447541-- entire array in the process, unless fused. Subject to fusion.
448542-- Performs replacement on invalid scalar values.
449543snoc :: Text -> Char -> Text
450−snoc t c = unstream (S.snoc (stream t) (safe c))
544+snoc = coerce JSS.snoc
545+-- snoc t c = unstream (S.snoc (stream t) (safe c))
451546{-# INLINE snoc #-}
452547
453548-- | /O(n)/ Appends one 'Text' to the other by copying both of them
454549-- into a new 'Text'. Subject to fusion.
455550append :: 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
551+append = coerce JSS.append
552+-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
553+-- | len1 == 0 = b
554+-- | len2 == 0 = a
555+-- | len > 0 = Text (A.run x) 0 len
556+-- | otherwise = overflowError "append"
557+-- where
558+-- len = len1+len2
559+-- x :: ST s (A.MArray s)
560+-- x = do
561+-- arr <- A.new len
562+-- A.copyI arr 0 arr1 off1 len1
563+-- A.copyI arr len1 arr2 off2 len
564+-- return arr
469565{-# NOINLINE append #-}
470566
471567{-# RULES
@@ -478,16 +574,18 @@ append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
478574-- | /O(1)/ Returns the first character of a 'Text', which must be
479575-- non-empty. Subject to fusion.
480576head :: Text -> Char
481−head t = S.head (stream t)
577+head = coerce JSS.head
578+-- head t = S.head (stream t)
482579{-# INLINE head #-}
483580
484581-- | /O(1)/ Returns the first character and rest of a 'Text', or
485582-- 'Nothing' if empty. Subject to fusion.
486583uncons :: 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))
584+uncons = coerce JSS.uncons
585+-- uncons t@(Text arr off len)
586+-- | len <= 0 = Nothing
587+-- | otherwise = Just $ let !(Iter c d) = iter t 0
588+-- in (c, text arr (off+d) (len-d))
491589{-# INLINE [1] uncons #-}
492590
493591-- | Lifted from Control.Arrow and specialized.
@@ -497,12 +595,13 @@ second f (a, b) = (a, f b)
497595-- | /O(1)/ Returns the last character of a 'Text', which must be
498596-- non-empty. Subject to fusion.
499597last :: 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)
598+last = coerce JSS.last
599+-- last (Text arr off len)
600+-- | len <= 0 = emptyError "last"
601+-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
602+-- | otherwise = U16.chr2 n0 n
603+-- where n = A.unsafeIndex arr (off+len-1)
604+-- n0 = A.unsafeIndex arr (off+len-2)
506605{-# INLINE [1] last #-}
507606
508607{-# RULES
@@ -515,10 +614,11 @@ last (Text arr off len)
515614-- | /O(1)/ Returns all characters after the head of a 'Text', which
516615-- must be non-empty. Subject to fusion.
517616tail :: 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
617+tail = coerce JSS.tail
618+-- tail t@(Text arr off len)
619+-- | len <= 0 = emptyError "tail"
620+-- | otherwise = text arr (off+d) (len-d)
621+-- where d = iter_ t 0
522622{-# INLINE [1] tail #-}
523623
524624{-# RULES
@@ -531,11 +631,12 @@ tail t@(Text arr off len)
531631-- | /O(1)/ Returns all but the last character of a 'Text', which must
532632-- be non-empty. Subject to fusion.
533633init :: 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)
634+init = coerce JSS.init
635+-- init (Text arr off len) | len <= 0 = emptyError "init"
636+-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
637+-- | otherwise = text arr off (len-1)
638+-- where
639+-- n = A.unsafeIndex arr (off+len-1)
539640{-# INLINE [1] init #-}
540641
541642{-# RULES
@@ -545,14 +646,29 @@ init (Text arr off len) | len <= 0 = emptyError "init"
545646 unstream (S.init (stream t)) = init t
546647 #-}
547648
649+-- | /O(1)/ Returns all but the last character and the last character of a
650+-- 'Text', or 'Nothing' if empty.
651+--
652+-- @since 1.2.3.0
653+unsnoc :: Text -> Maybe (Text, Char)
654+unsnoc = coerce JSS.unsnoc
655+-- unsnoc (Text arr off len)
656+-- | len <= 0 = Nothing
657+-- | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
658+-- | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
659+-- where n = A.unsafeIndex arr (off+len-1)
660+-- n0 = A.unsafeIndex arr (off+len-2)
661+{-# INLINE [1] unsnoc #-}
662+
548663-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
549664-- fusion.
550665null :: Text -> Bool
551−null (Text _arr _off len) =
552−#if defined(ASSERTS)
553− assert (len >= 0) $
554−#endif
555− len <= 0
666+null = coerce JSS.null
667+-- null (Text _arr _off len) =
668+-- #if defined(ASSERTS)
669+-- assert (len >= 0) $
670+-- #endif
671+-- len <= 0
556672{-# INLINE [1] null #-}
557673
558674{-# RULES
@@ -564,14 +680,15 @@ null (Text _arr _off len) =
564680
565681-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
566682-- Subject to fusion.
567−isSingleton :: Text -> Bool
568−isSingleton = S.isSingleton . stream
569−{-# INLINE isSingleton #-}
683+-- isSingleton :: Text -> Bool
684+-- isSingleton = S.isSingleton . stream
685+-- {-# INLINE isSingleton #-}
570686
571687-- | /O(n)/ Returns the number of characters in a 'Text'.
572688-- Subject to fusion.
573689length :: Text -> Int
574−length t = S.length (stream t)
690+length = coerce JSS.length
691+-- length t = S.length (stream t)
575692{-# INLINE [0] length #-}
576693-- length needs to be phased after the compareN/length rules otherwise
577694-- it may inline before the rules have an opportunity to fire.
@@ -624,29 +741,58 @@ compareLength t n = S.compareLengthI (stream t) n
624741-- -----------------------------------------------------------------------------
625742-- * Transformations
626743-- | /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.
744+-- each element of @t@.
745+--
746+-- Example:
747+--
748+-- >>> let message = pack "I am not angry. Not at all."
749+-- >>> T.map (\c -> if c == '.' then '!' else c) message
750+-- "I am not angry! Not at all!"
751+--
752+-- Subject to fusion. Performs replacement on invalid scalar values.
629753map :: (Char -> Char) -> Text -> Text
630−map f t = unstream (S.map (safe . f) (stream t))
754+map = coerce JSS.map
755+-- map f t = unstream (S.map (safe . f) (stream t))
631756{-# INLINE [1] map #-}
632757
633758-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
634759-- 'Text's and concatenates the list after interspersing the first
635760-- argument between each element of the list.
761+--
762+-- Example:
763+--
764+-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
765+-- "WeNI!seekNI!theNI!HolyNI!Grail"
636766intercalate :: Text -> [Text] -> Text
637−intercalate t = concat . (F.intersperse t)
767+intercalate = coerce JSS.intercalate
768+-- intercalate t = concat . (F.intersperse t)
638769{-# INLINE intercalate #-}
639770
640771-- | /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.
772+-- between the characters of a 'Text'.
773+--
774+-- Example:
775+--
776+-- >>> T.intersperse '.' "SHIELD"
777+-- "S.H.I.E.L.D"
778+--
779+-- Subject to fusion. Performs replacement on invalid scalar values.
643780intersperse :: Char -> Text -> Text
644−intersperse c t = unstream (S.intersperse (safe c) (stream t))
781+intersperse = coerce JSS.intersperse
782+-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
645783{-# INLINE intersperse #-}
646784
647−-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
785+-- | /O(n)/ Reverse the characters of a string.
786+--
787+-- Example:
788+--
789+-- >>> T.reverse "desrever"
790+-- "reversed"
791+--
792+-- Subject to fusion.
648793reverse :: Text -> Text
649−reverse t = S.reverse (stream t)
794+reverse = coerce JSS.reverse
795+-- reverse t = S.reverse (stream t)
650796{-# INLINE reverse #-}
651797
652798-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -663,12 +809,14 @@ reverse t = S.reverse (stream t)
663809-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
664810-- be replaced recursively:
665811--
666−-- > replace "oo" "foo" "oo" == "foo"
812+-- >>> replace "oo" "foo" "oo"
813+-- "foo"
667814--
668815-- In cases where several instances of @needle@ overlap, only the
669816-- first one will be replaced:
670817--
671−-- > replace "ofo" "bar" "ofofo" == "barfo"
818+-- >>> replace "ofo" "bar" "ofofo"
819+-- "barfo"
672820--
673821-- In (unlikely) bad cases, this function's time complexity degrades
674822-- towards /O(n*m)/.
@@ -680,28 +828,29 @@ replace :: Text
680828 -> Text
681829 -- ^ @haystack@ in which to search.
682830 -> 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
831+replace = coerce JSS.replace
832+-- replace needle@(Text _ _ neeLen)
833+-- (Text repArr repOff repLen)
834+-- haystack@(Text hayArr hayOff hayLen)
835+-- | neeLen == 0 = emptyError "replace"
836+-- | L.null ixs = haystack
837+-- | len > 0 = Text (A.run x) 0 len
838+-- | otherwise = empty
839+-- where
840+-- ixs = indices needle haystack
841+-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
842+-- x :: ST s (A.MArray s)
843+-- x = do
844+-- marr <- A.new len
845+-- let loop (i:is) o d = do
846+-- let d0 = d + i - o
847+-- d1 = d0 + repLen
848+-- A.copyI marr d hayArr (hayOff+o) d0
849+-- A.copyI marr d0 repArr repOff d1
850+-- loop is (i + neeLen) d1
851+-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
852+-- loop ixs 0 0
853+-- return marr
705854
706855-- ----------------------------------------------------------------------------
707856-- ** Case conversions (folds)
@@ -740,7 +889,8 @@ replace needle@(Text _ _ neeLen)
740889-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
741890-- instead of itself.
742891toCaseFold :: Text -> Text
743−toCaseFold t = unstream (S.toCaseFold (stream t))
892+-- toCaseFold t = unstream (S.toCaseFold (stream t))
893+toCaseFold = coerce JSS.toCaseFold
744894{-# INLINE toCaseFold #-}
745895
746896-- | /O(n)/ Convert a string to lower case, using simple case
@@ -751,7 +901,8 @@ toCaseFold t = unstream (S.toCaseFold (stream t))
751901-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
752902-- followed by \" ̇\" (combining dot above, U+0307).
753903toLower :: Text -> Text
754−toLower t = unstream (S.toLower (stream t))
904+-- toLower t = unstream (S.toLower (stream t))
905+toLower = coerce JSS.toLower
755906{-# INLINE toLower #-}
756907
757908-- | /O(n)/ Convert a string to upper case, using simple case
@@ -761,7 +912,8 @@ toLower t = unstream (S.toLower (stream t))
761912-- instance, the German \"ß\" (eszett, U+00DF) maps to the
762913-- two-letter sequence \"SS\".
763914toUpper :: Text -> Text
764−toUpper t = unstream (S.toUpper (stream t))
915+-- toUpper t = unstream (S.toUpper (stream t))
916+toUpper = coerce JSS.toUpper
765917{-# INLINE toUpper #-}
766918
767919-- | /O(n)/ Convert a string to title case, using simple case
@@ -782,8 +934,11 @@ toUpper t = unstream (S.toUpper (stream t))
782934-- guides disagree on whether the book name \"The Hill of the Red
783935-- Fox\" is correctly title cased—but this function will
784936-- capitalize /every/ word.
937+--
938+-- @since 1.0.0.0
785939toTitle :: Text -> Text
786−toTitle t = unstream (S.toTitle (stream t))
940+toTitle = coerce JSS.toTitle
941+-- toTitle t = unstream (S.toTitle (stream t))
787942{-# INLINE toTitle #-}
788943
789944-- | /O(n)/ Left-justify a string to the given length, using the
@@ -792,13 +947,17 @@ toTitle t = unstream (S.toTitle (stream t))
792947--
793948-- Examples:
794949--
795−-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796−-- > justifyLeft 3 'x' "foobar" == "foobar"
950+-- >>> justifyLeft 7 'x' "foo"
951+-- "fooxxxx"
952+--
953+-- >>> justifyLeft 3 'x' "foobar"
954+-- "foobar"
797955justifyLeft :: 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
956+justifyLeft = coerce JSS.justifyLeft
957+-- justifyLeft k c t
958+-- | len >= k = t
959+-- | otherwise = t `append` replicateChar (k-len) c
960+-- where len = length t
802961{-# INLINE [1] justifyLeft #-}
803962
804963{-# RULES
@@ -814,13 +973,17 @@ justifyLeft k c t
814973--
815974-- Examples:
816975--
817−-- > justifyRight 7 'x' "bar" == "xxxxbar"
818−-- > justifyRight 3 'x' "foobar" == "foobar"
976+-- >>> justifyRight 7 'x' "bar"
977+-- "xxxxbar"
978+--
979+-- >>> justifyRight 3 'x' "foobar"
980+-- "foobar"
819981justifyRight :: 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
982+justifyRight = coerce JSS.justifyRight
983+-- justifyRight k c t
984+-- | len >= k = t
985+-- | otherwise = replicateChar (k-len) c `append` t
986+-- where len = length t
824987{-# INLINE justifyRight #-}
825988
826989-- | /O(n)/ Center a string to the given length, using the specified
@@ -829,23 +992,34 @@ justifyRight k c t
829992--
830993-- Examples:
831994--
832−-- > center 8 'x' "HS" = "xxxHSxxx"
995+-- >>> center 8 'x' "HS"
996+-- "xxxHSxxx"
833997center :: 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
998+center = coerce JSS.center
999+-- center k c t
1000+-- | len >= k = t
1001+-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
1002+-- where len = length t
1003+-- d = k - len
1004+-- r = d `quot` 2
1005+-- l = d - r
8411006{-# INLINE center #-}
8421007
8431008-- | /O(n)/ The 'transpose' function transposes the rows and columns
8441009-- of its 'Text' argument. Note that this function uses 'pack',
8451010-- 'unpack', and the list version of transpose, and is thus not very
8461011-- efficient.
1012+--
1013+-- Examples:
1014+--
1015+-- >>> transpose ["green","orange"]
1016+-- ["go","rr","ea","en","ng","e"]
1017+--
1018+-- >>> transpose ["blue","red"]
1019+-- ["br","le","ud","e"]
8471020transpose :: [Text] -> [Text]
848−transpose ts = P.map pack (L.transpose (P.map unpack ts))
1021+transpose = coerce JSS.transpose
1022+-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
8491023
8501024-- -----------------------------------------------------------------------------
8511025-- * Reducing 'Text's (folds)
@@ -855,23 +1029,27 @@ transpose ts = P.map pack (L.transpose (P.map unpack ts))
8551029-- reduces the 'Text' using the binary operator, from left to right.
8561030-- Subject to fusion.
8571031foldl :: (a -> Char -> a) -> a -> Text -> a
858−foldl f z t = S.foldl f z (stream t)
1032+foldl f a0 (Text t) = JSS.foldl f a0 t
1033+-- foldl f z t = S.foldl f z (stream t)
8591034{-# INLINE foldl #-}
8601035
8611036-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
8621037foldl' :: (a -> Char -> a) -> a -> Text -> a
863−foldl' f z t = S.foldl' f z (stream t)
1038+foldl' f a0 (Text t) = JSS.foldl' f a0 t
1039+-- foldl' f z t = S.foldl' f z (stream t)
8641040{-# INLINE foldl' #-}
8651041
8661042-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
8671043-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
8681044foldl1 :: (Char -> Char -> Char) -> Text -> Char
869−foldl1 f t = S.foldl1 f (stream t)
1045+foldl1 = coerce JSS.foldl1
1046+-- foldl1 f t = S.foldl1 f (stream t)
8701047{-# INLINE foldl1 #-}
8711048
8721049-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
8731050foldl1' :: (Char -> Char -> Char) -> Text -> Char
874−foldl1' f t = S.foldl1' f (stream t)
1051+foldl1' = coerce JSS.foldl1'
1052+-- foldl1' f t = S.foldl1' f (stream t)
8751053{-# INLINE foldl1' #-}
8761054
8771055-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -879,14 +1057,16 @@ foldl1' f t = S.foldl1' f (stream t)
8791057-- reduces the 'Text' using the binary operator, from right to left.
8801058-- Subject to fusion.
8811059foldr :: (Char -> a -> a) -> a -> Text -> a
882−foldr f z t = S.foldr f z (stream t)
1060+foldr f a0 (Text t) = JSS.foldr f a0 t
1061+-- foldr f z t = S.foldr f z (stream t)
8831062{-# INLINE foldr #-}
8841063
8851064-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
8861065-- and thus must be applied to a non-empty 'Text'. Subject to
8871066-- fusion.
8881067foldr1 :: (Char -> Char -> Char) -> Text -> Char
889−foldr1 f t = S.foldr1 f (stream t)
1068+foldr1 = coerce JSS.foldr1
1069+-- foldr1 f t = S.foldr1 f (stream t)
8901070{-# INLINE foldr1 #-}
8911071
8921072-- -----------------------------------------------------------------------------
@@ -894,48 +1074,54 @@ foldr1 f t = S.foldr1 f (stream t)
8941074
8951075-- | /O(n)/ Concatenate a list of 'Text's.
8961076concat :: [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
1077+concat = coerce JSS.concat
1078+-- concat ts = case ts' of
1079+-- [] -> empty
1080+-- [t] -> t
1081+-- _ -> Text (A.run go) 0 len
1082+-- where
1083+-- ts' = L.filter (not . null) ts
1084+-- len = sumP "concat" $ L.map lengthWord16 ts'
1085+-- go :: ST s (A.MArray s)
1086+-- go = do
1087+-- arr <- A.new len
1088+-- let step i (Text a o l) =
1089+-- let !j = i + l in A.copyI arr i a o j >> return j
1090+-- foldM step 0 ts' >> return arr
9101091
9111092-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
9121093-- concatenate the results.
9131094concatMap :: (Char -> Text) -> Text -> Text
914−concatMap f = concat . foldr ((:) . f) []
1095+concatMap = coerce JSS.concatMap
1096+-- concatMap f = concat . foldr ((:) . f) []
9151097{-# INLINE concatMap #-}
9161098
9171099-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
9181100-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
9191101any :: (Char -> Bool) -> Text -> Bool
920−any p t = S.any p (stream t)
1102+any = coerce JSS.any
1103+-- any p t = S.any p (stream t)
9211104{-# INLINE any #-}
9221105
9231106-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
9241107-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
9251108all :: (Char -> Bool) -> Text -> Bool
926−all p t = S.all p (stream t)
1109+all = coerce JSS.all
1110+-- all p t = S.all p (stream t)
9271111{-# INLINE all #-}
9281112
9291113-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
9301114-- must be non-empty. Subject to fusion.
9311115maximum :: Text -> Char
932−maximum t = S.maximum (stream t)
1116+maximum = coerce JSS.maximum
1117+-- maximum t = S.maximum (stream t)
9331118{-# INLINE maximum #-}
9341119
9351120-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
9361121-- must be non-empty. Subject to fusion.
9371122minimum :: Text -> Char
938−minimum t = S.minimum (stream t)
1123+minimum = coerce JSS.minimum
1124+-- minimum t = S.minimum (stream t)
9391125{-# INLINE minimum #-}
9401126
9411127-- -----------------------------------------------------------------------------
@@ -951,8 +1137,9 @@ minimum t = S.minimum (stream t)
9511137--
9521138-- > last (scanl f z xs) == foldl f z xs.
9531139scanl :: (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)
1140+scanl = coerce JSS.scanl
1141+-- scanl f z t = unstream (S.scanl g z (stream t))
1142+-- where g a b = safe (f a b)
9561143{-# INLINE scanl #-}
9571144
9581145-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -961,8 +1148,9 @@ scanl f z t = unstream (S.scanl g z (stream t))
9611148--
9621149-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
9631150scanl1 :: (Char -> Char -> Char) -> Text -> Text
964−scanl1 f t | null t = empty
965− | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1151+scanl1 = coerce JSS.scanl1
1152+-- scanl1 f t | null t = empty
1153+-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
9661154{-# INLINE scanl1 #-}
9671155
9681156-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -970,16 +1158,18 @@ scanl1 f t | null t = empty
9701158--
9711159-- > scanr f v == reverse . scanl (flip f) v . reverse
9721160scanr :: (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)
1161+scanr = coerce JSS.scanr
1162+-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1163+-- where g a b = safe (f a b)
9751164{-# INLINE scanr #-}
9761165
9771166-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
9781167-- value argument. Subject to fusion. Performs replacement on
9791168-- invalid scalar values.
9801169scanr1 :: (Char -> Char -> Char) -> Text -> Text
981−scanr1 f t | null t = empty
982− | otherwise = scanr f (last t) (init t)
1170+scanr1 = coerce JSS.scanr1
1171+-- scanr1 f t | null t = empty
1172+-- | otherwise = scanr f (last t) (init t)
9831173{-# INLINE scanr1 #-}
9841174
9851175-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -987,8 +1177,11 @@ scanr1 f t | null t = empty
9871177-- parameter from left to right, and returns a final 'Text'. Performs
9881178-- replacement on invalid scalar values.
9891179mapAccumL :: (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)
1180+mapAccumL f z0 (Text a) =
1181+ let (a', t) = JSS.mapAccumL f z0 a
1182+ in (a', Text t)
1183+-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1184+-- where g a b = second safe (f a b)
9921185{-# INLINE mapAccumL #-}
9931186
9941187-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -998,8 +1191,11 @@ mapAccumL f z0 = S.mapAccumL g z0 . stream
9981191-- 'Text'.
9991192-- Performs replacement on invalid scalar values.
10001193mapAccumR :: (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)
1194+mapAccumR f z0 (Text a) =
1195+ let (a', t) = JSS.mapAccumR f z0 a
1196+ in (a', Text t)
1197+-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1198+-- where g a b = second safe (f a b)
10031199{-# INLINE mapAccumR #-}
10041200
10051201-- -----------------------------------------------------------------------------
@@ -1008,20 +1204,21 @@ mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
10081204-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
10091205-- @t@ repeated @n@ times.
10101206replicate :: 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
1207+replicate = coerce JSS.replicate
1208+-- replicate n t@(Text a o l)
1209+-- | n <= 0 || l <= 0 = empty
1210+-- | n == 1 = t
1211+-- | isSingleton t = replicateChar n (unsafeHead t)
1212+-- | otherwise = Text (A.run x) 0 len
1213+-- where
1214+-- len = l `mul` n
1215+-- x :: ST s (A.MArray s)
1216+-- x = do
1217+-- arr <- A.new len
1218+-- let loop !d !i | i >= n = return arr
1219+-- | otherwise = let m = d + l
1220+-- in A.copyI arr d a o m >> loop m (i+1)
1221+-- loop 0 0
10251222{-# INLINE [1] replicate #-}
10261223
10271224{-# RULES
@@ -1032,8 +1229,19 @@ replicate n t@(Text a o l)
10321229-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
10331230-- value of every element. Subject to fusion.
10341231replicateChar :: Int -> Char -> Text
1232+#ifndef __GHCJS__
10351233replicateChar n c = unstream (S.replicateCharI n (safe c))
10361234{-# INLINE replicateChar #-}
1235+#else
1236+replicateChar n c = Text $ js_replicateChar n c
1237+{-# INLINE [1] replicateChar #-}
1238+{-# RULES
1239+"JSSTRING replicateChar -> fused" [~1] forall n c.
1240+ replicateChar n c = unstream (S.replicateCharI n (safe c))
1241+"JSSTRING replicateChar -> unfused" [1] forall n c.
1242+ unstream (S.replicateCharI n (safe c)) = replicateChar n c
1243+ #-}
1244+#endif
10371245
10381246-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
10391247-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1043,7 +1251,8 @@ replicateChar n c = unstream (S.replicateCharI n (safe c))
10431251-- string, and @b@ is the seed value for further production. Subject
10441252-- to fusion. Performs replacement on invalid scalar values.
10451253unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1046−unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1254+unfoldr f a = Text $ JSS.unfoldr f a
1255+-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10471256{-# INLINE unfoldr #-}
10481257
10491258-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1053,7 +1262,8 @@ unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10531262-- correct, otherwise its performance is similar to 'unfoldr'. Subject
10541263-- to fusion. Performs replacement on invalid scalar values.
10551264unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1056−unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1265+unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1266+-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10571267{-# INLINE unfoldrN #-}
10581268
10591269-- -----------------------------------------------------------------------------
@@ -1063,18 +1273,19 @@ unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10631273-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
10641274-- the length of the Text. Subject to fusion.
10651275take :: 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)
1276+take = coerce JSS.take
1277+-- take n t@(Text arr off len)
1278+-- | n <= 0 = empty
1279+-- | n >= len = t
1280+-- | otherwise = text arr off (iterN n t)
10701281{-# INLINE [1] take #-}
10711282
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
1283+-- iterN :: Int -> Text -> Int
1284+-- iterN n t@(Text _arr _off len) = loop 0 0
1285+-- where loop !i !cnt
1286+-- | i >= len || cnt >= n = i
1287+-- | otherwise = loop (i+d) (cnt+1)
1288+-- where d = iter_ t i
10781289
10791290{-# RULES
10801291"TEXT take -> fused" [~1] forall n t.
@@ -1088,31 +1299,36 @@ iterN n t@(Text _arr _off len) = loop 0 0
10881299--
10891300-- Examples:
10901301--
1091−-- > takeEnd 3 "foobar" == "bar"
1302+-- >>> takeEnd 3 "foobar"
1303+-- "bar"
1304+--
1305+-- @since 1.1.1.0
10921306takeEnd :: 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
1307+takeEnd = coerce JSS.takeEnd
1308+-- takeEnd n t@(Text arr off len)
1309+-- | n <= 0 = empty
1310+-- | n >= len = t
1311+-- | otherwise = text arr (off+i) (len-i)
1312+-- where i = iterNEnd n t
1313+
1314+-- iterNEnd :: Int -> Text -> Int
1315+-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1316+-- where loop i !m
1317+-- | i <= 0 = 0
1318+-- | m <= 1 = i
1319+-- | otherwise = loop (i+d) (m-1)
1320+-- where d = reverseIter_ t i
11061321
11071322-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
11081323-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
11091324-- is greater than the length of the 'Text'. Subject to fusion.
11101325drop :: 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
1326+drop = coerce JSS.drop
1327+-- drop n t@(Text arr off len)
1328+-- | n <= 0 = t
1329+-- | n >= len = empty
1330+-- | otherwise = text arr (off+i) (len-i)
1331+-- where i = iterN n t
11161332{-# INLINE [1] drop #-}
11171333
11181334{-# RULES
@@ -1127,22 +1343,27 @@ drop n t@(Text arr off len)
11271343--
11281344-- Examples:
11291345--
1130−-- > dropEnd 3 "foobar" == "foo"
1346+-- >>> dropEnd 3 "foobar"
1347+-- "foo"
1348+--
1349+-- @since 1.1.1.0
11311350dropEnd :: 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)
1351+dropEnd = coerce JSS.dropEnd
1352+-- dropEnd n t@(Text arr off len)
1353+-- | n <= 0 = t
1354+-- | n >= len = empty
1355+-- | otherwise = text arr off (iterNEnd n t)
11361356
11371357-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
11381358-- returns the longest prefix (possibly empty) of elements that
11391359-- satisfy @p@. Subject to fusion.
11401360takeWhile :: (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
1361+takeWhile = coerce JSS.takeWhile
1362+-- takeWhile p t@(Text arr off len) = loop 0
1363+-- where loop !i | i >= len = t
1364+-- | p c = loop (i+d)
1365+-- | otherwise = text arr off i
1366+-- where Iter c d = iter t i
11461367{-# INLINE [1] takeWhile #-}
11471368
11481369{-# RULES
@@ -1157,13 +1378,25 @@ takeWhile p t@(Text arr off len) = loop 0
11571378-- satisfy @p@. Subject to fusion.
11581379-- Examples:
11591380--
1160−-- > takeWhileEnd (=='o') "foo" == "oo"
1381+-- >>> takeWhileEnd (=='o') "foo"
1382+-- "oo"
1383+--
1384+-- @since 1.2.2.0
11611385takeWhileEnd :: (Char -> Bool) -> Text -> Text
1386+#ifndef __GHCJS__
11621387takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11631388 where loop !i !l | l <= 0 = t
11641389 | p c = loop (i+d) (l+d)
11651390 | otherwise = text arr (off+l) (len-l)
11661391 where (c,d) = reverseIter t i
1392+#else
1393+takeWhileEnd p t@(Text jst) = loop (len-1) len
1394+ where loop !i !l | l <= 0 = t
1395+ | p c = loop (i+d) (l+d)
1396+ | otherwise = Text $ js_substr1 l jst
1397+ where (c,d) = reverseIter t i
1398+ len = js_length jst
1399+#endif
11671400{-# INLINE [1] takeWhileEnd #-}
11681401
11691402{-# RULES
@@ -1176,11 +1409,12 @@ takeWhileEnd p t@(Text arr off len) = loop (len-1) len
11761409-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
11771410-- 'takeWhile' @p@ @t@. Subject to fusion.
11781411dropWhile :: (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
1412+dropWhile = coerce JSS.dropWhile
1413+-- dropWhile p t@(Text arr off len) = loop 0 0
1414+-- where loop !i !l | l >= len = empty
1415+-- | p c = loop (i+d) (l+d)
1416+-- | otherwise = Text arr (off+i) (len-l)
1417+
11841418{-# INLINE [1] dropWhile #-}
11851419
11861420{-# RULES
@@ -1196,13 +1430,15 @@ dropWhile p t@(Text arr off len) = loop 0 0
11961430--
11971431-- Examples:
11981432--
1199−-- > dropWhileEnd (=='.') "foo..." == "foo"
1433+-- >>> dropWhileEnd (=='.') "foo..."
1434+-- "foo"
12001435dropWhileEnd :: (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
1436+dropWhileEnd = coerce JSS.dropWhileEnd
1437+-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1438+-- where loop !i !l | l <= 0 = empty
1439+-- | p c = loop (i+d) (l+d)
1440+-- | otherwise = Text arr off l
1441+-- where (c,d) = reverseIter t i
12061442{-# INLINE [1] dropWhileEnd #-}
12071443
12081444{-# RULES
@@ -1245,45 +1481,49 @@ strip = dropAround isSpace
12451481-- prefix of @t@ of length @n@, and whose second is the remainder of
12461482-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
12471483splitAt :: 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))
1484+splitAt = coerce JSS.splitAt
1485+-- splitAt n t@(Text arr off len)
1486+-- | n <= 0 = (empty, t)
1487+-- | n >= len = (t, empty)
1488+-- | otherwise = let k = iterN n t
1489+-- in (text arr off k, text arr (off+k) (len-k))
12531490
12541491-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
12551492-- a pair whose first element is the longest prefix (possibly empty)
12561493-- of @t@ of elements that satisfy @p@, and whose second is the
12571494-- remainder of the list.
12581495span :: (Char -> Bool) -> Text -> (Text, Text)
1259−span p t = case span_ p t of
1260− (# hd,tl #) -> (hd,tl)
1496+span = coerce JSS.span
1497+-- span p t = case span_ p t of
1498+-- (# hd,tl #) -> (hd,tl)
12611499{-# INLINE span #-}
12621500
12631501-- | /O(n)/ 'break' is like 'span', but the prefix returned is
12641502-- over elements that fail the predicate @p@.
12651503break :: (Char -> Bool) -> Text -> (Text, Text)
1266−break p = span (not . p)
1504+break = coerce JSS.break
1505+-- break p = span (not . p)
12671506{-# INLINE break #-}
12681507
12691508-- | /O(n)/ Group characters in a string according to a predicate.
12701509groupBy :: (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))
1510+groupBy = coerce JSS.groupBy
1511+-- groupBy p = loop
1512+-- where
1513+-- loop t@(Text arr off len)
1514+-- | null t = []
1515+-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1516+-- where Iter c d = iter t 0
1517+-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
12781518
12791519-- | Returns the /array/ index (in units of 'Word16') at which a
12801520-- character may be found. This is /not/ the same as the logical
12811521-- 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
1522+-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1523+-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1524+-- where go !i | i >= len || q c = i
1525+-- | otherwise = go (i+d)
1526+-- where Iter c d = iter t i
12871527
12881528-- | /O(n)/ Group characters in a string by equality.
12891529group :: Text -> [Text]
@@ -1292,15 +1532,17 @@ group = groupBy (==)
12921532-- | /O(n)/ Return all initial segments of the given 'Text', shortest
12931533-- first.
12941534inits :: 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)
1535+inits = coerce JSS.inits
1536+-- inits t@(Text arr off len) = loop 0
1537+-- where loop i | i >= len = [t]
1538+-- | otherwise = Text arr off i : loop (i + iter_ t i)
12981539
12991540-- | /O(n)/ Return all final segments of the given 'Text', longest
13001541-- first.
13011542tails :: Text -> [Text]
1302−tails t | null t = [empty]
1303− | otherwise = t : tails (unsafeTail t)
1543+tails = coerce JSS.tails
1544+-- tails t | null t = [empty]
1545+-- | otherwise = t : tails (unsafeTail t)
13041546
13051547-- $split
13061548--
@@ -1314,9 +1556,14 @@ tails t | null t = [empty]
13141556--
13151557-- Examples:
13161558--
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" == ["",""]
1559+-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1560+-- ["a","b","d","e"]
1561+--
1562+-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1563+-- ["","X","X","X",""]
1564+--
1565+-- >>> splitOn "x" "x"
1566+-- ["",""]
13201567--
13211568-- and
13221569--
@@ -1333,13 +1580,14 @@ splitOn :: Text
13331580 -> Text
13341581 -- ^ Input text.
13351582 -> [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)]
1583+splitOn = coerce JSS.splitOn
1584+-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1585+-- | l <= 0 = emptyError "splitOn"
1586+-- | isSingleton pat = split (== unsafeHead pat) src
1587+-- | otherwise = go 0 (indices pat src)
1588+-- where
1589+-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1590+-- go s _ = [text arr (s+off) (len-s)]
13431591{-# INLINE [1] splitOn #-}
13441592
13451593{-# RULES
@@ -1352,28 +1600,36 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
13521600-- resulting components do not contain the separators. Two adjacent
13531601-- separators result in an empty component in the output. eg.
13541602--
1355−-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356−-- > split (=='a') "" == [""]
1603+-- >>> split (=='a') "aabbaca"
1604+-- ["","","bb","c",""]
1605+--
1606+-- >>> split (=='a') ""
1607+-- [""]
13571608split :: (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
1609+split = coerce JSS.split
1610+-- split _ t@(Text _off _arr 0) = [t]
1611+-- split p t = loop t
1612+-- where loop s | null s' = [l]
1613+-- | otherwise = l : loop (unsafeTail s')
1614+-- where (# l, s' #) = span_ (not . p) s
13631615{-# INLINE split #-}
13641616
13651617-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
13661618-- element may be shorter than the other chunks, depending on the
13671619-- length of the input. Examples:
13681620--
1369−-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370−-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1621+-- >>> chunksOf 3 "foobarbaz"
1622+-- ["foo","bar","baz"]
1623+--
1624+-- >>> chunksOf 4 "haskell.org"
1625+-- ["hask","ell.","org"]
13711626chunksOf :: 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
1627+chunksOf = coerce JSS.chunksOf
1628+-- chunksOf k = go
1629+-- where
1630+-- go t = case splitAt k t of
1631+-- (a,b) | null a -> []
1632+-- | otherwise -> a : go b
13771633{-# INLINE chunksOf #-}
13781634
13791635-- ----------------------------------------------------------------------------
@@ -1386,7 +1642,8 @@ chunksOf k = go
13861642-- returns the first element matching the predicate, or 'Nothing' if
13871643-- there is no such element.
13881644find :: (Char -> Bool) -> Text -> Maybe Char
1389−find p t = S.findBy p (stream t)
1645+find = coerce JSS.find
1646+-- find p t = S.findBy p (stream t)
13901647{-# INLINE find #-}
13911648
13921649-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1395,14 +1652,16 @@ find p t = S.findBy p (stream t)
13951652--
13961653-- > partition p t == (filter p t, filter (not . p) t)
13971654partition :: (Char -> Bool) -> Text -> (Text, Text)
1398−partition p t = (filter p t, filter (not . p) t)
1655+partition = coerce JSS.partition
1656+-- partition p t = (filter p t, filter (not . p) t)
13991657{-# INLINE partition #-}
14001658
14011659-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
14021660-- returns a 'Text' containing those characters that satisfy the
14031661-- predicate.
14041662filter :: (Char -> Bool) -> Text -> Text
1405−filter p t = unstream (S.filter p (stream t))
1663+filter = coerce JSS.filter
1664+-- filter p t = unstream (S.filter p (stream t))
14061665{-# INLINE filter #-}
14071666
14081667-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1412,8 +1671,11 @@ filter p t = unstream (S.filter p (stream t))
14121671--
14131672-- Examples:
14141673--
1415−-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416−-- > breakOn "/" "foobar" ==> ("foobar", "")
1674+-- >>> breakOn "::" "a::b::c"
1675+-- ("a","::b::c")
1676+--
1677+-- >>> breakOn "/" "foobar"
1678+-- ("foobar","")
14171679--
14181680-- Laws:
14191681--
@@ -1427,11 +1689,12 @@ filter p t = unstream (S.filter p (stream t))
14271689-- In (unlikely) bad cases, this function's time complexity degrades
14281690-- towards /O(n*m)/.
14291691breakOn :: 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))
1692+breakOn = coerce JSS.breakOn
1693+-- breakOn pat src@(Text arr off len)
1694+-- | null pat = emptyError "breakOn"
1695+-- | otherwise = case indices pat src of
1696+-- [] -> (src, empty)
1697+-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
14351698{-# INLINE breakOn #-}
14361699
14371700-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1441,10 +1704,12 @@ breakOn pat src@(Text arr off len)
14411704-- up to and including the last match of @needle@. The second is the
14421705-- remainder of @haystack@, following the match.
14431706--
1444−-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1707+-- >>> breakOnEnd "::" "a::b::c"
1708+-- ("a::b::","c")
14451709breakOnEnd :: Text -> Text -> (Text, Text)
1446−breakOnEnd pat src = (reverse b, reverse a)
1447− where (a,b) = breakOn (reverse pat) (reverse src)
1710+breakOnEnd = coerce JSS.breakOnEnd
1711+-- breakOnEnd pat src = (reverse b, reverse a)
1712+-- where (a,b) = breakOn (reverse pat) (reverse src)
14481713{-# INLINE breakOnEnd #-}
14491714
14501715-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1456,10 +1721,11 @@ breakOnEnd pat src = (reverse b, reverse a)
14561721--
14571722-- Examples:
14581723--
1459−-- > breakOnAll "::" ""
1460−-- > ==> []
1461−-- > breakOnAll "/" "a/b/c/"
1462−-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
1724+-- >>> breakOnAll "::" ""
1725+-- []
1726+--
1727+-- >>> breakOnAll "/" "a/b/c/"
1728+-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
14631729--
14641730-- In (unlikely) bad cases, this function's time complexity degrades
14651731-- towards /O(n*m)/.
@@ -1468,12 +1734,13 @@ breakOnEnd pat src = (reverse b, reverse a)
14681734breakOnAll :: Text -- ^ @needle@ to search for
14691735 -> Text -- ^ @haystack@ in which to search
14701736 -> [(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
1737+breakOnAll = coerce JSS.breakOnAll
1738+-- breakOnAll pat src@(Text arr off slen)
1739+-- | null pat = emptyError "breakOnAll"
1740+-- | otherwise = L.map step (indices pat src)
1741+-- where
1742+-- step x = (chunk 0 x, chunk x (slen-x))
1743+-- chunk !n !l = text arr (n+off) l
14771744{-# INLINE breakOnAll #-}
14781745
14791746-------------------------------------------------------------------------------
@@ -1497,14 +1764,16 @@ breakOnAll pat src@(Text arr off slen)
14971764
14981765-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
14991766index :: Text -> Int -> Char
1500−index t n = S.index (stream t) n
1767+index = coerce JSS.index
1768+-- index t n = S.index (stream t) n
15011769{-# INLINE index #-}
15021770
15031771-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
15041772-- and returns the index of the first element in the 'Text' satisfying
15051773-- the predicate. Subject to fusion.
15061774findIndex :: (Char -> Bool) -> Text -> Maybe Int
1507−findIndex p t = S.findIndex p (stream t)
1775+findIndex = coerce JSS.findIndex
1776+-- findIndex p t = S.findIndex p (stream t)
15081777{-# INLINE findIndex #-}
15091778
15101779-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1514,10 +1783,11 @@ findIndex p t = S.findIndex p (stream t)
15141783-- In (unlikely) bad cases, this function's time complexity degrades
15151784-- towards /O(n*m)/.
15161785count :: 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)
1786+count = coerce JSS.count
1787+-- count pat src
1788+-- | null pat = emptyError "count"
1789+-- | isSingleton pat = countChar (unsafeHead pat) src
1790+-- | otherwise = L.length (indices pat src)
15211791{-# INLINE [1] count #-}
15221792
15231793{-# RULES
@@ -1539,42 +1809,46 @@ countChar c t = S.countChar c (stream t)
15391809-- excess elements of the longer 'Text' are discarded. This is
15401810-- equivalent to a pair of 'unpack' operations.
15411811zip :: Text -> Text -> [(Char,Char)]
1542−zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1812+zip = coerce JSS.zip
1813+-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
15431814{-# INLINE zip #-}
15441815
15451816-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
15461817-- given as the first argument, instead of a tupling function.
15471818-- Performs replacement on invalid scalar values.
15481819zipWith :: (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)
1820+zipWith = coerce JSS.zipWith
1821+-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1822+-- where g a b = safe (f a b)
15511823{-# INLINE zipWith #-}
15521824
15531825-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
15541826-- representing white space.
15551827words :: 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
1828+words = coerce JSS.words
1829+-- words t@(Text arr off len) = loop 0 0
1830+-- where
1831+-- loop !start !n
1832+-- | n >= len = if start == n
1833+-- then []
1834+-- else [Text arr (start+off) (n-start)]
1835+-- | isSpace c =
1836+-- if start == n
1837+-- then loop (start+1) (start+1)
1838+-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1839+-- | otherwise = loop start (n+d)
1840+-- where Iter c d = iter t n
15681841{-# INLINE words #-}
15691842
15701843-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
15711844-- newline 'Char's. The resulting strings do not contain newlines.
15721845lines :: 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
1846+lines = coerce JSS.lines
1847+-- lines ps | null ps = []
1848+-- | otherwise = h : if null t
1849+-- then []
1850+-- else lines (unsafeTail t)
1851+-- where (# h,t #) = span_ (/= '\n') ps
15781852{-# INLINE lines #-}
15791853
15801854{-
@@ -1602,34 +1876,40 @@ lines' ps | null ps = []
16021876-- | /O(n)/ Joins lines, after appending a terminating newline to
16031877-- each.
16041878unlines :: [Text] -> Text
1605−unlines = concat . L.map (`snoc` '\n')
1879+unlines = coerce JSS.unlines
1880+-- unlines = concat . L.map (`snoc` '\n')
16061881{-# INLINE unlines #-}
16071882
16081883-- | /O(n)/ Joins words using single space characters.
16091884unwords :: [Text] -> Text
1610−unwords = intercalate (singleton ' ')
1885+unwords = coerce JSS.unwords
1886+-- unwords = intercalate (singleton ' ')
16111887{-# INLINE unwords #-}
16121888
16131889-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
16141890-- 'True' iff the first is a prefix of the second. Subject to fusion.
16151891isPrefixOf :: Text -> Text -> Bool
1616−isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617− alen <= blen && S.isPrefixOf (stream a) (stream b)
1892+isPrefixOf = coerce JSS.isPrefixOf
1893+-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1894+-- alen <= blen && S.isPrefixOf (stream a) (stream b)
16181895{-# INLINE [1] isPrefixOf #-}
16191896
16201897{-# RULES
16211898"TEXT isPrefixOf -> fused" [~1] forall s t.
16221899 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1900+"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1901+ S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
16231902 #-}
16241903
16251904-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
16261905-- 'True' iff the first is a suffix of the second.
16271906isSuffixOf :: 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
1907+isSuffixOf = coerce JSS.isSuffixOf
1908+-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1909+-- d >= 0 && a == b'
1910+-- where d = blen - alen
1911+-- b' | d == 0 = b
1912+-- | otherwise = Text barr (boff+d) alen
16331913{-# INLINE isSuffixOf #-}
16341914
16351915-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1639,10 +1919,11 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
16391919-- In (unlikely) bad cases, this function's time complexity degrades
16401920-- towards /O(n*m)/.
16411921isInfixOf :: 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
1922+isInfixOf = coerce JSS.isInfixOf
1923+-- isInfixOf needle haystack
1924+-- | null needle = True
1925+-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1926+-- | otherwise = not . L.null . indices needle $ haystack
16461927{-# INLINE [1] isInfixOf #-}
16471928
16481929{-# RULES
@@ -1658,9 +1939,14 @@ isInfixOf needle haystack
16581939--
16591940-- Examples:
16601941--
1661−-- > stripPrefix "foo" "foobar" == Just "bar"
1662−-- > stripPrefix "" "baz" == Just "baz"
1663−-- > stripPrefix "foo" "quux" == Nothing
1942+-- >>> stripPrefix "foo" "foobar"
1943+-- Just "bar"
1944+--
1945+-- >>> stripPrefix "" "baz"
1946+-- Just "baz"
1947+--
1948+-- >>> stripPrefix "foo" "quux"
1949+-- Nothing
16641950--
16651951-- This is particularly useful with the @ViewPatterns@ extension to
16661952-- GHC, as follows:
@@ -1672,9 +1958,10 @@ isInfixOf needle haystack
16721958-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
16731959-- > fnordLength _ = -1
16741960stripPrefix :: 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
1961+stripPrefix = coerce JSS.stripPrefix
1962+-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1963+-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1964+-- | otherwise = Nothing
16781965
16791966-- | /O(n)/ Find the longest non-empty common prefix of two strings
16801967-- and return it, along with the suffixes of each string at which they
@@ -1685,28 +1972,39 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
16851972--
16861973-- Examples:
16871974--
1688−-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689−-- > commonPrefixes "veeble" "fetzer" == Nothing
1690−-- > commonPrefixes "" "baz" == Nothing
1975+-- >>> commonPrefixes "foobar" "fooquux"
1976+-- Just ("foo","bar","quux")
1977+--
1978+-- >>> commonPrefixes "veeble" "fetzer"
1979+-- Nothing
1980+--
1981+-- >>> commonPrefixes "" "baz"
1982+-- Nothing
16911983commonPrefixes :: 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
1984+commonPrefixes = coerce JSS.commonPrefixes
1985+-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1986+-- where
1987+-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1988+-- | i > 0 = Just (Text arr0 off0 i,
1989+-- text arr0 (off0+i) (len0-i),
1990+-- text arr1 (off1+j) (len1-j))
1991+-- | otherwise = Nothing
1992+-- where Iter a d0 = iter t0 i
1993+-- Iter b d1 = iter t1 j
17011994
17021995-- | /O(n)/ Return the prefix of the second string if its suffix
17031996-- matches the entire first string.
17041997--
17051998-- Examples:
17061999--
1707−-- > stripSuffix "bar" "foobar" == Just "foo"
1708−-- > stripSuffix "" "baz" == Just "baz"
1709−-- > stripSuffix "foo" "quux" == Nothing
2000+-- >>> stripSuffix "bar" "foobar"
2001+-- Just "foo"
2002+--
2003+-- >>> stripSuffix "" "baz"
2004+-- Just "baz"
2005+--
2006+-- >>> stripSuffix "foo" "quux"
2007+-- Nothing
17102008--
17112009-- This is particularly useful with the @ViewPatterns@ extension to
17122010-- GHC, as follows:
@@ -1718,21 +2016,22 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
17182016-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
17192017-- > quuxLength _ = -1
17202018stripSuffix :: 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"
2019+stripSuffix = coerce JSS.stripSuffix
2020+-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2021+-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
2022+-- | otherwise = Nothing
2023+
2024+-- -- | Add a list of non-negative numbers. Errors out on overflow.
2025+-- sumP :: String -> [Int] -> Int
2026+-- sumP fun = go 0
2027+-- where go !a (x:xs)
2028+-- | ax >= 0 = go ax xs
2029+-- | otherwise = overflowError fun
2030+-- where ax = a + x
2031+-- go a _ = a
2032+
2033+-- emptyError :: String -> a
2034+-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
17362035
17372036overflowError :: String -> a
17382037overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1746,10 +2045,24 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
17462045-- copy \"breaks the link\" to the original array, allowing it to be
17472046-- garbage collected if there are no other live references to it.
17482047copy :: 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
2048+copy = P.id -- TODO
2049+-- copy (Text arr off len) = Text (A.run go) 0 len
2050+-- where
2051+-- go :: ST s (A.MArray s)
2052+-- go = do
2053+-- marr <- A.new len
2054+
2055+foreign import javascript unsafe
2056+ "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
2057+foreign import javascript unsafe
2058+ "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString
2059+foreign import javascript unsafe
2060+ "$1.length" js_length :: JSString -> Int
2061+
2062+-------------------------------------------------
2063+-- NOTE: the named chunk below used by doctest;
2064+-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2065+
2066+-- $setup
2067+-- >>> :set -XOverloadedStrings
2068+-- >>> import qualified Data.Text as T