@@ -6,7 +6,7 @@
66#if __GLASGOW_HASKELL__ >= 708
77{-# LANGUAGE TypeFamilies #-}
88#endif
9−{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI, UnliftedFFITypes #-}
9+
1010-- |
1111-- Module : Data.Text
1212-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
@@ -15,6 +15,7 @@
1515--
1616-- License : BSD-style
1717-- Maintainer : bos@serpentine.com
18+-- Stability : experimental
1819-- Portability : GHC
1920--
2021-- A time and space-efficient implementation of Unicode text.
@@ -31,9 +32,8 @@
3132--
3233-- To use an extended and very rich family of functions for working
3334-- with Unicode text (including normalization, regular expressions,
34−-- non-standard encodings, text breaking, and locales), see the
35−-- <http://hackage.haskell.org/package/text-icu text-icu package >.
36−--
35+-- non-standard encodings, text breaking, and locales), see
36+-- <http://hackage.haskell.org/package/text-icu the text-icu package >.
3737
3838module Data.Text
3939 (
@@ -43,9 +43,6 @@ module Data.Text
4343 -- * Acceptable data
4444 -- $replacement
4545
46− -- * Definition of character
47− -- $character_definition
48−
4946 -- * Fusion
5047 -- $fusion
5148
@@ -63,7 +60,6 @@ module Data.Text
6360 , snoc
6461 , append
6562 , uncons
66− , unsnoc
6763 , head
6864 , last
6965 , tail
@@ -227,17 +223,16 @@ import qualified Data.Text.Internal.Fusion as S
227223import qualified Data.Text.Internal.Fusion.Common as S
228224import Data.Text.Encoding (decodeUtf8', encodeUtf8)
229225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
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#)
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#)
233229import qualified Prelude as P
234230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
235− reverseIter_, unsafeHead, unsafeTail,
236− unsafeDupablePerformIO)
231+ reverseIter_, unsafeHead, unsafeTail)
237232import Data.Text.Internal.Unsafe.Char (unsafeChr)
238233import qualified Data.Text.Internal.Functions as F
239234import qualified Data.Text.Internal.Encoding.Utf16 as U16
240−-- import Data.Text.Internal.Search (indices)
235+import Data.Text.Internal.Search (indices)
241236#if defined(__HADDOCK__)
242237import Data.ByteString (ByteString)
243238import qualified Data.Text.Lazy as L
@@ -251,80 +246,6 @@ import qualified GHC.Exts as Exts
251246import Text.Printf (PrintfArg, formatArg, formatString)
252247#endif
253248
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−
328249-- $strict
329250--
330251-- This package provides both strict and lazy 'Text' types. The
@@ -400,31 +321,22 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
400321-- Functions that can be fused by the compiler are documented with the
401322-- phrase \"Subject to fusion\".
402323
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 (==) #-}
408324instance Eq Text where
409− Text x == Text y = x == y
410−
411−-- instance Ord Text where
412−-- compare = compareText
325+ Text arrA offA lenA == Text arrB offB lenB
326+ | lenA == lenB = A.equal arrA offA arrB offB lenA
327+ | otherwise = False
328+ {-# INLINE (==) #-}
413329
414330instance Ord Text where
415− compare (Text x) (Text y) = compare x y
331+ compare = compareText
416332
417333instance Read Text where
418334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
419335
420336#if MIN_VERSION_base(4,9,0)
421−-- | Non-orphan 'Semigroup' instance only defined for
422−-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
423−-- provided by
424−-- the [semigroups](http://hackage.haskell.org/package/semigroups)
425−-- package
426−--
427−-- @since 1.2.2.0
337+-- Semigroup orphan instances for older GHCs are provided by
338+-- 'semigroups` package
339+
428340instance Semigroup Text where
429341 (<>) = append
430342#endif
@@ -442,7 +354,6 @@ instance IsString Text where
442354 fromString = pack
443355
444356#if __GLASGOW_HASKELL__ >= 708
445−-- | @since 1.2.0.0
446357instance Exts.IsList Text where
447358 type Item Text = Char
448359 fromList = pack
@@ -453,7 +364,6 @@ instance Exts.IsList Text where
453364instance NFData Text where rnf !_ = ()
454365#endif
455366
456−-- | @since 1.2.1.0
457367instance Binary Text where
458368 put t = put (encodeUtf8 t)
459369 get = do
@@ -487,10 +397,8 @@ instance Data Text where
487397
488398#if MIN_VERSION_base(4,7,0)
489399-- | Only defined for @base-4.7.0.0@ and later
490−--
491−-- @since 1.2.2.0
492400instance PrintfArg Text where
493− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
494402#endif
495403
496404packConstr :: Constr
@@ -499,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
499407textDataType :: DataType
500408textDataType = mkDataType "Data.Text.Text" [packConstr]
501409
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
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
515423
516424-- -----------------------------------------------------------------------------
517425-- * Conversion to/from 'Text'
@@ -519,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
519427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
520428-- fusion. Performs replacement on invalid scalar values.
521429pack :: String -> Text
522−pack = coerce JSS.pack
523−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
524431{-# INLINE [1] pack #-}
525432
526433-- -----------------------------------------------------------------------------
@@ -531,8 +438,7 @@ pack = coerce JSS.pack
531438-- copying a new array. Subject to fusion. Performs replacement on
532439-- invalid scalar values.
533440cons :: Char -> Text -> Text
534−cons = coerce JSS.cons
535−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
536442{-# INLINE cons #-}
537443
538444infixr 5 `cons`
@@ -541,27 +447,25 @@ infixr 5 `cons`
541447-- entire array in the process, unless fused. Subject to fusion.
542448-- Performs replacement on invalid scalar values.
543449snoc :: Text -> Char -> Text
544−snoc = coerce JSS.snoc
545−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
546451{-# INLINE snoc #-}
547452
548453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
549454-- into a new 'Text'. Subject to fusion.
550455append :: Text -> Text -> Text
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
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
565469{-# NOINLINE append #-}
566470
567471{-# RULES
@@ -574,18 +478,16 @@ append = coerce JSS.append
574478-- | /O(1)/ Returns the first character of a 'Text', which must be
575479-- non-empty. Subject to fusion.
576480head :: Text -> Char
577−head = coerce JSS.head
578−-- head t = S.head (stream t)
481+head t = S.head (stream t)
579482{-# INLINE head #-}
580483
581484-- | /O(1)/ Returns the first character and rest of a 'Text', or
582485-- 'Nothing' if empty. Subject to fusion.
583486uncons :: Text -> Maybe (Char, Text)
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))
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))
589491{-# INLINE [1] uncons #-}
590492
591493-- | Lifted from Control.Arrow and specialized.
@@ -595,13 +497,12 @@ second f (a, b) = (a, f b)
595497-- | /O(1)/ Returns the last character of a 'Text', which must be
596498-- non-empty. Subject to fusion.
597499last :: Text -> Char
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)
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)
605506{-# INLINE [1] last #-}
606507
607508{-# RULES
@@ -614,11 +515,10 @@ last = coerce JSS.last
614515-- | /O(1)/ Returns all characters after the head of a 'Text', which
615516-- must be non-empty. Subject to fusion.
616517tail :: Text -> Text
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
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
622522{-# INLINE [1] tail #-}
623523
624524{-# RULES
@@ -631,12 +531,11 @@ tail = coerce JSS.tail
631531-- | /O(1)/ Returns all but the last character of a 'Text', which must
632532-- be non-empty. Subject to fusion.
633533init :: Text -> Text
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)
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)
640539{-# INLINE [1] init #-}
641540
642541{-# RULES
@@ -646,29 +545,14 @@ init = coerce JSS.init
646545 unstream (S.init (stream t)) = init t
647546 #-}
648547
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−
663548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
664549-- fusion.
665550null :: Text -> Bool
666−null = coerce JSS.null
667−-- null (Text _arr _off len) =
668−-- #if defined(ASSERTS)
669−-- assert (len >= 0) $
670−-- #endif
671−-- len <= 0
551+null (Text _arr _off len) =
552+#if defined(ASSERTS)
553+ assert (len >= 0) $
554+#endif
555+ len <= 0
672556{-# INLINE [1] null #-}
673557
674558{-# RULES
@@ -680,15 +564,14 @@ null = coerce JSS.null
680564
681565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
682566-- Subject to fusion.
683−-- isSingleton :: Text -> Bool
684−-- isSingleton = S.isSingleton . stream
685−-- {-# INLINE isSingleton #-}
567+isSingleton :: Text -> Bool
568+isSingleton = S.isSingleton . stream
569+{-# INLINE isSingleton #-}
686570
687571-- | /O(n)/ Returns the number of characters in a 'Text'.
688572-- Subject to fusion.
689573length :: Text -> Int
690−length = coerce JSS.length
691−-- length t = S.length (stream t)
574+length t = S.length (stream t)
692575{-# INLINE [0] length #-}
693576-- length needs to be phased after the compareN/length rules otherwise
694577-- it may inline before the rules have an opportunity to fire.
@@ -741,58 +624,29 @@ compareLength t n = S.compareLengthI (stream t) n
741624-- -----------------------------------------------------------------------------
742625-- * Transformations
743626-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
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.
627+-- each element of @t@. Subject to fusion. Performs replacement on
628+-- invalid scalar values.
753629map :: (Char -> Char) -> Text -> Text
754−map = coerce JSS.map
755−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
756631{-# INLINE [1] map #-}
757632
758633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
759634-- 'Text's and concatenates the list after interspersing the first
760635-- 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"
766636intercalate :: Text -> [Text] -> Text
767−intercalate = coerce JSS.intercalate
768−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
769638{-# INLINE intercalate #-}
770639
771640-- | /O(n)/ The 'intersperse' function takes a character and places it
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.
641+-- between the characters of a 'Text'. Subject to fusion. Performs
642+-- replacement on invalid scalar values.
780643intersperse :: Char -> Text -> Text
781−intersperse = coerce JSS.intersperse
782−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
783645{-# INLINE intersperse #-}
784646
785−-- | /O(n)/ Reverse the characters of a string.
786−--
787−-- Example:
788−--
789−-- >>> T.reverse "desrever"
790−-- "reversed"
791−--
792−-- Subject to fusion.
647+-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
793648reverse :: Text -> Text
794−reverse = coerce JSS.reverse
795−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
796650{-# INLINE reverse #-}
797651
798652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -809,14 +663,12 @@ reverse = coerce JSS.reverse
809663-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
810664-- be replaced recursively:
811665--
812−-- >>> replace "oo" "foo" "oo"
813−-- "foo"
666+-- > replace "oo" "foo" "oo" == "foo"
814667--
815668-- In cases where several instances of @needle@ overlap, only the
816669-- first one will be replaced:
817670--
818−-- >>> replace "ofo" "bar" "ofofo"
819−-- "barfo"
671+-- > replace "ofo" "bar" "ofofo" == "barfo"
820672--
821673-- In (unlikely) bad cases, this function's time complexity degrades
822674-- towards /O(n*m)/.
@@ -828,29 +680,28 @@ replace :: Text
828680 -> Text
829681 -- ^ @haystack@ in which to search.
830682 -> Text
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
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
854705
855706-- ----------------------------------------------------------------------------
856707-- ** Case conversions (folds)
@@ -889,8 +740,7 @@ replace = coerce JSS.replace
889740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
890741-- instead of itself.
891742toCaseFold :: Text -> Text
892−-- toCaseFold t = unstream (S.toCaseFold (stream t))
893−toCaseFold = coerce JSS.toCaseFold
743+toCaseFold t = unstream (S.toCaseFold (stream t))
894744{-# INLINE toCaseFold #-}
895745
896746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -901,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
901751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
902752-- followed by \" ̇\" (combining dot above, U+0307).
903753toLower :: Text -> Text
904−-- toLower t = unstream (S.toLower (stream t))
905−toLower = coerce JSS.toLower
754+toLower t = unstream (S.toLower (stream t))
906755{-# INLINE toLower #-}
907756
908757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -912,8 +761,7 @@ toLower = coerce JSS.toLower
912761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
913762-- two-letter sequence \"SS\".
914763toUpper :: Text -> Text
915−-- toUpper t = unstream (S.toUpper (stream t))
916−toUpper = coerce JSS.toUpper
764+toUpper t = unstream (S.toUpper (stream t))
917765{-# INLINE toUpper #-}
918766
919767-- | /O(n)/ Convert a string to title case, using simple case
@@ -934,11 +782,8 @@ toUpper = coerce JSS.toUpper
934782-- guides disagree on whether the book name \"The Hill of the Red
935783-- Fox\" is correctly title cased—but this function will
936784-- capitalize /every/ word.
937−--
938−-- @since 1.0.0.0
939785toTitle :: Text -> Text
940−toTitle = coerce JSS.toTitle
941−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
942787{-# INLINE toTitle #-}
943788
944789-- | /O(n)/ Left-justify a string to the given length, using the
@@ -947,17 +792,13 @@ toTitle = coerce JSS.toTitle
947792--
948793-- Examples:
949794--
950−-- >>> justifyLeft 7 'x' "foo"
951−-- "fooxxxx"
952−--
953−-- >>> justifyLeft 3 'x' "foobar"
954−-- "foobar"
795+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796+-- > justifyLeft 3 'x' "foobar" == "foobar"
955797justifyLeft :: Int -> Char -> Text -> Text
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
798+justifyLeft k c t
799+ | len >= k = t
800+ | otherwise = t `append` replicateChar (k-len) c
801+ where len = length t
961802{-# INLINE [1] justifyLeft #-}
962803
963804{-# RULES
@@ -973,17 +814,13 @@ justifyLeft = coerce JSS.justifyLeft
973814--
974815-- Examples:
975816--
976−-- >>> justifyRight 7 'x' "bar"
977−-- "xxxxbar"
978−--
979−-- >>> justifyRight 3 'x' "foobar"
980−-- "foobar"
817+-- > justifyRight 7 'x' "bar" == "xxxxbar"
818+-- > justifyRight 3 'x' "foobar" == "foobar"
981819justifyRight :: Int -> Char -> Text -> Text
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
820+justifyRight k c t
821+ | len >= k = t
822+ | otherwise = replicateChar (k-len) c `append` t
823+ where len = length t
987824{-# INLINE justifyRight #-}
988825
989826-- | /O(n)/ Center a string to the given length, using the specified
@@ -992,34 +829,23 @@ justifyRight = coerce JSS.justifyRight
992829--
993830-- Examples:
994831--
995−-- >>> center 8 'x' "HS"
996−-- "xxxHSxxx"
832+-- > center 8 'x' "HS" = "xxxHSxxx"
997833center :: Int -> Char -> Text -> Text
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
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
1006841{-# INLINE center #-}
1007842
1008843-- | /O(n)/ The 'transpose' function transposes the rows and columns
1009844-- of its 'Text' argument. Note that this function uses 'pack',
1010845-- 'unpack', and the list version of transpose, and is thus not very
1011846-- 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"]
1020847transpose :: [Text] -> [Text]
1021−transpose = coerce JSS.transpose
1022−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
1023849
1024850-- -----------------------------------------------------------------------------
1025851-- * Reducing 'Text's (folds)
@@ -1029,27 +855,23 @@ transpose = coerce JSS.transpose
1029855-- reduces the 'Text' using the binary operator, from left to right.
1030856-- Subject to fusion.
1031857foldl :: (a -> Char -> a) -> a -> Text -> a
1032−foldl f a0 (Text t) = JSS.foldl f a0 t
1033−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
1034859{-# INLINE foldl #-}
1035860
1036861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
1037862foldl' :: (a -> Char -> a) -> a -> Text -> a
1038−foldl' f a0 (Text t) = JSS.foldl' f a0 t
1039−-- foldl' f z t = S.foldl' f z (stream t)
863+foldl' f z t = S.foldl' f z (stream t)
1040864{-# INLINE foldl' #-}
1041865
1042866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
1043867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
1044868foldl1 :: (Char -> Char -> Char) -> Text -> Char
1045−foldl1 = coerce JSS.foldl1
1046−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
1047870{-# INLINE foldl1 #-}
1048871
1049872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
1050873foldl1' :: (Char -> Char -> Char) -> Text -> Char
1051−foldl1' = coerce JSS.foldl1'
1052−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
1053875{-# INLINE foldl1' #-}
1054876
1055877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -1057,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
1057879-- reduces the 'Text' using the binary operator, from right to left.
1058880-- Subject to fusion.
1059881foldr :: (Char -> a -> a) -> a -> Text -> a
1060−foldr f a0 (Text t) = JSS.foldr f a0 t
1061−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
1062883{-# INLINE foldr #-}
1063884
1064885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
1065886-- and thus must be applied to a non-empty 'Text'. Subject to
1066887-- fusion.
1067888foldr1 :: (Char -> Char -> Char) -> Text -> Char
1068−foldr1 = coerce JSS.foldr1
1069−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
1070890{-# INLINE foldr1 #-}
1071891
1072892-- -----------------------------------------------------------------------------
@@ -1074,54 +894,48 @@ foldr1 = coerce JSS.foldr1
1074894
1075895-- | /O(n)/ Concatenate a list of 'Text's.
1076896concat :: [Text] -> Text
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
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
1091910
1092911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
1093912-- concatenate the results.
1094913concatMap :: (Char -> Text) -> Text -> Text
1095−concatMap = coerce JSS.concatMap
1096−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
1097915{-# INLINE concatMap #-}
1098916
1099917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1100918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1101919any :: (Char -> Bool) -> Text -> Bool
1102−any = coerce JSS.any
1103−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1104921{-# INLINE any #-}
1105922
1106923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1107924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1108925all :: (Char -> Bool) -> Text -> Bool
1109−all = coerce JSS.all
1110−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1111927{-# INLINE all #-}
1112928
1113929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1114930-- must be non-empty. Subject to fusion.
1115931maximum :: Text -> Char
1116−maximum = coerce JSS.maximum
1117−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1118933{-# INLINE maximum #-}
1119934
1120935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1121936-- must be non-empty. Subject to fusion.
1122937minimum :: Text -> Char
1123−minimum = coerce JSS.minimum
1124−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1125939{-# INLINE minimum #-}
1126940
1127941-- -----------------------------------------------------------------------------
@@ -1137,9 +951,8 @@ minimum = coerce JSS.minimum
1137951--
1138952-- > last (scanl f z xs) == foldl f z xs.
1139953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
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)
954+scanl f z t = unstream (S.scanl g z (stream t))
955+ where g a b = safe (f a b)
1143956{-# INLINE scanl #-}
1144957
1145958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -1148,9 +961,8 @@ scanl = coerce JSS.scanl
1148961--
1149962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1150963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1151−scanl1 = coerce JSS.scanl1
1152−-- scanl1 f t | null t = empty
1153−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
964+scanl1 f t | null t = empty
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1154966{-# INLINE scanl1 #-}
1155967
1156968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -1158,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1158970--
1159971-- > scanr f v == reverse . scanl (flip f) v . reverse
1160972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
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)
973+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
974+ where g a b = safe (f a b)
1164975{-# INLINE scanr #-}
1165976
1166977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1167978-- value argument. Subject to fusion. Performs replacement on
1168979-- invalid scalar values.
1169980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1170−scanr1 = coerce JSS.scanr1
1171−-- scanr1 f t | null t = empty
1172−-- | otherwise = scanr f (last t) (init t)
981+scanr1 f t | null t = empty
982+ | otherwise = scanr f (last t) (init t)
1173983{-# INLINE scanr1 #-}
1174984
1175985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1177,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1177987-- parameter from left to right, and returns a final 'Text'. Performs
1178988-- replacement on invalid scalar values.
1179989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
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)
990+mapAccumL f z0 = S.mapAccumL g z0 . stream
991+ where g a b = second safe (f a b)
1185992{-# INLINE mapAccumL #-}
1186993
1187994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1191,11 +998,8 @@ mapAccumL f z0 (Text a) =
1191998-- 'Text'.
1192999-- Performs replacement on invalid scalar values.
11931000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
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)
1001+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1002+ where g a b = second safe (f a b)
11991003{-# INLINE mapAccumR #-}
12001004
12011005-- -----------------------------------------------------------------------------
@@ -1204,21 +1008,20 @@ mapAccumR f z0 (Text a) =
12041008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
12051009-- @t@ repeated @n@ times.
12061010replicate :: Int -> Text -> Text
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
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
12221025{-# INLINE [1] replicate #-}
12231026
12241027{-# RULES
@@ -1229,19 +1032,8 @@ replicate = coerce JSS.replicate
12291032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
12301033-- value of every element. Subject to fusion.
12311034replicateChar :: Int -> Char -> Text
1232−#ifndef __GHCJS__
12331035replicateChar n c = unstream (S.replicateCharI n (safe c))
12341036{-# 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
12451037
12461038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
12471039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1251,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
12511043-- string, and @b@ is the seed value for further production. Subject
12521044-- to fusion. Performs replacement on invalid scalar values.
12531045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1254−unfoldr f a = Text $ JSS.unfoldr f a
1255−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
12561047{-# INLINE unfoldr #-}
12571048
12581049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1262,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
12621053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
12631054-- to fusion. Performs replacement on invalid scalar values.
12641055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1265−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1266−-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1056+unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
12671057{-# INLINE unfoldrN #-}
12681058
12691059-- -----------------------------------------------------------------------------
@@ -1273,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
12731063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
12741064-- the length of the Text. Subject to fusion.
12751065take :: Int -> Text -> Text
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)
1066+take n t@(Text arr off len)
1067+ | n <= 0 = empty
1068+ | n >= len = t
1069+ | otherwise = text arr off (iterN n t)
12811070{-# INLINE [1] take #-}
12821071
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
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
12891078
12901079{-# RULES
12911080"TEXT take -> fused" [~1] forall n t.
@@ -1299,36 +1088,31 @@ take = coerce JSS.take
12991088--
13001089-- Examples:
13011090--
1302−-- >>> takeEnd 3 "foobar"
1303−-- "bar"
1304−--
1305−-- @since 1.1.1.0
1091+-- > takeEnd 3 "foobar" == "bar"
13061092takeEnd :: Int -> Text -> Text
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
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
13211106
13221107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
13231108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
13241109-- is greater than the length of the 'Text'. Subject to fusion.
13251110drop :: Int -> Text -> Text
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
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
13321116{-# INLINE [1] drop #-}
13331117
13341118{-# RULES
@@ -1343,27 +1127,22 @@ drop = coerce JSS.drop
13431127--
13441128-- Examples:
13451129--
1346−-- >>> dropEnd 3 "foobar"
1347−-- "foo"
1348−--
1349−-- @since 1.1.1.0
1130+-- > dropEnd 3 "foobar" == "foo"
13501131dropEnd :: Int -> Text -> Text
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)
1132+dropEnd n t@(Text arr off len)
1133+ | n <= 0 = t
1134+ | n >= len = empty
1135+ | otherwise = text arr off (iterNEnd n t)
13561136
13571137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
13581138-- returns the longest prefix (possibly empty) of elements that
13591139-- satisfy @p@. Subject to fusion.
13601140takeWhile :: (Char -> Bool) -> Text -> Text
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
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
13671146{-# INLINE [1] takeWhile #-}
13681147
13691148{-# RULES
@@ -1378,25 +1157,13 @@ takeWhile = coerce JSS.takeWhile
13781157-- satisfy @p@. Subject to fusion.
13791158-- Examples:
13801159--
1381−-- >>> takeWhileEnd (=='o') "foo"
1382−-- "oo"
1383−--
1384−-- @since 1.2.2.0
1160+-- > takeWhileEnd (=='o') "foo" == "oo"
13851161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1386−#ifndef __GHCJS__
13871162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
13881163 where loop !i !l | l <= 0 = t
13891164 | p c = loop (i+d) (l+d)
13901165 | otherwise = text arr (off+l) (len-l)
13911166 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
14001167{-# INLINE [1] takeWhileEnd #-}
14011168
14021169{-# RULES
@@ -1409,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
14091176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
14101177-- 'takeWhile' @p@ @t@. Subject to fusion.
14111178dropWhile :: (Char -> Bool) -> Text -> Text
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−
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
14181184{-# INLINE [1] dropWhile #-}
14191185
14201186{-# RULES
@@ -1430,15 +1196,13 @@ dropWhile = coerce JSS.dropWhile
14301196--
14311197-- Examples:
14321198--
1433−-- >>> dropWhileEnd (=='.') "foo..."
1434−-- "foo"
1199+-- > dropWhileEnd (=='.') "foo..." == "foo"
14351200dropWhileEnd :: (Char -> Bool) -> Text -> Text
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
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
14421206{-# INLINE [1] dropWhileEnd #-}
14431207
14441208{-# RULES
@@ -1481,49 +1245,45 @@ strip = dropAround isSpace
14811245-- prefix of @t@ of length @n@, and whose second is the remainder of
14821246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
14831247splitAt :: Int -> Text -> (Text, Text)
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))
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))
14901253
14911254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
14921255-- a pair whose first element is the longest prefix (possibly empty)
14931256-- of @t@ of elements that satisfy @p@, and whose second is the
14941257-- remainder of the list.
14951258span :: (Char -> Bool) -> Text -> (Text, Text)
1496−span = coerce JSS.span
1497−-- span p t = case span_ p t of
1498−-- (# hd,tl #) -> (hd,tl)
1259+span p t = case span_ p t of
1260+ (# hd,tl #) -> (hd,tl)
14991261{-# INLINE span #-}
15001262
15011263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
15021264-- over elements that fail the predicate @p@.
15031265break :: (Char -> Bool) -> Text -> (Text, Text)
1504−break = coerce JSS.break
1505−-- break p = span (not . p)
1266+break p = span (not . p)
15061267{-# INLINE break #-}
15071268
15081269-- | /O(n)/ Group characters in a string according to a predicate.
15091270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
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))
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))
15181278
15191279-- | Returns the /array/ index (in units of 'Word16') at which a
15201280-- character may be found. This is /not/ the same as the logical
15211281-- index returned by e.g. 'findIndex'.
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
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
15271287
15281288-- | /O(n)/ Group characters in a string by equality.
15291289group :: Text -> [Text]
@@ -1532,17 +1292,15 @@ group = groupBy (==)
15321292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
15331293-- first.
15341294inits :: Text -> [Text]
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)
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)
15391298
15401299-- | /O(n)/ Return all final segments of the given 'Text', longest
15411300-- first.
15421301tails :: Text -> [Text]
1543−tails = coerce JSS.tails
1544−-- tails t | null t = [empty]
1545−-- | otherwise = t : tails (unsafeTail t)
1302+tails t | null t = [empty]
1303+ | otherwise = t : tails (unsafeTail t)
15461304
15471305-- $split
15481306--
@@ -1556,14 +1314,9 @@ tails = coerce JSS.tails
15561314--
15571315-- Examples:
15581316--
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−-- ["",""]
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" == ["",""]
15671320--
15681321-- and
15691322--
@@ -1580,14 +1333,13 @@ splitOn :: Text
15801333 -> Text
15811334 -- ^ Input text.
15821335 -> [Text]
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)]
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)]
15911343{-# INLINE [1] splitOn #-}
15921344
15931345{-# RULES
@@ -1600,36 +1352,28 @@ splitOn = coerce JSS.splitOn
16001352-- resulting components do not contain the separators. Two adjacent
16011353-- separators result in an empty component in the output. eg.
16021354--
1603−-- >>> split (=='a') "aabbaca"
1604−-- ["","","bb","c",""]
1605−--
1606−-- >>> split (=='a') ""
1607−-- [""]
1355+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356+-- > split (=='a') "" == [""]
16081357split :: (Char -> Bool) -> Text -> [Text]
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
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
16151363{-# INLINE split #-}
16161364
16171365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
16181366-- element may be shorter than the other chunks, depending on the
16191367-- length of the input. Examples:
16201368--
1621−-- >>> chunksOf 3 "foobarbaz"
1622−-- ["foo","bar","baz"]
1623−--
1624−-- >>> chunksOf 4 "haskell.org"
1625−-- ["hask","ell.","org"]
1369+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
16261371chunksOf :: Int -> Text -> [Text]
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
1372+chunksOf k = go
1373+ where
1374+ go t = case splitAt k t of
1375+ (a,b) | null a -> []
1376+ | otherwise -> a : go b
16331377{-# INLINE chunksOf #-}
16341378
16351379-- ----------------------------------------------------------------------------
@@ -1642,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
16421386-- returns the first element matching the predicate, or 'Nothing' if
16431387-- there is no such element.
16441388find :: (Char -> Bool) -> Text -> Maybe Char
1645−find = coerce JSS.find
1646−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
16471390{-# INLINE find #-}
16481391
16491392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1652,16 +1395,14 @@ find = coerce JSS.find
16521395--
16531396-- > partition p t == (filter p t, filter (not . p) t)
16541397partition :: (Char -> Bool) -> Text -> (Text, Text)
1655−partition = coerce JSS.partition
1656−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
16571399{-# INLINE partition #-}
16581400
16591401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
16601402-- returns a 'Text' containing those characters that satisfy the
16611403-- predicate.
16621404filter :: (Char -> Bool) -> Text -> Text
1663−filter = coerce JSS.filter
1664−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
16651406{-# INLINE filter #-}
16661407
16671408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1671,11 +1412,8 @@ filter = coerce JSS.filter
16711412--
16721413-- Examples:
16731414--
1674−-- >>> breakOn "::" "a::b::c"
1675−-- ("a","::b::c")
1676−--
1677−-- >>> breakOn "/" "foobar"
1678−-- ("foobar","")
1415+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416+-- > breakOn "/" "foobar" ==> ("foobar", "")
16791417--
16801418-- Laws:
16811419--
@@ -1689,12 +1427,11 @@ filter = coerce JSS.filter
16891427-- In (unlikely) bad cases, this function's time complexity degrades
16901428-- towards /O(n*m)/.
16911429breakOn :: Text -> Text -> (Text, Text)
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))
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))
16981435{-# INLINE breakOn #-}
16991436
17001437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1704,12 +1441,10 @@ breakOn = coerce JSS.breakOn
17041441-- up to and including the last match of @needle@. The second is the
17051442-- remainder of @haystack@, following the match.
17061443--
1707−-- >>> breakOnEnd "::" "a::b::c"
1708−-- ("a::b::","c")
1444+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
17091445breakOnEnd :: Text -> Text -> (Text, Text)
1710−breakOnEnd = coerce JSS.breakOnEnd
1711−-- breakOnEnd pat src = (reverse b, reverse a)
1712−-- where (a,b) = breakOn (reverse pat) (reverse src)
1446+breakOnEnd pat src = (reverse b, reverse a)
1447+ where (a,b) = breakOn (reverse pat) (reverse src)
17131448{-# INLINE breakOnEnd #-}
17141449
17151450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1721,11 +1456,10 @@ breakOnEnd = coerce JSS.breakOnEnd
17211456--
17221457-- Examples:
17231458--
1724−-- >>> breakOnAll "::" ""
1725−-- []
1726−--
1727−-- >>> breakOnAll "/" "a/b/c/"
1728−-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
1459+-- > breakOnAll "::" ""
1460+-- > ==> []
1461+-- > breakOnAll "/" "a/b/c/"
1462+-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
17291463--
17301464-- In (unlikely) bad cases, this function's time complexity degrades
17311465-- towards /O(n*m)/.
@@ -1734,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
17341468breakOnAll :: Text -- ^ @needle@ to search for
17351469 -> Text -- ^ @haystack@ in which to search
17361470 -> [(Text, Text)]
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
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
17441477{-# INLINE breakOnAll #-}
17451478
17461479-------------------------------------------------------------------------------
@@ -1764,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
17641497
17651498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
17661499index :: Text -> Int -> Char
1767−index = coerce JSS.index
1768−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
17691501{-# INLINE index #-}
17701502
17711503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
17721504-- and returns the index of the first element in the 'Text' satisfying
17731505-- the predicate. Subject to fusion.
17741506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1775−findIndex = coerce JSS.findIndex
1776−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
17771508{-# INLINE findIndex #-}
17781509
17791510-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1783,11 +1514,10 @@ findIndex = coerce JSS.findIndex
17831514-- In (unlikely) bad cases, this function's time complexity degrades
17841515-- towards /O(n*m)/.
17851516count :: Text -> Text -> Int
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)
1517+count pat src
1518+ | null pat = emptyError "count"
1519+ | isSingleton pat = countChar (unsafeHead pat) src
1520+ | otherwise = L.length (indices pat src)
17911521{-# INLINE [1] count #-}
17921522
17931523{-# RULES
@@ -1809,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
18091539-- excess elements of the longer 'Text' are discarded. This is
18101540-- equivalent to a pair of 'unpack' operations.
18111541zip :: Text -> Text -> [(Char,Char)]
1812−zip = coerce JSS.zip
1813−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
18141543{-# INLINE zip #-}
18151544
18161545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
18171546-- given as the first argument, instead of a tupling function.
18181547-- Performs replacement on invalid scalar values.
18191548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
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)
1549+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550+ where g a b = safe (f a b)
18231551{-# INLINE zipWith #-}
18241552
18251553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
18261554-- representing white space.
18271555words :: Text -> [Text]
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
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
18411568{-# INLINE words #-}
18421569
18431570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
18441571-- newline 'Char's. The resulting strings do not contain newlines.
18451572lines :: Text -> [Text]
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
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
18521578{-# INLINE lines #-}
18531579
18541580{-
@@ -1876,40 +1602,34 @@ lines' ps | null ps = []
18761602-- | /O(n)/ Joins lines, after appending a terminating newline to
18771603-- each.
18781604unlines :: [Text] -> Text
1879−unlines = coerce JSS.unlines
1880−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
18811606{-# INLINE unlines #-}
18821607
18831608-- | /O(n)/ Joins words using single space characters.
18841609unwords :: [Text] -> Text
1885−unwords = coerce JSS.unwords
1886−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
18871611{-# INLINE unwords #-}
18881612
18891613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
18901614-- 'True' iff the first is a prefix of the second. Subject to fusion.
18911615isPrefixOf :: Text -> Text -> Bool
1892−isPrefixOf = coerce JSS.isPrefixOf
1893−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1894−-- alen <= blen && S.isPrefixOf (stream a) (stream b)
1616+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617+ alen <= blen && S.isPrefixOf (stream a) (stream b)
18951618{-# INLINE [1] isPrefixOf #-}
18961619
18971620{-# RULES
18981621"TEXT isPrefixOf -> fused" [~1] forall s t.
18991622 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
19021623 #-}
19031624
19041625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
19051626-- 'True' iff the first is a suffix of the second.
19061627isSuffixOf :: Text -> Text -> Bool
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
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
19131633{-# INLINE isSuffixOf #-}
19141634
19151635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1919,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
19191639-- In (unlikely) bad cases, this function's time complexity degrades
19201640-- towards /O(n*m)/.
19211641isInfixOf :: Text -> Text -> Bool
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
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
19271646{-# INLINE [1] isInfixOf #-}
19281647
19291648{-# RULES
@@ -1939,14 +1658,9 @@ isInfixOf = coerce JSS.isInfixOf
19391658--
19401659-- Examples:
19411660--
1942−-- >>> stripPrefix "foo" "foobar"
1943−-- Just "bar"
1944−--
1945−-- >>> stripPrefix "" "baz"
1946−-- Just "baz"
1947−--
1948−-- >>> stripPrefix "foo" "quux"
1949−-- Nothing
1661+-- > stripPrefix "foo" "foobar" == Just "bar"
1662+-- > stripPrefix "" "baz" == Just "baz"
1663+-- > stripPrefix "foo" "quux" == Nothing
19501664--
19511665-- This is particularly useful with the @ViewPatterns@ extension to
19521666-- GHC, as follows:
@@ -1958,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
19581672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
19591673-- > fnordLength _ = -1
19601674stripPrefix :: Text -> Text -> Maybe Text
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
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
19651678
19661679-- | /O(n)/ Find the longest non-empty common prefix of two strings
19671680-- and return it, along with the suffixes of each string at which they
@@ -1972,39 +1685,28 @@ stripPrefix = coerce JSS.stripPrefix
19721685--
19731686-- Examples:
19741687--
1975−-- >>> commonPrefixes "foobar" "fooquux"
1976−-- Just ("foo","bar","quux")
1977−--
1978−-- >>> commonPrefixes "veeble" "fetzer"
1979−-- Nothing
1980−--
1981−-- >>> commonPrefixes "" "baz"
1982−-- Nothing
1688+-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689+-- > commonPrefixes "veeble" "fetzer" == Nothing
1690+-- > commonPrefixes "" "baz" == Nothing
19831691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1984−commonPrefixes = coerce JSS.commonPrefixes
1985−-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
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
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
19941701
19951702-- | /O(n)/ Return the prefix of the second string if its suffix
19961703-- matches the entire first string.
19971704--
19981705-- Examples:
19991706--
2000−-- >>> stripSuffix "bar" "foobar"
2001−-- Just "foo"
2002−--
2003−-- >>> stripSuffix "" "baz"
2004−-- Just "baz"
2005−--
2006−-- >>> stripSuffix "foo" "quux"
2007−-- Nothing
1707+-- > stripSuffix "bar" "foobar" == Just "foo"
1708+-- > stripSuffix "" "baz" == Just "baz"
1709+-- > stripSuffix "foo" "quux" == Nothing
20081710--
20091711-- This is particularly useful with the @ViewPatterns@ extension to
20101712-- GHC, as follows:
@@ -2016,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
20161718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
20171719-- > quuxLength _ = -1
20181720stripSuffix :: Text -> Text -> Maybe Text
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"
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"
20351736
20361737overflowError :: String -> a
20371738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -2045,24 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
20451746-- copy \"breaks the link\" to the original array, allowing it to be
20461747-- garbage collected if there are no other live references to it.
20471748copy :: Text -> Text
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
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