@@ -6,7 +6,7 @@
6#if __GLASGOW_HASKELL__ >= 708
6#if __GLASGOW_HASKELL__ >= 708
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
9−{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
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,53 +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 (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.Exts (Addr#)
256−import qualified GHC.CString as GHC
258−unpack :: Text -> String
259−unpack = coerce JSS.unpack
260−{-# INLINE [1] unpack #-}
262−singleton :: Char -> Text
263−singleton = coerce JSS.singleton
264−{-# INLINE [1] singleton #-}
267−"JSSTRING singleton -> fused" [~1] forall a.
268− singleton a = unstream (S.singleton (safe a))
269−"JSSTRING singleton -> unfused" [1] forall a.
270− unstream (S.singleton (safe a)) = singleton a
273−-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
274−unpackCString# :: Addr# -> Text
275−unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
276−{-# NOINLINE unpackCString# #-}
278−{-# RULES "TEXT literal" forall a.
279− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
280− = unpackCString# a #-}
282−{-# RULES "TEXT literal UTF8" [1] forall a.
283− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
284− = unpackCString# a #-}
286−{-# RULES "TEXT empty literal" [1]
287− unstream (S.map safe (S.streamList []))
290−{-# RULES "TEXT singleton literal" [1] forall a.
291− unstream (S.map safe (S.streamList [a]))
294−---------------------------------------------------------------------
296-- $strict
249-- $strict
298-- This package provides both strict and lazy 'Text' types. The
251-- This package provides both strict and lazy 'Text' types. The
@@ -368,19 +321,14 @@ unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
368-- 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
369-- phrase \"Subject to fusion\".
322-- phrase \"Subject to fusion\".
371−-- instance Eq Text where
372−-- Text arrA offA lenA == Text arrB offB lenB
373−-- | lenA == lenB = A.equal arrA offA arrB offB lenA
374−-- | otherwise = False
375−-- {-# INLINE (==) #-}
376instance Eq Text where
324instance Eq Text where
377− Text x == Text y = x == y
325+ Text arrA offA lenA == Text arrB offB lenB
378−
326+ | lenA == lenB = A.equal arrA offA arrB offB lenA
379−-- instance Ord Text where
327+ | otherwise = False
380−-- compare = compareText
328+ {-# INLINE (==) #-}
382instance Ord Text where
330instance Ord Text where
383− compare (Text x) (Text y) = compare x y
331+ compare = compareText
385instance Read Text where
333instance Read Text where
386 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
@@ -450,7 +398,7 @@ instance Data Text where
450#if MIN_VERSION_base(4,7,0)
398#if MIN_VERSION_base(4,7,0)
451-- | Only defined for @base-4.7.0.0@ and later
399-- | Only defined for @base-4.7.0.0@ and later
452instance PrintfArg Text where
400instance PrintfArg Text where
453− formatArg t = formatString $ unpack t
401+ formatArg txt = formatString $ unpack txt
456packConstr :: Constr
404packConstr :: Constr
@@ -459,19 +407,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
459textDataType :: DataType
407textDataType :: DataType
460textDataType = mkDataType "Data.Text.Text" [packConstr]
408textDataType = mkDataType "Data.Text.Text" [packConstr]
462−-- -- | /O(n)/ Compare two 'Text' values lexicographically.
410+-- | /O(n)/ Compare two 'Text' values lexicographically.
463−-- compareText :: Text -> Text -> Ordering
411+compareText :: Text -> Text -> Ordering
464−-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
412+compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
465−-- | lenA == 0 && lenB == 0 = EQ
413+ | lenA == 0 && lenB == 0 = EQ
466−-- | otherwise = go 0 0
414+ | otherwise = go 0 0
468−-- go !i !j
416+ go !i !j
469−-- | i >= lenA || j >= lenB = compare lenA lenB
417+ | i >= lenA || j >= lenB = compare lenA lenB
470−-- | a < b = LT
418+ | a < b = LT
471−-- | a > b = GT
419+ | a > b = GT
472−-- | otherwise = go (i+di) (j+dj)
420+ | otherwise = go (i+di) (j+dj)
473−-- where Iter a di = iter ta i
421+ where Iter a di = iter ta i
474−-- Iter b dj = iter tb j
422+ Iter b dj = iter tb j
476-- -----------------------------------------------------------------------------
424-- -----------------------------------------------------------------------------
477-- * Conversion to/from 'Text'
425-- * Conversion to/from 'Text'
@@ -479,8 +427,7 @@ textDataType = mkDataType "Data.Text.Text" [packConstr]
479-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
480-- fusion. Performs replacement on invalid scalar values.
428-- fusion. Performs replacement on invalid scalar values.
481pack :: String -> Text
429pack :: String -> Text
482−pack = coerce JSS.pack
483−-- pack = unstream . S.map safe . S.streamList
430+pack = unstream . S.map safe . S.streamList
484{-# INLINE [1] pack #-}
431{-# INLINE [1] pack #-}
486-- -----------------------------------------------------------------------------
433-- -----------------------------------------------------------------------------
@@ -491,8 +438,7 @@ pack = coerce JSS.pack
491-- copying a new array. Subject to fusion. Performs replacement on
438-- copying a new array. Subject to fusion. Performs replacement on
492-- invalid scalar values.
439-- invalid scalar values.
493cons :: Char -> Text -> Text
440cons :: Char -> Text -> Text
494−cons = coerce JSS.cons
495−-- cons c t = unstream (S.cons (safe c) (stream t))
441+cons c t = unstream (S.cons (safe c) (stream t))
496{-# INLINE cons #-}
442{-# INLINE cons #-}
498infixr 5 `cons`
444infixr 5 `cons`
@@ -501,27 +447,25 @@ infixr 5 `cons`
501-- entire array in the process, unless fused. Subject to fusion.
447-- entire array in the process, unless fused. Subject to fusion.
502-- Performs replacement on invalid scalar values.
448-- Performs replacement on invalid scalar values.
503snoc :: Text -> Char -> Text
449snoc :: Text -> Char -> Text
504−snoc = coerce JSS.snoc
505−-- snoc t c = unstream (S.snoc (stream t) (safe c))
450+snoc t c = unstream (S.snoc (stream t) (safe c))
506{-# INLINE snoc #-}
451{-# INLINE snoc #-}
508-- | /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
509-- into a new 'Text'. Subject to fusion.
454-- into a new 'Text'. Subject to fusion.
510append :: Text -> Text -> Text
455append :: Text -> Text -> Text
511−append = coerce JSS.append
512−-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
456+append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
513−-- | len1 == 0 = b
457+ | len1 == 0 = b
514−-- | len2 == 0 = a
458+ | len2 == 0 = a
515−-- | len > 0 = Text (A.run x) 0 len
459+ | len > 0 = Text (A.run x) 0 len
516−-- | otherwise = overflowError "append"
460+ | otherwise = overflowError "append"
518−-- len = len1+len2
462+ len = len1+len2
519−-- x :: ST s (A.MArray s)
463+ x :: ST s (A.MArray s)
521−-- arr <- A.new len
465+ arr <- A.new len
522−-- A.copyI arr 0 arr1 off1 len1
466+ A.copyI arr 0 arr1 off1 len1
523−-- A.copyI arr len1 arr2 off2 len
467+ A.copyI arr len1 arr2 off2 len
524−-- return arr
468+ return arr
525{-# NOINLINE append #-}
469{-# NOINLINE append #-}
@@ -534,18 +478,16 @@ append = coerce JSS.append
534-- | /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
535-- non-empty. Subject to fusion.
479-- non-empty. Subject to fusion.
536head :: Text -> Char
480head :: Text -> Char
537−head = coerce JSS.head
538−-- head t = S.head (stream t)
481+head t = S.head (stream t)
539{-# INLINE head #-}
482{-# INLINE head #-}
541-- | /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
542-- 'Nothing' if empty. Subject to fusion.
485-- 'Nothing' if empty. Subject to fusion.
543uncons :: Text -> Maybe (Char, Text)
486uncons :: Text -> Maybe (Char, Text)
544−uncons = coerce JSS.uncons
545−-- uncons t@(Text arr off len)
487+uncons t@(Text arr off len)
546−-- | len <= 0 = Nothing
488+ | len <= 0 = Nothing
547−-- | otherwise = Just $ let !(Iter c d) = iter t 0
489+ | otherwise = Just $ let !(Iter c d) = iter t 0
548−-- in (c, text arr (off+d) (len-d))
490+ in (c, text arr (off+d) (len-d))
549{-# INLINE [1] uncons #-}
491{-# INLINE [1] uncons #-}
551-- | Lifted from Control.Arrow and specialized.
493-- | Lifted from Control.Arrow and specialized.
@@ -555,13 +497,12 @@ second f (a, b) = (a, f b)
555-- | /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
556-- non-empty. Subject to fusion.
498-- non-empty. Subject to fusion.
557last :: Text -> Char
499last :: Text -> Char
558−last = coerce JSS.last
559−-- last (Text arr off len)
500+last (Text arr off len)
560−-- | len <= 0 = emptyError "last"
501+ | len <= 0 = emptyError "last"
561−-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
502+ | n < 0xDC00 || n > 0xDFFF = unsafeChr n
562−-- | otherwise = U16.chr2 n0 n
503+ | otherwise = U16.chr2 n0 n
563−-- where n = A.unsafeIndex arr (off+len-1)
504+ where n = A.unsafeIndex arr (off+len-1)
564−-- n0 = A.unsafeIndex arr (off+len-2)
505+ n0 = A.unsafeIndex arr (off+len-2)
565{-# INLINE [1] last #-}
506{-# INLINE [1] last #-}
@@ -574,11 +515,10 @@ last = coerce JSS.last
574-- | /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
575-- must be non-empty. Subject to fusion.
516-- must be non-empty. Subject to fusion.
576tail :: Text -> Text
517tail :: Text -> Text
577−tail = coerce JSS.tail
578−-- tail t@(Text arr off len)
518+tail t@(Text arr off len)
579−-- | len <= 0 = emptyError "tail"
519+ | len <= 0 = emptyError "tail"
580−-- | otherwise = text arr (off+d) (len-d)
520+ | otherwise = text arr (off+d) (len-d)
581−-- where d = iter_ t 0
521+ where d = iter_ t 0
582{-# INLINE [1] tail #-}
522{-# INLINE [1] tail #-}
@@ -591,12 +531,11 @@ tail = coerce JSS.tail
591-- | /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
592-- be non-empty. Subject to fusion.
532-- be non-empty. Subject to fusion.
593init :: Text -> Text
533init :: Text -> Text
594−init = coerce JSS.init
595−-- init (Text arr off len) | len <= 0 = emptyError "init"
534+init (Text arr off len) | len <= 0 = emptyError "init"
596−-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
535+ | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
597−-- | otherwise = text arr off (len-1)
536+ | otherwise = text arr off (len-1)
599−-- n = A.unsafeIndex arr (off+len-1)
538+ n = A.unsafeIndex arr (off+len-1)
600{-# INLINE [1] init #-}
539{-# INLINE [1] init #-}
@@ -609,12 +548,11 @@ init = coerce JSS.init
609-- | /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
610-- fusion.
549-- fusion.
611null :: Text -> Bool
550null :: Text -> Bool
612−null = coerce JSS.null
613−-- null (Text _arr _off len) =
551+null (Text _arr _off len) =
614−-- #if defined(ASSERTS)
552+#if defined(ASSERTS)
615−-- assert (len >= 0) $
553+ assert (len >= 0) $
617−-- len <= 0
555+ len <= 0
618{-# INLINE [1] null #-}
556{-# INLINE [1] null #-}
@@ -626,15 +564,14 @@ null = coerce JSS.null
627-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
628-- Subject to fusion.
566-- Subject to fusion.
629−-- isSingleton :: Text -> Bool
567+isSingleton :: Text -> Bool
630−-- isSingleton = S.isSingleton . stream
568+isSingleton = S.isSingleton . stream
631−-- {-# INLINE isSingleton #-}
569+{-# INLINE isSingleton #-}
633-- | /O(n)/ Returns the number of characters in a 'Text'.
571-- | /O(n)/ Returns the number of characters in a 'Text'.
634-- Subject to fusion.
572-- Subject to fusion.
635length :: Text -> Int
573length :: Text -> Int
636−length = coerce JSS.length
637−-- length t = S.length (stream t)
574+length t = S.length (stream t)
638{-# INLINE [0] length #-}
575{-# INLINE [0] length #-}
639-- length needs to be phased after the compareN/length rules otherwise
576-- length needs to be phased after the compareN/length rules otherwise
640-- it may inline before the rules have an opportunity to fire.
577-- it may inline before the rules have an opportunity to fire.
@@ -690,30 +627,26 @@ compareLength t n = S.compareLengthI (stream t) n
690-- each element of @t@. Subject to fusion. Performs replacement on
627-- each element of @t@. Subject to fusion. Performs replacement on
691-- invalid scalar values.
628-- invalid scalar values.
692map :: (Char -> Char) -> Text -> Text
629map :: (Char -> Char) -> Text -> Text
694−-- map f t = unstream (S.map (safe . f) (stream t))
630+map f t = unstream (S.map (safe . f) (stream t))
695{-# INLINE [1] map #-}
631{-# INLINE [1] map #-}
697-- | /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
698-- 'Text's and concatenates the list after interspersing the first
634-- 'Text's and concatenates the list after interspersing the first
699-- argument between each element of the list.
635-- argument between each element of the list.
700intercalate :: Text -> [Text] -> Text
636intercalate :: Text -> [Text] -> Text
701−intercalate = coerce JSS.intercalate
702−-- intercalate t = concat . (F.intersperse t)
637+intercalate t = concat . (F.intersperse t)
703{-# INLINE intercalate #-}
638{-# INLINE intercalate #-}
705-- | /O(n)/ The 'intersperse' function takes a character and places it
640-- | /O(n)/ The 'intersperse' function takes a character and places it
706-- between the characters of a 'Text'. Subject to fusion. Performs
641-- between the characters of a 'Text'. Subject to fusion. Performs
707-- replacement on invalid scalar values.
642-- replacement on invalid scalar values.
708intersperse :: Char -> Text -> Text
643intersperse :: Char -> Text -> Text
709−intersperse = coerce JSS.intersperse
710−-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
644+intersperse c t = unstream (S.intersperse (safe c) (stream t))
711{-# INLINE intersperse #-}
645{-# INLINE intersperse #-}
713-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
647-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
714reverse :: Text -> Text
648reverse :: Text -> Text
715−reverse = coerce JSS.reverse
716−-- reverse t = S.reverse (stream t)
649+reverse t = S.reverse (stream t)
717{-# INLINE reverse #-}
650{-# INLINE reverse #-}
719-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -747,29 +680,28 @@ replace :: Text
748 -- ^ @haystack@ in which to search.
681 -- ^ @haystack@ in which to search.
750−replace = coerce JSS.replace
751−-- replace needle@(Text _ _ neeLen)
683+replace needle@(Text _ _ neeLen)
752−-- (Text repArr repOff repLen)
684+ (Text repArr repOff repLen)
753−-- haystack@(Text hayArr hayOff hayLen)
685+ haystack@(Text hayArr hayOff hayLen)
754−-- | neeLen == 0 = emptyError "replace"
686+ | neeLen == 0 = emptyError "replace"
755−-- | L.null ixs = haystack
687+ | L.null ixs = haystack
756−-- | len > 0 = Text (A.run x) 0 len
688+ | len > 0 = Text (A.run x) 0 len
757−-- | otherwise = empty
689+ | otherwise = empty
759−-- ixs = indices needle haystack
691+ ixs = indices needle haystack
760−-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
692+ len = hayLen - (neeLen - repLen) `mul` L.length ixs
761−-- x :: ST s (A.MArray s)
693+ x :: ST s (A.MArray s)
763−-- marr <- A.new len
695+ marr <- A.new len
764−-- let loop (i:is) o d = do
696+ let loop (i:is) o d = do
765−-- let d0 = d + i - o
697+ let d0 = d + i - o
766−-- d1 = d0 + repLen
698+ d1 = d0 + repLen
767−-- A.copyI marr d hayArr (hayOff+o) d0
699+ A.copyI marr d hayArr (hayOff+o) d0
768−-- A.copyI marr d0 repArr repOff d1
700+ A.copyI marr d0 repArr repOff d1
769−-- loop is (i + neeLen) d1
701+ loop is (i + neeLen) d1
770−-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
702+ loop [] o d = A.copyI marr d hayArr (hayOff+o) len
771−-- loop ixs 0 0
703+ loop ixs 0 0
772−-- return marr
704+ return marr
774-- ----------------------------------------------------------------------------
706-- ----------------------------------------------------------------------------
775-- ** Case conversions (folds)
707-- ** Case conversions (folds)
@@ -808,8 +740,7 @@ replace = coerce JSS.replace
808-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
809-- instead of itself.
741-- instead of itself.
810toCaseFold :: Text -> Text
742toCaseFold :: Text -> Text
811−-- toCaseFold t = unstream (S.toCaseFold (stream t))
743+toCaseFold t = unstream (S.toCaseFold (stream t))
812−toCaseFold = coerce JSS.toCaseFold
813{-# INLINE toCaseFold #-}
744{-# INLINE toCaseFold #-}
815-- | /O(n)/ Convert a string to lower case, using simple case
746-- | /O(n)/ Convert a string to lower case, using simple case
@@ -820,8 +751,7 @@ toCaseFold = coerce JSS.toCaseFold
820-- 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)
821-- followed by \" ̇\" (combining dot above, U+0307).
752-- followed by \" ̇\" (combining dot above, U+0307).
822toLower :: Text -> Text
753toLower :: Text -> Text
823−-- toLower t = unstream (S.toLower (stream t))
754+toLower t = unstream (S.toLower (stream t))
824−toLower = coerce JSS.toLower
825{-# INLINE toLower #-}
755{-# INLINE toLower #-}
827-- | /O(n)/ Convert a string to upper case, using simple case
757-- | /O(n)/ Convert a string to upper case, using simple case
@@ -831,8 +761,7 @@ toLower = coerce JSS.toLower
831-- instance, the German \"ß\" (eszett, U+00DF) maps to the
761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
832-- two-letter sequence \"SS\".
762-- two-letter sequence \"SS\".
833toUpper :: Text -> Text
763toUpper :: Text -> Text
834−-- toUpper t = unstream (S.toUpper (stream t))
764+toUpper t = unstream (S.toUpper (stream t))
835−toUpper = coerce JSS.toUpper
836{-# INLINE toUpper #-}
765{-# INLINE toUpper #-}
838-- | /O(n)/ Convert a string to title case, using simple case
767-- | /O(n)/ Convert a string to title case, using simple case
@@ -854,8 +783,7 @@ toUpper = coerce JSS.toUpper
854-- Fox\" is correctly title cased—but this function will
783-- Fox\" is correctly title cased—but this function will
855-- capitalize /every/ word.
784-- capitalize /every/ word.
856toTitle :: Text -> Text
785toTitle :: Text -> Text
857−toTitle = coerce JSS.toTitle
858−-- toTitle t = unstream (S.toTitle (stream t))
786+toTitle t = unstream (S.toTitle (stream t))
859{-# INLINE toTitle #-}
787{-# INLINE toTitle #-}
861-- | /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
@@ -867,11 +795,10 @@ toTitle = coerce JSS.toTitle
867-- > justifyLeft 7 'x' "foo" == "fooxxxx"
795-- > justifyLeft 7 'x' "foo" == "fooxxxx"
868-- > justifyLeft 3 'x' "foobar" == "foobar"
796-- > justifyLeft 3 'x' "foobar" == "foobar"
869justifyLeft :: Int -> Char -> Text -> Text
797justifyLeft :: Int -> Char -> Text -> Text
870−justifyLeft = coerce JSS.justifyLeft
798+justifyLeft k c t
872−-- | len >= k = t
799+ | len >= k = t
873−-- | otherwise = t `append` replicateChar (k-len) c
800+ | otherwise = t `append` replicateChar (k-len) c
874−-- where len = length t
801+ where len = length t
875{-# INLINE [1] justifyLeft #-}
802{-# INLINE [1] justifyLeft #-}
@@ -890,11 +817,10 @@ justifyLeft = coerce JSS.justifyLeft
890-- > justifyRight 7 'x' "bar" == "xxxxbar"
817-- > justifyRight 7 'x' "bar" == "xxxxbar"
891-- > justifyRight 3 'x' "foobar" == "foobar"
818-- > justifyRight 3 'x' "foobar" == "foobar"
892justifyRight :: Int -> Char -> Text -> Text
819justifyRight :: Int -> Char -> Text -> Text
893−justifyRight = coerce JSS.justifyRight
820+justifyRight k c t
894−-- justifyRight k c t
895−-- | len >= k = t
821+ | len >= k = t
896−-- | otherwise = replicateChar (k-len) c `append` t
822+ | otherwise = replicateChar (k-len) c `append` t
897−-- where len = length t
823+ where len = length t
898{-# INLINE justifyRight #-}
824{-# INLINE justifyRight #-}
900-- | /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
@@ -905,14 +831,13 @@ justifyRight = coerce JSS.justifyRight
906-- > center 8 'x' "HS" = "xxxHSxxx"
832-- > center 8 'x' "HS" = "xxxHSxxx"
907center :: Int -> Char -> Text -> Text
833center :: Int -> Char -> Text -> Text
908−center = coerce JSS.center
834+center k c t
910−-- | len >= k = t
835+ | len >= k = t
911−-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
836+ | otherwise = replicateChar l c `append` t `append` replicateChar r c
912−-- where len = length t
837+ where len = length t
913−-- d = k - len
838+ d = k - len
914−-- r = d `quot` 2
839+ r = d `quot` 2
915−-- l = d - r
840+ l = d - r
916{-# INLINE center #-}
841{-# INLINE center #-}
918-- | /O(n)/ The 'transpose' function transposes the rows and columns
843-- | /O(n)/ The 'transpose' function transposes the rows and columns
@@ -920,8 +845,7 @@ center = coerce JSS.center
920-- '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
921-- efficient.
846-- efficient.
922transpose :: [Text] -> [Text]
847transpose :: [Text] -> [Text]
923−transpose = coerce JSS.transpose
924−-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
848+transpose ts = P.map pack (L.transpose (P.map unpack ts))
926-- -----------------------------------------------------------------------------
850-- -----------------------------------------------------------------------------
927-- * Reducing 'Text's (folds)
851-- * Reducing 'Text's (folds)
@@ -931,27 +855,23 @@ transpose = coerce JSS.transpose
931-- reduces the 'Text' using the binary operator, from left to right.
855-- reduces the 'Text' using the binary operator, from left to right.
932-- Subject to fusion.
856-- Subject to fusion.
933foldl :: (a -> Char -> a) -> a -> Text -> a
857foldl :: (a -> Char -> a) -> a -> Text -> a
934−foldl f a0 (Text t) = JSS.foldl f a0 t
935−-- foldl f z t = S.foldl f z (stream t)
858+foldl f z t = S.foldl f z (stream t)
936{-# INLINE foldl #-}
859{-# INLINE foldl #-}
938-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
939foldl' :: (a -> Char -> a) -> a -> Text -> a
862foldl' :: (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)
863+foldl' f z t = S.foldl' f z (stream t)
942{-# INLINE foldl' #-}
864{-# INLINE foldl' #-}
944-- | /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,
945-- 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.
946foldl1 :: (Char -> Char -> Char) -> Text -> Char
868foldl1 :: (Char -> Char -> Char) -> Text -> Char
947−foldl1 = coerce JSS.foldl1
948−-- foldl1 f t = S.foldl1 f (stream t)
869+foldl1 f t = S.foldl1 f (stream t)
949{-# INLINE foldl1 #-}
870{-# INLINE foldl1 #-}
951-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
952foldl1' :: (Char -> Char -> Char) -> Text -> Char
873foldl1' :: (Char -> Char -> Char) -> Text -> Char
953−foldl1' = coerce JSS.foldl1'
954−-- foldl1' f t = S.foldl1' f (stream t)
874+foldl1' f t = S.foldl1' f (stream t)
955{-# INLINE foldl1' #-}
875{-# INLINE foldl1' #-}
957-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -959,16 +879,14 @@ foldl1' = coerce JSS.foldl1'
959-- reduces the 'Text' using the binary operator, from right to left.
879-- reduces the 'Text' using the binary operator, from right to left.
960-- Subject to fusion.
880-- Subject to fusion.
961foldr :: (Char -> a -> a) -> a -> Text -> a
881foldr :: (Char -> a -> a) -> a -> Text -> a
962−foldr f a0 (Text t) = JSS.foldr f a0 t
963−-- foldr f z t = S.foldr f z (stream t)
882+foldr f z t = S.foldr f z (stream t)
964{-# INLINE foldr #-}
883{-# INLINE foldr #-}
966-- | /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,
967-- 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
968-- fusion.
887-- fusion.
969foldr1 :: (Char -> Char -> Char) -> Text -> Char
888foldr1 :: (Char -> Char -> Char) -> Text -> Char
970−foldr1 = coerce JSS.foldr1
971−-- foldr1 f t = S.foldr1 f (stream t)
889+foldr1 f t = S.foldr1 f (stream t)
972{-# INLINE foldr1 #-}
890{-# INLINE foldr1 #-}
974-- -----------------------------------------------------------------------------
892-- -----------------------------------------------------------------------------
@@ -976,54 +894,48 @@ foldr1 = coerce JSS.foldr1
977-- | /O(n)/ Concatenate a list of 'Text's.
895-- | /O(n)/ Concatenate a list of 'Text's.
978concat :: [Text] -> Text
896concat :: [Text] -> Text
979−concat = coerce JSS.concat
980−-- concat ts = case ts' of
897+concat ts = case ts' of
981−-- [] -> empty
898+ [] -> empty
982−-- [t] -> t
899+ [t] -> t
983−-- _ -> Text (A.run go) 0 len
900+ _ -> Text (A.run go) 0 len
985−-- ts' = L.filter (not . null) ts
902+ ts' = L.filter (not . null) ts
986−-- len = sumP "concat" $ L.map lengthWord16 ts'
903+ len = sumP "concat" $ L.map lengthWord16 ts'
987−-- go :: ST s (A.MArray s)
904+ go :: ST s (A.MArray s)
988−-- go = do
905+ go = do
989−-- arr <- A.new len
906+ arr <- A.new len
990−-- let step i (Text a o l) =
907+ let step i (Text a o l) =
991−-- 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
992−-- foldM step 0 ts' >> return arr
909+ foldM step 0 ts' >> return arr
994-- | /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
995-- concatenate the results.
912-- concatenate the results.
996concatMap :: (Char -> Text) -> Text -> Text
913concatMap :: (Char -> Text) -> Text -> Text
997−concatMap = coerce JSS.concatMap
998−-- concatMap f = concat . foldr ((:) . f) []
914+concatMap f = concat . foldr ((:) . f) []
999{-# INLINE concatMap #-}
915{-# INLINE concatMap #-}
1001-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
1002-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
1003any :: (Char -> Bool) -> Text -> Bool
919any :: (Char -> Bool) -> Text -> Bool
1004−any = coerce JSS.any
1005−-- any p t = S.any p (stream t)
920+any p t = S.any p (stream t)
1006{-# INLINE any #-}
921{-# INLINE any #-}
1008-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1009-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1010all :: (Char -> Bool) -> Text -> Bool
925all :: (Char -> Bool) -> Text -> Bool
1011−all = coerce JSS.all
1012−-- all p t = S.all p (stream t)
926+all p t = S.all p (stream t)
1013{-# INLINE all #-}
927{-# INLINE all #-}
1015-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1016-- must be non-empty. Subject to fusion.
930-- must be non-empty. Subject to fusion.
1017maximum :: Text -> Char
931maximum :: Text -> Char
1018−maximum = coerce JSS.maximum
1019−-- maximum t = S.maximum (stream t)
932+maximum t = S.maximum (stream t)
1020{-# INLINE maximum #-}
933{-# INLINE maximum #-}
1022-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1023-- must be non-empty. Subject to fusion.
936-- must be non-empty. Subject to fusion.
1024minimum :: Text -> Char
937minimum :: Text -> Char
1025−minimum = coerce JSS.minimum
1026−-- minimum t = S.minimum (stream t)
938+minimum t = S.minimum (stream t)
1027{-# INLINE minimum #-}
939{-# INLINE minimum #-}
1029-- -----------------------------------------------------------------------------
941-- -----------------------------------------------------------------------------
@@ -1039,9 +951,8 @@ minimum = coerce JSS.minimum
1040-- > last (scanl f z xs) == foldl f z xs.
952-- > last (scanl f z xs) == foldl f z xs.
1041scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1042−scanl = coerce JSS.scanl
1043−-- scanl f z t = unstream (S.scanl g z (stream t))
954+scanl f z t = unstream (S.scanl g z (stream t))
1044−-- where g a b = safe (f a b)
955+ where g a b = safe (f a b)
1045{-# INLINE scanl #-}
956{-# INLINE scanl #-}
1047-- | /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
@@ -1050,9 +961,8 @@ scanl = coerce JSS.scanl
1051-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1052scanl1 :: (Char -> Char -> Char) -> Text -> Text
963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1053−scanl1 = coerce JSS.scanl1
1054−-- scanl1 f t | null t = empty
964+scanl1 f t | null t = empty
1055−-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
965+ | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1056{-# INLINE scanl1 #-}
966{-# INLINE scanl1 #-}
1058-- | /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
@@ -1060,18 +970,16 @@ scanl1 = coerce JSS.scanl1
1061-- > scanr f v == reverse . scanl (flip f) v . reverse
971-- > scanr f v == reverse . scanl (flip f) v . reverse
1062scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1063−scanr = coerce JSS.scanr
1064−-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
973+scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1065−-- where g a b = safe (f a b)
974+ where g a b = safe (f a b)
1066{-# INLINE scanr #-}
975{-# INLINE scanr #-}
1068-- | /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
1069-- value argument. Subject to fusion. Performs replacement on
978-- value argument. Subject to fusion. Performs replacement on
1070-- invalid scalar values.
979-- invalid scalar values.
1071scanr1 :: (Char -> Char -> Char) -> Text -> Text
980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1072−scanr1 = coerce JSS.scanr1
1073−-- scanr1 f t | null t = empty
981+scanr1 f t | null t = empty
1074−-- | otherwise = scanr f (last t) (init t)
982+ | otherwise = scanr f (last t) (init t)
1075{-# INLINE scanr1 #-}
983{-# INLINE scanr1 #-}
1077-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -1079,11 +987,8 @@ scanr1 = coerce JSS.scanr1
1079-- parameter from left to right, and returns a final 'Text'. Performs
987-- parameter from left to right, and returns a final 'Text'. Performs
1080-- replacement on invalid scalar values.
988-- replacement on invalid scalar values.
1081mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1082−mapAccumL f z0 (Text a) =
1083− let (a', t) = JSS.mapAccumL f z0 a
1085−-- mapAccumL f z0 = S.mapAccumL g z0 . stream
990+mapAccumL f z0 = S.mapAccumL g z0 . stream
1086−-- where g a b = second safe (f a b)
991+ where g a b = second safe (f a b)
1087{-# INLINE mapAccumL #-}
992{-# INLINE mapAccumL #-}
1089-- | The 'mapAccumR' function behaves like a combination of 'map' and
994-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -1093,11 +998,8 @@ mapAccumL f z0 (Text a) =
1093-- 'Text'.
998-- 'Text'.
1094-- Performs replacement on invalid scalar values.
999-- Performs replacement on invalid scalar values.
1095mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1096−mapAccumR f z0 (Text a) =
1097− let (a', t) = JSS.mapAccumR f z0 a
1099−-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1001+mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1100−-- where g a b = second safe (f a b)
1002+ where g a b = second safe (f a b)
1101{-# INLINE mapAccumR #-}
1003{-# INLINE mapAccumR #-}
1103-- -----------------------------------------------------------------------------
1005-- -----------------------------------------------------------------------------
@@ -1106,21 +1008,20 @@ mapAccumR f z0 (Text a) =
1106-- | /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
1107-- @t@ repeated @n@ times.
1009-- @t@ repeated @n@ times.
1108replicate :: Int -> Text -> Text
1010replicate :: Int -> Text -> Text
1109−replicate = coerce JSS.replicate
1110−-- replicate n t@(Text a o l)
1011+replicate n t@(Text a o l)
1111−-- | n <= 0 || l <= 0 = empty
1012+ | n <= 0 || l <= 0 = empty
1112−-- | n == 1 = t
1013+ | n == 1 = t
1113−-- | isSingleton t = replicateChar n (unsafeHead t)
1014+ | isSingleton t = replicateChar n (unsafeHead t)
1114−-- | otherwise = Text (A.run x) 0 len
1015+ | otherwise = Text (A.run x) 0 len
1116−-- len = l `mul` n
1017+ len = l `mul` n
1117−-- x :: ST s (A.MArray s)
1018+ x :: ST s (A.MArray s)
1118−-- x = do
1019+ x = do
1119−-- arr <- A.new len
1020+ arr <- A.new len
1120−-- let loop !d !i | i >= n = return arr
1021+ let loop !d !i | i >= n = return arr
1121−-- | otherwise = let m = d + l
1022+ | otherwise = let m = d + l
1122−-- 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)
1123−-- loop 0 0
1024+ loop 0 0
1124{-# INLINE [1] replicate #-}
1025{-# INLINE [1] replicate #-}
1126{-# RULES
1027{-# RULES
@@ -1131,19 +1032,8 @@ replicate = coerce JSS.replicate
1131-- | /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
1132-- value of every element. Subject to fusion.
1033-- value of every element. Subject to fusion.
1133replicateChar :: Int -> Char -> Text
1034replicateChar :: Int -> Char -> Text
1135replicateChar n c = unstream (S.replicateCharI n (safe c))
1035replicateChar n c = unstream (S.replicateCharI n (safe c))
1136{-# INLINE replicateChar #-}
1036{-# INLINE replicateChar #-}
1138−replicateChar n c = Text $ js_replicateChar n c
1139−{-# INLINE [1] replicateChar #-}
1141−"JSSTRING replicateChar -> fused" [~1] forall n c.
1142− replicateChar n c = unstream (S.replicateCharI n (safe c))
1143−"JSSTRING replicateChar -> unfused" [1] forall n c.
1144− unstream (S.replicateCharI n (safe c)) = replicateChar n c
1148-- | /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'
1149-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
1039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1153,8 +1043,7 @@ replicateChar n c = Text $ js_replicateChar n c
1153-- string, and @b@ is the seed value for further production. Subject
1043-- string, and @b@ is the seed value for further production. Subject
1154-- to fusion. Performs replacement on invalid scalar values.
1044-- to fusion. Performs replacement on invalid scalar values.
1155unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1156−unfoldr f a = Text $ JSS.unfoldr f a
1157−-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1046+unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1158{-# INLINE unfoldr #-}
1047{-# INLINE unfoldr #-}
1160-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
1049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1164,8 +1053,7 @@ unfoldr f a = Text $ JSS.unfoldr f a
1164-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1165-- to fusion. Performs replacement on invalid scalar values.
1054-- to fusion. Performs replacement on invalid scalar values.
1166unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1167−unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1168−-- 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)
1169{-# INLINE unfoldrN #-}
1057{-# INLINE unfoldrN #-}
1171-- -----------------------------------------------------------------------------
1059-- -----------------------------------------------------------------------------
@@ -1175,19 +1063,18 @@ unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1175-- '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
1176-- the length of the Text. Subject to fusion.
1064-- the length of the Text. Subject to fusion.
1177take :: Int -> Text -> Text
1065take :: Int -> Text -> Text
1178−take = coerce JSS.take
1179−-- take n t@(Text arr off len)
1066+take n t@(Text arr off len)
1180−-- | n <= 0 = empty
1067+ | n <= 0 = empty
1181−-- | n >= len = t
1068+ | n >= len = t
1182−-- | otherwise = text arr off (iterN n t)
1069+ | otherwise = text arr off (iterN n t)
1183{-# INLINE [1] take #-}
1070{-# INLINE [1] take #-}
1185−-- iterN :: Int -> Text -> Int
1072+iterN :: Int -> Text -> Int
1186−-- iterN n t@(Text _arr _off len) = loop 0 0
1073+iterN n t@(Text _arr _off len) = loop 0 0
1187−-- where loop !i !cnt
1074+ where loop !i !cnt
1188−-- | i >= len || cnt >= n = i
1075+ | i >= len || cnt >= n = i
1189−-- | otherwise = loop (i+d) (cnt+1)
1076+ | otherwise = loop (i+d) (cnt+1)
1190−-- where d = iter_ t i
1077+ where d = iter_ t i
1192{-# RULES
1079{-# RULES
1193"TEXT take -> fused" [~1] forall n t.
1080"TEXT take -> fused" [~1] forall n t.
@@ -1203,31 +1090,29 @@ take = coerce JSS.take
1204-- > takeEnd 3 "foobar" == "bar"
1091-- > takeEnd 3 "foobar" == "bar"
1205takeEnd :: Int -> Text -> Text
1092takeEnd :: Int -> Text -> Text
1206−takeEnd = coerce JSS.takeEnd
1207−-- takeEnd n t@(Text arr off len)
1093+takeEnd n t@(Text arr off len)
1208−-- | n <= 0 = empty
1094+ | n <= 0 = empty
1209−-- | n >= len = t
1095+ | n >= len = t
1210−-- | otherwise = text arr (off+i) (len-i)
1096+ | otherwise = text arr (off+i) (len-i)
1211−-- where i = iterNEnd n t
1097+ where i = iterNEnd n t
1213−-- iterNEnd :: Int -> Text -> Int
1099+iterNEnd :: Int -> Text -> Int
1214−-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1100+iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1215−-- where loop i !m
1101+ where loop i !m
1216−-- | i <= 0 = 0
1102+ | i <= 0 = 0
1217−-- | m <= 1 = i
1103+ | m <= 1 = i
1218−-- | otherwise = loop (i+d) (m-1)
1104+ | otherwise = loop (i+d) (m-1)
1219−-- where d = reverseIter_ t i
1105+ where d = reverseIter_ t i
1221-- | /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
1222-- '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@
1223-- is greater than the length of the 'Text'. Subject to fusion.
1109-- is greater than the length of the 'Text'. Subject to fusion.
1224drop :: Int -> Text -> Text
1110drop :: Int -> Text -> Text
1225−drop = coerce JSS.drop
1226−-- drop n t@(Text arr off len)
1111+drop n t@(Text arr off len)
1227−-- | n <= 0 = t
1112+ | n <= 0 = t
1228−-- | n >= len = empty
1113+ | n >= len = empty
1229−-- | otherwise = text arr (off+i) (len-i)
1114+ | otherwise = text arr (off+i) (len-i)
1230−-- where i = iterN n t
1115+ where i = iterN n t
1231{-# INLINE [1] drop #-}
1116{-# INLINE [1] drop #-}
1233{-# RULES
1118{-# RULES
@@ -1244,22 +1129,20 @@ drop = coerce JSS.drop
1245-- > dropEnd 3 "foobar" == "foo"
1130-- > dropEnd 3 "foobar" == "foo"
1246dropEnd :: Int -> Text -> Text
1131dropEnd :: Int -> Text -> Text
1247−dropEnd = coerce JSS.dropEnd
1248−-- dropEnd n t@(Text arr off len)
1132+dropEnd n t@(Text arr off len)
1249−-- | n <= 0 = t
1133+ | n <= 0 = t
1250−-- | n >= len = empty
1134+ | n >= len = empty
1251−-- | otherwise = text arr off (iterNEnd n t)
1135+ | otherwise = text arr off (iterNEnd n t)
1253-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1254-- returns the longest prefix (possibly empty) of elements that
1138-- returns the longest prefix (possibly empty) of elements that
1255-- satisfy @p@. Subject to fusion.
1139-- satisfy @p@. Subject to fusion.
1256takeWhile :: (Char -> Bool) -> Text -> Text
1140takeWhile :: (Char -> Bool) -> Text -> Text
1257−takeWhile = coerce JSS.takeWhile
1258−-- takeWhile p t@(Text arr off len) = loop 0
1141+takeWhile p t@(Text arr off len) = loop 0
1259−-- where loop !i | i >= len = t
1142+ where loop !i | i >= len = t
1260−-- | p c = loop (i+d)
1143+ | p c = loop (i+d)
1261−-- | otherwise = text arr off i
1144+ | otherwise = text arr off i
1262−-- where Iter c d = iter t i
1145+ where Iter c d = iter t i
1263{-# INLINE [1] takeWhile #-}
1146{-# INLINE [1] takeWhile #-}
1265{-# RULES
1148{-# RULES
@@ -1276,20 +1159,11 @@ takeWhile = coerce JSS.takeWhile
1277-- > takeWhileEnd (=='o') "foo" == "oo"
1160-- > takeWhileEnd (=='o') "foo" == "oo"
1278takeWhileEnd :: (Char -> Bool) -> Text -> Text
1161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1280takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1281 where loop !i !l | l <= 0 = t
1163 where loop !i !l | l <= 0 = t
1282 | p c = loop (i+d) (l+d)
1164 | p c = loop (i+d) (l+d)
1283 | otherwise = text arr (off+l) (len-l)
1165 | otherwise = text arr (off+l) (len-l)
1284 where (c,d) = reverseIter t i
1166 where (c,d) = reverseIter t i
1286−takeWhileEnd p t@(Text jst) = loop (len-1) len
1287− where loop !i !l | l <= 0 = t
1288− | p c = loop (i+d) (l+d)
1289− | otherwise = Text $ js_substr1 l jst
1290− where (c,d) = reverseIter t i
1293{-# INLINE [1] takeWhileEnd #-}
1167{-# INLINE [1] takeWhileEnd #-}
1295{-# RULES
1169{-# RULES
@@ -1302,12 +1176,11 @@ takeWhileEnd p t@(Text jst) = loop (len-1) len
1302-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1303-- 'takeWhile' @p@ @t@. Subject to fusion.
1177-- 'takeWhile' @p@ @t@. Subject to fusion.
1304dropWhile :: (Char -> Bool) -> Text -> Text
1178dropWhile :: (Char -> Bool) -> Text -> Text
1305−dropWhile = coerce JSS.dropWhile
1306−-- dropWhile p t@(Text arr off len) = loop 0 0
1179+dropWhile p t@(Text arr off len) = loop 0 0
1307−-- where loop !i !l | l >= len = empty
1180+ where loop !i !l | l >= len = empty
1308−-- | p c = loop (i+d) (l+d)
1181+ | p c = loop (i+d) (l+d)
1309−-- | otherwise = Text arr (off+i) (len-l)
1182+ | otherwise = Text arr (off+i) (len-l)
1310−
1183+ where Iter c d = iter t i
1311{-# INLINE [1] dropWhile #-}
1184{-# INLINE [1] dropWhile #-}
1313{-# RULES
1186{-# RULES
@@ -1325,12 +1198,11 @@ dropWhile = coerce JSS.dropWhile
1326-- > dropWhileEnd (=='.') "foo..." == "foo"
1199-- > dropWhileEnd (=='.') "foo..." == "foo"
1327dropWhileEnd :: (Char -> Bool) -> Text -> Text
1200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1328−dropWhileEnd = coerce JSS.dropWhileEnd
1329−-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1201+dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1330−-- where loop !i !l | l <= 0 = empty
1202+ where loop !i !l | l <= 0 = empty
1331−-- | p c = loop (i+d) (l+d)
1203+ | p c = loop (i+d) (l+d)
1332−-- | otherwise = Text arr off l
1204+ | otherwise = Text arr off l
1333−-- where (c,d) = reverseIter t i
1205+ where (c,d) = reverseIter t i
1334{-# INLINE [1] dropWhileEnd #-}
1206{-# INLINE [1] dropWhileEnd #-}
1336{-# RULES
1208{-# RULES
@@ -1373,49 +1245,45 @@ strip = dropAround isSpace
1373-- 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
1374-- 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)@.
1375splitAt :: Int -> Text -> (Text, Text)
1247splitAt :: Int -> Text -> (Text, Text)
1376−splitAt = coerce JSS.splitAt
1377−-- splitAt n t@(Text arr off len)
1248+splitAt n t@(Text arr off len)
1378−-- | n <= 0 = (empty, t)
1249+ | n <= 0 = (empty, t)
1379−-- | n >= len = (t, empty)
1250+ | n >= len = (t, empty)
1380−-- | otherwise = let k = iterN n t
1251+ | otherwise = let k = iterN n t
1381−-- in (text arr off k, text arr (off+k) (len-k))
1252+ in (text arr off k, text arr (off+k) (len-k))
1383-- | /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
1384-- a pair whose first element is the longest prefix (possibly empty)
1255-- a pair whose first element is the longest prefix (possibly empty)
1385-- 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
1386-- remainder of the list.
1257-- remainder of the list.
1387span :: (Char -> Bool) -> Text -> (Text, Text)
1258span :: (Char -> Bool) -> Text -> (Text, Text)
1388−span = coerce JSS.span
1389−-- span p t = case span_ p t of
1259+span p t = case span_ p t of
1390−-- (# hd,tl #) -> (hd,tl)
1260+ (# hd,tl #) -> (hd,tl)
1391{-# INLINE span #-}
1261{-# INLINE span #-}
1393-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1394-- over elements that fail the predicate @p@.
1264-- over elements that fail the predicate @p@.
1395break :: (Char -> Bool) -> Text -> (Text, Text)
1265break :: (Char -> Bool) -> Text -> (Text, Text)
1396−break = coerce JSS.break
1397−-- break p = span (not . p)
1266+break p = span (not . p)
1398{-# INLINE break #-}
1267{-# INLINE break #-}
1400-- | /O(n)/ Group characters in a string according to a predicate.
1269-- | /O(n)/ Group characters in a string according to a predicate.
1401groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1402−groupBy = coerce JSS.groupBy
1403−-- groupBy p = loop
1271+groupBy p = loop
1405−-- loop t@(Text arr off len)
1273+ loop t@(Text arr off len)
1406−-- | null t = []
1274+ | null t = []
1407−-- | 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))
1408−-- where Iter c d = iter t 0
1276+ where Iter c d = iter t 0
1409−-- 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))
1411-- | Returns the /array/ index (in units of 'Word16') at which a
1279-- | Returns the /array/ index (in units of 'Word16') at which a
1412-- 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
1413-- index returned by e.g. 'findIndex'.
1281-- index returned by e.g. 'findIndex'.
1414−-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1282+findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1415−-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1283+findAIndexOrEnd q t@(Text _arr _off len) = go 0
1416−-- where go !i | i >= len || q c = i
1284+ where go !i | i >= len || q c = i
1417−-- | otherwise = go (i+d)
1285+ | otherwise = go (i+d)
1418−-- where Iter c d = iter t i
1286+ where Iter c d = iter t i
1420-- | /O(n)/ Group characters in a string by equality.
1288-- | /O(n)/ Group characters in a string by equality.
1421group :: Text -> [Text]
1289group :: Text -> [Text]
@@ -1424,17 +1292,15 @@ group = groupBy (==)
1424-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1425-- first.
1293-- first.
1426inits :: Text -> [Text]
1294inits :: Text -> [Text]
1427−inits = coerce JSS.inits
1428−-- inits t@(Text arr off len) = loop 0
1295+inits t@(Text arr off len) = loop 0
1429−-- where loop i | i >= len = [t]
1296+ where loop i | i >= len = [t]
1430−-- | otherwise = Text arr off i : loop (i + iter_ t i)
1297+ | otherwise = Text arr off i : loop (i + iter_ t i)
1432-- | /O(n)/ Return all final segments of the given 'Text', longest
1299-- | /O(n)/ Return all final segments of the given 'Text', longest
1433-- first.
1300-- first.
1434tails :: Text -> [Text]
1301tails :: Text -> [Text]
1435−tails = coerce JSS.tails
1436−-- tails t | null t = [empty]
1302+tails t | null t = [empty]
1437−-- | otherwise = t : tails (unsafeTail t)
1303+ | otherwise = t : tails (unsafeTail t)
1439-- $split
1305-- $split
@@ -1467,14 +1333,13 @@ splitOn :: Text
1468 -- ^ Input text.
1334 -- ^ Input text.
1469 -> [Text]
1335 -> [Text]
1470−splitOn = coerce JSS.splitOn
1471−-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1336+splitOn pat@(Text _ _ l) src@(Text arr off len)
1472−-- | l <= 0 = emptyError "splitOn"
1337+ | l <= 0 = emptyError "splitOn"
1473−-- | isSingleton pat = split (== unsafeHead pat) src
1338+ | isSingleton pat = split (== unsafeHead pat) src
1474−-- | otherwise = go 0 (indices pat src)
1339+ | otherwise = go 0 (indices pat src)
1476−-- 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
1477−-- go s _ = [text arr (s+off) (len-s)]
1342+ go s _ = [text arr (s+off) (len-s)]
1478{-# INLINE [1] splitOn #-}
1343{-# INLINE [1] splitOn #-}
1480{-# RULES
1345{-# RULES
@@ -1490,12 +1355,11 @@ splitOn = coerce JSS.splitOn
1490-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1355-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1491-- > split (=='a') "" == [""]
1356-- > split (=='a') "" == [""]
1492split :: (Char -> Bool) -> Text -> [Text]
1357split :: (Char -> Bool) -> Text -> [Text]
1493−split = coerce JSS.split
1494−-- split _ t@(Text _off _arr 0) = [t]
1358+split _ t@(Text _off _arr 0) = [t]
1495−-- split p t = loop t
1359+split p t = loop t
1496−-- where loop s | null s' = [l]
1360+ where loop s | null s' = [l]
1497−-- | otherwise = l : loop (unsafeTail s')
1361+ | otherwise = l : loop (unsafeTail s')
1498−-- where (# l, s' #) = span_ (not . p) s
1362+ where (# l, s' #) = span_ (not . p) s
1499{-# INLINE split #-}
1363{-# INLINE split #-}
1501-- | /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
@@ -1505,12 +1369,11 @@ split = coerce JSS.split
1505-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1369-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1506-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1370-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1507chunksOf :: Int -> Text -> [Text]
1371chunksOf :: Int -> Text -> [Text]
1508−chunksOf = coerce JSS.chunksOf
1372+chunksOf k = go
1511−-- go t = case splitAt k t of
1374+ go t = case splitAt k t of
1512−-- (a,b) | null a -> []
1375+ (a,b) | null a -> []
1513−-- | otherwise -> a : go b
1376+ | otherwise -> a : go b
1514{-# INLINE chunksOf #-}
1377{-# INLINE chunksOf #-}
1516-- ----------------------------------------------------------------------------
1379-- ----------------------------------------------------------------------------
@@ -1523,8 +1386,7 @@ chunksOf = coerce JSS.chunksOf
1523-- returns the first element matching the predicate, or 'Nothing' if
1386-- returns the first element matching the predicate, or 'Nothing' if
1524-- there is no such element.
1387-- there is no such element.
1525find :: (Char -> Bool) -> Text -> Maybe Char
1388find :: (Char -> Bool) -> Text -> Maybe Char
1526−find = coerce JSS.find
1527−-- find p t = S.findBy p (stream t)
1389+find p t = S.findBy p (stream t)
1528{-# INLINE find #-}
1390{-# INLINE find #-}
1530-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
1392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1533,16 +1395,14 @@ find = coerce JSS.find
1534-- > partition p t == (filter p t, filter (not . p) t)
1396-- > partition p t == (filter p t, filter (not . p) t)
1535partition :: (Char -> Bool) -> Text -> (Text, Text)
1397partition :: (Char -> Bool) -> Text -> (Text, Text)
1536−partition = coerce JSS.partition
1537−-- partition p t = (filter p t, filter (not . p) t)
1398+partition p t = (filter p t, filter (not . p) t)
1538{-# INLINE partition #-}
1399{-# INLINE partition #-}
1540-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1541-- returns a 'Text' containing those characters that satisfy the
1402-- returns a 'Text' containing those characters that satisfy the
1542-- predicate.
1403-- predicate.
1543filter :: (Char -> Bool) -> Text -> Text
1404filter :: (Char -> Bool) -> Text -> Text
1544−filter = coerce JSS.filter
1545−-- filter p t = unstream (S.filter p (stream t))
1405+filter p t = unstream (S.filter p (stream t))
1546{-# INLINE filter #-}
1406{-# INLINE filter #-}
1548-- | /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
@@ -1567,12 +1427,11 @@ filter = coerce JSS.filter
1567-- In (unlikely) bad cases, this function's time complexity degrades
1427-- In (unlikely) bad cases, this function's time complexity degrades
1568-- towards /O(n*m)/.
1428-- towards /O(n*m)/.
1569breakOn :: Text -> Text -> (Text, Text)
1429breakOn :: Text -> Text -> (Text, Text)
1570−breakOn = coerce JSS.breakOn
1571−-- breakOn pat src@(Text arr off len)
1430+breakOn pat src@(Text arr off len)
1572−-- | null pat = emptyError "breakOn"
1431+ | null pat = emptyError "breakOn"
1573−-- | otherwise = case indices pat src of
1432+ | otherwise = case indices pat src of
1574−-- [] -> (src, empty)
1433+ [] -> (src, empty)
1575−-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
1434+ (x:_) -> (text arr off x, text arr (off+x) (len-x))
1576{-# INLINE breakOn #-}
1435{-# INLINE breakOn #-}
1578-- | /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
@@ -1584,9 +1443,8 @@ breakOn = coerce JSS.breakOn
1585-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1444-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1586breakOnEnd :: Text -> Text -> (Text, Text)
1445breakOnEnd :: Text -> Text -> (Text, Text)
1587−breakOnEnd = coerce JSS.breakOnEnd
1588−-- breakOnEnd pat src = (reverse b, reverse a)
1446+breakOnEnd pat src = (reverse b, reverse a)
1589−-- where (a,b) = breakOn (reverse pat) (reverse src)
1447+ where (a,b) = breakOn (reverse pat) (reverse src)
1590{-# INLINE breakOnEnd #-}
1448{-# INLINE breakOnEnd #-}
1592-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1610,13 +1468,12 @@ breakOnEnd = coerce JSS.breakOnEnd
1610breakOnAll :: Text -- ^ @needle@ to search for
1468breakOnAll :: Text -- ^ @needle@ to search for
1611 -> Text -- ^ @haystack@ in which to search
1469 -> Text -- ^ @haystack@ in which to search
1612 -> [(Text, Text)]
1470 -> [(Text, Text)]
1613−breakOnAll = coerce JSS.breakOnAll
1614−-- breakOnAll pat src@(Text arr off slen)
1471+breakOnAll pat src@(Text arr off slen)
1615−-- | null pat = emptyError "breakOnAll"
1472+ | null pat = emptyError "breakOnAll"
1616−-- | otherwise = L.map step (indices pat src)
1473+ | otherwise = L.map step (indices pat src)
1618−-- step x = (chunk 0 x, chunk x (slen-x))
1475+ step x = (chunk 0 x, chunk x (slen-x))
1619−-- chunk !n !l = text arr (n+off) l
1476+ chunk !n !l = text arr (n+off) l
1620{-# INLINE breakOnAll #-}
1477{-# INLINE breakOnAll #-}
1622-------------------------------------------------------------------------------
1479-------------------------------------------------------------------------------
@@ -1640,16 +1497,14 @@ breakOnAll = coerce JSS.breakOnAll
1641-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1642index :: Text -> Int -> Char
1499index :: Text -> Int -> Char
1643−index = coerce JSS.index
1644−-- index t n = S.index (stream t) n
1500+index t n = S.index (stream t) n
1645{-# INLINE index #-}
1501{-# INLINE index #-}
1647-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1648-- 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
1649-- the predicate. Subject to fusion.
1505-- the predicate. Subject to fusion.
1650findIndex :: (Char -> Bool) -> Text -> Maybe Int
1506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1651−findIndex = coerce JSS.findIndex
1652−-- findIndex p t = S.findIndex p (stream t)
1507+findIndex p t = S.findIndex p (stream t)
1653{-# INLINE findIndex #-}
1508{-# INLINE findIndex #-}
1655-- | /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
@@ -1659,11 +1514,10 @@ findIndex = coerce JSS.findIndex
1659-- In (unlikely) bad cases, this function's time complexity degrades
1514-- In (unlikely) bad cases, this function's time complexity degrades
1660-- towards /O(n*m)/.
1515-- towards /O(n*m)/.
1661count :: Text -> Text -> Int
1516count :: Text -> Text -> Int
1662−count = coerce JSS.count
1663−-- count pat src
1517+count pat src
1664−-- | null pat = emptyError "count"
1518+ | null pat = emptyError "count"
1665−-- | isSingleton pat = countChar (unsafeHead pat) src
1519+ | isSingleton pat = countChar (unsafeHead pat) src
1666−-- | otherwise = L.length (indices pat src)
1520+ | otherwise = L.length (indices pat src)
1667{-# INLINE [1] count #-}
1521{-# INLINE [1] count #-}
1669{-# RULES
1523{-# RULES
@@ -1685,46 +1539,42 @@ countChar c t = S.countChar c (stream t)
1685-- excess elements of the longer 'Text' are discarded. This is
1539-- excess elements of the longer 'Text' are discarded. This is
1686-- equivalent to a pair of 'unpack' operations.
1540-- equivalent to a pair of 'unpack' operations.
1687zip :: Text -> Text -> [(Char,Char)]
1541zip :: Text -> Text -> [(Char,Char)]
1688−zip = coerce JSS.zip
1689−-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1542+zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1690{-# INLINE zip #-}
1543{-# INLINE zip #-}
1692-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1693-- given as the first argument, instead of a tupling function.
1546-- given as the first argument, instead of a tupling function.
1694-- Performs replacement on invalid scalar values.
1547-- Performs replacement on invalid scalar values.
1695zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1696−zipWith = coerce JSS.zipWith
1697−-- 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))
1698−-- where g a b = safe (f a b)
1550+ where g a b = safe (f a b)
1699{-# INLINE zipWith #-}
1551{-# INLINE zipWith #-}
1701-- | /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
1702-- representing white space.
1554-- representing white space.
1703words :: Text -> [Text]
1555words :: Text -> [Text]
1704−words = coerce JSS.words
1705−-- words t@(Text arr off len) = loop 0 0
1556+words t@(Text arr off len) = loop 0 0
1707−-- loop !start !n
1558+ loop !start !n
1708−-- | n >= len = if start == n
1559+ | n >= len = if start == n
1709−-- then []
1560+ then []
1710−-- else [Text arr (start+off) (n-start)]
1561+ else [Text arr (start+off) (n-start)]
1711−-- | isSpace c =
1562+ | isSpace c =
1712−-- if start == n
1563+ if start == n
1713−-- then loop (start+1) (start+1)
1564+ then loop (start+1) (start+1)
1714−-- 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)
1715−-- | otherwise = loop start (n+d)
1566+ | otherwise = loop start (n+d)
1716−-- where Iter c d = iter t n
1567+ where Iter c d = iter t n
1717{-# INLINE words #-}
1568{-# INLINE words #-}
1719-- | /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
1720-- newline 'Char's. The resulting strings do not contain newlines.
1571-- newline 'Char's. The resulting strings do not contain newlines.
1721lines :: Text -> [Text]
1572lines :: Text -> [Text]
1722−lines = coerce JSS.lines
1723−-- lines ps | null ps = []
1573+lines ps | null ps = []
1724−-- | otherwise = h : if null t
1574+ | otherwise = h : if null t
1725−-- then []
1575+ then []
1726−-- else lines (unsafeTail t)
1576+ else lines (unsafeTail t)
1727−-- where (# h,t #) = span_ (/= '\n') ps
1577+ where (# h,t #) = span_ (/= '\n') ps
1728{-# INLINE lines #-}
1578{-# INLINE lines #-}
@@ -1752,40 +1602,34 @@ lines' ps | null ps = []
1752-- | /O(n)/ Joins lines, after appending a terminating newline to
1602-- | /O(n)/ Joins lines, after appending a terminating newline to
1754unlines :: [Text] -> Text
1604unlines :: [Text] -> Text
1755−unlines = coerce JSS.unlines
1756−-- unlines = concat . L.map (`snoc` '\n')
1605+unlines = concat . L.map (`snoc` '\n')
1757{-# INLINE unlines #-}
1606{-# INLINE unlines #-}
1759-- | /O(n)/ Joins words using single space characters.
1608-- | /O(n)/ Joins words using single space characters.
1760unwords :: [Text] -> Text
1609unwords :: [Text] -> Text
1761−unwords = coerce JSS.unwords
1762−-- unwords = intercalate (singleton ' ')
1610+unwords = intercalate (singleton ' ')
1763{-# INLINE unwords #-}
1611{-# INLINE unwords #-}
1765-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1766-- '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.
1767isPrefixOf :: Text -> Text -> Bool
1615isPrefixOf :: Text -> Text -> Bool
1768−isPrefixOf = coerce JSS.isPrefixOf
1769−-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1616+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1770−-- alen <= blen && S.isPrefixOf (stream a) (stream b)
1617+ alen <= blen && S.isPrefixOf (stream a) (stream b)
1771{-# INLINE [1] isPrefixOf #-}
1618{-# INLINE [1] isPrefixOf #-}
1773{-# RULES
1620{-# RULES
1774"TEXT isPrefixOf -> fused" [~1] forall s t.
1621"TEXT isPrefixOf -> fused" [~1] forall s t.
1775 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1776−"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1777− S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
1780-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1781-- 'True' iff the first is a suffix of the second.
1626-- 'True' iff the first is a suffix of the second.
1782isSuffixOf :: Text -> Text -> Bool
1627isSuffixOf :: Text -> Text -> Bool
1783−isSuffixOf = coerce JSS.isSuffixOf
1784−-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1628+isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1785−-- d >= 0 && a == b'
1629+ d >= 0 && a == b'
1786−-- where d = blen - alen
1630+ where d = blen - alen
1787−-- b' | d == 0 = b
1631+ b' | d == 0 = b
1788−-- | otherwise = Text barr (boff+d) alen
1632+ | otherwise = Text barr (boff+d) alen
1789{-# INLINE isSuffixOf #-}
1633{-# INLINE isSuffixOf #-}
1791-- | /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
@@ -1795,11 +1639,10 @@ isSuffixOf = coerce JSS.isSuffixOf
1795-- In (unlikely) bad cases, this function's time complexity degrades
1639-- In (unlikely) bad cases, this function's time complexity degrades
1796-- towards /O(n*m)/.
1640-- towards /O(n*m)/.
1797isInfixOf :: Text -> Text -> Bool
1641isInfixOf :: Text -> Text -> Bool
1798−isInfixOf = coerce JSS.isInfixOf
1799−-- isInfixOf needle haystack
1642+isInfixOf needle haystack
1800−-- | null needle = True
1643+ | null needle = True
1801−-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1644+ | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1802−-- | otherwise = not . L.null . indices needle $ haystack
1645+ | otherwise = not . L.null . indices needle $ haystack
1803{-# INLINE [1] isInfixOf #-}
1646{-# INLINE [1] isInfixOf #-}
1805{-# RULES
1648{-# RULES
@@ -1829,10 +1672,9 @@ isInfixOf = coerce JSS.isInfixOf
1829-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1830-- > fnordLength _ = -1
1673-- > fnordLength _ = -1
1831stripPrefix :: Text -> Text -> Maybe Text
1674stripPrefix :: Text -> Text -> Maybe Text
1832−stripPrefix = coerce JSS.stripPrefix
1833−-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1675+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1834−-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1676+ | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1835−-- | otherwise = Nothing
1677+ | otherwise = Nothing
1837-- | /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
1838-- 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
@@ -1847,16 +1689,15 @@ stripPrefix = coerce JSS.stripPrefix
1847-- > commonPrefixes "veeble" "fetzer" == Nothing
1689-- > commonPrefixes "veeble" "fetzer" == Nothing
1848-- > commonPrefixes "" "baz" == Nothing
1690-- > commonPrefixes "" "baz" == Nothing
1849commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1850−commonPrefixes = coerce JSS.commonPrefixes
1851−-- 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
1853−-- 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)
1854−-- | i > 0 = Just (Text arr0 off0 i,
1695+ | i > 0 = Just (Text arr0 off0 i,
1855−-- text arr0 (off0+i) (len0-i),
1696+ text arr0 (off0+i) (len0-i),
1856−-- text arr1 (off1+j) (len1-j))
1697+ text arr1 (off1+j) (len1-j))
1857−-- | otherwise = Nothing
1698+ | otherwise = Nothing
1858−-- where Iter a d0 = iter t0 i
1699+ where Iter a d0 = iter t0 i
1859−-- Iter b d1 = iter t1 j
1700+ Iter b d1 = iter t1 j
1861-- | /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
1862-- matches the entire first string.
1703-- matches the entire first string.
@@ -1877,22 +1718,21 @@ commonPrefixes = coerce JSS.commonPrefixes
1877-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1878-- > quuxLength _ = -1
1719-- > quuxLength _ = -1
1879stripSuffix :: Text -> Text -> Maybe Text
1720stripSuffix :: Text -> Text -> Maybe Text
1880−stripSuffix = coerce JSS.stripSuffix
1881−-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1721+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1882−-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
1722+ | p `isSuffixOf` t = Just $! text arr off (len-plen)
1883−-- | otherwise = Nothing
1723+ | otherwise = Nothing
1885−-- -- | Add a list of non-negative numbers. Errors out on overflow.
1725+-- | Add a list of non-negative numbers. Errors out on overflow.
1886−-- sumP :: String -> [Int] -> Int
1726+sumP :: String -> [Int] -> Int
1887−-- sumP fun = go 0
1727+sumP fun = go 0
1888−-- where go !a (x:xs)
1728+ where go !a (x:xs)
1889−-- | ax >= 0 = go ax xs
1729+ | ax >= 0 = go ax xs
1890−-- | otherwise = overflowError fun
1730+ | otherwise = overflowError fun
1891−-- where ax = a + x
1731+ where ax = a + x
1892−-- go a _ = a
1732+ go a _ = a
1894−-- emptyError :: String -> a
1734+emptyError :: String -> a
1895−-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1735+emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1897overflowError :: String -> a
1737overflowError :: String -> a
1898overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1906,14 +1746,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1906-- 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
1907-- garbage collected if there are no other live references to it.
1747-- garbage collected if there are no other live references to it.
1908copy :: Text -> Text
1748copy :: Text -> Text
1910−-- copy (Text arr off len) = Text (A.run go) 0 len
1749+copy (Text arr off len) = Text (A.run go) 0 len
1912−-- go :: ST s (A.MArray s)
1751+ go :: ST s (A.MArray s)
1913−-- go = do
1752+ go = do
1914−-- marr <- A.new len
1753+ marr <- A.new len
1915−
1754+ A.copyI marr 0 arr off len
1916−foreign import javascript unsafe
1755+ return marr
1917− "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString
1918−foreign import javascript unsafe
1919− "$2.substr($1)" js_substr1 :: Int -> JSString -> JSString