@@ -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,76 +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 (unsafeUnpackJSString#)
264−
265−unpack :: Text -> String
266−unpack = coerce JSS.unpack
267−{-# INLINE [1] unpack #-}
268−
269−singleton :: Char -> Text
270−singleton = coerce JSS.singleton
271−{-# INLINE [1] singleton #-}
272−
273−{-# RULES
274−"JSSTRING singleton -> fused" [~1] forall a.
275− singleton a = unstream (S.singleton (safe a))
276−"JSSTRING singleton -> unfused" [1] forall a.
277− unstream (S.singleton (safe a)) = singleton a
278− #-}
279−
280−{-# RULES "inlineJSString" forall s.
281− pack (GHC.Base.build (GHC.unpackFoldrCString# s)) =
282− Text (JSString (unsafeUnpackJSString# s)) #-}
283−
284−-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
285−unpackCString# :: Addr# -> Text
286−unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
287−{-# NOINLINE unpackCString# #-}
288−
289−{-# RULES "TEXT literal" forall a.
290− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
291− = unpackCString# a #-}
292−
293−{-# RULES "TEXT literal UTF8" [1] forall a.
294− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
295− = unpackCString# a #-}
296−
297−{-# RULES "TEXT empty literal" [1]
298− unstream (S.map safe (S.streamList []))
299− = empty_ #-}
300−
301−{-# RULES "TEXT singleton literal" [1] forall a.
302− unstream (S.map safe (S.streamList [a]))
303− = singleton a #-}
304−
305−#if MIN_VERSION_ghcjs_prim(0,1,1)
306−{-# RULES "TEXT literal prim" [0] forall a.
307− unpackCString# a = Text (JSString (unsafeUnpackJSString# a))
308− #-}
309−#endif
310−
311−---------------------------------------------------------------------
312−-- $character_definition
313−--
314−-- This package uses the term /character/ to denote Unicode /code points/.
315−--
316−-- Note that this is not the same thing as a grapheme (e.g. a
317−-- composition of code points that form one visual symbol). For
318−-- instance, consider the grapheme \"ä\". This symbol has two
319−-- Unicode representations: a single code-point representation
320−-- @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code point),
321−-- and a two code point representation @U+0061@ (the \"@A@\" code
322−-- point) and @U+0308@ (the @COMBINING DIAERESIS@ code point).
323−
324249-- $strict
325250--
326251-- This package provides both strict and lazy 'Text' types. The
@@ -396,31 +321,22 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
396321-- Functions that can be fused by the compiler are documented with the
397322-- phrase \"Subject to fusion\".
398323
399−-- instance Eq Text where
400−-- Text arrA offA lenA == Text arrB offB lenB
401−-- | lenA == lenB = A.equal arrA offA arrB offB lenA
402−-- | otherwise = False
403−-- {-# INLINE (==) #-}
404324instance Eq Text where
405− Text x == Text y = x == y
406−
407−-- instance Ord Text where
408−-- 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 (==) #-}
409329
410330instance Ord Text where
411− compare (Text x) (Text y) = compare x y
331+ compare = compareText
412332
413333instance Read Text where
414334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
415335
416336#if MIN_VERSION_base(4,9,0)
417−-- | Non-orphan 'Semigroup' instance only defined for
418−-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
419−-- provided by
420−-- the [semigroups](http://hackage.haskell.org/package/semigroups)
421−-- package
422−--
423−-- @since 1.2.2.0
337+-- Semigroup orphan instances for older GHCs are provided by
338+-- 'semigroups` package
339+
424340instance Semigroup Text where
425341 (<>) = append
426342#endif
@@ -438,7 +354,6 @@ instance IsString Text where
438354 fromString = pack
439355
440356#if __GLASGOW_HASKELL__ >= 708
441−-- | @since 1.2.0.0
442357instance Exts.IsList Text where
443358 type Item Text = Char
444359 fromList = pack
@@ -449,7 +364,6 @@ instance Exts.IsList Text where
449364instance NFData Text where rnf !_ = ()
450365#endif
451366
452−-- | @since 1.2.1.0
453367instance Binary Text where
454368 put t = put (encodeUtf8 t)
455369 get = do
@@ -483,10 +397,8 @@ instance Data Text where
483397
484398#if MIN_VERSION_base(4,7,0)
485399-- | Only defined for @base-4.7.0.0@ and later
486−--
487−-- @since 1.2.2.0
488400instance PrintfArg Text where
489− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
490402#endif
491403
492404packConstr :: Constr
@@ -495,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
495407textDataType :: DataType
496408textDataType = mkDataType "Data.Text.Text" [packConstr]
497409
498−-- -- | /O(n)/ Compare two 'Text' values lexicographically.
499−-- compareText :: Text -> Text -> Ordering
500−-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
501−-- | lenA == 0 && lenB == 0 = EQ
502−-- | otherwise = go 0 0
503−-- where
504−-- go !i !j
505−-- | i >= lenA || j >= lenB = compare lenA lenB
506−-- | a < b = LT
507−-- | a > b = GT
508−-- | otherwise = go (i+di) (j+dj)
509−-- where Iter a di = iter ta i
510−-- Iter b dj = iter tb j
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
511423
512424-- -----------------------------------------------------------------------------
513425-- * Conversion to/from 'Text'
@@ -515,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
515427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
516428-- fusion. Performs replacement on invalid scalar values.
517429pack :: String -> Text
518−pack = coerce JSS.pack
519−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
520431{-# INLINE [1] pack #-}
521432
522433-- -----------------------------------------------------------------------------
@@ -527,8 +438,7 @@ pack = coerce JSS.pack
527438-- copying a new array. Subject to fusion. Performs replacement on
528439-- invalid scalar values.
529440cons :: Char -> Text -> Text
530−cons = coerce JSS.cons
531−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
532442{-# INLINE cons #-}
533443
534444infixr 5 `cons`
@@ -537,27 +447,25 @@ infixr 5 `cons`
537447-- entire array in the process, unless fused. Subject to fusion.
538448-- Performs replacement on invalid scalar values.
539449snoc :: Text -> Char -> Text
540−snoc = coerce JSS.snoc
541−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
542451{-# INLINE snoc #-}
543452
544453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
545454-- into a new 'Text'. Subject to fusion.
546455append :: Text -> Text -> Text
547−append = coerce JSS.append
548−-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
549−-- | len1 == 0 = b
550−-- | len2 == 0 = a
551−-- | len > 0 = Text (A.run x) 0 len
552−-- | otherwise = overflowError "append"
553−-- where
554−-- len = len1+len2
555−-- x :: ST s (A.MArray s)
556−-- x = do
557−-- arr <- A.new len
558−-- A.copyI arr 0 arr1 off1 len1
559−-- A.copyI arr len1 arr2 off2 len
560−-- return arr
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
561469{-# NOINLINE append #-}
562470
563471{-# RULES
@@ -570,18 +478,16 @@ append = coerce JSS.append
570478-- | /O(1)/ Returns the first character of a 'Text', which must be
571479-- non-empty. Subject to fusion.
572480head :: Text -> Char
573−head = coerce JSS.head
574−-- head t = S.head (stream t)
481+head t = S.head (stream t)
575482{-# INLINE head #-}
576483
577484-- | /O(1)/ Returns the first character and rest of a 'Text', or
578485-- 'Nothing' if empty. Subject to fusion.
579486uncons :: Text -> Maybe (Char, Text)
580−uncons = coerce JSS.uncons
581−-- uncons t@(Text arr off len)
582−-- | len <= 0 = Nothing
583−-- | otherwise = Just $ let !(Iter c d) = iter t 0
584−-- in (c, text arr (off+d) (len-d))
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))
585491{-# INLINE [1] uncons #-}
586492
587493-- | Lifted from Control.Arrow and specialized.
@@ -591,13 +497,12 @@ second f (a, b) = (a, f b)
591497-- | /O(1)/ Returns the last character of a 'Text', which must be
592498-- non-empty. Subject to fusion.
593499last :: Text -> Char
594−last = coerce JSS.last
595−-- last (Text arr off len)
596−-- | len <= 0 = emptyError "last"
597−-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
598−-- | otherwise = U16.chr2 n0 n
599−-- where n = A.unsafeIndex arr (off+len-1)
600−-- n0 = A.unsafeIndex arr (off+len-2)
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)
601506{-# INLINE [1] last #-}
602507
603508{-# RULES
@@ -610,11 +515,10 @@ last = coerce JSS.last
610515-- | /O(1)/ Returns all characters after the head of a 'Text', which
611516-- must be non-empty. Subject to fusion.
612517tail :: Text -> Text
613−tail = coerce JSS.tail
614−-- tail t@(Text arr off len)
615−-- | len <= 0 = emptyError "tail"
616−-- | otherwise = text arr (off+d) (len-d)
617−-- where d = iter_ t 0
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
618522{-# INLINE [1] tail #-}
619523
620524{-# RULES
@@ -627,12 +531,11 @@ tail = coerce JSS.tail
627531-- | /O(1)/ Returns all but the last character of a 'Text', which must
628532-- be non-empty. Subject to fusion.
629533init :: Text -> Text
630−init = coerce JSS.init
631−-- init (Text arr off len) | len <= 0 = emptyError "init"
632−-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
633−-- | otherwise = text arr off (len-1)
634−-- where
635−-- n = A.unsafeIndex arr (off+len-1)
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)
636539{-# INLINE [1] init #-}
637540
638541{-# RULES
@@ -642,29 +545,14 @@ init = coerce JSS.init
642545 unstream (S.init (stream t)) = init t
643546 #-}
644547
645−-- | /O(1)/ Returns all but the last character and the last character of a
646−-- 'Text', or 'Nothing' if empty.
647−--
648−-- @since 1.2.3.0
649−unsnoc :: Text -> Maybe (Text, Char)
650−unsnoc = coerce JSS.unsnoc
651−-- unsnoc (Text arr off len)
652−-- | len <= 0 = Nothing
653−-- | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
654−-- | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
655−-- where n = A.unsafeIndex arr (off+len-1)
656−-- n0 = A.unsafeIndex arr (off+len-2)
657−{-# INLINE [1] unsnoc #-}
658−
659548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
660549-- fusion.
661550null :: Text -> Bool
662−null = coerce JSS.null
663−-- null (Text _arr _off len) =
664−-- #if defined(ASSERTS)
665−-- assert (len >= 0) $
666−-- #endif
667−-- len <= 0
551+null (Text _arr _off len) =
552+#if defined(ASSERTS)
553+ assert (len >= 0) $
554+#endif
555+ len <= 0
668556{-# INLINE [1] null #-}
669557
670558{-# RULES
@@ -676,15 +564,14 @@ null = coerce JSS.null
676564
677565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
678566-- Subject to fusion.
679−-- isSingleton :: Text -> Bool
680−-- isSingleton = S.isSingleton . stream
681−-- {-# INLINE isSingleton #-}
567+isSingleton :: Text -> Bool
568+isSingleton = S.isSingleton . stream
569+{-# INLINE isSingleton #-}
682570
683571-- | /O(n)/ Returns the number of characters in a 'Text'.
684572-- Subject to fusion.
685573length :: Text -> Int
686−length = coerce JSS.length
687−-- length t = S.length (stream t)
574+length t = S.length (stream t)
688575{-# INLINE [0] length #-}
689576-- length needs to be phased after the compareN/length rules otherwise
690577-- it may inline before the rules have an opportunity to fire.
@@ -737,58 +624,29 @@ compareLength t n = S.compareLengthI (stream t) n
737624-- -----------------------------------------------------------------------------
738625-- * Transformations
739626-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
740−-- each element of @t@.
741−--
742−-- Example:
743−--
744−-- >>> let message = pack "I am not angry. Not at all."
745−-- >>> T.map (\c -> if c == '.' then '!' else c) message
746−-- "I am not angry! Not at all!"
747−--
748−-- Subject to fusion. Performs replacement on invalid scalar values.
627+-- each element of @t@. Subject to fusion. Performs replacement on
628+-- invalid scalar values.
749629map :: (Char -> Char) -> Text -> Text
750−map = coerce JSS.map
751−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
752631{-# INLINE [1] map #-}
753632
754633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
755634-- 'Text's and concatenates the list after interspersing the first
756635-- argument between each element of the list.
757−--
758−-- Example:
759−--
760−-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
761−-- "WeNI!seekNI!theNI!HolyNI!Grail"
762636intercalate :: Text -> [Text] -> Text
763−intercalate = coerce JSS.intercalate
764−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
765638{-# INLINE intercalate #-}
766639
767640-- | /O(n)/ The 'intersperse' function takes a character and places it
768−-- between the characters of a 'Text'.
769−--
770−-- Example:
771−--
772−-- >>> T.intersperse '.' "SHIELD"
773−-- "S.H.I.E.L.D"
774−--
775−-- Subject to fusion. Performs replacement on invalid scalar values.
641+-- between the characters of a 'Text'. Subject to fusion. Performs
642+-- replacement on invalid scalar values.
776643intersperse :: Char -> Text -> Text
777−intersperse = coerce JSS.intersperse
778−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
779645{-# INLINE intersperse #-}
780646
781−-- | /O(n)/ Reverse the characters of a string.
782−--
783−-- Example:
784−--
785−-- >>> T.reverse "desrever"
786−-- "reversed"
787−--
788−-- Subject to fusion.
647+-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
789648reverse :: Text -> Text
790−reverse = coerce JSS.reverse
791−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
792650{-# INLINE reverse #-}
793651
794652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -805,14 +663,12 @@ reverse = coerce JSS.reverse
805663-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
806664-- be replaced recursively:
807665--
808−-- >>> replace "oo" "foo" "oo"
809−-- "foo"
666+-- > replace "oo" "foo" "oo" == "foo"
810667--
811668-- In cases where several instances of @needle@ overlap, only the
812669-- first one will be replaced:
813670--
814−-- >>> replace "ofo" "bar" "ofofo"
815−-- "barfo"
671+-- > replace "ofo" "bar" "ofofo" == "barfo"
816672--
817673-- In (unlikely) bad cases, this function's time complexity degrades
818674-- towards /O(n*m)/.
@@ -824,29 +680,28 @@ replace :: Text
824680 -> Text
825681 -- ^ @haystack@ in which to search.
826682 -> Text
827−replace = coerce JSS.replace
828−-- replace needle@(Text _ _ neeLen)
829−-- (Text repArr repOff repLen)
830−-- haystack@(Text hayArr hayOff hayLen)
831−-- | neeLen == 0 = emptyError "replace"
832−-- | L.null ixs = haystack
833−-- | len > 0 = Text (A.run x) 0 len
834−-- | otherwise = empty
835−-- where
836−-- ixs = indices needle haystack
837−-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
838−-- x :: ST s (A.MArray s)
839−-- x = do
840−-- marr <- A.new len
841−-- let loop (i:is) o d = do
842−-- let d0 = d + i - o
843−-- d1 = d0 + repLen
844−-- A.copyI marr d hayArr (hayOff+o) d0
845−-- A.copyI marr d0 repArr repOff d1
846−-- loop is (i + neeLen) d1
847−-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
848−-- loop ixs 0 0
849−-- return marr
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
850705
851706-- ----------------------------------------------------------------------------
852707-- ** Case conversions (folds)
@@ -885,8 +740,7 @@ replace = coerce JSS.replace
885740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
886741-- instead of itself.
887742toCaseFold :: Text -> Text
888−-- toCaseFold t = unstream (S.toCaseFold (stream t))
889−toCaseFold = coerce JSS.toCaseFold
743+toCaseFold t = unstream (S.toCaseFold (stream t))
890744{-# INLINE toCaseFold #-}
891745
892746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -897,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
897751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
898752-- followed by \" ̇\" (combining dot above, U+0307).
899753toLower :: Text -> Text
900−-- toLower t = unstream (S.toLower (stream t))
901−toLower = coerce JSS.toLower
754+toLower t = unstream (S.toLower (stream t))
902755{-# INLINE toLower #-}
903756
904757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -908,8 +761,7 @@ toLower = coerce JSS.toLower
908761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
909762-- two-letter sequence \"SS\".
910763toUpper :: Text -> Text
911−-- toUpper t = unstream (S.toUpper (stream t))
912−toUpper = coerce JSS.toUpper
764+toUpper t = unstream (S.toUpper (stream t))
913765{-# INLINE toUpper #-}
914766
915767-- | /O(n)/ Convert a string to title case, using simple case
@@ -930,11 +782,8 @@ toUpper = coerce JSS.toUpper
930782-- guides disagree on whether the book name \"The Hill of the Red
931783-- Fox\" is correctly title cased—but this function will
932784-- capitalize /every/ word.
933−--
934−-- @since 1.0.0.0
935785toTitle :: Text -> Text
936−toTitle = coerce JSS.toTitle
937−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
938787{-# INLINE toTitle #-}
939788
940789-- | /O(n)/ Left-justify a string to the given length, using the
@@ -943,17 +792,13 @@ toTitle = coerce JSS.toTitle
943792--
944793-- Examples:
945794--
946−-- >>> justifyLeft 7 'x' "foo"
947−-- "fooxxxx"
948−--
949−-- >>> justifyLeft 3 'x' "foobar"
950−-- "foobar"
795+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796+-- > justifyLeft 3 'x' "foobar" == "foobar"
951797justifyLeft :: Int -> Char -> Text -> Text
952−justifyLeft = coerce JSS.justifyLeft
953−-- justifyLeft k c t
954−-- | len >= k = t
955−-- | otherwise = t `append` replicateChar (k-len) c
956−-- where len = length t
798+justifyLeft k c t
799+ | len >= k = t
800+ | otherwise = t `append` replicateChar (k-len) c
801+ where len = length t
957802{-# INLINE [1] justifyLeft #-}
958803
959804{-# RULES
@@ -969,17 +814,13 @@ justifyLeft = coerce JSS.justifyLeft
969814--
970815-- Examples:
971816--
972−-- >>> justifyRight 7 'x' "bar"
973−-- "xxxxbar"
974−--
975−-- >>> justifyRight 3 'x' "foobar"
976−-- "foobar"
817+-- > justifyRight 7 'x' "bar" == "xxxxbar"
818+-- > justifyRight 3 'x' "foobar" == "foobar"
977819justifyRight :: Int -> Char -> Text -> Text
978−justifyRight = coerce JSS.justifyRight
979−-- justifyRight k c t
980−-- | len >= k = t
981−-- | otherwise = replicateChar (k-len) c `append` t
982−-- where len = length t
820+justifyRight k c t
821+ | len >= k = t
822+ | otherwise = replicateChar (k-len) c `append` t
823+ where len = length t
983824{-# INLINE justifyRight #-}
984825
985826-- | /O(n)/ Center a string to the given length, using the specified
@@ -988,34 +829,23 @@ justifyRight = coerce JSS.justifyRight
988829--
989830-- Examples:
990831--
991−-- >>> center 8 'x' "HS"
992−-- "xxxHSxxx"
832+-- > center 8 'x' "HS" = "xxxHSxxx"
993833center :: Int -> Char -> Text -> Text
994−center = coerce JSS.center
995−-- center k c t
996−-- | len >= k = t
997−-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
998−-- where len = length t
999−-- d = k - len
1000−-- r = d `quot` 2
1001−-- l = d - r
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
1002841{-# INLINE center #-}
1003842
1004843-- | /O(n)/ The 'transpose' function transposes the rows and columns
1005844-- of its 'Text' argument. Note that this function uses 'pack',
1006845-- 'unpack', and the list version of transpose, and is thus not very
1007846-- efficient.
1008−--
1009−-- Examples:
1010−--
1011−-- >>> transpose ["green","orange"]
1012−-- ["go","rr","ea","en","ng","e"]
1013−--
1014−-- >>> transpose ["blue","red"]
1015−-- ["br","le","ud","e"]
1016847transpose :: [Text] -> [Text]
1017−transpose = coerce JSS.transpose
1018−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
1019849
1020850-- -----------------------------------------------------------------------------
1021851-- * Reducing 'Text's (folds)
@@ -1025,27 +855,23 @@ transpose = coerce JSS.transpose
1025855-- reduces the 'Text' using the binary operator, from left to right.
1026856-- Subject to fusion.
1027857foldl :: (a -> Char -> a) -> a -> Text -> a
1028−foldl f a0 (Text t) = JSS.foldl f a0 t
1029−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
1030859{-# INLINE foldl #-}
1031860
1032861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
1033862foldl' :: (a -> Char -> a) -> a -> Text -> a
1034−foldl' f a0 (Text t) = JSS.foldl' f a0 t
1035−-- foldl' f z t = S.foldl' f z (stream t)
863+foldl' f z t = S.foldl' f z (stream t)
1036864{-# INLINE foldl' #-}
1037865
1038866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
1039867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
1040868foldl1 :: (Char -> Char -> Char) -> Text -> Char
1041−foldl1 = coerce JSS.foldl1
1042−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
1043870{-# INLINE foldl1 #-}
1044871
1045872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
1046873foldl1' :: (Char -> Char -> Char) -> Text -> Char
1047−foldl1' = coerce JSS.foldl1'
1048−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
1049875{-# INLINE foldl1' #-}
1050876
1051877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -1053,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
1053879-- reduces the 'Text' using the binary operator, from right to left.
1054880-- Subject to fusion.
1055881foldr :: (Char -> a -> a) -> a -> Text -> a
1056−foldr f a0 (Text t) = JSS.foldr f a0 t
1057−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
1058883{-# INLINE foldr #-}
1059884
1060885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
1061886-- and thus must be applied to a non-empty 'Text'. Subject to
1062887-- fusion.
1063888foldr1 :: (Char -> Char -> Char) -> Text -> Char
1064−foldr1 = coerce JSS.foldr1
1065−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
1066890{-# INLINE foldr1 #-}
1067891
1068892-- -----------------------------------------------------------------------------
@@ -1070,54 +894,48 @@ foldr1 = coerce JSS.foldr1
1070894
1071895-- | /O(n)/ Concatenate a list of 'Text's.
1072896concat :: [Text] -> Text
1073−concat = coerce JSS.concat
1074−-- concat ts = case ts' of
1075−-- [] -> empty
1076−-- [t] -> t
1077−-- _ -> Text (A.run go) 0 len
1078−-- where
1079−-- ts' = L.filter (not . null) ts
1080−-- len = sumP "concat" $ L.map lengthWord16 ts'
1081−-- go :: ST s (A.MArray s)
1082−-- go = do
1083−-- arr <- A.new len
1084−-- let step i (Text a o l) =
1085−-- let !j = i + l in A.copyI arr i a o j >> return j
1086−-- foldM step 0 ts' >> return arr
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
1087910
1088911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
1089912-- concatenate the results.
1090913concatMap :: (Char -> Text) -> Text -> Text
1091−concatMap = coerce JSS.concatMap
1092−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
1093915{-# INLINE concatMap #-}
1094916
1095917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1096918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1097919any :: (Char -> Bool) -> Text -> Bool
1098−any = coerce JSS.any
1099−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1100921{-# INLINE any #-}
1101922
1102923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1103924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1104925all :: (Char -> Bool) -> Text -> Bool
1105−all = coerce JSS.all
1106−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1107927{-# INLINE all #-}
1108928
1109929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1110930-- must be non-empty. Subject to fusion.
1111931maximum :: Text -> Char
1112−maximum = coerce JSS.maximum
1113−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1114933{-# INLINE maximum #-}
1115934
1116935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1117936-- must be non-empty. Subject to fusion.
1118937minimum :: Text -> Char
1119−minimum = coerce JSS.minimum
1120−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1121939{-# INLINE minimum #-}
1122940
1123941-- -----------------------------------------------------------------------------
@@ -1133,9 +951,8 @@ minimum = coerce JSS.minimum
1133951--
1134952-- > last (scanl f z xs) == foldl f z xs.
1135953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1136−scanl = coerce JSS.scanl
1137−-- scanl f z t = unstream (S.scanl g z (stream t))
1138−-- where g a b = safe (f a b)
954+scanl f z t = unstream (S.scanl g z (stream t))
955+ where g a b = safe (f a b)
1139956{-# INLINE scanl #-}
1140957
1141958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -1144,9 +961,8 @@ scanl = coerce JSS.scanl
1144961--
1145962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1146963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1147−scanl1 = coerce JSS.scanl1
1148−-- scanl1 f t | null t = empty
1149−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
964+scanl1 f t | null t = empty
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1150966{-# INLINE scanl1 #-}
1151967
1152968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -1154,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1154970--
1155971-- > scanr f v == reverse . scanl (flip f) v . reverse
1156972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1157−scanr = coerce JSS.scanr
1158−-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1159−-- where g a b = safe (f a b)
973+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
974+ where g a b = safe (f a b)
1160975{-# INLINE scanr #-}
1161976
1162977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1163978-- value argument. Subject to fusion. Performs replacement on
1164979-- invalid scalar values.
1165980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1166−scanr1 = coerce JSS.scanr1
1167−-- scanr1 f t | null t = empty
1168−-- | otherwise = scanr f (last t) (init t)
981+scanr1 f t | null t = empty
982+ | otherwise = scanr f (last t) (init t)
1169983{-# INLINE scanr1 #-}
1170984
1171985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1173,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1173987-- parameter from left to right, and returns a final 'Text'. Performs
1174988-- replacement on invalid scalar values.
1175989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1176−mapAccumL f z0 (Text a) =
1177− let (a', t) = JSS.mapAccumL f z0 a
1178− in (a', Text t)
1179−-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1180−-- where g a b = second safe (f a b)
990+mapAccumL f z0 = S.mapAccumL g z0 . stream
991+ where g a b = second safe (f a b)
1181992{-# INLINE mapAccumL #-}
1182993
1183994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1187,11 +998,8 @@ mapAccumL f z0 (Text a) =
1187998-- 'Text'.
1188999-- Performs replacement on invalid scalar values.
11891000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1190−mapAccumR f z0 (Text a) =
1191− let (a', t) = JSS.mapAccumR f z0 a
1192− in (a', Text t)
1193−-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1194−-- where g a b = second safe (f a b)
1001+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1002+ where g a b = second safe (f a b)
11951003{-# INLINE mapAccumR #-}
11961004
11971005-- -----------------------------------------------------------------------------
@@ -1200,21 +1008,20 @@ mapAccumR f z0 (Text a) =
12001008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
12011009-- @t@ repeated @n@ times.
12021010replicate :: Int -> Text -> Text
1203−replicate = coerce JSS.replicate
1204−-- replicate n t@(Text a o l)
1205−-- | n <= 0 || l <= 0 = empty
1206−-- | n == 1 = t
1207−-- | isSingleton t = replicateChar n (unsafeHead t)
1208−-- | otherwise = Text (A.run x) 0 len
1209−-- where
1210−-- len = l `mul` n
1211−-- x :: ST s (A.MArray s)
1212−-- x = do
1213−-- arr <- A.new len
1214−-- let loop !d !i | i >= n = return arr
1215−-- | otherwise = let m = d + l
1216−-- in A.copyI arr d a o m >> loop m (i+1)
1217−-- loop 0 0
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
12181025{-# INLINE [1] replicate #-}
12191026
12201027{-# RULES
@@ -1225,19 +1032,8 @@ replicate = coerce JSS.replicate
12251032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
12261033-- value of every element. Subject to fusion.
12271034replicateChar :: Int -> Char -> Text
1228−#ifndef __GHCJS__
12291035replicateChar n c = unstream (S.replicateCharI n (safe c))
12301036{-# INLINE replicateChar #-}
1231−#else
1232−replicateChar n c = Text $ js_replicateChar n c
1233−{-# INLINE [1] replicateChar #-}
1234−{-# RULES
1235−"JSSTRING replicateChar -> fused" [~1] forall n c.
1236− replicateChar n c = unstream (S.replicateCharI n (safe c))
1237−"JSSTRING replicateChar -> unfused" [1] forall n c.
1238− unstream (S.replicateCharI n (safe c)) = replicateChar n c
1239− #-}
1240−#endif
12411037
12421038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
12431039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1247,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
12471043-- string, and @b@ is the seed value for further production. Subject
12481044-- to fusion. Performs replacement on invalid scalar values.
12491045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1250−unfoldr f a = Text $ JSS.unfoldr f a
1251−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
12521047{-# INLINE unfoldr #-}
12531048
12541049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1258,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
12581053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
12591054-- to fusion. Performs replacement on invalid scalar values.
12601055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1261−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1262−-- 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)
12631057{-# INLINE unfoldrN #-}
12641058
12651059-- -----------------------------------------------------------------------------
@@ -1269,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
12691063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
12701064-- the length of the Text. Subject to fusion.
12711065take :: Int -> Text -> Text
1272−take = coerce JSS.take
1273−-- take n t@(Text arr off len)
1274−-- | n <= 0 = empty
1275−-- | n >= len = t
1276−-- | otherwise = text arr off (iterN n t)
1066+take n t@(Text arr off len)
1067+ | n <= 0 = empty
1068+ | n >= len = t
1069+ | otherwise = text arr off (iterN n t)
12771070{-# INLINE [1] take #-}
12781071
1279−-- iterN :: Int -> Text -> Int
1280−-- iterN n t@(Text _arr _off len) = loop 0 0
1281−-- where loop !i !cnt
1282−-- | i >= len || cnt >= n = i
1283−-- | otherwise = loop (i+d) (cnt+1)
1284−-- where d = iter_ t i
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
12851078
12861079{-# RULES
12871080"TEXT take -> fused" [~1] forall n t.
@@ -1295,36 +1088,31 @@ take = coerce JSS.take
12951088--
12961089-- Examples:
12971090--
1298−-- >>> takeEnd 3 "foobar"
1299−-- "bar"
1300−--
1301−-- @since 1.1.1.0
1091+-- > takeEnd 3 "foobar" == "bar"
13021092takeEnd :: Int -> Text -> Text
1303−takeEnd = coerce JSS.takeEnd
1304−-- takeEnd n t@(Text arr off len)
1305−-- | n <= 0 = empty
1306−-- | n >= len = t
1307−-- | otherwise = text arr (off+i) (len-i)
1308−-- where i = iterNEnd n t
1309−
1310−-- iterNEnd :: Int -> Text -> Int
1311−-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1312−-- where loop i !m
1313−-- | i <= 0 = 0
1314−-- | m <= 1 = i
1315−-- | otherwise = loop (i+d) (m-1)
1316−-- where d = reverseIter_ t i
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
13171106
13181107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
13191108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
13201109-- is greater than the length of the 'Text'. Subject to fusion.
13211110drop :: Int -> Text -> Text
1322−drop = coerce JSS.drop
1323−-- drop n t@(Text arr off len)
1324−-- | n <= 0 = t
1325−-- | n >= len = empty
1326−-- | otherwise = text arr (off+i) (len-i)
1327−-- where i = iterN n t
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
13281116{-# INLINE [1] drop #-}
13291117
13301118{-# RULES
@@ -1339,27 +1127,22 @@ drop = coerce JSS.drop
13391127--
13401128-- Examples:
13411129--
1342−-- >>> dropEnd 3 "foobar"
1343−-- "foo"
1344−--
1345−-- @since 1.1.1.0
1130+-- > dropEnd 3 "foobar" == "foo"
13461131dropEnd :: Int -> Text -> Text
1347−dropEnd = coerce JSS.dropEnd
1348−-- dropEnd n t@(Text arr off len)
1349−-- | n <= 0 = t
1350−-- | n >= len = empty
1351−-- | otherwise = text arr off (iterNEnd n t)
1132+dropEnd n t@(Text arr off len)
1133+ | n <= 0 = t
1134+ | n >= len = empty
1135+ | otherwise = text arr off (iterNEnd n t)
13521136
13531137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
13541138-- returns the longest prefix (possibly empty) of elements that
13551139-- satisfy @p@. Subject to fusion.
13561140takeWhile :: (Char -> Bool) -> Text -> Text
1357−takeWhile = coerce JSS.takeWhile
1358−-- takeWhile p t@(Text arr off len) = loop 0
1359−-- where loop !i | i >= len = t
1360−-- | p c = loop (i+d)
1361−-- | otherwise = text arr off i
1362−-- where Iter c d = iter t i
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
13631146{-# INLINE [1] takeWhile #-}
13641147
13651148{-# RULES
@@ -1374,25 +1157,13 @@ takeWhile = coerce JSS.takeWhile
13741157-- satisfy @p@. Subject to fusion.
13751158-- Examples:
13761159--
1377−-- >>> takeWhileEnd (=='o') "foo"
1378−-- "oo"
1379−--
1380−-- @since 1.2.2.0
1160+-- > takeWhileEnd (=='o') "foo" == "oo"
13811161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1382−#ifndef __GHCJS__
13831162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
13841163 where loop !i !l | l <= 0 = t
13851164 | p c = loop (i+d) (l+d)
13861165 | otherwise = text arr (off+l) (len-l)
13871166 where (c,d) = reverseIter t i
1388−#else
1389−takeWhileEnd p t@(Text jst) = loop (len-1) len
1390− where loop !i !l | l <= 0 = t
1391− | p c = loop (i+d) (l+d)
1392− | otherwise = Text $ js_substr1 l jst
1393− where (c,d) = reverseIter t i
1394− len = length t
1395−#endif
13961167{-# INLINE [1] takeWhileEnd #-}
13971168
13981169{-# RULES
@@ -1405,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
14051176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
14061177-- 'takeWhile' @p@ @t@. Subject to fusion.
14071178dropWhile :: (Char -> Bool) -> Text -> Text
1408−dropWhile = coerce JSS.dropWhile
1409−-- dropWhile p t@(Text arr off len) = loop 0 0
1410−-- where loop !i !l | l >= len = empty
1411−-- | p c = loop (i+d) (l+d)
1412−-- | otherwise = Text arr (off+i) (len-l)
1413−
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
14141184{-# INLINE [1] dropWhile #-}
14151185
14161186{-# RULES
@@ -1426,15 +1196,13 @@ dropWhile = coerce JSS.dropWhile
14261196--
14271197-- Examples:
14281198--
1429−-- >>> dropWhileEnd (=='.') "foo..."
1430−-- "foo"
1199+-- > dropWhileEnd (=='.') "foo..." == "foo"
14311200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1432−dropWhileEnd = coerce JSS.dropWhileEnd
1433−-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1434−-- where loop !i !l | l <= 0 = empty
1435−-- | p c = loop (i+d) (l+d)
1436−-- | otherwise = Text arr off l
1437−-- where (c,d) = reverseIter t i
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
14381206{-# INLINE [1] dropWhileEnd #-}
14391207
14401208{-# RULES
@@ -1477,49 +1245,45 @@ strip = dropAround isSpace
14771245-- prefix of @t@ of length @n@, and whose second is the remainder of
14781246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
14791247splitAt :: Int -> Text -> (Text, Text)
1480−splitAt = coerce JSS.splitAt
1481−-- splitAt n t@(Text arr off len)
1482−-- | n <= 0 = (empty, t)
1483−-- | n >= len = (t, empty)
1484−-- | otherwise = let k = iterN n t
1485−-- in (text arr off k, text arr (off+k) (len-k))
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))
14861253
14871254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
14881255-- a pair whose first element is the longest prefix (possibly empty)
14891256-- of @t@ of elements that satisfy @p@, and whose second is the
14901257-- remainder of the list.
14911258span :: (Char -> Bool) -> Text -> (Text, Text)
1492−span = coerce JSS.span
1493−-- span p t = case span_ p t of
1494−-- (# hd,tl #) -> (hd,tl)
1259+span p t = case span_ p t of
1260+ (# hd,tl #) -> (hd,tl)
14951261{-# INLINE span #-}
14961262
14971263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
14981264-- over elements that fail the predicate @p@.
14991265break :: (Char -> Bool) -> Text -> (Text, Text)
1500−break = coerce JSS.break
1501−-- break p = span (not . p)
1266+break p = span (not . p)
15021267{-# INLINE break #-}
15031268
15041269-- | /O(n)/ Group characters in a string according to a predicate.
15051270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1506−groupBy = coerce JSS.groupBy
1507−-- groupBy p = loop
1508−-- where
1509−-- loop t@(Text arr off len)
1510−-- | null t = []
1511−-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1512−-- where Iter c d = iter t 0
1513−-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
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))
15141278
15151279-- | Returns the /array/ index (in units of 'Word16') at which a
15161280-- character may be found. This is /not/ the same as the logical
15171281-- index returned by e.g. 'findIndex'.
1518−-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1519−-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1520−-- where go !i | i >= len || q c = i
1521−-- | otherwise = go (i+d)
1522−-- where Iter c d = iter t i
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
15231287
15241288-- | /O(n)/ Group characters in a string by equality.
15251289group :: Text -> [Text]
@@ -1528,17 +1292,15 @@ group = groupBy (==)
15281292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
15291293-- first.
15301294inits :: Text -> [Text]
1531−inits = coerce JSS.inits
1532−-- inits t@(Text arr off len) = loop 0
1533−-- where loop i | i >= len = [t]
1534−-- | otherwise = Text arr off i : loop (i + iter_ t i)
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)
15351298
15361299-- | /O(n)/ Return all final segments of the given 'Text', longest
15371300-- first.
15381301tails :: Text -> [Text]
1539−tails = coerce JSS.tails
1540−-- tails t | null t = [empty]
1541−-- | otherwise = t : tails (unsafeTail t)
1302+tails t | null t = [empty]
1303+ | otherwise = t : tails (unsafeTail t)
15421304
15431305-- $split
15441306--
@@ -1552,14 +1314,9 @@ tails = coerce JSS.tails
15521314--
15531315-- Examples:
15541316--
1555−-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1556−-- ["a","b","d","e"]
1557−--
1558−-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1559−-- ["","X","X","X",""]
1560−--
1561−-- >>> splitOn "x" "x"
1562−-- ["",""]
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" == ["",""]
15631320--
15641321-- and
15651322--
@@ -1576,14 +1333,13 @@ splitOn :: Text
15761333 -> Text
15771334 -- ^ Input text.
15781335 -> [Text]
1579−splitOn = coerce JSS.splitOn
1580−-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1581−-- | l <= 0 = emptyError "splitOn"
1582−-- | isSingleton pat = split (== unsafeHead pat) src
1583−-- | otherwise = go 0 (indices pat src)
1584−-- where
1585−-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1586−-- go s _ = [text arr (s+off) (len-s)]
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)]
15871343{-# INLINE [1] splitOn #-}
15881344
15891345{-# RULES
@@ -1596,36 +1352,28 @@ splitOn = coerce JSS.splitOn
15961352-- resulting components do not contain the separators. Two adjacent
15971353-- separators result in an empty component in the output. eg.
15981354--
1599−-- >>> split (=='a') "aabbaca"
1600−-- ["","","bb","c",""]
1601−--
1602−-- >>> split (=='a') ""
1603−-- [""]
1355+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356+-- > split (=='a') "" == [""]
16041357split :: (Char -> Bool) -> Text -> [Text]
1605−split = coerce JSS.split
1606−-- split _ t@(Text _off _arr 0) = [t]
1607−-- split p t = loop t
1608−-- where loop s | null s' = [l]
1609−-- | otherwise = l : loop (unsafeTail s')
1610−-- where (# l, s' #) = span_ (not . p) s
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
16111363{-# INLINE split #-}
16121364
16131365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
16141366-- element may be shorter than the other chunks, depending on the
16151367-- length of the input. Examples:
16161368--
1617−-- >>> chunksOf 3 "foobarbaz"
1618−-- ["foo","bar","baz"]
1619−--
1620−-- >>> chunksOf 4 "haskell.org"
1621−-- ["hask","ell.","org"]
1369+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
16221371chunksOf :: Int -> Text -> [Text]
1623−chunksOf = coerce JSS.chunksOf
1624−-- chunksOf k = go
1625−-- where
1626−-- go t = case splitAt k t of
1627−-- (a,b) | null a -> []
1628−-- | otherwise -> a : go b
1372+chunksOf k = go
1373+ where
1374+ go t = case splitAt k t of
1375+ (a,b) | null a -> []
1376+ | otherwise -> a : go b
16291377{-# INLINE chunksOf #-}
16301378
16311379-- ----------------------------------------------------------------------------
@@ -1638,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
16381386-- returns the first element matching the predicate, or 'Nothing' if
16391387-- there is no such element.
16401388find :: (Char -> Bool) -> Text -> Maybe Char
1641−find = coerce JSS.find
1642−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
16431390{-# INLINE find #-}
16441391
16451392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1648,16 +1395,14 @@ find = coerce JSS.find
16481395--
16491396-- > partition p t == (filter p t, filter (not . p) t)
16501397partition :: (Char -> Bool) -> Text -> (Text, Text)
1651−partition = coerce JSS.partition
1652−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
16531399{-# INLINE partition #-}
16541400
16551401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
16561402-- returns a 'Text' containing those characters that satisfy the
16571403-- predicate.
16581404filter :: (Char -> Bool) -> Text -> Text
1659−filter = coerce JSS.filter
1660−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
16611406{-# INLINE filter #-}
16621407
16631408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1667,11 +1412,8 @@ filter = coerce JSS.filter
16671412--
16681413-- Examples:
16691414--
1670−-- >>> breakOn "::" "a::b::c"
1671−-- ("a","::b::c")
1672−--
1673−-- >>> breakOn "/" "foobar"
1674−-- ("foobar","")
1415+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416+-- > breakOn "/" "foobar" ==> ("foobar", "")
16751417--
16761418-- Laws:
16771419--
@@ -1685,12 +1427,11 @@ filter = coerce JSS.filter
16851427-- In (unlikely) bad cases, this function's time complexity degrades
16861428-- towards /O(n*m)/.
16871429breakOn :: Text -> Text -> (Text, Text)
1688−breakOn = coerce JSS.breakOn
1689−-- breakOn pat src@(Text arr off len)
1690−-- | null pat = emptyError "breakOn"
1691−-- | otherwise = case indices pat src of
1692−-- [] -> (src, empty)
1693−-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
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))
16941435{-# INLINE breakOn #-}
16951436
16961437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1700,12 +1441,10 @@ breakOn = coerce JSS.breakOn
17001441-- up to and including the last match of @needle@. The second is the
17011442-- remainder of @haystack@, following the match.
17021443--
1703−-- >>> breakOnEnd "::" "a::b::c"
1704−-- ("a::b::","c")
1444+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
17051445breakOnEnd :: Text -> Text -> (Text, Text)
1706−breakOnEnd = coerce JSS.breakOnEnd
1707−-- breakOnEnd pat src = (reverse b, reverse a)
1708−-- 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)
17091448{-# INLINE breakOnEnd #-}
17101449
17111450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1717,11 +1456,10 @@ breakOnEnd = coerce JSS.breakOnEnd
17171456--
17181457-- Examples:
17191458--
1720−-- >>> breakOnAll "::" ""
1721−-- []
1722−--
1723−-- >>> breakOnAll "/" "a/b/c/"
1724−-- [("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", "/")]
17251463--
17261464-- In (unlikely) bad cases, this function's time complexity degrades
17271465-- towards /O(n*m)/.
@@ -1730,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
17301468breakOnAll :: Text -- ^ @needle@ to search for
17311469 -> Text -- ^ @haystack@ in which to search
17321470 -> [(Text, Text)]
1733−breakOnAll = coerce JSS.breakOnAll
1734−-- breakOnAll pat src@(Text arr off slen)
1735−-- | null pat = emptyError "breakOnAll"
1736−-- | otherwise = L.map step (indices pat src)
1737−-- where
1738−-- step x = (chunk 0 x, chunk x (slen-x))
1739−-- chunk !n !l = text arr (n+off) l
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
17401477{-# INLINE breakOnAll #-}
17411478
17421479-------------------------------------------------------------------------------
@@ -1760,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
17601497
17611498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
17621499index :: Text -> Int -> Char
1763−index = coerce JSS.index
1764−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
17651501{-# INLINE index #-}
17661502
17671503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
17681504-- and returns the index of the first element in the 'Text' satisfying
17691505-- the predicate. Subject to fusion.
17701506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1771−findIndex = coerce JSS.findIndex
1772−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
17731508{-# INLINE findIndex #-}
17741509
17751510-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1779,11 +1514,10 @@ findIndex = coerce JSS.findIndex
17791514-- In (unlikely) bad cases, this function's time complexity degrades
17801515-- towards /O(n*m)/.
17811516count :: Text -> Text -> Int
1782−count = coerce JSS.count
1783−-- count pat src
1784−-- | null pat = emptyError "count"
1785−-- | isSingleton pat = countChar (unsafeHead pat) src
1786−-- | otherwise = L.length (indices pat src)
1517+count pat src
1518+ | null pat = emptyError "count"
1519+ | isSingleton pat = countChar (unsafeHead pat) src
1520+ | otherwise = L.length (indices pat src)
17871521{-# INLINE [1] count #-}
17881522
17891523{-# RULES
@@ -1805,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
18051539-- excess elements of the longer 'Text' are discarded. This is
18061540-- equivalent to a pair of 'unpack' operations.
18071541zip :: Text -> Text -> [(Char,Char)]
1808−zip = coerce JSS.zip
1809−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
18101543{-# INLINE zip #-}
18111544
18121545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
18131546-- given as the first argument, instead of a tupling function.
18141547-- Performs replacement on invalid scalar values.
18151548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1816−zipWith = coerce JSS.zipWith
1817−-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1818−-- where g a b = safe (f a b)
1549+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550+ where g a b = safe (f a b)
18191551{-# INLINE zipWith #-}
18201552
18211553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
18221554-- representing white space.
18231555words :: Text -> [Text]
1824−words = coerce JSS.words
1825−-- words t@(Text arr off len) = loop 0 0
1826−-- where
1827−-- loop !start !n
1828−-- | n >= len = if start == n
1829−-- then []
1830−-- else [Text arr (start+off) (n-start)]
1831−-- | isSpace c =
1832−-- if start == n
1833−-- then loop (start+1) (start+1)
1834−-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1835−-- | otherwise = loop start (n+d)
1836−-- where Iter c d = iter t n
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
18371568{-# INLINE words #-}
18381569
18391570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
18401571-- newline 'Char's. The resulting strings do not contain newlines.
18411572lines :: Text -> [Text]
1842−lines = coerce JSS.lines
1843−-- lines ps | null ps = []
1844−-- | otherwise = h : if null t
1845−-- then []
1846−-- else lines (unsafeTail t)
1847−-- where (# h,t #) = span_ (/= '\n') ps
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
18481578{-# INLINE lines #-}
18491579
18501580{-
@@ -1872,40 +1602,34 @@ lines' ps | null ps = []
18721602-- | /O(n)/ Joins lines, after appending a terminating newline to
18731603-- each.
18741604unlines :: [Text] -> Text
1875−unlines = coerce JSS.unlines
1876−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
18771606{-# INLINE unlines #-}
18781607
18791608-- | /O(n)/ Joins words using single space characters.
18801609unwords :: [Text] -> Text
1881−unwords = coerce JSS.unwords
1882−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
18831611{-# INLINE unwords #-}
18841612
18851613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
18861614-- 'True' iff the first is a prefix of the second. Subject to fusion.
18871615isPrefixOf :: Text -> Text -> Bool
1888−isPrefixOf = coerce JSS.isPrefixOf
1889−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1890−-- 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)
18911618{-# INLINE [1] isPrefixOf #-}
18921619
18931620{-# RULES
18941621"TEXT isPrefixOf -> fused" [~1] forall s t.
18951622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1896−"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1897− S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
18981623 #-}
18991624
19001625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
19011626-- 'True' iff the first is a suffix of the second.
19021627isSuffixOf :: Text -> Text -> Bool
1903−isSuffixOf = coerce JSS.isSuffixOf
1904−-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1905−-- d >= 0 && a == b'
1906−-- where d = blen - alen
1907−-- b' | d == 0 = b
1908−-- | otherwise = Text barr (boff+d) alen
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
19091633{-# INLINE isSuffixOf #-}
19101634
19111635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1915,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
19151639-- In (unlikely) bad cases, this function's time complexity degrades
19161640-- towards /O(n*m)/.
19171641isInfixOf :: Text -> Text -> Bool
1918−isInfixOf = coerce JSS.isInfixOf
1919−-- isInfixOf needle haystack
1920−-- | null needle = True
1921−-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1922−-- | otherwise = not . L.null . indices needle $ haystack
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
19231646{-# INLINE [1] isInfixOf #-}
19241647
19251648{-# RULES
@@ -1935,14 +1658,9 @@ isInfixOf = coerce JSS.isInfixOf
19351658--
19361659-- Examples:
19371660--
1938−-- >>> stripPrefix "foo" "foobar"
1939−-- Just "bar"
1940−--
1941−-- >>> stripPrefix "" "baz"
1942−-- Just "baz"
1943−--
1944−-- >>> stripPrefix "foo" "quux"
1945−-- Nothing
1661+-- > stripPrefix "foo" "foobar" == Just "bar"
1662+-- > stripPrefix "" "baz" == Just "baz"
1663+-- > stripPrefix "foo" "quux" == Nothing
19461664--
19471665-- This is particularly useful with the @ViewPatterns@ extension to
19481666-- GHC, as follows:
@@ -1954,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
19541672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
19551673-- > fnordLength _ = -1
19561674stripPrefix :: Text -> Text -> Maybe Text
1957−stripPrefix = coerce JSS.stripPrefix
1958−-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1959−-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1960−-- | otherwise = Nothing
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
19611678
19621679-- | /O(n)/ Find the longest non-empty common prefix of two strings
19631680-- and return it, along with the suffixes of each string at which they
@@ -1968,39 +1685,28 @@ stripPrefix = coerce JSS.stripPrefix
19681685--
19691686-- Examples:
19701687--
1971−-- >>> commonPrefixes "foobar" "fooquux"
1972−-- Just ("foo","bar","quux")
1973−--
1974−-- >>> commonPrefixes "veeble" "fetzer"
1975−-- Nothing
1976−--
1977−-- >>> commonPrefixes "" "baz"
1978−-- Nothing
1688+-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689+-- > commonPrefixes "veeble" "fetzer" == Nothing
1690+-- > commonPrefixes "" "baz" == Nothing
19791691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1980−commonPrefixes = coerce JSS.commonPrefixes
1981−-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1982−-- where
1983−-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1984−-- | i > 0 = Just (Text arr0 off0 i,
1985−-- text arr0 (off0+i) (len0-i),
1986−-- text arr1 (off1+j) (len1-j))
1987−-- | otherwise = Nothing
1988−-- where Iter a d0 = iter t0 i
1989−-- Iter b d1 = iter t1 j
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
19901701
19911702-- | /O(n)/ Return the prefix of the second string if its suffix
19921703-- matches the entire first string.
19931704--
19941705-- Examples:
19951706--
1996−-- >>> stripSuffix "bar" "foobar"
1997−-- Just "foo"
1998−--
1999−-- >>> stripSuffix "" "baz"
2000−-- Just "baz"
2001−--
2002−-- >>> stripSuffix "foo" "quux"
2003−-- Nothing
1707+-- > stripSuffix "bar" "foobar" == Just "foo"
1708+-- > stripSuffix "" "baz" == Just "baz"
1709+-- > stripSuffix "foo" "quux" == Nothing
20041710--
20051711-- This is particularly useful with the @ViewPatterns@ extension to
20061712-- GHC, as follows:
@@ -2012,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
20121718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
20131719-- > quuxLength _ = -1
20141720stripSuffix :: Text -> Text -> Maybe Text
2015−stripSuffix = coerce JSS.stripSuffix
2016−-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
2017−-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
2018−-- | otherwise = Nothing
2019−
2020−-- -- | Add a list of non-negative numbers. Errors out on overflow.
2021−-- sumP :: String -> [Int] -> Int
2022−-- sumP fun = go 0
2023−-- where go !a (x:xs)
2024−-- | ax >= 0 = go ax xs
2025−-- | otherwise = overflowError fun
2026−-- where ax = a + x
2027−-- go a _ = a
2028−
2029−-- emptyError :: String -> a
2030−-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
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"
20311736
20321737overflowError :: String -> a
20331738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -2041,22 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
20411746-- copy \"breaks the link\" to the original array, allowing it to be
20421747-- garbage collected if there are no other live references to it.
20431748copy :: Text -> Text
2044−copy = P.id -- TODO
2045−-- copy (Text arr off len) = Text (A.run go) 0 len
2046−-- where
2047−-- go :: ST s (A.MArray s)
2048−-- go = do
2049−-- marr <- A.new len
2050−
2051−foreign import javascript unsafe
2052− "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
2053−foreign import javascript unsafe
2054− "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString
2055−
2056−-------------------------------------------------
2057−-- NOTE: the named chunk below used by doctest;
2058−-- verify the doctests via `doctest -fobject-code Data/Text.hs`
2059−
2060−-- $setup
2061−-- >>> :set -XOverloadedStrings
2062−-- >>> import qualified Data.Text as T
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