@@ -6,7 +6,7 @@
6#if __GLASGOW_HASKELL__ >= 708
6#if __GLASGOW_HASKELL__ >= 708
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
9−{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI, UnliftedFFITypes #-}
9+
11-- Module : Data.Text
11-- Module : Data.Text
12-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
12-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
@@ -223,16 +223,16 @@ import qualified Data.Text.Internal.Fusion as S
223import qualified Data.Text.Internal.Fusion.Common as S
223import qualified Data.Text.Internal.Fusion.Common as S
224import Data.Text.Encoding (decodeUtf8', encodeUtf8)
224import Data.Text.Encoding (decodeUtf8', encodeUtf8)
225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
226−-- import Data.Text.Internal.Private (span_)
226+import Data.Text.Internal.Private (span_)
227−import Data.Text.Internal (Text(..), empty, empty_, firstf, mul, safe, text)
227+import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
228−-- import Data.Text.Show (singleton, unpack, unpackCString#)
228+import Data.Text.Show (singleton, unpack, unpackCString#)
229import qualified Prelude as P
229import qualified Prelude as P
230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
231 reverseIter_, unsafeHead, unsafeTail)
231 reverseIter_, unsafeHead, unsafeTail)
232import Data.Text.Internal.Unsafe.Char (unsafeChr)
232import Data.Text.Internal.Unsafe.Char (unsafeChr)
233import qualified Data.Text.Internal.Functions as F
233import qualified Data.Text.Internal.Functions as F
234import qualified Data.Text.Internal.Encoding.Utf16 as U16
234import qualified Data.Text.Internal.Encoding.Utf16 as U16
235−-- import Data.Text.Internal.Search (indices)
235+import Data.Text.Internal.Search (indices)
236#if defined(__HADDOCK__)
236#if defined(__HADDOCK__)
237import Data.ByteString (ByteString)
237import Data.ByteString (ByteString)
238import qualified Data.Text.Lazy as L
238import qualified Data.Text.Lazy as L
@@ -246,59 +246,6 @@ import qualified GHC.Exts as Exts
246import Text.Printf (PrintfArg, formatArg, formatString)
246import Text.Printf (PrintfArg, formatArg, formatString)
249−-- GHCJS ------------------------------------------------------------
250−import Data.JSString.Internal.Type (JSString (..))
251−import qualified Data.JSString as JSS
252−import qualified Data.JSString.Internal.Fusion as SJS
253−import qualified Data.JSString.Internal.Fusion.Common as SJS
255−import GHC.Base (build)
256−import qualified GHC.CString as GHC
257−import GHC.Exts (Addr#, ByteArray#)
258−import GHCJS.Prim (JSVal (..))
260−unpack :: Text -> String
261−unpack = coerce JSS.unpack
262−{-# INLINE [1] unpack #-}
264−singleton :: Char -> Text
265−singleton = coerce JSS.singleton
266−{-# INLINE [1] singleton #-}
269−"JSSTRING singleton -> fused" [~1] forall a.
270− singleton a = unstream (S.singleton (safe a))
271−"JSSTRING singleton -> unfused" [1] forall a.
272− unstream (S.singleton (safe a)) = singleton a
275−foreign import javascript unsafe "$r = $1;" addrToByteArray :: Addr# -> ByteArray#
277−{-# RULES "inlineJSString" forall s. pack (GHC.Base.build (GHC.unpackFoldrCString# s)) = Text (JSString (JSVal (addrToByteArray s))) #-}
279−-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
280−unpackCString# :: Addr# -> Text
281−unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
282−{-# NOINLINE unpackCString# #-}
284−{-# RULES "TEXT literal" forall a.
285− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
286− = unpackCString# a #-}
288−{-# RULES "TEXT literal UTF8" [1] forall a.
289− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
290− = unpackCString# a #-}
292−{-# RULES "TEXT empty literal" [1]
293− unstream (S.map safe (S.streamList []))
296−{-# RULES "TEXT singleton literal" [1] forall a.
297− unstream (S.map safe (S.streamList [a]))
300−---------------------------------------------------------------------
302-- $strict
249-- $strict
304-- This package provides both strict and lazy 'Text' types. The
251-- This package provides both strict and lazy 'Text' types. The
@@ -374,19 +321,14 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
374-- Functions that can be fused by the compiler are documented with the
321-- Functions that can be fused by the compiler are documented with the
375-- phrase \"Subject to fusion\".
322-- phrase \"Subject to fusion\".
377−-- instance Eq Text where
378−-- Text arrA offA lenA == Text arrB offB lenB
379−-- | lenA == lenB = A.equal arrA offA arrB offB lenA
380−-- | otherwise = False
381−-- {-# INLINE (==) #-}
382instance Eq Text where
324instance Eq Text where
383− Text x == Text y = x == y
325+ Text arrA offA lenA == Text arrB offB lenB
384−
326+ | lenA == lenB = A.equal arrA offA arrB offB lenA
385−-- instance Ord Text where
327+ | otherwise = False
386−-- compare = compareText
328+ {-# INLINE (==) #-}
388instance Ord Text where
330instance Ord Text where
389− compare (Text x) (Text y) = compare x y
331+ compare = compareText
391instance Read Text where
333instance Read Text where
392 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
@@ -456,7 +398,7 @@ instance Data Text where
456#if MIN_VERSION_base(4,7,0)
398#if MIN_VERSION_base(4,7,0)
457-- | Only defined for @base-4.7.0.0@ and later
399-- | Only defined for @base-4.7.0.0@ and later
458instance PrintfArg Text where
400instance PrintfArg Text where
459− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
462packConstr :: Constr
404packConstr :: Constr
@@ -465,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
465textDataType :: DataType
407textDataType :: DataType
466textDataType = mkDataType "Data.Text.Text" [packConstr]
408textDataType = mkDataType "Data.Text.Text" [packConstr]
468−-- -- | /O(n)/ Compare two 'Text' values lexicographically.
410+-- | /O(n)/ Compare two 'Text' values lexicographically.
469−-- compareText :: Text -> Text -> Ordering
411+compareText :: Text -> Text -> Ordering
470−-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
412+compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
471−-- | lenA == 0 && lenB == 0 = EQ
413+ | lenA == 0 && lenB == 0 = EQ
472−-- | otherwise = go 0 0
414+ | otherwise = go 0 0
474−-- go !i !j
416+ go !i !j
475−-- | i >= lenA || j >= lenB = compare lenA lenB
417+ | i >= lenA || j >= lenB = compare lenA lenB
476−-- | a < b = LT
418+ | a < b = LT
477−-- | a > b = GT
419+ | a > b = GT
478−-- | otherwise = go (i+di) (j+dj)
420+ | otherwise = go (i+di) (j+dj)
479−-- where Iter a di = iter ta i
421+ where Iter a di = iter ta i
480−-- Iter b dj = iter tb j
422+ Iter b dj = iter tb j
482-- -----------------------------------------------------------------------------
424-- -----------------------------------------------------------------------------
483-- * Conversion to/from 'Text'
425-- * Conversion to/from 'Text'
@@ -485,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
485-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
486-- fusion. Performs replacement on invalid scalar values.
428-- fusion. Performs replacement on invalid scalar values.
487pack :: String -> Text
429pack :: String -> Text
488−pack = coerce JSS.pack
489−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
490{-# INLINE [1] pack #-}
431{-# INLINE [1] pack #-}
492-- -----------------------------------------------------------------------------
433-- -----------------------------------------------------------------------------
@@ -497,8 +438,7 @@ pack = coerce JSS.pack
497-- copying a new array. Subject to fusion. Performs replacement on
438-- copying a new array. Subject to fusion. Performs replacement on
498-- invalid scalar values.
439-- invalid scalar values.
499cons :: Char -> Text -> Text
440cons :: Char -> Text -> Text
500−cons = coerce JSS.cons
501−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
502{-# INLINE cons #-}
442{-# INLINE cons #-}
504infixr 5 `cons`
444infixr 5 `cons`
@@ -507,27 +447,25 @@ infixr 5 `cons`
507-- entire array in the process, unless fused. Subject to fusion.
447-- entire array in the process, unless fused. Subject to fusion.
508-- Performs replacement on invalid scalar values.
448-- Performs replacement on invalid scalar values.
509snoc :: Text -> Char -> Text
449snoc :: Text -> Char -> Text
510−snoc = coerce JSS.snoc
511−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
512{-# INLINE snoc #-}
451{-# INLINE snoc #-}
514-- | /O(n)/ Appends one 'Text' to the other by copying both of them
453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
515-- into a new 'Text'. Subject to fusion.
454-- into a new 'Text'. Subject to fusion.
516append :: Text -> Text -> Text
455append :: Text -> Text -> Text
517−append = coerce JSS.append
518−-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
456+append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
519−-- | len1 == 0 = b
457+ | len1 == 0 = b
520−-- | len2 == 0 = a
458+ | len2 == 0 = a
521−-- | len > 0 = Text (A.run x) 0 len
459+ | len > 0 = Text (A.run x) 0 len
522−-- | otherwise = overflowError "append"
460+ | otherwise = overflowError "append"
524−-- len = len1+len2
462+ len = len1+len2
525−-- x :: ST s (A.MArray s)
463+ x :: ST s (A.MArray s)
527−-- arr <- A.new len
465+ arr <- A.new len
528−-- A.copyI arr 0 arr1 off1 len1
466+ A.copyI arr 0 arr1 off1 len1
529−-- A.copyI arr len1 arr2 off2 len
467+ A.copyI arr len1 arr2 off2 len
530−-- return arr
468+ return arr
531{-# NOINLINE append #-}
469{-# NOINLINE append #-}
@@ -540,18 +478,16 @@ append = coerce JSS.append
540-- | /O(1)/ Returns the first character of a 'Text', which must be
478-- | /O(1)/ Returns the first character of a 'Text', which must be
541-- non-empty. Subject to fusion.
479-- non-empty. Subject to fusion.
542head :: Text -> Char
480head :: Text -> Char
543−head = coerce JSS.head
544−-- head t = S.head (stream t)
481+head t = S.head (stream t)
545{-# INLINE head #-}
482{-# INLINE head #-}
547-- | /O(1)/ Returns the first character and rest of a 'Text', or
484-- | /O(1)/ Returns the first character and rest of a 'Text', or
548-- 'Nothing' if empty. Subject to fusion.
485-- 'Nothing' if empty. Subject to fusion.
549uncons :: Text -> Maybe (Char, Text)
486uncons :: Text -> Maybe (Char, Text)
550−uncons = coerce JSS.uncons
551−-- uncons t@(Text arr off len)
487+uncons t@(Text arr off len)
552−-- | len <= 0 = Nothing
488+ | len <= 0 = Nothing
553−-- | otherwise = Just $ let !(Iter c d) = iter t 0
489+ | otherwise = Just $ let !(Iter c d) = iter t 0
554−-- in (c, text arr (off+d) (len-d))
490+ in (c, text arr (off+d) (len-d))
555{-# INLINE [1] uncons #-}
491{-# INLINE [1] uncons #-}
557-- | Lifted from Control.Arrow and specialized.
493-- | Lifted from Control.Arrow and specialized.
@@ -561,13 +497,12 @@ second f (a, b) = (a, f b)
561-- | /O(1)/ Returns the last character of a 'Text', which must be
497-- | /O(1)/ Returns the last character of a 'Text', which must be
562-- non-empty. Subject to fusion.
498-- non-empty. Subject to fusion.
563last :: Text -> Char
499last :: Text -> Char
564−last = coerce JSS.last
565−-- last (Text arr off len)
500+last (Text arr off len)
566−-- | len <= 0 = emptyError "last"
501+ | len <= 0 = emptyError "last"
567−-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
502+ | n < 0xDC00 || n > 0xDFFF = unsafeChr n
568−-- | otherwise = U16.chr2 n0 n
503+ | otherwise = U16.chr2 n0 n
569−-- where n = A.unsafeIndex arr (off+len-1)
504+ where n = A.unsafeIndex arr (off+len-1)
570−-- n0 = A.unsafeIndex arr (off+len-2)
505+ n0 = A.unsafeIndex arr (off+len-2)
571{-# INLINE [1] last #-}
506{-# INLINE [1] last #-}
@@ -580,11 +515,10 @@ last = coerce JSS.last
580-- | /O(1)/ Returns all characters after the head of a 'Text', which
515-- | /O(1)/ Returns all characters after the head of a 'Text', which
581-- must be non-empty. Subject to fusion.
516-- must be non-empty. Subject to fusion.
582tail :: Text -> Text
517tail :: Text -> Text
583−tail = coerce JSS.tail
584−-- tail t@(Text arr off len)
518+tail t@(Text arr off len)
585−-- | len <= 0 = emptyError "tail"
519+ | len <= 0 = emptyError "tail"
586−-- | otherwise = text arr (off+d) (len-d)
520+ | otherwise = text arr (off+d) (len-d)
587−-- where d = iter_ t 0
521+ where d = iter_ t 0
588{-# INLINE [1] tail #-}
522{-# INLINE [1] tail #-}
@@ -597,12 +531,11 @@ tail = coerce JSS.tail
597-- | /O(1)/ Returns all but the last character of a 'Text', which must
531-- | /O(1)/ Returns all but the last character of a 'Text', which must
598-- be non-empty. Subject to fusion.
532-- be non-empty. Subject to fusion.
599init :: Text -> Text
533init :: Text -> Text
600−init = coerce JSS.init
601−-- init (Text arr off len) | len <= 0 = emptyError "init"
534+init (Text arr off len) | len <= 0 = emptyError "init"
602−-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
535+ | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
603−-- | otherwise = text arr off (len-1)
536+ | otherwise = text arr off (len-1)
605−-- n = A.unsafeIndex arr (off+len-1)
538+ n = A.unsafeIndex arr (off+len-1)
606{-# INLINE [1] init #-}
539{-# INLINE [1] init #-}
@@ -615,12 +548,11 @@ init = coerce JSS.init
615-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
616-- fusion.
549-- fusion.
617null :: Text -> Bool
550null :: Text -> Bool
618−null = coerce JSS.null
619−-- null (Text _arr _off len) =
551+null (Text _arr _off len) =
620−-- #if defined(ASSERTS)
552+#if defined(ASSERTS)
621−-- assert (len >= 0) $
553+ assert (len >= 0) $
623−-- len <= 0
555+ len <= 0
624{-# INLINE [1] null #-}
556{-# INLINE [1] null #-}
@@ -632,15 +564,14 @@ null = coerce JSS.null
633-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
634-- Subject to fusion.
566-- Subject to fusion.
635−-- isSingleton :: Text -> Bool
567+isSingleton :: Text -> Bool
636−-- isSingleton = S.isSingleton . stream
568+isSingleton = S.isSingleton . stream
637−-- {-# INLINE isSingleton #-}
569+{-# INLINE isSingleton #-}
639-- | /O(n)/ Returns the number of characters in a 'Text'.
571-- | /O(n)/ Returns the number of characters in a 'Text'.
640-- Subject to fusion.
572-- Subject to fusion.
641length :: Text -> Int
573length :: Text -> Int
642−length = coerce JSS.length
643−-- length t = S.length (stream t)
574+length t = S.length (stream t)
644{-# INLINE [0] length #-}
575{-# INLINE [0] length #-}
645-- length needs to be phased after the compareN/length rules otherwise
576-- length needs to be phased after the compareN/length rules otherwise
646-- it may inline before the rules have an opportunity to fire.
577-- it may inline before the rules have an opportunity to fire.
@@ -696,30 +627,26 @@ compareLength t n = S.compareLengthI (stream t) n
696-- each element of @t@. Subject to fusion. Performs replacement on
627-- each element of @t@. Subject to fusion. Performs replacement on
697-- invalid scalar values.
628-- invalid scalar values.
698map :: (Char -> Char) -> Text -> Text
629map :: (Char -> Char) -> Text -> Text
700−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
701{-# INLINE [1] map #-}
631{-# INLINE [1] map #-}
703-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
704-- 'Text's and concatenates the list after interspersing the first
634-- 'Text's and concatenates the list after interspersing the first
705-- argument between each element of the list.
635-- argument between each element of the list.
706intercalate :: Text -> [Text] -> Text
636intercalate :: Text -> [Text] -> Text
707−intercalate = coerce JSS.intercalate
708−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
709{-# INLINE intercalate #-}
638{-# INLINE intercalate #-}
711-- | /O(n)/ The 'intersperse' function takes a character and places it
640-- | /O(n)/ The 'intersperse' function takes a character and places it
712-- between the characters of a 'Text'. Subject to fusion. Performs
641-- between the characters of a 'Text'. Subject to fusion. Performs
713-- replacement on invalid scalar values.
642-- replacement on invalid scalar values.
714intersperse :: Char -> Text -> Text
643intersperse :: Char -> Text -> Text
715−intersperse = coerce JSS.intersperse
716−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
717{-# INLINE intersperse #-}
645{-# INLINE intersperse #-}
719-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
647-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
720reverse :: Text -> Text
648reverse :: Text -> Text
721−reverse = coerce JSS.reverse
722−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
723{-# INLINE reverse #-}
650{-# INLINE reverse #-}
725-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -753,29 +680,28 @@ replace :: Text
754 -- ^ @haystack@ in which to search.
681 -- ^ @haystack@ in which to search.
756−replace = coerce JSS.replace
757−-- replace needle@(Text _ _ neeLen)
683+replace needle@(Text _ _ neeLen)
758−-- (Text repArr repOff repLen)
684+ (Text repArr repOff repLen)
759−-- haystack@(Text hayArr hayOff hayLen)
685+ haystack@(Text hayArr hayOff hayLen)
760−-- | neeLen == 0 = emptyError "replace"
686+ | neeLen == 0 = emptyError "replace"
761−-- | L.null ixs = haystack
687+ | L.null ixs = haystack
762−-- | len > 0 = Text (A.run x) 0 len
688+ | len > 0 = Text (A.run x) 0 len
763−-- | otherwise = empty
689+ | otherwise = empty
765−-- ixs = indices needle haystack
691+ ixs = indices needle haystack
766−-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
692+ len = hayLen - (neeLen - repLen) `mul` L.length ixs
767−-- x :: ST s (A.MArray s)
693+ x :: ST s (A.MArray s)
769−-- marr <- A.new len
695+ marr <- A.new len
770−-- let loop (i:is) o d = do
696+ let loop (i:is) o d = do
771−-- let d0 = d + i - o
697+ let d0 = d + i - o
772−-- d1 = d0 + repLen
698+ d1 = d0 + repLen
773−-- A.copyI marr d hayArr (hayOff+o) d0
699+ A.copyI marr d hayArr (hayOff+o) d0
774−-- A.copyI marr d0 repArr repOff d1
700+ A.copyI marr d0 repArr repOff d1
775−-- loop is (i + neeLen) d1
701+ loop is (i + neeLen) d1
776−-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
702+ loop [] o d = A.copyI marr d hayArr (hayOff+o) len
777−-- loop ixs 0 0
703+ loop ixs 0 0
778−-- return marr
704+ return marr
780-- ----------------------------------------------------------------------------
706-- ----------------------------------------------------------------------------
781-- ** Case conversions (folds)
707-- ** Case conversions (folds)
@@ -814,8 +740,7 @@ replace = coerce JSS.replace
814-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
815-- instead of itself.
741-- instead of itself.
816toCaseFold :: Text -> Text
742toCaseFold :: Text -> Text
817−-- toCaseFold t = unstream (S.toCaseFold (stream t))
743+toCaseFold t = unstream (S.toCaseFold (stream t))
818−toCaseFold = coerce JSS.toCaseFold
819{-# INLINE toCaseFold #-}
744{-# INLINE toCaseFold #-}
821-- | /O(n)/ Convert a string to lower case, using simple case
746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -826,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
826-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
827-- followed by \" ̇\" (combining dot above, U+0307).
752-- followed by \" ̇\" (combining dot above, U+0307).
828toLower :: Text -> Text
753toLower :: Text -> Text
829−-- toLower t = unstream (S.toLower (stream t))
754+toLower t = unstream (S.toLower (stream t))
830−toLower = coerce JSS.toLower
831{-# INLINE toLower #-}
755{-# INLINE toLower #-}
833-- | /O(n)/ Convert a string to upper case, using simple case
757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -837,8 +761,7 @@ toLower = coerce JSS.toLower
837-- instance, the German \"ß\" (eszett, U+00DF) maps to the
761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
838-- two-letter sequence \"SS\".
762-- two-letter sequence \"SS\".
839toUpper :: Text -> Text
763toUpper :: Text -> Text
840−-- toUpper t = unstream (S.toUpper (stream t))
764+toUpper t = unstream (S.toUpper (stream t))
841−toUpper = coerce JSS.toUpper
842{-# INLINE toUpper #-}
765{-# INLINE toUpper #-}
844-- | /O(n)/ Convert a string to title case, using simple case
767-- | /O(n)/ Convert a string to title case, using simple case
@@ -860,8 +783,7 @@ toUpper = coerce JSS.toUpper
860-- Fox\" is correctly title cased—but this function will
783-- Fox\" is correctly title cased—but this function will
861-- capitalize /every/ word.
784-- capitalize /every/ word.
862toTitle :: Text -> Text
785toTitle :: Text -> Text
863−toTitle = coerce JSS.toTitle
864−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
865{-# INLINE toTitle #-}
787{-# INLINE toTitle #-}
867-- | /O(n)/ Left-justify a string to the given length, using the
789-- | /O(n)/ Left-justify a string to the given length, using the
@@ -873,11 +795,10 @@ toTitle = coerce JSS.toTitle
873-- > justifyLeft 7 'x' "foo" == "fooxxxx"
795-- > justifyLeft 7 'x' "foo" == "fooxxxx"
874-- > justifyLeft 3 'x' "foobar" == "foobar"
796-- > justifyLeft 3 'x' "foobar" == "foobar"
875justifyLeft :: Int -> Char -> Text -> Text
797justifyLeft :: Int -> Char -> Text -> Text
876−justifyLeft = coerce JSS.justifyLeft
798+justifyLeft k c t
878−-- | len >= k = t
799+ | len >= k = t
879−-- | otherwise = t `append` replicateChar (k-len) c
800+ | otherwise = t `append` replicateChar (k-len) c
880−-- where len = length t
801+ where len = length t
881{-# INLINE [1] justifyLeft #-}
802{-# INLINE [1] justifyLeft #-}
@@ -896,11 +817,10 @@ justifyLeft = coerce JSS.justifyLeft
896-- > justifyRight 7 'x' "bar" == "xxxxbar"
817-- > justifyRight 7 'x' "bar" == "xxxxbar"
897-- > justifyRight 3 'x' "foobar" == "foobar"
818-- > justifyRight 3 'x' "foobar" == "foobar"
898justifyRight :: Int -> Char -> Text -> Text
819justifyRight :: Int -> Char -> Text -> Text
899−justifyRight = coerce JSS.justifyRight
820+justifyRight k c t
900−-- justifyRight k c t
901−-- | len >= k = t
821+ | len >= k = t
902−-- | otherwise = replicateChar (k-len) c `append` t
822+ | otherwise = replicateChar (k-len) c `append` t
903−-- where len = length t
823+ where len = length t
904{-# INLINE justifyRight #-}
824{-# INLINE justifyRight #-}
906-- | /O(n)/ Center a string to the given length, using the specified
826-- | /O(n)/ Center a string to the given length, using the specified
@@ -911,14 +831,13 @@ justifyRight = coerce JSS.justifyRight
912-- > center 8 'x' "HS" = "xxxHSxxx"
832-- > center 8 'x' "HS" = "xxxHSxxx"
913center :: Int -> Char -> Text -> Text
833center :: Int -> Char -> Text -> Text
914−center = coerce JSS.center
834+center k c t
916−-- | len >= k = t
835+ | len >= k = t
917−-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
836+ | otherwise = replicateChar l c `append` t `append` replicateChar r c
918−-- where len = length t
837+ where len = length t
919−-- d = k - len
838+ d = k - len
920−-- r = d `quot` 2
839+ r = d `quot` 2
921−-- l = d - r
840+ l = d - r
922{-# INLINE center #-}
841{-# INLINE center #-}
924-- | /O(n)/ The 'transpose' function transposes the rows and columns
843-- | /O(n)/ The 'transpose' function transposes the rows and columns
@@ -926,8 +845,7 @@ center = coerce JSS.center
926-- 'unpack', and the list version of transpose, and is thus not very
845-- 'unpack', and the list version of transpose, and is thus not very
927-- efficient.
846-- efficient.
928transpose :: [Text] -> [Text]
847transpose :: [Text] -> [Text]
929−transpose = coerce JSS.transpose
930−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
932-- -----------------------------------------------------------------------------
850-- -----------------------------------------------------------------------------
933-- * Reducing 'Text's (folds)
851-- * Reducing 'Text's (folds)
@@ -937,27 +855,23 @@ transpose = coerce JSS.transpose
937-- reduces the 'Text' using the binary operator, from left to right.
855-- reduces the 'Text' using the binary operator, from left to right.
938-- Subject to fusion.
856-- Subject to fusion.
939foldl :: (a -> Char -> a) -> a -> Text -> a
857foldl :: (a -> Char -> a) -> a -> Text -> a
940−foldl f a0 (Text t) = JSS.foldl f a0 t
941−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
942{-# INLINE foldl #-}
859{-# INLINE foldl #-}
944-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
945foldl' :: (a -> Char -> a) -> a -> Text -> a
862foldl' :: (a -> Char -> a) -> a -> Text -> a
946−foldl' f a0 (Text t) = JSS.foldl' f a0 t
947−-- foldl' f z t = S.foldl' f z (stream t)
863+foldl' f z t = S.foldl' f z (stream t)
948{-# INLINE foldl' #-}
864{-# INLINE foldl' #-}
950-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
951-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
952foldl1 :: (Char -> Char -> Char) -> Text -> Char
868foldl1 :: (Char -> Char -> Char) -> Text -> Char
953−foldl1 = coerce JSS.foldl1
954−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
955{-# INLINE foldl1 #-}
870{-# INLINE foldl1 #-}
957-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
958foldl1' :: (Char -> Char -> Char) -> Text -> Char
873foldl1' :: (Char -> Char -> Char) -> Text -> Char
959−foldl1' = coerce JSS.foldl1'
960−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
961{-# INLINE foldl1' #-}
875{-# INLINE foldl1' #-}
963-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -965,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
965-- reduces the 'Text' using the binary operator, from right to left.
879-- reduces the 'Text' using the binary operator, from right to left.
966-- Subject to fusion.
880-- Subject to fusion.
967foldr :: (Char -> a -> a) -> a -> Text -> a
881foldr :: (Char -> a -> a) -> a -> Text -> a
968−foldr f a0 (Text t) = JSS.foldr f a0 t
969−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
970{-# INLINE foldr #-}
883{-# INLINE foldr #-}
972-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
973-- and thus must be applied to a non-empty 'Text'. Subject to
886-- and thus must be applied to a non-empty 'Text'. Subject to
974-- fusion.
887-- fusion.
975foldr1 :: (Char -> Char -> Char) -> Text -> Char
888foldr1 :: (Char -> Char -> Char) -> Text -> Char
976−foldr1 = coerce JSS.foldr1
977−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
978{-# INLINE foldr1 #-}
890{-# INLINE foldr1 #-}
980-- -----------------------------------------------------------------------------
892-- -----------------------------------------------------------------------------
@@ -982,54 +894,48 @@ foldr1 = coerce JSS.foldr1
983-- | /O(n)/ Concatenate a list of 'Text's.
895-- | /O(n)/ Concatenate a list of 'Text's.
984concat :: [Text] -> Text
896concat :: [Text] -> Text
985−concat = coerce JSS.concat
986−-- concat ts = case ts' of
897+concat ts = case ts' of
987−-- [] -> empty
898+ [] -> empty
988−-- [t] -> t
899+ [t] -> t
989−-- _ -> Text (A.run go) 0 len
900+ _ -> Text (A.run go) 0 len
991−-- ts' = L.filter (not . null) ts
902+ ts' = L.filter (not . null) ts
992−-- len = sumP "concat" $ L.map lengthWord16 ts'
903+ len = sumP "concat" $ L.map lengthWord16 ts'
993−-- go :: ST s (A.MArray s)
904+ go :: ST s (A.MArray s)
994−-- go = do
905+ go = do
995−-- arr <- A.new len
906+ arr <- A.new len
996−-- let step i (Text a o l) =
907+ let step i (Text a o l) =
997−-- let !j = i + l in A.copyI arr i a o j >> return j
908+ let !j = i + l in A.copyI arr i a o j >> return j
998−-- foldM step 0 ts' >> return arr
909+ foldM step 0 ts' >> return arr
1000-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
1001-- concatenate the results.
912-- concatenate the results.
1002concatMap :: (Char -> Text) -> Text -> Text
913concatMap :: (Char -> Text) -> Text -> Text
1003−concatMap = coerce JSS.concatMap
1004−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
1005{-# INLINE concatMap #-}
915{-# INLINE concatMap #-}
1007-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1008-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1009any :: (Char -> Bool) -> Text -> Bool
919any :: (Char -> Bool) -> Text -> Bool
1010−any = coerce JSS.any
1011−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1012{-# INLINE any #-}
921{-# INLINE any #-}
1014-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1015-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1016all :: (Char -> Bool) -> Text -> Bool
925all :: (Char -> Bool) -> Text -> Bool
1017−all = coerce JSS.all
1018−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1019{-# INLINE all #-}
927{-# INLINE all #-}
1021-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1022-- must be non-empty. Subject to fusion.
930-- must be non-empty. Subject to fusion.
1023maximum :: Text -> Char
931maximum :: Text -> Char
1024−maximum = coerce JSS.maximum
1025−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1026{-# INLINE maximum #-}
933{-# INLINE maximum #-}
1028-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1029-- must be non-empty. Subject to fusion.
936-- must be non-empty. Subject to fusion.
1030minimum :: Text -> Char
937minimum :: Text -> Char
1031−minimum = coerce JSS.minimum
1032−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1033{-# INLINE minimum #-}
939{-# INLINE minimum #-}
1035-- -----------------------------------------------------------------------------
941-- -----------------------------------------------------------------------------
@@ -1045,9 +951,8 @@ minimum = coerce JSS.minimum
1046-- > last (scanl f z xs) == foldl f z xs.
952-- > last (scanl f z xs) == foldl f z xs.
1047scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1048−scanl = coerce JSS.scanl
1049−-- scanl f z t = unstream (S.scanl g z (stream t))
954+scanl f z t = unstream (S.scanl g z (stream t))
1050−-- where g a b = safe (f a b)
955+ where g a b = safe (f a b)
1051{-# INLINE scanl #-}
956{-# INLINE scanl #-}
1053-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -1056,9 +961,8 @@ scanl = coerce JSS.scanl
1057-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1058scanl1 :: (Char -> Char -> Char) -> Text -> Text
963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1059−scanl1 = coerce JSS.scanl1
1060−-- scanl1 f t | null t = empty
964+scanl1 f t | null t = empty
1061−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1062{-# INLINE scanl1 #-}
966{-# INLINE scanl1 #-}
1064-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -1066,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1067-- > scanr f v == reverse . scanl (flip f) v . reverse
971-- > scanr f v == reverse . scanl (flip f) v . reverse
1068scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1069−scanr = coerce JSS.scanr
1070−-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
973+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1071−-- where g a b = safe (f a b)
974+ where g a b = safe (f a b)
1072{-# INLINE scanr #-}
975{-# INLINE scanr #-}
1074-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1075-- value argument. Subject to fusion. Performs replacement on
978-- value argument. Subject to fusion. Performs replacement on
1076-- invalid scalar values.
979-- invalid scalar values.
1077scanr1 :: (Char -> Char -> Char) -> Text -> Text
980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1078−scanr1 = coerce JSS.scanr1
1079−-- scanr1 f t | null t = empty
981+scanr1 f t | null t = empty
1080−-- | otherwise = scanr f (last t) (init t)
982+ | otherwise = scanr f (last t) (init t)
1081{-# INLINE scanr1 #-}
983{-# INLINE scanr1 #-}
1083-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1085,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1085-- parameter from left to right, and returns a final 'Text'. Performs
987-- parameter from left to right, and returns a final 'Text'. Performs
1086-- replacement on invalid scalar values.
988-- replacement on invalid scalar values.
1087mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1088−mapAccumL f z0 (Text a) =
1089− let (a', t) = JSS.mapAccumL f z0 a
1091−-- mapAccumL f z0 = S.mapAccumL g z0 . stream
990+mapAccumL f z0 = S.mapAccumL g z0 . stream
1092−-- where g a b = second safe (f a b)
991+ where g a b = second safe (f a b)
1093{-# INLINE mapAccumL #-}
992{-# INLINE mapAccumL #-}
1095-- | The 'mapAccumR' function behaves like a combination of 'map' and
994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1099,11 +998,8 @@ mapAccumL f z0 (Text a) =
1099-- 'Text'.
998-- 'Text'.
1100-- Performs replacement on invalid scalar values.
999-- Performs replacement on invalid scalar values.
1101mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1102−mapAccumR f z0 (Text a) =
1103− let (a', t) = JSS.mapAccumR f z0 a
1105−-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1001+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1106−-- where g a b = second safe (f a b)
1002+ where g a b = second safe (f a b)
1107{-# INLINE mapAccumR #-}
1003{-# INLINE mapAccumR #-}
1109-- -----------------------------------------------------------------------------
1005-- -----------------------------------------------------------------------------
@@ -1112,21 +1008,20 @@ mapAccumR f z0 (Text a) =
1112-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1113-- @t@ repeated @n@ times.
1009-- @t@ repeated @n@ times.
1114replicate :: Int -> Text -> Text
1010replicate :: Int -> Text -> Text
1115−replicate = coerce JSS.replicate
1116−-- replicate n t@(Text a o l)
1011+replicate n t@(Text a o l)
1117−-- | n <= 0 || l <= 0 = empty
1012+ | n <= 0 || l <= 0 = empty
1118−-- | n == 1 = t
1013+ | n == 1 = t
1119−-- | isSingleton t = replicateChar n (unsafeHead t)
1014+ | isSingleton t = replicateChar n (unsafeHead t)
1120−-- | otherwise = Text (A.run x) 0 len
1015+ | otherwise = Text (A.run x) 0 len
1122−-- len = l `mul` n
1017+ len = l `mul` n
1123−-- x :: ST s (A.MArray s)
1018+ x :: ST s (A.MArray s)
1124−-- x = do
1019+ x = do
1125−-- arr <- A.new len
1020+ arr <- A.new len
1126−-- let loop !d !i | i >= n = return arr
1021+ let loop !d !i | i >= n = return arr
1127−-- | otherwise = let m = d + l
1022+ | otherwise = let m = d + l
1128−-- in A.copyI arr d a o m >> loop m (i+1)
1023+ in A.copyI arr d a o m >> loop m (i+1)
1129−-- loop 0 0
1024+ loop 0 0
1130{-# INLINE [1] replicate #-}
1025{-# INLINE [1] replicate #-}
1132{-# RULES
1027{-# RULES
@@ -1137,19 +1032,8 @@ replicate = coerce JSS.replicate
1137-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1138-- value of every element. Subject to fusion.
1033-- value of every element. Subject to fusion.
1139replicateChar :: Int -> Char -> Text
1034replicateChar :: Int -> Char -> Text
1141replicateChar n c = unstream (S.replicateCharI n (safe c))
1035replicateChar n c = unstream (S.replicateCharI n (safe c))
1142{-# INLINE replicateChar #-}
1036{-# INLINE replicateChar #-}
1144−replicateChar n c = Text $ js_replicateChar n c
1145−{-# INLINE [1] replicateChar #-}
1147−"JSSTRING replicateChar -> fused" [~1] forall n c.
1148− replicateChar n c = unstream (S.replicateCharI n (safe c))
1149−"JSSTRING replicateChar -> unfused" [1] forall n c.
1150− unstream (S.replicateCharI n (safe c)) = replicateChar n c
1154-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1155-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
1039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1159,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
1159-- string, and @b@ is the seed value for further production. Subject
1043-- string, and @b@ is the seed value for further production. Subject
1160-- to fusion. Performs replacement on invalid scalar values.
1044-- to fusion. Performs replacement on invalid scalar values.
1161unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1162−unfoldr f a = Text $ JSS.unfoldr f a
1163−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1164{-# INLINE unfoldr #-}
1047{-# INLINE unfoldr #-}
1166-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
1049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1170,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
1170-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1171-- to fusion. Performs replacement on invalid scalar values.
1054-- to fusion. Performs replacement on invalid scalar values.
1172unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1173−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1174−-- 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)
1175{-# INLINE unfoldrN #-}
1057{-# INLINE unfoldrN #-}
1177-- -----------------------------------------------------------------------------
1059-- -----------------------------------------------------------------------------
@@ -1181,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1181-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1182-- the length of the Text. Subject to fusion.
1064-- the length of the Text. Subject to fusion.
1183take :: Int -> Text -> Text
1065take :: Int -> Text -> Text
1184−take = coerce JSS.take
1185−-- take n t@(Text arr off len)
1066+take n t@(Text arr off len)
1186−-- | n <= 0 = empty
1067+ | n <= 0 = empty
1187−-- | n >= len = t
1068+ | n >= len = t
1188−-- | otherwise = text arr off (iterN n t)
1069+ | otherwise = text arr off (iterN n t)
1189{-# INLINE [1] take #-}
1070{-# INLINE [1] take #-}
1191−-- iterN :: Int -> Text -> Int
1072+iterN :: Int -> Text -> Int
1192−-- iterN n t@(Text _arr _off len) = loop 0 0
1073+iterN n t@(Text _arr _off len) = loop 0 0
1193−-- where loop !i !cnt
1074+ where loop !i !cnt
1194−-- | i >= len || cnt >= n = i
1075+ | i >= len || cnt >= n = i
1195−-- | otherwise = loop (i+d) (cnt+1)
1076+ | otherwise = loop (i+d) (cnt+1)
1196−-- where d = iter_ t i
1077+ where d = iter_ t i
1198{-# RULES
1079{-# RULES
1199"TEXT take -> fused" [~1] forall n t.
1080"TEXT take -> fused" [~1] forall n t.
@@ -1209,31 +1090,29 @@ take = coerce JSS.take
1210-- > takeEnd 3 "foobar" == "bar"
1091-- > takeEnd 3 "foobar" == "bar"
1211takeEnd :: Int -> Text -> Text
1092takeEnd :: Int -> Text -> Text
1212−takeEnd = coerce JSS.takeEnd
1213−-- takeEnd n t@(Text arr off len)
1093+takeEnd n t@(Text arr off len)
1214−-- | n <= 0 = empty
1094+ | n <= 0 = empty
1215−-- | n >= len = t
1095+ | n >= len = t
1216−-- | otherwise = text arr (off+i) (len-i)
1096+ | otherwise = text arr (off+i) (len-i)
1217−-- where i = iterNEnd n t
1097+ where i = iterNEnd n t
1219−-- iterNEnd :: Int -> Text -> Int
1099+iterNEnd :: Int -> Text -> Int
1220−-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1100+iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1221−-- where loop i !m
1101+ where loop i !m
1222−-- | i <= 0 = 0
1102+ | i <= 0 = 0
1223−-- | m <= 1 = i
1103+ | m <= 1 = i
1224−-- | otherwise = loop (i+d) (m-1)
1104+ | otherwise = loop (i+d) (m-1)
1225−-- where d = reverseIter_ t i
1105+ where d = reverseIter_ t i
1227-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1228-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1229-- is greater than the length of the 'Text'. Subject to fusion.
1109-- is greater than the length of the 'Text'. Subject to fusion.
1230drop :: Int -> Text -> Text
1110drop :: Int -> Text -> Text
1231−drop = coerce JSS.drop
1232−-- drop n t@(Text arr off len)
1111+drop n t@(Text arr off len)
1233−-- | n <= 0 = t
1112+ | n <= 0 = t
1234−-- | n >= len = empty
1113+ | n >= len = empty
1235−-- | otherwise = text arr (off+i) (len-i)
1114+ | otherwise = text arr (off+i) (len-i)
1236−-- where i = iterN n t
1115+ where i = iterN n t
1237{-# INLINE [1] drop #-}
1116{-# INLINE [1] drop #-}
1239{-# RULES
1118{-# RULES
@@ -1250,22 +1129,20 @@ drop = coerce JSS.drop
1251-- > dropEnd 3 "foobar" == "foo"
1130-- > dropEnd 3 "foobar" == "foo"
1252dropEnd :: Int -> Text -> Text
1131dropEnd :: Int -> Text -> Text
1253−dropEnd = coerce JSS.dropEnd
1254−-- dropEnd n t@(Text arr off len)
1132+dropEnd n t@(Text arr off len)
1255−-- | n <= 0 = t
1133+ | n <= 0 = t
1256−-- | n >= len = empty
1134+ | n >= len = empty
1257−-- | otherwise = text arr off (iterNEnd n t)
1135+ | otherwise = text arr off (iterNEnd n t)
1259-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1260-- returns the longest prefix (possibly empty) of elements that
1138-- returns the longest prefix (possibly empty) of elements that
1261-- satisfy @p@. Subject to fusion.
1139-- satisfy @p@. Subject to fusion.
1262takeWhile :: (Char -> Bool) -> Text -> Text
1140takeWhile :: (Char -> Bool) -> Text -> Text
1263−takeWhile = coerce JSS.takeWhile
1264−-- takeWhile p t@(Text arr off len) = loop 0
1141+takeWhile p t@(Text arr off len) = loop 0
1265−-- where loop !i | i >= len = t
1142+ where loop !i | i >= len = t
1266−-- | p c = loop (i+d)
1143+ | p c = loop (i+d)
1267−-- | otherwise = text arr off i
1144+ | otherwise = text arr off i
1268−-- where Iter c d = iter t i
1145+ where Iter c d = iter t i
1269{-# INLINE [1] takeWhile #-}
1146{-# INLINE [1] takeWhile #-}
1271{-# RULES
1148{-# RULES
@@ -1282,20 +1159,11 @@ takeWhile = coerce JSS.takeWhile
1283-- > takeWhileEnd (=='o') "foo" == "oo"
1160-- > takeWhileEnd (=='o') "foo" == "oo"
1284takeWhileEnd :: (Char -> Bool) -> Text -> Text
1161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1286takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1287 where loop !i !l | l <= 0 = t
1163 where loop !i !l | l <= 0 = t
1288 | p c = loop (i+d) (l+d)
1164 | p c = loop (i+d) (l+d)
1289 | otherwise = text arr (off+l) (len-l)
1165 | otherwise = text arr (off+l) (len-l)
1290 where (c,d) = reverseIter t i
1166 where (c,d) = reverseIter t i
1292−takeWhileEnd p t@(Text jst) = loop (len-1) len
1293− where loop !i !l | l <= 0 = t
1294− | p c = loop (i+d) (l+d)
1295− | otherwise = Text $ js_substr1 l jst
1296− where (c,d) = reverseIter t i
1299{-# INLINE [1] takeWhileEnd #-}
1167{-# INLINE [1] takeWhileEnd #-}
1301{-# RULES
1169{-# RULES
@@ -1308,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
1308-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1309-- 'takeWhile' @p@ @t@. Subject to fusion.
1177-- 'takeWhile' @p@ @t@. Subject to fusion.
1310dropWhile :: (Char -> Bool) -> Text -> Text
1178dropWhile :: (Char -> Bool) -> Text -> Text
1311−dropWhile = coerce JSS.dropWhile
1312−-- dropWhile p t@(Text arr off len) = loop 0 0
1179+dropWhile p t@(Text arr off len) = loop 0 0
1313−-- where loop !i !l | l >= len = empty
1180+ where loop !i !l | l >= len = empty
1314−-- | p c = loop (i+d) (l+d)
1181+ | p c = loop (i+d) (l+d)
1315−-- | otherwise = Text arr (off+i) (len-l)
1182+ | otherwise = Text arr (off+i) (len-l)
1316−
1183+ where Iter c d = iter t i
1317{-# INLINE [1] dropWhile #-}
1184{-# INLINE [1] dropWhile #-}
1319{-# RULES
1186{-# RULES
@@ -1331,12 +1198,11 @@ dropWhile = coerce JSS.dropWhile
1332-- > dropWhileEnd (=='.') "foo..." == "foo"
1199-- > dropWhileEnd (=='.') "foo..." == "foo"
1333dropWhileEnd :: (Char -> Bool) -> Text -> Text
1200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1334−dropWhileEnd = coerce JSS.dropWhileEnd
1335−-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1201+dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1336−-- where loop !i !l | l <= 0 = empty
1202+ where loop !i !l | l <= 0 = empty
1337−-- | p c = loop (i+d) (l+d)
1203+ | p c = loop (i+d) (l+d)
1338−-- | otherwise = Text arr off l
1204+ | otherwise = Text arr off l
1339−-- where (c,d) = reverseIter t i
1205+ where (c,d) = reverseIter t i
1340{-# INLINE [1] dropWhileEnd #-}
1206{-# INLINE [1] dropWhileEnd #-}
1342{-# RULES
1208{-# RULES
@@ -1379,49 +1245,45 @@ strip = dropAround isSpace
1379-- prefix of @t@ of length @n@, and whose second is the remainder of
1245-- prefix of @t@ of length @n@, and whose second is the remainder of
1380-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1381splitAt :: Int -> Text -> (Text, Text)
1247splitAt :: Int -> Text -> (Text, Text)
1382−splitAt = coerce JSS.splitAt
1383−-- splitAt n t@(Text arr off len)
1248+splitAt n t@(Text arr off len)
1384−-- | n <= 0 = (empty, t)
1249+ | n <= 0 = (empty, t)
1385−-- | n >= len = (t, empty)
1250+ | n >= len = (t, empty)
1386−-- | otherwise = let k = iterN n t
1251+ | otherwise = let k = iterN n t
1387−-- in (text arr off k, text arr (off+k) (len-k))
1252+ in (text arr off k, text arr (off+k) (len-k))
1389-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1390-- a pair whose first element is the longest prefix (possibly empty)
1255-- a pair whose first element is the longest prefix (possibly empty)
1391-- of @t@ of elements that satisfy @p@, and whose second is the
1256-- of @t@ of elements that satisfy @p@, and whose second is the
1392-- remainder of the list.
1257-- remainder of the list.
1393span :: (Char -> Bool) -> Text -> (Text, Text)
1258span :: (Char -> Bool) -> Text -> (Text, Text)
1394−span = coerce JSS.span
1395−-- span p t = case span_ p t of
1259+span p t = case span_ p t of
1396−-- (# hd,tl #) -> (hd,tl)
1260+ (# hd,tl #) -> (hd,tl)
1397{-# INLINE span #-}
1261{-# INLINE span #-}
1399-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1400-- over elements that fail the predicate @p@.
1264-- over elements that fail the predicate @p@.
1401break :: (Char -> Bool) -> Text -> (Text, Text)
1265break :: (Char -> Bool) -> Text -> (Text, Text)
1402−break = coerce JSS.break
1403−-- break p = span (not . p)
1266+break p = span (not . p)
1404{-# INLINE break #-}
1267{-# INLINE break #-}
1406-- | /O(n)/ Group characters in a string according to a predicate.
1269-- | /O(n)/ Group characters in a string according to a predicate.
1407groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1408−groupBy = coerce JSS.groupBy
1409−-- groupBy p = loop
1271+groupBy p = loop
1411−-- loop t@(Text arr off len)
1273+ loop t@(Text arr off len)
1412−-- | null t = []
1274+ | null t = []
1413−-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1275+ | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1414−-- where Iter c d = iter t 0
1276+ where Iter c d = iter t 0
1415−-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1277+ n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1417-- | Returns the /array/ index (in units of 'Word16') at which a
1279-- | Returns the /array/ index (in units of 'Word16') at which a
1418-- character may be found. This is /not/ the same as the logical
1280-- character may be found. This is /not/ the same as the logical
1419-- index returned by e.g. 'findIndex'.
1281-- index returned by e.g. 'findIndex'.
1420−-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1282+findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1421−-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1283+findAIndexOrEnd q t@(Text _arr _off len) = go 0
1422−-- where go !i | i >= len || q c = i
1284+ where go !i | i >= len || q c = i
1423−-- | otherwise = go (i+d)
1285+ | otherwise = go (i+d)
1424−-- where Iter c d = iter t i
1286+ where Iter c d = iter t i
1426-- | /O(n)/ Group characters in a string by equality.
1288-- | /O(n)/ Group characters in a string by equality.
1427group :: Text -> [Text]
1289group :: Text -> [Text]
@@ -1430,17 +1292,15 @@ group = groupBy (==)
1430-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1431-- first.
1293-- first.
1432inits :: Text -> [Text]
1294inits :: Text -> [Text]
1433−inits = coerce JSS.inits
1434−-- inits t@(Text arr off len) = loop 0
1295+inits t@(Text arr off len) = loop 0
1435−-- where loop i | i >= len = [t]
1296+ where loop i | i >= len = [t]
1436−-- | otherwise = Text arr off i : loop (i + iter_ t i)
1297+ | otherwise = Text arr off i : loop (i + iter_ t i)
1438-- | /O(n)/ Return all final segments of the given 'Text', longest
1299-- | /O(n)/ Return all final segments of the given 'Text', longest
1439-- first.
1300-- first.
1440tails :: Text -> [Text]
1301tails :: Text -> [Text]
1441−tails = coerce JSS.tails
1442−-- tails t | null t = [empty]
1302+tails t | null t = [empty]
1443−-- | otherwise = t : tails (unsafeTail t)
1303+ | otherwise = t : tails (unsafeTail t)
1445-- $split
1305-- $split
@@ -1473,14 +1333,13 @@ splitOn :: Text
1474 -- ^ Input text.
1334 -- ^ Input text.
1475 -> [Text]
1335 -> [Text]
1476−splitOn = coerce JSS.splitOn
1477−-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1336+splitOn pat@(Text _ _ l) src@(Text arr off len)
1478−-- | l <= 0 = emptyError "splitOn"
1337+ | l <= 0 = emptyError "splitOn"
1479−-- | isSingleton pat = split (== unsafeHead pat) src
1338+ | isSingleton pat = split (== unsafeHead pat) src
1480−-- | otherwise = go 0 (indices pat src)
1339+ | otherwise = go 0 (indices pat src)
1482−-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1341+ go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1483−-- go s _ = [text arr (s+off) (len-s)]
1342+ go s _ = [text arr (s+off) (len-s)]
1484{-# INLINE [1] splitOn #-}
1343{-# INLINE [1] splitOn #-}
1486{-# RULES
1345{-# RULES
@@ -1496,12 +1355,11 @@ splitOn = coerce JSS.splitOn
1496-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1355-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1497-- > split (=='a') "" == [""]
1356-- > split (=='a') "" == [""]
1498split :: (Char -> Bool) -> Text -> [Text]
1357split :: (Char -> Bool) -> Text -> [Text]
1499−split = coerce JSS.split
1500−-- split _ t@(Text _off _arr 0) = [t]
1358+split _ t@(Text _off _arr 0) = [t]
1501−-- split p t = loop t
1359+split p t = loop t
1502−-- where loop s | null s' = [l]
1360+ where loop s | null s' = [l]
1503−-- | otherwise = l : loop (unsafeTail s')
1361+ | otherwise = l : loop (unsafeTail s')
1504−-- where (# l, s' #) = span_ (not . p) s
1362+ where (# l, s' #) = span_ (not . p) s
1505{-# INLINE split #-}
1363{-# INLINE split #-}
1507-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
@@ -1511,12 +1369,11 @@ split = coerce JSS.split
1511-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1369-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1512-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1370-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1513chunksOf :: Int -> Text -> [Text]
1371chunksOf :: Int -> Text -> [Text]
1514−chunksOf = coerce JSS.chunksOf
1372+chunksOf k = go
1517−-- go t = case splitAt k t of
1374+ go t = case splitAt k t of
1518−-- (a,b) | null a -> []
1375+ (a,b) | null a -> []
1519−-- | otherwise -> a : go b
1376+ | otherwise -> a : go b
1520{-# INLINE chunksOf #-}
1377{-# INLINE chunksOf #-}
1522-- ----------------------------------------------------------------------------
1379-- ----------------------------------------------------------------------------
@@ -1529,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
1529-- returns the first element matching the predicate, or 'Nothing' if
1386-- returns the first element matching the predicate, or 'Nothing' if
1530-- there is no such element.
1387-- there is no such element.
1531find :: (Char -> Bool) -> Text -> Maybe Char
1388find :: (Char -> Bool) -> Text -> Maybe Char
1532−find = coerce JSS.find
1533−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
1534{-# INLINE find #-}
1390{-# INLINE find #-}
1536-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
1392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1539,16 +1395,14 @@ find = coerce JSS.find
1540-- > partition p t == (filter p t, filter (not . p) t)
1396-- > partition p t == (filter p t, filter (not . p) t)
1541partition :: (Char -> Bool) -> Text -> (Text, Text)
1397partition :: (Char -> Bool) -> Text -> (Text, Text)
1542−partition = coerce JSS.partition
1543−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
1544{-# INLINE partition #-}
1399{-# INLINE partition #-}
1546-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1547-- returns a 'Text' containing those characters that satisfy the
1402-- returns a 'Text' containing those characters that satisfy the
1548-- predicate.
1403-- predicate.
1549filter :: (Char -> Bool) -> Text -> Text
1404filter :: (Char -> Bool) -> Text -> Text
1550−filter = coerce JSS.filter
1551−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
1552{-# INLINE filter #-}
1406{-# INLINE filter #-}
1554-- | /O(n+m)/ Find the first instance of @needle@ (which must be
1408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1573,12 +1427,11 @@ filter = coerce JSS.filter
1573-- In (unlikely) bad cases, this function's time complexity degrades
1427-- In (unlikely) bad cases, this function's time complexity degrades
1574-- towards /O(n*m)/.
1428-- towards /O(n*m)/.
1575breakOn :: Text -> Text -> (Text, Text)
1429breakOn :: Text -> Text -> (Text, Text)
1576−breakOn = coerce JSS.breakOn
1577−-- breakOn pat src@(Text arr off len)
1430+breakOn pat src@(Text arr off len)
1578−-- | null pat = emptyError "breakOn"
1431+ | null pat = emptyError "breakOn"
1579−-- | otherwise = case indices pat src of
1432+ | otherwise = case indices pat src of
1580−-- [] -> (src, empty)
1433+ [] -> (src, empty)
1581−-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
1434+ (x:_) -> (text arr off x, text arr (off+x) (len-x))
1582{-# INLINE breakOn #-}
1435{-# INLINE breakOn #-}
1584-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
1437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1590,9 +1443,8 @@ breakOn = coerce JSS.breakOn
1591-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1444-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1592breakOnEnd :: Text -> Text -> (Text, Text)
1445breakOnEnd :: Text -> Text -> (Text, Text)
1593−breakOnEnd = coerce JSS.breakOnEnd
1594−-- breakOnEnd pat src = (reverse b, reverse a)
1446+breakOnEnd pat src = (reverse b, reverse a)
1595−-- where (a,b) = breakOn (reverse pat) (reverse src)
1447+ where (a,b) = breakOn (reverse pat) (reverse src)
1596{-# INLINE breakOnEnd #-}
1448{-# INLINE breakOnEnd #-}
1598-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1616,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
1616breakOnAll :: Text -- ^ @needle@ to search for
1468breakOnAll :: Text -- ^ @needle@ to search for
1617 -> Text -- ^ @haystack@ in which to search
1469 -> Text -- ^ @haystack@ in which to search
1618 -> [(Text, Text)]
1470 -> [(Text, Text)]
1619−breakOnAll = coerce JSS.breakOnAll
1620−-- breakOnAll pat src@(Text arr off slen)
1471+breakOnAll pat src@(Text arr off slen)
1621−-- | null pat = emptyError "breakOnAll"
1472+ | null pat = emptyError "breakOnAll"
1622−-- | otherwise = L.map step (indices pat src)
1473+ | otherwise = L.map step (indices pat src)
1624−-- step x = (chunk 0 x, chunk x (slen-x))
1475+ step x = (chunk 0 x, chunk x (slen-x))
1625−-- chunk !n !l = text arr (n+off) l
1476+ chunk !n !l = text arr (n+off) l
1626{-# INLINE breakOnAll #-}
1477{-# INLINE breakOnAll #-}
1628-------------------------------------------------------------------------------
1479-------------------------------------------------------------------------------
@@ -1646,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
1647-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1648index :: Text -> Int -> Char
1499index :: Text -> Int -> Char
1649−index = coerce JSS.index
1650−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
1651{-# INLINE index #-}
1501{-# INLINE index #-}
1653-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1654-- and returns the index of the first element in the 'Text' satisfying
1504-- and returns the index of the first element in the 'Text' satisfying
1655-- the predicate. Subject to fusion.
1505-- the predicate. Subject to fusion.
1656findIndex :: (Char -> Bool) -> Text -> Maybe Int
1506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1657−findIndex = coerce JSS.findIndex
1658−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
1659{-# INLINE findIndex #-}
1508{-# INLINE findIndex #-}
1661-- | /O(n+m)/ The 'count' function returns the number of times the
1510-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1665,11 +1514,10 @@ findIndex = coerce JSS.findIndex
1665-- In (unlikely) bad cases, this function's time complexity degrades
1514-- In (unlikely) bad cases, this function's time complexity degrades
1666-- towards /O(n*m)/.
1515-- towards /O(n*m)/.
1667count :: Text -> Text -> Int
1516count :: Text -> Text -> Int
1668−count = coerce JSS.count
1669−-- count pat src
1517+count pat src
1670−-- | null pat = emptyError "count"
1518+ | null pat = emptyError "count"
1671−-- | isSingleton pat = countChar (unsafeHead pat) src
1519+ | isSingleton pat = countChar (unsafeHead pat) src
1672−-- | otherwise = L.length (indices pat src)
1520+ | otherwise = L.length (indices pat src)
1673{-# INLINE [1] count #-}
1521{-# INLINE [1] count #-}
1675{-# RULES
1523{-# RULES
@@ -1691,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
1691-- excess elements of the longer 'Text' are discarded. This is
1539-- excess elements of the longer 'Text' are discarded. This is
1692-- equivalent to a pair of 'unpack' operations.
1540-- equivalent to a pair of 'unpack' operations.
1693zip :: Text -> Text -> [(Char,Char)]
1541zip :: Text -> Text -> [(Char,Char)]
1694−zip = coerce JSS.zip
1695−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1696{-# INLINE zip #-}
1543{-# INLINE zip #-}
1698-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1699-- given as the first argument, instead of a tupling function.
1546-- given as the first argument, instead of a tupling function.
1700-- Performs replacement on invalid scalar values.
1547-- Performs replacement on invalid scalar values.
1701zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1702−zipWith = coerce JSS.zipWith
1703−-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1549+zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1704−-- where g a b = safe (f a b)
1550+ where g a b = safe (f a b)
1705{-# INLINE zipWith #-}
1551{-# INLINE zipWith #-}
1707-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1708-- representing white space.
1554-- representing white space.
1709words :: Text -> [Text]
1555words :: Text -> [Text]
1710−words = coerce JSS.words
1711−-- words t@(Text arr off len) = loop 0 0
1556+words t@(Text arr off len) = loop 0 0
1713−-- loop !start !n
1558+ loop !start !n
1714−-- | n >= len = if start == n
1559+ | n >= len = if start == n
1715−-- then []
1560+ then []
1716−-- else [Text arr (start+off) (n-start)]
1561+ else [Text arr (start+off) (n-start)]
1717−-- | isSpace c =
1562+ | isSpace c =
1718−-- if start == n
1563+ if start == n
1719−-- then loop (start+1) (start+1)
1564+ then loop (start+1) (start+1)
1720−-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1565+ else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1721−-- | otherwise = loop start (n+d)
1566+ | otherwise = loop start (n+d)
1722−-- where Iter c d = iter t n
1567+ where Iter c d = iter t n
1723{-# INLINE words #-}
1568{-# INLINE words #-}
1725-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1726-- newline 'Char's. The resulting strings do not contain newlines.
1571-- newline 'Char's. The resulting strings do not contain newlines.
1727lines :: Text -> [Text]
1572lines :: Text -> [Text]
1728−lines = coerce JSS.lines
1729−-- lines ps | null ps = []
1573+lines ps | null ps = []
1730−-- | otherwise = h : if null t
1574+ | otherwise = h : if null t
1731−-- then []
1575+ then []
1732−-- else lines (unsafeTail t)
1576+ else lines (unsafeTail t)
1733−-- where (# h,t #) = span_ (/= '\n') ps
1577+ where (# h,t #) = span_ (/= '\n') ps
1734{-# INLINE lines #-}
1578{-# INLINE lines #-}
@@ -1758,40 +1602,34 @@ lines' ps | null ps = []
1758-- | /O(n)/ Joins lines, after appending a terminating newline to
1602-- | /O(n)/ Joins lines, after appending a terminating newline to
1760unlines :: [Text] -> Text
1604unlines :: [Text] -> Text
1761−unlines = coerce JSS.unlines
1762−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
1763{-# INLINE unlines #-}
1606{-# INLINE unlines #-}
1765-- | /O(n)/ Joins words using single space characters.
1608-- | /O(n)/ Joins words using single space characters.
1766unwords :: [Text] -> Text
1609unwords :: [Text] -> Text
1767−unwords = coerce JSS.unwords
1768−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
1769{-# INLINE unwords #-}
1611{-# INLINE unwords #-}
1771-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1772-- 'True' iff the first is a prefix of the second. Subject to fusion.
1614-- 'True' iff the first is a prefix of the second. Subject to fusion.
1773isPrefixOf :: Text -> Text -> Bool
1615isPrefixOf :: Text -> Text -> Bool
1774−isPrefixOf = coerce JSS.isPrefixOf
1775−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1616+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1776−-- alen <= blen && S.isPrefixOf (stream a) (stream b)
1617+ alen <= blen && S.isPrefixOf (stream a) (stream b)
1777{-# INLINE [1] isPrefixOf #-}
1618{-# INLINE [1] isPrefixOf #-}
1779{-# RULES
1620{-# RULES
1780"TEXT isPrefixOf -> fused" [~1] forall s t.
1621"TEXT isPrefixOf -> fused" [~1] forall s t.
1781 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1782−"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1783− S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
1786-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1787-- 'True' iff the first is a suffix of the second.
1626-- 'True' iff the first is a suffix of the second.
1788isSuffixOf :: Text -> Text -> Bool
1627isSuffixOf :: Text -> Text -> Bool
1789−isSuffixOf = coerce JSS.isSuffixOf
1790−-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1628+isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1791−-- d >= 0 && a == b'
1629+ d >= 0 && a == b'
1792−-- where d = blen - alen
1630+ where d = blen - alen
1793−-- b' | d == 0 = b
1631+ b' | d == 0 = b
1794−-- | otherwise = Text barr (boff+d) alen
1632+ | otherwise = Text barr (boff+d) alen
1795{-# INLINE isSuffixOf #-}
1633{-# INLINE isSuffixOf #-}
1797-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
1635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1801,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
1801-- In (unlikely) bad cases, this function's time complexity degrades
1639-- In (unlikely) bad cases, this function's time complexity degrades
1802-- towards /O(n*m)/.
1640-- towards /O(n*m)/.
1803isInfixOf :: Text -> Text -> Bool
1641isInfixOf :: Text -> Text -> Bool
1804−isInfixOf = coerce JSS.isInfixOf
1805−-- isInfixOf needle haystack
1642+isInfixOf needle haystack
1806−-- | null needle = True
1643+ | null needle = True
1807−-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1644+ | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1808−-- | otherwise = not . L.null . indices needle $ haystack
1645+ | otherwise = not . L.null . indices needle $ haystack
1809{-# INLINE [1] isInfixOf #-}
1646{-# INLINE [1] isInfixOf #-}
1811{-# RULES
1648{-# RULES
@@ -1835,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
1835-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1836-- > fnordLength _ = -1
1673-- > fnordLength _ = -1
1837stripPrefix :: Text -> Text -> Maybe Text
1674stripPrefix :: Text -> Text -> Maybe Text
1838−stripPrefix = coerce JSS.stripPrefix
1839−-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1675+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1840−-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1676+ | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1841−-- | otherwise = Nothing
1677+ | otherwise = Nothing
1843-- | /O(n)/ Find the longest non-empty common prefix of two strings
1679-- | /O(n)/ Find the longest non-empty common prefix of two strings
1844-- and return it, along with the suffixes of each string at which they
1680-- and return it, along with the suffixes of each string at which they
@@ -1853,16 +1689,15 @@ stripPrefix = coerce JSS.stripPrefix
1853-- > commonPrefixes "veeble" "fetzer" == Nothing
1689-- > commonPrefixes "veeble" "fetzer" == Nothing
1854-- > commonPrefixes "" "baz" == Nothing
1690-- > commonPrefixes "" "baz" == Nothing
1855commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1856−commonPrefixes = coerce JSS.commonPrefixes
1857−-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1692+commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1859−-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1694+ go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1860−-- | i > 0 = Just (Text arr0 off0 i,
1695+ | i > 0 = Just (Text arr0 off0 i,
1861−-- text arr0 (off0+i) (len0-i),
1696+ text arr0 (off0+i) (len0-i),
1862−-- text arr1 (off1+j) (len1-j))
1697+ text arr1 (off1+j) (len1-j))
1863−-- | otherwise = Nothing
1698+ | otherwise = Nothing
1864−-- where Iter a d0 = iter t0 i
1699+ where Iter a d0 = iter t0 i
1865−-- Iter b d1 = iter t1 j
1700+ Iter b d1 = iter t1 j
1867-- | /O(n)/ Return the prefix of the second string if its suffix
1702-- | /O(n)/ Return the prefix of the second string if its suffix
1868-- matches the entire first string.
1703-- matches the entire first string.
@@ -1883,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
1883-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1884-- > quuxLength _ = -1
1719-- > quuxLength _ = -1
1885stripSuffix :: Text -> Text -> Maybe Text
1720stripSuffix :: Text -> Text -> Maybe Text
1886−stripSuffix = coerce JSS.stripSuffix
1887−-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1721+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1888−-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
1722+ | p `isSuffixOf` t = Just $! text arr off (len-plen)
1889−-- | otherwise = Nothing
1723+ | otherwise = Nothing
1891−-- -- | Add a list of non-negative numbers. Errors out on overflow.
1725+-- | Add a list of non-negative numbers. Errors out on overflow.
1892−-- sumP :: String -> [Int] -> Int
1726+sumP :: String -> [Int] -> Int
1893−-- sumP fun = go 0
1727+sumP fun = go 0
1894−-- where go !a (x:xs)
1728+ where go !a (x:xs)
1895−-- | ax >= 0 = go ax xs
1729+ | ax >= 0 = go ax xs
1896−-- | otherwise = overflowError fun
1730+ | otherwise = overflowError fun
1897−-- where ax = a + x
1731+ where ax = a + x
1898−-- go a _ = a
1732+ go a _ = a
1900−-- emptyError :: String -> a
1734+emptyError :: String -> a
1901−-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1735+emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1903overflowError :: String -> a
1737overflowError :: String -> a
1904overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1912,14 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1912-- copy \"breaks the link\" to the original array, allowing it to be
1746-- copy \"breaks the link\" to the original array, allowing it to be
1913-- garbage collected if there are no other live references to it.
1747-- garbage collected if there are no other live references to it.
1914copy :: Text -> Text
1748copy :: Text -> Text
1916−-- copy (Text arr off len) = Text (A.run go) 0 len
1749+copy (Text arr off len) = Text (A.run go) 0 len
1918−-- go :: ST s (A.MArray s)
1751+ go :: ST s (A.MArray s)
1919−-- go = do
1752+ go = do
1920−-- marr <- A.new len
1753+ marr <- A.new len
1921−
1754+ A.copyI marr 0 arr off len
1922−foreign import javascript unsafe
1755+ return marr
1923− "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
1924−foreign import javascript unsafe
1925− "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString