@@ -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,68 +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−import GHCJS.Prim (JSVal (..), unpackJSString#)
264−
265−unpack :: Text -> String
266−unpack = coerce JSS.unpack
267−{-# INLINE [1] unpack #-}
268−
269−singleton :: Char -> Text
270−singleton = coerce JSS.singleton
271−{-# INLINE [1] singleton #-}
272−
273−{-# RULES
274−"JSSTRING singleton -> fused" [~1] forall a.
275− singleton a = unstream (S.singleton (safe a))
276−"JSSTRING singleton -> unfused" [1] forall a.
277− unstream (S.singleton (safe a)) = singleton a
278− #-}
279−
280−{-# RULES "inlineJSString" forall s. pack (GHC.Base.build (GHC.unpackFoldrCString# s)) = Text (JSString (unsafeDupablePerformIO (unpackJSString# s))) #-}
281−
282−-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
283−unpackCString# :: Addr# -> Text
284−unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
285−{-# NOINLINE unpackCString# #-}
286−
287−{-# RULES "TEXT literal" forall a.
288− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
289− = unpackCString# a #-}
290−
291−{-# RULES "TEXT literal UTF8" [1] forall a.
292− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
293− = unpackCString# a #-}
294−
295−{-# RULES "TEXT empty literal" [1]
296− unstream (S.map safe (S.streamList []))
297− = empty_ #-}
298−
299−{-# RULES "TEXT singleton literal" [1] forall a.
300− unstream (S.map safe (S.streamList [a]))
301− = singleton a #-}
302−
303−---------------------------------------------------------------------
304−-- $character_definition
305−--
306−-- This package uses the term /character/ to denote Unicode /code points/.
307−--
308−-- Note that this is not the same thing as a grapheme (e.g. a
309−-- composition of code points that form one visual symbol). For
310−-- instance, consider the grapheme \"ä\". This symbol has two
311−-- Unicode representations: a single code-point representation
312−-- @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code point),
313−-- and a two code point representation @U+0061@ (the \"@A@\" code
314−-- point) and @U+0308@ (the @COMBINING DIAERESIS@ code point).
315−
316249-- $strict
317250--
318251-- This package provides both strict and lazy 'Text' types. The
@@ -388,31 +321,22 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
388321-- Functions that can be fused by the compiler are documented with the
389322-- phrase \"Subject to fusion\".
390323
391−-- instance Eq Text where
392−-- Text arrA offA lenA == Text arrB offB lenB
393−-- | lenA == lenB = A.equal arrA offA arrB offB lenA
394−-- | otherwise = False
395−-- {-# INLINE (==) #-}
396324instance Eq Text where
397− Text x == Text y = x == y
398−
399−-- instance Ord Text where
400−-- 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 (==) #-}
401329
402330instance Ord Text where
403− compare (Text x) (Text y) = compare x y
331+ compare = compareText
404332
405333instance Read Text where
406334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
407335
408336#if MIN_VERSION_base(4,9,0)
409−-- | Non-orphan 'Semigroup' instance only defined for
410−-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
411−-- provided by
412−-- the [semigroups](http://hackage.haskell.org/package/semigroups)
413−-- package
414−--
415−-- @since 1.2.2.0
337+-- Semigroup orphan instances for older GHCs are provided by
338+-- 'semigroups` package
339+
416340instance Semigroup Text where
417341 (<>) = append
418342#endif
@@ -430,7 +354,6 @@ instance IsString Text where
430354 fromString = pack
431355
432356#if __GLASGOW_HASKELL__ >= 708
433−-- | @since 1.2.0.0
434357instance Exts.IsList Text where
435358 type Item Text = Char
436359 fromList = pack
@@ -441,7 +364,6 @@ instance Exts.IsList Text where
441364instance NFData Text where rnf !_ = ()
442365#endif
443366
444−-- | @since 1.2.1.0
445367instance Binary Text where
446368 put t = put (encodeUtf8 t)
447369 get = do
@@ -475,10 +397,8 @@ instance Data Text where
475397
476398#if MIN_VERSION_base(4,7,0)
477399-- | Only defined for @base-4.7.0.0@ and later
478−--
479−-- @since 1.2.2.0
480400instance PrintfArg Text where
481− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
482402#endif
483403
484404packConstr :: Constr
@@ -487,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
487407textDataType :: DataType
488408textDataType = mkDataType "Data.Text.Text" [packConstr]
489409
490−-- -- | /O(n)/ Compare two 'Text' values lexicographically.
491−-- compareText :: Text -> Text -> Ordering
492−-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
493−-- | lenA == 0 && lenB == 0 = EQ
494−-- | otherwise = go 0 0
495−-- where
496−-- go !i !j
497−-- | i >= lenA || j >= lenB = compare lenA lenB
498−-- | a < b = LT
499−-- | a > b = GT
500−-- | otherwise = go (i+di) (j+dj)
501−-- where Iter a di = iter ta i
502−-- Iter b dj = iter tb j
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
503423
504424-- -----------------------------------------------------------------------------
505425-- * Conversion to/from 'Text'
@@ -507,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
507427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
508428-- fusion. Performs replacement on invalid scalar values.
509429pack :: String -> Text
510−pack = coerce JSS.pack
511−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
512431{-# INLINE [1] pack #-}
513432
514433-- -----------------------------------------------------------------------------
@@ -519,8 +438,7 @@ pack = coerce JSS.pack
519438-- copying a new array. Subject to fusion. Performs replacement on
520439-- invalid scalar values.
521440cons :: Char -> Text -> Text
522−cons = coerce JSS.cons
523−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
524442{-# INLINE cons #-}
525443
526444infixr 5 `cons`
@@ -529,27 +447,25 @@ infixr 5 `cons`
529447-- entire array in the process, unless fused. Subject to fusion.
530448-- Performs replacement on invalid scalar values.
531449snoc :: Text -> Char -> Text
532−snoc = coerce JSS.snoc
533−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
534451{-# INLINE snoc #-}
535452
536453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
537454-- into a new 'Text'. Subject to fusion.
538455append :: Text -> Text -> Text
539−append = coerce JSS.append
540−-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
541−-- | len1 == 0 = b
542−-- | len2 == 0 = a
543−-- | len > 0 = Text (A.run x) 0 len
544−-- | otherwise = overflowError "append"
545−-- where
546−-- len = len1+len2
547−-- x :: ST s (A.MArray s)
548−-- x = do
549−-- arr <- A.new len
550−-- A.copyI arr 0 arr1 off1 len1
551−-- A.copyI arr len1 arr2 off2 len
552−-- return arr
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
553469{-# NOINLINE append #-}
554470
555471{-# RULES
@@ -562,18 +478,16 @@ append = coerce JSS.append
562478-- | /O(1)/ Returns the first character of a 'Text', which must be
563479-- non-empty. Subject to fusion.
564480head :: Text -> Char
565−head = coerce JSS.head
566−-- head t = S.head (stream t)
481+head t = S.head (stream t)
567482{-# INLINE head #-}
568483
569484-- | /O(1)/ Returns the first character and rest of a 'Text', or
570485-- 'Nothing' if empty. Subject to fusion.
571486uncons :: Text -> Maybe (Char, Text)
572−uncons = coerce JSS.uncons
573−-- uncons t@(Text arr off len)
574−-- | len <= 0 = Nothing
575−-- | otherwise = Just $ let !(Iter c d) = iter t 0
576−-- in (c, text arr (off+d) (len-d))
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))
577491{-# INLINE [1] uncons #-}
578492
579493-- | Lifted from Control.Arrow and specialized.
@@ -583,13 +497,12 @@ second f (a, b) = (a, f b)
583497-- | /O(1)/ Returns the last character of a 'Text', which must be
584498-- non-empty. Subject to fusion.
585499last :: Text -> Char
586−last = coerce JSS.last
587−-- last (Text arr off len)
588−-- | len <= 0 = emptyError "last"
589−-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
590−-- | otherwise = U16.chr2 n0 n
591−-- where n = A.unsafeIndex arr (off+len-1)
592−-- n0 = A.unsafeIndex arr (off+len-2)
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)
593506{-# INLINE [1] last #-}
594507
595508{-# RULES
@@ -602,11 +515,10 @@ last = coerce JSS.last
602515-- | /O(1)/ Returns all characters after the head of a 'Text', which
603516-- must be non-empty. Subject to fusion.
604517tail :: Text -> Text
605−tail = coerce JSS.tail
606−-- tail t@(Text arr off len)
607−-- | len <= 0 = emptyError "tail"
608−-- | otherwise = text arr (off+d) (len-d)
609−-- where d = iter_ t 0
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
610522{-# INLINE [1] tail #-}
611523
612524{-# RULES
@@ -619,12 +531,11 @@ tail = coerce JSS.tail
619531-- | /O(1)/ Returns all but the last character of a 'Text', which must
620532-- be non-empty. Subject to fusion.
621533init :: Text -> Text
622−init = coerce JSS.init
623−-- init (Text arr off len) | len <= 0 = emptyError "init"
624−-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
625−-- | otherwise = text arr off (len-1)
626−-- where
627−-- n = A.unsafeIndex arr (off+len-1)
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)
628539{-# INLINE [1] init #-}
629540
630541{-# RULES
@@ -634,28 +545,14 @@ init = coerce JSS.init
634545 unstream (S.init (stream t)) = init t
635546 #-}
636547
637−-- | /O(1)/ Returns all but the last character and the last character of a
638−-- 'Text', or 'Nothing' if empty.
639−--
640−-- @since 1.2.3.0
641−unsnoc :: Text -> Maybe (Text, Char)
642−unsnoc (Text arr off len)
643− | len <= 0 = Nothing
644− | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
645− | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
646− where n = A.unsafeIndex arr (off+len-1)
647− n0 = A.unsafeIndex arr (off+len-2)
648−{-# INLINE [1] unsnoc #-}
649−
650548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
651549-- fusion.
652550null :: Text -> Bool
653−null = coerce JSS.null
654−-- null (Text _arr _off len) =
655−-- #if defined(ASSERTS)
656−-- assert (len >= 0) $
657−-- #endif
658−-- len <= 0
551+null (Text _arr _off len) =
552+#if defined(ASSERTS)
553+ assert (len >= 0) $
554+#endif
555+ len <= 0
659556{-# INLINE [1] null #-}
660557
661558{-# RULES
@@ -667,15 +564,14 @@ null = coerce JSS.null
667564
668565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
669566-- Subject to fusion.
670−-- isSingleton :: Text -> Bool
671−-- isSingleton = S.isSingleton . stream
672−-- {-# INLINE isSingleton #-}
567+isSingleton :: Text -> Bool
568+isSingleton = S.isSingleton . stream
569+{-# INLINE isSingleton #-}
673570
674571-- | /O(n)/ Returns the number of characters in a 'Text'.
675572-- Subject to fusion.
676573length :: Text -> Int
677−length = coerce JSS.length
678−-- length t = S.length (stream t)
574+length t = S.length (stream t)
679575{-# INLINE [0] length #-}
680576-- length needs to be phased after the compareN/length rules otherwise
681577-- it may inline before the rules have an opportunity to fire.
@@ -728,58 +624,29 @@ compareLength t n = S.compareLengthI (stream t) n
728624-- -----------------------------------------------------------------------------
729625-- * Transformations
730626-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
731−-- each element of @t@.
732−--
733−-- Example:
734−--
735−-- >>> let message = pack "I am not angry. Not at all."
736−-- >>> T.map (\c -> if c == '.' then '!' else c) message
737−-- "I am not angry! Not at all!"
738−--
739−-- Subject to fusion. Performs replacement on invalid scalar values.
627+-- each element of @t@. Subject to fusion. Performs replacement on
628+-- invalid scalar values.
740629map :: (Char -> Char) -> Text -> Text
741−map = coerce JSS.map
742−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
743631{-# INLINE [1] map #-}
744632
745633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
746634-- 'Text's and concatenates the list after interspersing the first
747635-- argument between each element of the list.
748−--
749−-- Example:
750−--
751−-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
752−-- "WeNI!seekNI!theNI!HolyNI!Grail"
753636intercalate :: Text -> [Text] -> Text
754−intercalate = coerce JSS.intercalate
755−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
756638{-# INLINE intercalate #-}
757639
758640-- | /O(n)/ The 'intersperse' function takes a character and places it
759−-- between the characters of a 'Text'.
760−--
761−-- Example:
762−--
763−-- >>> T.intersperse '.' "SHIELD"
764−-- "S.H.I.E.L.D"
765−--
766−-- 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.
767643intersperse :: Char -> Text -> Text
768−intersperse = coerce JSS.intersperse
769−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
770645{-# INLINE intersperse #-}
771646
772−-- | /O(n)/ Reverse the characters of a string.
773−--
774−-- Example:
775−--
776−-- >>> T.reverse "desrever"
777−-- "reversed"
778−--
779−-- Subject to fusion.
647+-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
780648reverse :: Text -> Text
781−reverse = coerce JSS.reverse
782−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
783650{-# INLINE reverse #-}
784651
785652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -796,14 +663,12 @@ reverse = coerce JSS.reverse
796663-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
797664-- be replaced recursively:
798665--
799−-- >>> replace "oo" "foo" "oo"
800−-- "foo"
666+-- > replace "oo" "foo" "oo" == "foo"
801667--
802668-- In cases where several instances of @needle@ overlap, only the
803669-- first one will be replaced:
804670--
805−-- >>> replace "ofo" "bar" "ofofo"
806−-- "barfo"
671+-- > replace "ofo" "bar" "ofofo" == "barfo"
807672--
808673-- In (unlikely) bad cases, this function's time complexity degrades
809674-- towards /O(n*m)/.
@@ -815,29 +680,28 @@ replace :: Text
815680 -> Text
816681 -- ^ @haystack@ in which to search.
817682 -> Text
818−replace = coerce JSS.replace
819−-- replace needle@(Text _ _ neeLen)
820−-- (Text repArr repOff repLen)
821−-- haystack@(Text hayArr hayOff hayLen)
822−-- | neeLen == 0 = emptyError "replace"
823−-- | L.null ixs = haystack
824−-- | len > 0 = Text (A.run x) 0 len
825−-- | otherwise = empty
826−-- where
827−-- ixs = indices needle haystack
828−-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
829−-- x :: ST s (A.MArray s)
830−-- x = do
831−-- marr <- A.new len
832−-- let loop (i:is) o d = do
833−-- let d0 = d + i - o
834−-- d1 = d0 + repLen
835−-- A.copyI marr d hayArr (hayOff+o) d0
836−-- A.copyI marr d0 repArr repOff d1
837−-- loop is (i + neeLen) d1
838−-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
839−-- loop ixs 0 0
840−-- 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
841705
842706-- ----------------------------------------------------------------------------
843707-- ** Case conversions (folds)
@@ -876,8 +740,7 @@ replace = coerce JSS.replace
876740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
877741-- instead of itself.
878742toCaseFold :: Text -> Text
879−-- toCaseFold t = unstream (S.toCaseFold (stream t))
880−toCaseFold = coerce JSS.toCaseFold
743+toCaseFold t = unstream (S.toCaseFold (stream t))
881744{-# INLINE toCaseFold #-}
882745
883746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -888,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
888751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
889752-- followed by \" ̇\" (combining dot above, U+0307).
890753toLower :: Text -> Text
891−-- toLower t = unstream (S.toLower (stream t))
892−toLower = coerce JSS.toLower
754+toLower t = unstream (S.toLower (stream t))
893755{-# INLINE toLower #-}
894756
895757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -899,8 +761,7 @@ toLower = coerce JSS.toLower
899761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
900762-- two-letter sequence \"SS\".
901763toUpper :: Text -> Text
902−-- toUpper t = unstream (S.toUpper (stream t))
903−toUpper = coerce JSS.toUpper
764+toUpper t = unstream (S.toUpper (stream t))
904765{-# INLINE toUpper #-}
905766
906767-- | /O(n)/ Convert a string to title case, using simple case
@@ -921,11 +782,8 @@ toUpper = coerce JSS.toUpper
921782-- guides disagree on whether the book name \"The Hill of the Red
922783-- Fox\" is correctly title cased—but this function will
923784-- capitalize /every/ word.
924−--
925−-- @since 1.0.0.0
926785toTitle :: Text -> Text
927−toTitle = coerce JSS.toTitle
928−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
929787{-# INLINE toTitle #-}
930788
931789-- | /O(n)/ Left-justify a string to the given length, using the
@@ -934,17 +792,13 @@ toTitle = coerce JSS.toTitle
934792--
935793-- Examples:
936794--
937−-- >>> justifyLeft 7 'x' "foo"
938−-- "fooxxxx"
939−--
940−-- >>> justifyLeft 3 'x' "foobar"
941−-- "foobar"
795+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796+-- > justifyLeft 3 'x' "foobar" == "foobar"
942797justifyLeft :: Int -> Char -> Text -> Text
943−justifyLeft = coerce JSS.justifyLeft
944−-- justifyLeft k c t
945−-- | len >= k = t
946−-- | otherwise = t `append` replicateChar (k-len) c
947−-- 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
948802{-# INLINE [1] justifyLeft #-}
949803
950804{-# RULES
@@ -960,17 +814,13 @@ justifyLeft = coerce JSS.justifyLeft
960814--
961815-- Examples:
962816--
963−-- >>> justifyRight 7 'x' "bar"
964−-- "xxxxbar"
965−--
966−-- >>> justifyRight 3 'x' "foobar"
967−-- "foobar"
817+-- > justifyRight 7 'x' "bar" == "xxxxbar"
818+-- > justifyRight 3 'x' "foobar" == "foobar"
968819justifyRight :: Int -> Char -> Text -> Text
969−justifyRight = coerce JSS.justifyRight
970−-- justifyRight k c t
971−-- | len >= k = t
972−-- | otherwise = replicateChar (k-len) c `append` t
973−-- 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
974824{-# INLINE justifyRight #-}
975825
976826-- | /O(n)/ Center a string to the given length, using the specified
@@ -979,34 +829,23 @@ justifyRight = coerce JSS.justifyRight
979829--
980830-- Examples:
981831--
982−-- >>> center 8 'x' "HS"
983−-- "xxxHSxxx"
832+-- > center 8 'x' "HS" = "xxxHSxxx"
984833center :: Int -> Char -> Text -> Text
985−center = coerce JSS.center
986−-- center k c t
987−-- | len >= k = t
988−-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
989−-- where len = length t
990−-- d = k - len
991−-- r = d `quot` 2
992−-- 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
993841{-# INLINE center #-}
994842
995843-- | /O(n)/ The 'transpose' function transposes the rows and columns
996844-- of its 'Text' argument. Note that this function uses 'pack',
997845-- 'unpack', and the list version of transpose, and is thus not very
998846-- efficient.
999−--
1000−-- Examples:
1001−--
1002−-- >>> transpose ["green","orange"]
1003−-- ["go","rr","ea","en","ng","e"]
1004−--
1005−-- >>> transpose ["blue","red"]
1006−-- ["br","le","ud","e"]
1007847transpose :: [Text] -> [Text]
1008−transpose = coerce JSS.transpose
1009−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
1010849
1011850-- -----------------------------------------------------------------------------
1012851-- * Reducing 'Text's (folds)
@@ -1016,27 +855,23 @@ transpose = coerce JSS.transpose
1016855-- reduces the 'Text' using the binary operator, from left to right.
1017856-- Subject to fusion.
1018857foldl :: (a -> Char -> a) -> a -> Text -> a
1019−foldl f a0 (Text t) = JSS.foldl f a0 t
1020−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
1021859{-# INLINE foldl #-}
1022860
1023861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
1024862foldl' :: (a -> Char -> a) -> a -> Text -> a
1025−foldl' f a0 (Text t) = JSS.foldl' f a0 t
1026−-- foldl' f z t = S.foldl' f z (stream t)
863+foldl' f z t = S.foldl' f z (stream t)
1027864{-# INLINE foldl' #-}
1028865
1029866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
1030867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
1031868foldl1 :: (Char -> Char -> Char) -> Text -> Char
1032−foldl1 = coerce JSS.foldl1
1033−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
1034870{-# INLINE foldl1 #-}
1035871
1036872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
1037873foldl1' :: (Char -> Char -> Char) -> Text -> Char
1038−foldl1' = coerce JSS.foldl1'
1039−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
1040875{-# INLINE foldl1' #-}
1041876
1042877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -1044,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
1044879-- reduces the 'Text' using the binary operator, from right to left.
1045880-- Subject to fusion.
1046881foldr :: (Char -> a -> a) -> a -> Text -> a
1047−foldr f a0 (Text t) = JSS.foldr f a0 t
1048−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
1049883{-# INLINE foldr #-}
1050884
1051885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
1052886-- and thus must be applied to a non-empty 'Text'. Subject to
1053887-- fusion.
1054888foldr1 :: (Char -> Char -> Char) -> Text -> Char
1055−foldr1 = coerce JSS.foldr1
1056−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
1057890{-# INLINE foldr1 #-}
1058891
1059892-- -----------------------------------------------------------------------------
@@ -1061,54 +894,48 @@ foldr1 = coerce JSS.foldr1
1061894
1062895-- | /O(n)/ Concatenate a list of 'Text's.
1063896concat :: [Text] -> Text
1064−concat = coerce JSS.concat
1065−-- concat ts = case ts' of
1066−-- [] -> empty
1067−-- [t] -> t
1068−-- _ -> Text (A.run go) 0 len
1069−-- where
1070−-- ts' = L.filter (not . null) ts
1071−-- len = sumP "concat" $ L.map lengthWord16 ts'
1072−-- go :: ST s (A.MArray s)
1073−-- go = do
1074−-- arr <- A.new len
1075−-- let step i (Text a o l) =
1076−-- let !j = i + l in A.copyI arr i a o j >> return j
1077−-- 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
1078910
1079911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
1080912-- concatenate the results.
1081913concatMap :: (Char -> Text) -> Text -> Text
1082−concatMap = coerce JSS.concatMap
1083−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
1084915{-# INLINE concatMap #-}
1085916
1086917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1087918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1088919any :: (Char -> Bool) -> Text -> Bool
1089−any = coerce JSS.any
1090−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1091921{-# INLINE any #-}
1092922
1093923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1094924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1095925all :: (Char -> Bool) -> Text -> Bool
1096−all = coerce JSS.all
1097−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1098927{-# INLINE all #-}
1099928
1100929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1101930-- must be non-empty. Subject to fusion.
1102931maximum :: Text -> Char
1103−maximum = coerce JSS.maximum
1104−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1105933{-# INLINE maximum #-}
1106934
1107935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1108936-- must be non-empty. Subject to fusion.
1109937minimum :: Text -> Char
1110−minimum = coerce JSS.minimum
1111−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1112939{-# INLINE minimum #-}
1113940
1114941-- -----------------------------------------------------------------------------
@@ -1124,9 +951,8 @@ minimum = coerce JSS.minimum
1124951--
1125952-- > last (scanl f z xs) == foldl f z xs.
1126953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1127−scanl = coerce JSS.scanl
1128−-- scanl f z t = unstream (S.scanl g z (stream t))
1129−-- 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)
1130956{-# INLINE scanl #-}
1131957
1132958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -1135,9 +961,8 @@ scanl = coerce JSS.scanl
1135961--
1136962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1137963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1138−scanl1 = coerce JSS.scanl1
1139−-- scanl1 f t | null t = empty
1140−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
964+scanl1 f t | null t = empty
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1141966{-# INLINE scanl1 #-}
1142967
1143968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -1145,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1145970--
1146971-- > scanr f v == reverse . scanl (flip f) v . reverse
1147972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1148−scanr = coerce JSS.scanr
1149−-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1150−-- 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)
1151975{-# INLINE scanr #-}
1152976
1153977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1154978-- value argument. Subject to fusion. Performs replacement on
1155979-- invalid scalar values.
1156980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1157−scanr1 = coerce JSS.scanr1
1158−-- scanr1 f t | null t = empty
1159−-- | otherwise = scanr f (last t) (init t)
981+scanr1 f t | null t = empty
982+ | otherwise = scanr f (last t) (init t)
1160983{-# INLINE scanr1 #-}
1161984
1162985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1164,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1164987-- parameter from left to right, and returns a final 'Text'. Performs
1165988-- replacement on invalid scalar values.
1166989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1167−mapAccumL f z0 (Text a) =
1168− let (a', t) = JSS.mapAccumL f z0 a
1169− in (a', Text t)
1170−-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1171−-- 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)
1172992{-# INLINE mapAccumL #-}
1173993
1174994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1178,11 +998,8 @@ mapAccumL f z0 (Text a) =
1178998-- 'Text'.
1179999-- Performs replacement on invalid scalar values.
11801000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1181−mapAccumR f z0 (Text a) =
1182− let (a', t) = JSS.mapAccumR f z0 a
1183− in (a', Text t)
1184−-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1185−-- 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)
11861003{-# INLINE mapAccumR #-}
11871004
11881005-- -----------------------------------------------------------------------------
@@ -1191,21 +1008,20 @@ mapAccumR f z0 (Text a) =
11911008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
11921009-- @t@ repeated @n@ times.
11931010replicate :: Int -> Text -> Text
1194−replicate = coerce JSS.replicate
1195−-- replicate n t@(Text a o l)
1196−-- | n <= 0 || l <= 0 = empty
1197−-- | n == 1 = t
1198−-- | isSingleton t = replicateChar n (unsafeHead t)
1199−-- | otherwise = Text (A.run x) 0 len
1200−-- where
1201−-- len = l `mul` n
1202−-- x :: ST s (A.MArray s)
1203−-- x = do
1204−-- arr <- A.new len
1205−-- let loop !d !i | i >= n = return arr
1206−-- | otherwise = let m = d + l
1207−-- in A.copyI arr d a o m >> loop m (i+1)
1208−-- 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
12091025{-# INLINE [1] replicate #-}
12101026
12111027{-# RULES
@@ -1216,19 +1032,8 @@ replicate = coerce JSS.replicate
12161032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
12171033-- value of every element. Subject to fusion.
12181034replicateChar :: Int -> Char -> Text
1219−#ifndef __GHCJS__
12201035replicateChar n c = unstream (S.replicateCharI n (safe c))
12211036{-# INLINE replicateChar #-}
1222−#else
1223−replicateChar n c = Text $ js_replicateChar n c
1224−{-# INLINE [1] replicateChar #-}
1225−{-# RULES
1226−"JSSTRING replicateChar -> fused" [~1] forall n c.
1227− replicateChar n c = unstream (S.replicateCharI n (safe c))
1228−"JSSTRING replicateChar -> unfused" [1] forall n c.
1229− unstream (S.replicateCharI n (safe c)) = replicateChar n c
1230− #-}
1231−#endif
12321037
12331038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
12341039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1238,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
12381043-- string, and @b@ is the seed value for further production. Subject
12391044-- to fusion. Performs replacement on invalid scalar values.
12401045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1241−unfoldr f a = Text $ JSS.unfoldr f a
1242−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
12431047{-# INLINE unfoldr #-}
12441048
12451049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1249,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
12491053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
12501054-- to fusion. Performs replacement on invalid scalar values.
12511055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1252−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1253−-- 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)
12541057{-# INLINE unfoldrN #-}
12551058
12561059-- -----------------------------------------------------------------------------
@@ -1260,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
12601063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
12611064-- the length of the Text. Subject to fusion.
12621065take :: Int -> Text -> Text
1263−take = coerce JSS.take
1264−-- take n t@(Text arr off len)
1265−-- | n <= 0 = empty
1266−-- | n >= len = t
1267−-- | 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)
12681070{-# INLINE [1] take #-}
12691071
1270−-- iterN :: Int -> Text -> Int
1271−-- iterN n t@(Text _arr _off len) = loop 0 0
1272−-- where loop !i !cnt
1273−-- | i >= len || cnt >= n = i
1274−-- | otherwise = loop (i+d) (cnt+1)
1275−-- 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
12761078
12771079{-# RULES
12781080"TEXT take -> fused" [~1] forall n t.
@@ -1286,36 +1088,31 @@ take = coerce JSS.take
12861088--
12871089-- Examples:
12881090--
1289−-- >>> takeEnd 3 "foobar"
1290−-- "bar"
1291−--
1292−-- @since 1.1.1.0
1091+-- > takeEnd 3 "foobar" == "bar"
12931092takeEnd :: Int -> Text -> Text
1294−takeEnd = coerce JSS.takeEnd
1295−-- takeEnd n t@(Text arr off len)
1296−-- | n <= 0 = empty
1297−-- | n >= len = t
1298−-- | otherwise = text arr (off+i) (len-i)
1299−-- where i = iterNEnd n t
1300−
1301−-- iterNEnd :: Int -> Text -> Int
1302−-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1303−-- where loop i !m
1304−-- | i <= 0 = 0
1305−-- | m <= 1 = i
1306−-- | otherwise = loop (i+d) (m-1)
1307−-- 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
13081106
13091107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
13101108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
13111109-- is greater than the length of the 'Text'. Subject to fusion.
13121110drop :: Int -> Text -> Text
1313−drop = coerce JSS.drop
1314−-- drop n t@(Text arr off len)
1315−-- | n <= 0 = t
1316−-- | n >= len = empty
1317−-- | otherwise = text arr (off+i) (len-i)
1318−-- 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
13191116{-# INLINE [1] drop #-}
13201117
13211118{-# RULES
@@ -1330,27 +1127,22 @@ drop = coerce JSS.drop
13301127--
13311128-- Examples:
13321129--
1333−-- >>> dropEnd 3 "foobar"
1334−-- "foo"
1335−--
1336−-- @since 1.1.1.0
1130+-- > dropEnd 3 "foobar" == "foo"
13371131dropEnd :: Int -> Text -> Text
1338−dropEnd = coerce JSS.dropEnd
1339−-- dropEnd n t@(Text arr off len)
1340−-- | n <= 0 = t
1341−-- | n >= len = empty
1342−-- | 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)
13431136
13441137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
13451138-- returns the longest prefix (possibly empty) of elements that
13461139-- satisfy @p@. Subject to fusion.
13471140takeWhile :: (Char -> Bool) -> Text -> Text
1348−takeWhile = coerce JSS.takeWhile
1349−-- takeWhile p t@(Text arr off len) = loop 0
1350−-- where loop !i | i >= len = t
1351−-- | p c = loop (i+d)
1352−-- | otherwise = text arr off i
1353−-- 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
13541146{-# INLINE [1] takeWhile #-}
13551147
13561148{-# RULES
@@ -1365,25 +1157,13 @@ takeWhile = coerce JSS.takeWhile
13651157-- satisfy @p@. Subject to fusion.
13661158-- Examples:
13671159--
1368−-- >>> takeWhileEnd (=='o') "foo"
1369−-- "oo"
1370−--
1371−-- @since 1.2.2.0
1160+-- > takeWhileEnd (=='o') "foo" == "oo"
13721161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1373−#ifndef __GHCJS__
13741162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
13751163 where loop !i !l | l <= 0 = t
13761164 | p c = loop (i+d) (l+d)
13771165 | otherwise = text arr (off+l) (len-l)
13781166 where (c,d) = reverseIter t i
1379−#else
1380−takeWhileEnd p t@(Text jst) = loop (len-1) len
1381− where loop !i !l | l <= 0 = t
1382− | p c = loop (i+d) (l+d)
1383− | otherwise = Text $ js_substr1 l jst
1384− where (c,d) = reverseIter t i
1385− len = length t
1386−#endif
13871167{-# INLINE [1] takeWhileEnd #-}
13881168
13891169{-# RULES
@@ -1396,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
13961176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
13971177-- 'takeWhile' @p@ @t@. Subject to fusion.
13981178dropWhile :: (Char -> Bool) -> Text -> Text
1399−dropWhile = coerce JSS.dropWhile
1400−-- dropWhile p t@(Text arr off len) = loop 0 0
1401−-- where loop !i !l | l >= len = empty
1402−-- | p c = loop (i+d) (l+d)
1403−-- | otherwise = Text arr (off+i) (len-l)
1404−
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
14051184{-# INLINE [1] dropWhile #-}
14061185
14071186{-# RULES
@@ -1417,15 +1196,13 @@ dropWhile = coerce JSS.dropWhile
14171196--
14181197-- Examples:
14191198--
1420−-- >>> dropWhileEnd (=='.') "foo..."
1421−-- "foo"
1199+-- > dropWhileEnd (=='.') "foo..." == "foo"
14221200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1423−dropWhileEnd = coerce JSS.dropWhileEnd
1424−-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1425−-- where loop !i !l | l <= 0 = empty
1426−-- | p c = loop (i+d) (l+d)
1427−-- | otherwise = Text arr off l
1428−-- 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
14291206{-# INLINE [1] dropWhileEnd #-}
14301207
14311208{-# RULES
@@ -1468,49 +1245,45 @@ strip = dropAround isSpace
14681245-- prefix of @t@ of length @n@, and whose second is the remainder of
14691246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
14701247splitAt :: Int -> Text -> (Text, Text)
1471−splitAt = coerce JSS.splitAt
1472−-- splitAt n t@(Text arr off len)
1473−-- | n <= 0 = (empty, t)
1474−-- | n >= len = (t, empty)
1475−-- | otherwise = let k = iterN n t
1476−-- 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))
14771253
14781254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
14791255-- a pair whose first element is the longest prefix (possibly empty)
14801256-- of @t@ of elements that satisfy @p@, and whose second is the
14811257-- remainder of the list.
14821258span :: (Char -> Bool) -> Text -> (Text, Text)
1483−span = coerce JSS.span
1484−-- span p t = case span_ p t of
1485−-- (# hd,tl #) -> (hd,tl)
1259+span p t = case span_ p t of
1260+ (# hd,tl #) -> (hd,tl)
14861261{-# INLINE span #-}
14871262
14881263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
14891264-- over elements that fail the predicate @p@.
14901265break :: (Char -> Bool) -> Text -> (Text, Text)
1491−break = coerce JSS.break
1492−-- break p = span (not . p)
1266+break p = span (not . p)
14931267{-# INLINE break #-}
14941268
14951269-- | /O(n)/ Group characters in a string according to a predicate.
14961270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1497−groupBy = coerce JSS.groupBy
1498−-- groupBy p = loop
1499−-- where
1500−-- loop t@(Text arr off len)
1501−-- | null t = []
1502−-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1503−-- where Iter c d = iter t 0
1504−-- 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))
15051278
15061279-- | Returns the /array/ index (in units of 'Word16') at which a
15071280-- character may be found. This is /not/ the same as the logical
15081281-- index returned by e.g. 'findIndex'.
1509−-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1510−-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1511−-- where go !i | i >= len || q c = i
1512−-- | otherwise = go (i+d)
1513−-- 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
15141287
15151288-- | /O(n)/ Group characters in a string by equality.
15161289group :: Text -> [Text]
@@ -1519,17 +1292,15 @@ group = groupBy (==)
15191292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
15201293-- first.
15211294inits :: Text -> [Text]
1522−inits = coerce JSS.inits
1523−-- inits t@(Text arr off len) = loop 0
1524−-- where loop i | i >= len = [t]
1525−-- | 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)
15261298
15271299-- | /O(n)/ Return all final segments of the given 'Text', longest
15281300-- first.
15291301tails :: Text -> [Text]
1530−tails = coerce JSS.tails
1531−-- tails t | null t = [empty]
1532−-- | otherwise = t : tails (unsafeTail t)
1302+tails t | null t = [empty]
1303+ | otherwise = t : tails (unsafeTail t)
15331304
15341305-- $split
15351306--
@@ -1543,14 +1314,9 @@ tails = coerce JSS.tails
15431314--
15441315-- Examples:
15451316--
1546−-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1547−-- ["a","b","d","e"]
1548−--
1549−-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1550−-- ["","X","X","X",""]
1551−--
1552−-- >>> splitOn "x" "x"
1553−-- ["",""]
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" == ["",""]
15541320--
15551321-- and
15561322--
@@ -1567,14 +1333,13 @@ splitOn :: Text
15671333 -> Text
15681334 -- ^ Input text.
15691335 -> [Text]
1570−splitOn = coerce JSS.splitOn
1571−-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1572−-- | l <= 0 = emptyError "splitOn"
1573−-- | isSingleton pat = split (== unsafeHead pat) src
1574−-- | otherwise = go 0 (indices pat src)
1575−-- where
1576−-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1577−-- 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)]
15781343{-# INLINE [1] splitOn #-}
15791344
15801345{-# RULES
@@ -1587,36 +1352,28 @@ splitOn = coerce JSS.splitOn
15871352-- resulting components do not contain the separators. Two adjacent
15881353-- separators result in an empty component in the output. eg.
15891354--
1590−-- >>> split (=='a') "aabbaca"
1591−-- ["","","bb","c",""]
1592−--
1593−-- >>> split (=='a') ""
1594−-- [""]
1355+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356+-- > split (=='a') "" == [""]
15951357split :: (Char -> Bool) -> Text -> [Text]
1596−split = coerce JSS.split
1597−-- split _ t@(Text _off _arr 0) = [t]
1598−-- split p t = loop t
1599−-- where loop s | null s' = [l]
1600−-- | otherwise = l : loop (unsafeTail s')
1601−-- 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
16021363{-# INLINE split #-}
16031364
16041365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
16051366-- element may be shorter than the other chunks, depending on the
16061367-- length of the input. Examples:
16071368--
1608−-- >>> chunksOf 3 "foobarbaz"
1609−-- ["foo","bar","baz"]
1610−--
1611−-- >>> chunksOf 4 "haskell.org"
1612−-- ["hask","ell.","org"]
1369+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
16131371chunksOf :: Int -> Text -> [Text]
1614−chunksOf = coerce JSS.chunksOf
1615−-- chunksOf k = go
1616−-- where
1617−-- go t = case splitAt k t of
1618−-- (a,b) | null a -> []
1619−-- | 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
16201377{-# INLINE chunksOf #-}
16211378
16221379-- ----------------------------------------------------------------------------
@@ -1629,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
16291386-- returns the first element matching the predicate, or 'Nothing' if
16301387-- there is no such element.
16311388find :: (Char -> Bool) -> Text -> Maybe Char
1632−find = coerce JSS.find
1633−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
16341390{-# INLINE find #-}
16351391
16361392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1639,16 +1395,14 @@ find = coerce JSS.find
16391395--
16401396-- > partition p t == (filter p t, filter (not . p) t)
16411397partition :: (Char -> Bool) -> Text -> (Text, Text)
1642−partition = coerce JSS.partition
1643−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
16441399{-# INLINE partition #-}
16451400
16461401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
16471402-- returns a 'Text' containing those characters that satisfy the
16481403-- predicate.
16491404filter :: (Char -> Bool) -> Text -> Text
1650−filter = coerce JSS.filter
1651−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
16521406{-# INLINE filter #-}
16531407
16541408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1658,11 +1412,8 @@ filter = coerce JSS.filter
16581412--
16591413-- Examples:
16601414--
1661−-- >>> breakOn "::" "a::b::c"
1662−-- ("a","::b::c")
1663−--
1664−-- >>> breakOn "/" "foobar"
1665−-- ("foobar","")
1415+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416+-- > breakOn "/" "foobar" ==> ("foobar", "")
16661417--
16671418-- Laws:
16681419--
@@ -1676,12 +1427,11 @@ filter = coerce JSS.filter
16761427-- In (unlikely) bad cases, this function's time complexity degrades
16771428-- towards /O(n*m)/.
16781429breakOn :: Text -> Text -> (Text, Text)
1679−breakOn = coerce JSS.breakOn
1680−-- breakOn pat src@(Text arr off len)
1681−-- | null pat = emptyError "breakOn"
1682−-- | otherwise = case indices pat src of
1683−-- [] -> (src, empty)
1684−-- (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))
16851435{-# INLINE breakOn #-}
16861436
16871437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1691,12 +1441,10 @@ breakOn = coerce JSS.breakOn
16911441-- up to and including the last match of @needle@. The second is the
16921442-- remainder of @haystack@, following the match.
16931443--
1694−-- >>> breakOnEnd "::" "a::b::c"
1695−-- ("a::b::","c")
1444+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
16961445breakOnEnd :: Text -> Text -> (Text, Text)
1697−breakOnEnd = coerce JSS.breakOnEnd
1698−-- breakOnEnd pat src = (reverse b, reverse a)
1699−-- 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)
17001448{-# INLINE breakOnEnd #-}
17011449
17021450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1708,11 +1456,10 @@ breakOnEnd = coerce JSS.breakOnEnd
17081456--
17091457-- Examples:
17101458--
1711−-- >>> breakOnAll "::" ""
1712−-- []
1713−--
1714−-- >>> breakOnAll "/" "a/b/c/"
1715−-- [("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", "/")]
17161463--
17171464-- In (unlikely) bad cases, this function's time complexity degrades
17181465-- towards /O(n*m)/.
@@ -1721,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
17211468breakOnAll :: Text -- ^ @needle@ to search for
17221469 -> Text -- ^ @haystack@ in which to search
17231470 -> [(Text, Text)]
1724−breakOnAll = coerce JSS.breakOnAll
1725−-- breakOnAll pat src@(Text arr off slen)
1726−-- | null pat = emptyError "breakOnAll"
1727−-- | otherwise = L.map step (indices pat src)
1728−-- where
1729−-- step x = (chunk 0 x, chunk x (slen-x))
1730−-- 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
17311477{-# INLINE breakOnAll #-}
17321478
17331479-------------------------------------------------------------------------------
@@ -1751,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
17511497
17521498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
17531499index :: Text -> Int -> Char
1754−index = coerce JSS.index
1755−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
17561501{-# INLINE index #-}
17571502
17581503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
17591504-- and returns the index of the first element in the 'Text' satisfying
17601505-- the predicate. Subject to fusion.
17611506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1762−findIndex = coerce JSS.findIndex
1763−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
17641508{-# INLINE findIndex #-}
17651509
17661510-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1770,11 +1514,10 @@ findIndex = coerce JSS.findIndex
17701514-- In (unlikely) bad cases, this function's time complexity degrades
17711515-- towards /O(n*m)/.
17721516count :: Text -> Text -> Int
1773−count = coerce JSS.count
1774−-- count pat src
1775−-- | null pat = emptyError "count"
1776−-- | isSingleton pat = countChar (unsafeHead pat) src
1777−-- | 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)
17781521{-# INLINE [1] count #-}
17791522
17801523{-# RULES
@@ -1796,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
17961539-- excess elements of the longer 'Text' are discarded. This is
17971540-- equivalent to a pair of 'unpack' operations.
17981541zip :: Text -> Text -> [(Char,Char)]
1799−zip = coerce JSS.zip
1800−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
18011543{-# INLINE zip #-}
18021544
18031545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
18041546-- given as the first argument, instead of a tupling function.
18051547-- Performs replacement on invalid scalar values.
18061548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1807−zipWith = coerce JSS.zipWith
1808−-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1809−-- 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)
18101551{-# INLINE zipWith #-}
18111552
18121553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
18131554-- representing white space.
18141555words :: Text -> [Text]
1815−words = coerce JSS.words
1816−-- words t@(Text arr off len) = loop 0 0
1817−-- where
1818−-- loop !start !n
1819−-- | n >= len = if start == n
1820−-- then []
1821−-- else [Text arr (start+off) (n-start)]
1822−-- | isSpace c =
1823−-- if start == n
1824−-- then loop (start+1) (start+1)
1825−-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1826−-- | otherwise = loop start (n+d)
1827−-- 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
18281568{-# INLINE words #-}
18291569
18301570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
18311571-- newline 'Char's. The resulting strings do not contain newlines.
18321572lines :: Text -> [Text]
1833−lines = coerce JSS.lines
1834−-- lines ps | null ps = []
1835−-- | otherwise = h : if null t
1836−-- then []
1837−-- else lines (unsafeTail t)
1838−-- 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
18391578{-# INLINE lines #-}
18401579
18411580{-
@@ -1863,40 +1602,34 @@ lines' ps | null ps = []
18631602-- | /O(n)/ Joins lines, after appending a terminating newline to
18641603-- each.
18651604unlines :: [Text] -> Text
1866−unlines = coerce JSS.unlines
1867−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
18681606{-# INLINE unlines #-}
18691607
18701608-- | /O(n)/ Joins words using single space characters.
18711609unwords :: [Text] -> Text
1872−unwords = coerce JSS.unwords
1873−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
18741611{-# INLINE unwords #-}
18751612
18761613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
18771614-- 'True' iff the first is a prefix of the second. Subject to fusion.
18781615isPrefixOf :: Text -> Text -> Bool
1879−isPrefixOf = coerce JSS.isPrefixOf
1880−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1881−-- 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)
18821618{-# INLINE [1] isPrefixOf #-}
18831619
18841620{-# RULES
18851621"TEXT isPrefixOf -> fused" [~1] forall s t.
18861622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1887−"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1888− S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
18891623 #-}
18901624
18911625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
18921626-- 'True' iff the first is a suffix of the second.
18931627isSuffixOf :: Text -> Text -> Bool
1894−isSuffixOf = coerce JSS.isSuffixOf
1895−-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1896−-- d >= 0 && a == b'
1897−-- where d = blen - alen
1898−-- b' | d == 0 = b
1899−-- | 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
19001633{-# INLINE isSuffixOf #-}
19011634
19021635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1906,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
19061639-- In (unlikely) bad cases, this function's time complexity degrades
19071640-- towards /O(n*m)/.
19081641isInfixOf :: Text -> Text -> Bool
1909−isInfixOf = coerce JSS.isInfixOf
1910−-- isInfixOf needle haystack
1911−-- | null needle = True
1912−-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1913−-- | 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
19141646{-# INLINE [1] isInfixOf #-}
19151647
19161648{-# RULES
@@ -1926,14 +1658,9 @@ isInfixOf = coerce JSS.isInfixOf
19261658--
19271659-- Examples:
19281660--
1929−-- >>> stripPrefix "foo" "foobar"
1930−-- Just "bar"
1931−--
1932−-- >>> stripPrefix "" "baz"
1933−-- Just "baz"
1934−--
1935−-- >>> stripPrefix "foo" "quux"
1936−-- Nothing
1661+-- > stripPrefix "foo" "foobar" == Just "bar"
1662+-- > stripPrefix "" "baz" == Just "baz"
1663+-- > stripPrefix "foo" "quux" == Nothing
19371664--
19381665-- This is particularly useful with the @ViewPatterns@ extension to
19391666-- GHC, as follows:
@@ -1945,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
19451672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
19461673-- > fnordLength _ = -1
19471674stripPrefix :: Text -> Text -> Maybe Text
1948−stripPrefix = coerce JSS.stripPrefix
1949−-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1950−-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1951−-- | 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
19521678
19531679-- | /O(n)/ Find the longest non-empty common prefix of two strings
19541680-- and return it, along with the suffixes of each string at which they
@@ -1959,39 +1685,28 @@ stripPrefix = coerce JSS.stripPrefix
19591685--
19601686-- Examples:
19611687--
1962−-- >>> commonPrefixes "foobar" "fooquux"
1963−-- Just ("foo","bar","quux")
1964−--
1965−-- >>> commonPrefixes "veeble" "fetzer"
1966−-- Nothing
1967−--
1968−-- >>> commonPrefixes "" "baz"
1969−-- Nothing
1688+-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689+-- > commonPrefixes "veeble" "fetzer" == Nothing
1690+-- > commonPrefixes "" "baz" == Nothing
19701691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1971−commonPrefixes = coerce JSS.commonPrefixes
1972−-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1973−-- where
1974−-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1975−-- | i > 0 = Just (Text arr0 off0 i,
1976−-- text arr0 (off0+i) (len0-i),
1977−-- text arr1 (off1+j) (len1-j))
1978−-- | otherwise = Nothing
1979−-- where Iter a d0 = iter t0 i
1980−-- 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
19811701
19821702-- | /O(n)/ Return the prefix of the second string if its suffix
19831703-- matches the entire first string.
19841704--
19851705-- Examples:
19861706--
1987−-- >>> stripSuffix "bar" "foobar"
1988−-- Just "foo"
1989−--
1990−-- >>> stripSuffix "" "baz"
1991−-- Just "baz"
1992−--
1993−-- >>> stripSuffix "foo" "quux"
1994−-- Nothing
1707+-- > stripSuffix "bar" "foobar" == Just "foo"
1708+-- > stripSuffix "" "baz" == Just "baz"
1709+-- > stripSuffix "foo" "quux" == Nothing
19951710--
19961711-- This is particularly useful with the @ViewPatterns@ extension to
19971712-- GHC, as follows:
@@ -2003,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
20031718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
20041719-- > quuxLength _ = -1
20051720stripSuffix :: Text -> Text -> Maybe Text
2006−stripSuffix = coerce JSS.stripSuffix
2007−-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2008−-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
2009−-- | otherwise = Nothing
2010−
2011−-- -- | Add a list of non-negative numbers. Errors out on overflow.
2012−-- sumP :: String -> [Int] -> Int
2013−-- sumP fun = go 0
2014−-- where go !a (x:xs)
2015−-- | ax >= 0 = go ax xs
2016−-- | otherwise = overflowError fun
2017−-- where ax = a + x
2018−-- go a _ = a
2019−
2020−-- emptyError :: String -> a
2021−-- 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"
20221736
20231737overflowError :: String -> a
20241738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -2032,22 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
20321746-- copy \"breaks the link\" to the original array, allowing it to be
20331747-- garbage collected if there are no other live references to it.
20341748copy :: Text -> Text
2035−copy = P.id -- TODO
2036−-- copy (Text arr off len) = Text (A.run go) 0 len
2037−-- where
2038−-- go :: ST s (A.MArray s)
2039−-- go = do
2040−-- marr <- A.new len
2041−
2042−foreign import javascript unsafe
2043− "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
2044−foreign import javascript unsafe
2045− "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString
2046−
2047−-------------------------------------------------
2048−-- NOTE: the named chunk below used by doctest;
2049−-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2050−
2051−-- $setup
2052−-- >>> :set -XOverloadedStrings
2053−-- >>> 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