@@ -6,7 +6,7 @@
6#if __GLASGOW_HASKELL__ >= 708
6#if __GLASGOW_HASKELL__ >= 708
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
9−
9+{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
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, firstf, mul, safe, text)
227+import Data.Text.Internal (Text(..), empty, 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,6 +246,53 @@ 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+---------------------------------------------------------------------
249-- $strict
296-- $strict
251-- This package provides both strict and lazy 'Text' types. The
298-- This package provides both strict and lazy 'Text' types. The
@@ -321,17 +368,17 @@ import Text.Printf (PrintfArg, formatArg, formatString)
321-- Functions that can be fused by the compiler are documented with the
368-- Functions that can be fused by the compiler are documented with the
322-- phrase \"Subject to fusion\".
369-- phrase \"Subject to fusion\".
324−instance Eq Text where
371+-- instance Eq Text where
325− Text arrA offA lenA == Text arrB offB lenB
372+-- Text arrA offA lenA == Text arrB offB lenB
326− | lenA == lenB = A.equal arrA offA arrB offB lenA
373+-- | lenA == lenB = A.equal arrA offA arrB offB lenA
327− | otherwise = False
374+-- | otherwise = False
328− {-# INLINE (==) #-}
375+-- {-# INLINE (==) #-}
330−instance Ord Text where
377+-- instance Ord Text where
331− compare = compareText
378+-- compare = compareText
333−instance Read Text where
380+-- instance Read Text where
334− readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
381+-- readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
336#if MIN_VERSION_base(4,9,0)
383#if MIN_VERSION_base(4,9,0)
337-- Semigroup orphan instances for older GHCs are provided by
384-- Semigroup orphan instances for older GHCs are provided by
@@ -341,17 +388,17 @@ instance Semigroup Text where
341 (<>) = append
388 (<>) = append
344−instance Monoid Text where
391+-- instance Monoid Text where
345− mempty = empty
392+-- mempty = empty
346−#if MIN_VERSION_base(4,9,0)
393+-- #if MIN_VERSION_base(4,9,0)
347− mappend = (<>) -- future-proof definition
394+-- mappend = (<>) -- future-proof definition
349− mappend = append
396+-- mappend = append
351− mconcat = concat
398+-- mconcat = concat
353−instance IsString Text where
400+-- instance IsString Text where
354− fromString = pack
401+-- fromString = pack
356#if __GLASGOW_HASKELL__ >= 708
403#if __GLASGOW_HASKELL__ >= 708
357instance Exts.IsList Text where
404instance Exts.IsList Text where
@@ -387,18 +434,18 @@ instance Binary Text where
387-- and 'Data.Map.Map' is archived here:
434-- and 'Data.Map.Map' is archived here:
388-- <http://markmail.org/message/trovdc6zkphyi3cr#query:+page:1+mid:a46der3iacwjcf6n+state:results Proposal: Allow gunfold for Data.Map, ... >
435-- <http://markmail.org/message/trovdc6zkphyi3cr#query:+page:1+mid:a46der3iacwjcf6n+state:results Proposal: Allow gunfold for Data.Map, ... >
390−instance Data Text where
437+-- instance Data Text where
391− gfoldl f z txt = z pack `f` (unpack txt)
438+-- gfoldl f z txt = z pack `f` (unpack txt)
392− toConstr _ = packConstr
439+-- toConstr _ = packConstr
393− gunfold k z c = case constrIndex c of
440+-- gunfold k z c = case constrIndex c of
394− 1 -> k (z pack)
441+-- 1 -> k (z pack)
395− _ -> P.error "gunfold"
442+-- _ -> P.error "gunfold"
396− dataTypeOf _ = textDataType
443+-- dataTypeOf _ = textDataType
398#if MIN_VERSION_base(4,7,0)
445#if MIN_VERSION_base(4,7,0)
399-- | Only defined for @base-4.7.0.0@ and later
446-- | Only defined for @base-4.7.0.0@ and later
400instance PrintfArg Text where
447instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
448+ formatArg t = formatString $ unpack t
404packConstr :: Constr
451packConstr :: Constr
@@ -407,19 +454,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
407textDataType :: DataType
454textDataType :: DataType
408textDataType = mkDataType "Data.Text.Text" [packConstr]
455textDataType = mkDataType "Data.Text.Text" [packConstr]
410−-- | /O(n)/ Compare two 'Text' values lexicographically.
457+-- -- | /O(n)/ Compare two 'Text' values lexicographically.
411−compareText :: Text -> Text -> Ordering
458+-- compareText :: Text -> Text -> Ordering
412−compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
459+-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
413− | lenA == 0 && lenB == 0 = EQ
460+-- | lenA == 0 && lenB == 0 = EQ
414− | otherwise = go 0 0
461+-- | otherwise = go 0 0
416− go !i !j
463+-- go !i !j
417− | i >= lenA || j >= lenB = compare lenA lenB
464+-- | i >= lenA || j >= lenB = compare lenA lenB
418− | a < b = LT
465+-- | a < b = LT
419− | a > b = GT
466+-- | a > b = GT
420− | otherwise = go (i+di) (j+dj)
467+-- | otherwise = go (i+di) (j+dj)
421− where Iter a di = iter ta i
468+-- where Iter a di = iter ta i
422− Iter b dj = iter tb j
469+-- Iter b dj = iter tb j
424-- -----------------------------------------------------------------------------
471-- -----------------------------------------------------------------------------
425-- * Conversion to/from 'Text'
472-- * Conversion to/from 'Text'
@@ -427,7 +474,8 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
427-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
474-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
428-- fusion. Performs replacement on invalid scalar values.
475-- fusion. Performs replacement on invalid scalar values.
429pack :: String -> Text
476pack :: String -> Text
477+pack = coerce JSS.pack
430−pack = unstream . S.map safe . S.streamList
478+-- pack = unstream . S.map safe . S.streamList
431{-# INLINE [1] pack #-}
479{-# INLINE [1] pack #-}
433-- -----------------------------------------------------------------------------
481-- -----------------------------------------------------------------------------
@@ -438,7 +486,8 @@ pack = unstream . S.map safe . S.streamList
438-- copying a new array. Subject to fusion. Performs replacement on
486-- copying a new array. Subject to fusion. Performs replacement on
439-- invalid scalar values.
487-- invalid scalar values.
440cons :: Char -> Text -> Text
488cons :: Char -> Text -> Text
489+cons = coerce JSS.cons
441−cons c t = unstream (S.cons (safe c) (stream t))
490+-- cons c t = unstream (S.cons (safe c) (stream t))
442{-# INLINE cons #-}
491{-# INLINE cons #-}
444infixr 5 `cons`
493infixr 5 `cons`
@@ -447,25 +496,27 @@ infixr 5 `cons`
447-- entire array in the process, unless fused. Subject to fusion.
496-- entire array in the process, unless fused. Subject to fusion.
448-- Performs replacement on invalid scalar values.
497-- Performs replacement on invalid scalar values.
449snoc :: Text -> Char -> Text
498snoc :: Text -> Char -> Text
499+snoc = coerce JSS.snoc
450−snoc t c = unstream (S.snoc (stream t) (safe c))
500+-- snoc t c = unstream (S.snoc (stream t) (safe c))
451{-# INLINE snoc #-}
501{-# INLINE snoc #-}
453-- | /O(n)/ Appends one 'Text' to the other by copying both of them
503-- | /O(n)/ Appends one 'Text' to the other by copying both of them
454-- into a new 'Text'. Subject to fusion.
504-- into a new 'Text'. Subject to fusion.
455append :: Text -> Text -> Text
505append :: Text -> Text -> Text
506+append = coerce JSS.append
456−append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
507+-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
457− | len1 == 0 = b
508+-- | len1 == 0 = b
458− | len2 == 0 = a
509+-- | len2 == 0 = a
459− | len > 0 = Text (A.run x) 0 len
510+-- | len > 0 = Text (A.run x) 0 len
460− | otherwise = overflowError "append"
511+-- | otherwise = overflowError "append"
462− len = len1+len2
513+-- len = len1+len2
463− x :: ST s (A.MArray s)
514+-- x :: ST s (A.MArray s)
465− arr <- A.new len
516+-- arr <- A.new len
466− A.copyI arr 0 arr1 off1 len1
517+-- A.copyI arr 0 arr1 off1 len1
467− A.copyI arr len1 arr2 off2 len
518+-- A.copyI arr len1 arr2 off2 len
468− return arr
519+-- return arr
469{-# NOINLINE append #-}
520{-# NOINLINE append #-}
@@ -478,16 +529,18 @@ append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
478-- | /O(1)/ Returns the first character of a 'Text', which must be
529-- | /O(1)/ Returns the first character of a 'Text', which must be
479-- non-empty. Subject to fusion.
530-- non-empty. Subject to fusion.
480head :: Text -> Char
531head :: Text -> Char
532+head = coerce JSS.head
481−head t = S.head (stream t)
533+-- head t = S.head (stream t)
482{-# INLINE head #-}
534{-# INLINE head #-}
484-- | /O(1)/ Returns the first character and rest of a 'Text', or
536-- | /O(1)/ Returns the first character and rest of a 'Text', or
485-- 'Nothing' if empty. Subject to fusion.
537-- 'Nothing' if empty. Subject to fusion.
486uncons :: Text -> Maybe (Char, Text)
538uncons :: Text -> Maybe (Char, Text)
539+uncons = coerce JSS.uncons
487−uncons t@(Text arr off len)
540+-- uncons t@(Text arr off len)
488− | len <= 0 = Nothing
541+-- | len <= 0 = Nothing
489− | otherwise = Just $ let !(Iter c d) = iter t 0
542+-- | otherwise = Just $ let !(Iter c d) = iter t 0
490− in (c, text arr (off+d) (len-d))
543+-- in (c, text arr (off+d) (len-d))
491{-# INLINE [1] uncons #-}
544{-# INLINE [1] uncons #-}
493-- | Lifted from Control.Arrow and specialized.
546-- | Lifted from Control.Arrow and specialized.
@@ -497,12 +550,13 @@ second f (a, b) = (a, f b)
497-- | /O(1)/ Returns the last character of a 'Text', which must be
550-- | /O(1)/ Returns the last character of a 'Text', which must be
498-- non-empty. Subject to fusion.
551-- non-empty. Subject to fusion.
499last :: Text -> Char
552last :: Text -> Char
553+last = coerce JSS.last
500−last (Text arr off len)
554+-- last (Text arr off len)
501− | len <= 0 = emptyError "last"
555+-- | len <= 0 = emptyError "last"
502− | n < 0xDC00 || n > 0xDFFF = unsafeChr n
556+-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
503− | otherwise = U16.chr2 n0 n
557+-- | otherwise = U16.chr2 n0 n
504− where n = A.unsafeIndex arr (off+len-1)
558+-- where n = A.unsafeIndex arr (off+len-1)
505− n0 = A.unsafeIndex arr (off+len-2)
559+-- n0 = A.unsafeIndex arr (off+len-2)
506{-# INLINE [1] last #-}
560{-# INLINE [1] last #-}
@@ -515,10 +569,11 @@ last (Text arr off len)
515-- | /O(1)/ Returns all characters after the head of a 'Text', which
569-- | /O(1)/ Returns all characters after the head of a 'Text', which
516-- must be non-empty. Subject to fusion.
570-- must be non-empty. Subject to fusion.
517tail :: Text -> Text
571tail :: Text -> Text
572+tail = coerce JSS.tail
518−tail t@(Text arr off len)
573+-- tail t@(Text arr off len)
519− | len <= 0 = emptyError "tail"
574+-- | len <= 0 = emptyError "tail"
520− | otherwise = text arr (off+d) (len-d)
575+-- | otherwise = text arr (off+d) (len-d)
521− where d = iter_ t 0
576+-- where d = iter_ t 0
522{-# INLINE [1] tail #-}
577{-# INLINE [1] tail #-}
@@ -531,11 +586,12 @@ tail t@(Text arr off len)
531-- | /O(1)/ Returns all but the last character of a 'Text', which must
586-- | /O(1)/ Returns all but the last character of a 'Text', which must
532-- be non-empty. Subject to fusion.
587-- be non-empty. Subject to fusion.
533init :: Text -> Text
588init :: Text -> Text
589+init = coerce JSS.init
534−init (Text arr off len) | len <= 0 = emptyError "init"
590+-- init (Text arr off len) | len <= 0 = emptyError "init"
535− | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
591+-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
536− | otherwise = text arr off (len-1)
592+-- | otherwise = text arr off (len-1)
538− n = A.unsafeIndex arr (off+len-1)
594+-- n = A.unsafeIndex arr (off+len-1)
539{-# INLINE [1] init #-}
595{-# INLINE [1] init #-}
@@ -548,11 +604,12 @@ init (Text arr off len) | len <= 0 = emptyError "init"
548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
604-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
549-- fusion.
605-- fusion.
550null :: Text -> Bool
606null :: Text -> Bool
607+null = coerce JSS.null
551−null (Text _arr _off len) =
608+-- null (Text _arr _off len) =
552−#if defined(ASSERTS)
609+-- #if defined(ASSERTS)
553− assert (len >= 0) $
610+-- assert (len >= 0) $
555− len <= 0
612+-- len <= 0
556{-# INLINE [1] null #-}
613{-# INLINE [1] null #-}
@@ -564,14 +621,15 @@ null (Text _arr _off len) =
565-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
622-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
566-- Subject to fusion.
623-- Subject to fusion.
567−isSingleton :: Text -> Bool
624+-- isSingleton :: Text -> Bool
568−isSingleton = S.isSingleton . stream
625+-- isSingleton = S.isSingleton . stream
569−{-# INLINE isSingleton #-}
626+-- {-# INLINE isSingleton #-}
571-- | /O(n)/ Returns the number of characters in a 'Text'.
628-- | /O(n)/ Returns the number of characters in a 'Text'.
572-- Subject to fusion.
629-- Subject to fusion.
573length :: Text -> Int
630length :: Text -> Int
631+length = coerce JSS.length
574−length t = S.length (stream t)
632+-- length t = S.length (stream t)
575{-# INLINE [0] length #-}
633{-# INLINE [0] length #-}
576-- length needs to be phased after the compareN/length rules otherwise
634-- length needs to be phased after the compareN/length rules otherwise
577-- it may inline before the rules have an opportunity to fire.
635-- it may inline before the rules have an opportunity to fire.
@@ -627,26 +685,30 @@ compareLength t n = S.compareLengthI (stream t) n
627-- each element of @t@. Subject to fusion. Performs replacement on
685-- each element of @t@. Subject to fusion. Performs replacement on
628-- invalid scalar values.
686-- invalid scalar values.
629map :: (Char -> Char) -> Text -> Text
687map :: (Char -> Char) -> Text -> Text
630−map f t = unstream (S.map (safe . f) (stream t))
689+-- map f t = unstream (S.map (safe . f) (stream t))
631{-# INLINE [1] map #-}
690{-# INLINE [1] map #-}
633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
692-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
634-- 'Text's and concatenates the list after interspersing the first
693-- 'Text's and concatenates the list after interspersing the first
635-- argument between each element of the list.
694-- argument between each element of the list.
636intercalate :: Text -> [Text] -> Text
695intercalate :: Text -> [Text] -> Text
696+intercalate = coerce JSS.intercalate
637−intercalate t = concat . (F.intersperse t)
697+-- intercalate t = concat . (F.intersperse t)
638{-# INLINE intercalate #-}
698{-# INLINE intercalate #-}
640-- | /O(n)/ The 'intersperse' function takes a character and places it
700-- | /O(n)/ The 'intersperse' function takes a character and places it
641-- between the characters of a 'Text'. Subject to fusion. Performs
701-- between the characters of a 'Text'. Subject to fusion. Performs
642-- replacement on invalid scalar values.
702-- replacement on invalid scalar values.
643intersperse :: Char -> Text -> Text
703intersperse :: Char -> Text -> Text
704+intersperse = coerce JSS.intersperse
644−intersperse c t = unstream (S.intersperse (safe c) (stream t))
705+-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
645{-# INLINE intersperse #-}
706{-# INLINE intersperse #-}
647-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
708-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
648reverse :: Text -> Text
709reverse :: Text -> Text
710+reverse = coerce JSS.reverse
649−reverse t = S.reverse (stream t)
711+-- reverse t = S.reverse (stream t)
650{-# INLINE reverse #-}
712{-# INLINE reverse #-}
652-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
714-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -680,28 +742,29 @@ replace :: Text
681 -- ^ @haystack@ in which to search.
743 -- ^ @haystack@ in which to search.
745+replace = coerce JSS.replace
683−replace needle@(Text _ _ neeLen)
746+-- replace needle@(Text _ _ neeLen)
684− (Text repArr repOff repLen)
747+-- (Text repArr repOff repLen)
685− haystack@(Text hayArr hayOff hayLen)
748+-- haystack@(Text hayArr hayOff hayLen)
686− | neeLen == 0 = emptyError "replace"
749+-- | neeLen == 0 = emptyError "replace"
687− | L.null ixs = haystack
750+-- | L.null ixs = haystack
688− | len > 0 = Text (A.run x) 0 len
751+-- | len > 0 = Text (A.run x) 0 len
689− | otherwise = empty
752+-- | otherwise = empty
691− ixs = indices needle haystack
754+-- ixs = indices needle haystack
692− len = hayLen - (neeLen - repLen) `mul` L.length ixs
755+-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
693− x :: ST s (A.MArray s)
756+-- x :: ST s (A.MArray s)
695− marr <- A.new len
758+-- marr <- A.new len
696− let loop (i:is) o d = do
759+-- let loop (i:is) o d = do
697− let d0 = d + i - o
760+-- let d0 = d + i - o
698− d1 = d0 + repLen
761+-- d1 = d0 + repLen
699− A.copyI marr d hayArr (hayOff+o) d0
762+-- A.copyI marr d hayArr (hayOff+o) d0
700− A.copyI marr d0 repArr repOff d1
763+-- A.copyI marr d0 repArr repOff d1
701− loop is (i + neeLen) d1
764+-- loop is (i + neeLen) d1
702− loop [] o d = A.copyI marr d hayArr (hayOff+o) len
765+-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
703− loop ixs 0 0
766+-- loop ixs 0 0
704− return marr
767+-- return marr
706-- ----------------------------------------------------------------------------
769-- ----------------------------------------------------------------------------
707-- ** Case conversions (folds)
770-- ** Case conversions (folds)
@@ -740,7 +803,8 @@ replace needle@(Text _ _ neeLen)
740-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
803-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
741-- instead of itself.
804-- instead of itself.
742toCaseFold :: Text -> Text
805toCaseFold :: Text -> Text
743−toCaseFold t = unstream (S.toCaseFold (stream t))
806+-- toCaseFold t = unstream (S.toCaseFold (stream t))
807+toCaseFold = coerce JSS.toCaseFold
744{-# INLINE toCaseFold #-}
808{-# INLINE toCaseFold #-}
746-- | /O(n)/ Convert a string to lower case, using simple case
810-- | /O(n)/ Convert a string to lower case, using simple case
@@ -751,7 +815,8 @@ toCaseFold t = unstream (S.toCaseFold (stream t))
751-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
815-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
752-- followed by \" ̇\" (combining dot above, U+0307).
816-- followed by \" ̇\" (combining dot above, U+0307).
753toLower :: Text -> Text
817toLower :: Text -> Text
754−toLower t = unstream (S.toLower (stream t))
818+-- toLower t = unstream (S.toLower (stream t))
819+toLower = coerce JSS.toLower
755{-# INLINE toLower #-}
820{-# INLINE toLower #-}
757-- | /O(n)/ Convert a string to upper case, using simple case
822-- | /O(n)/ Convert a string to upper case, using simple case
@@ -761,7 +826,8 @@ toLower t = unstream (S.toLower (stream t))
761-- instance, the German \"ß\" (eszett, U+00DF) maps to the
826-- instance, the German \"ß\" (eszett, U+00DF) maps to the
762-- two-letter sequence \"SS\".
827-- two-letter sequence \"SS\".
763toUpper :: Text -> Text
828toUpper :: Text -> Text
764−toUpper t = unstream (S.toUpper (stream t))
829+-- toUpper t = unstream (S.toUpper (stream t))
830+toUpper = coerce JSS.toUpper
765{-# INLINE toUpper #-}
831{-# INLINE toUpper #-}
767-- | /O(n)/ Convert a string to title case, using simple case
833-- | /O(n)/ Convert a string to title case, using simple case
@@ -783,7 +849,8 @@ toUpper t = unstream (S.toUpper (stream t))
783-- Fox\" is correctly title cased—but this function will
849-- Fox\" is correctly title cased—but this function will
784-- capitalize /every/ word.
850-- capitalize /every/ word.
785toTitle :: Text -> Text
851toTitle :: Text -> Text
852+toTitle = coerce JSS.toTitle
786−toTitle t = unstream (S.toTitle (stream t))
853+-- toTitle t = unstream (S.toTitle (stream t))
787{-# INLINE toTitle #-}
854{-# INLINE toTitle #-}
789-- | /O(n)/ Left-justify a string to the given length, using the
856-- | /O(n)/ Left-justify a string to the given length, using the
@@ -795,10 +862,11 @@ toTitle t = unstream (S.toTitle (stream t))
795-- > justifyLeft 7 'x' "foo" == "fooxxxx"
862-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796-- > justifyLeft 3 'x' "foobar" == "foobar"
863-- > justifyLeft 3 'x' "foobar" == "foobar"
797justifyLeft :: Int -> Char -> Text -> Text
864justifyLeft :: Int -> Char -> Text -> Text
798−justifyLeft k c t
865+justifyLeft = coerce JSS.justifyLeft
799− | len >= k = t
867+-- | len >= k = t
800− | otherwise = t `append` replicateChar (k-len) c
868+-- | otherwise = t `append` replicateChar (k-len) c
801− where len = length t
869+-- where len = length t
802{-# INLINE [1] justifyLeft #-}
870{-# INLINE [1] justifyLeft #-}
@@ -817,10 +885,11 @@ justifyLeft k c t
817-- > justifyRight 7 'x' "bar" == "xxxxbar"
885-- > justifyRight 7 'x' "bar" == "xxxxbar"
818-- > justifyRight 3 'x' "foobar" == "foobar"
886-- > justifyRight 3 'x' "foobar" == "foobar"
819justifyRight :: Int -> Char -> Text -> Text
887justifyRight :: Int -> Char -> Text -> Text
820−justifyRight k c t
888+justifyRight = coerce JSS.justifyRight
889+-- justifyRight k c t
821− | len >= k = t
890+-- | len >= k = t
822− | otherwise = replicateChar (k-len) c `append` t
891+-- | otherwise = replicateChar (k-len) c `append` t
823− where len = length t
892+-- where len = length t
824{-# INLINE justifyRight #-}
893{-# INLINE justifyRight #-}
826-- | /O(n)/ Center a string to the given length, using the specified
895-- | /O(n)/ Center a string to the given length, using the specified
@@ -831,13 +900,14 @@ justifyRight k c t
832-- > center 8 'x' "HS" = "xxxHSxxx"
901-- > center 8 'x' "HS" = "xxxHSxxx"
833center :: Int -> Char -> Text -> Text
902center :: Int -> Char -> Text -> Text
834−center k c t
903+center = coerce JSS.center
835− | len >= k = t
905+-- | len >= k = t
836− | otherwise = replicateChar l c `append` t `append` replicateChar r c
906+-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
837− where len = length t
907+-- where len = length t
838− d = k - len
908+-- d = k - len
839− r = d `quot` 2
909+-- r = d `quot` 2
840− l = d - r
910+-- l = d - r
841{-# INLINE center #-}
911{-# INLINE center #-}
843-- | /O(n)/ The 'transpose' function transposes the rows and columns
913-- | /O(n)/ The 'transpose' function transposes the rows and columns
@@ -845,7 +915,8 @@ center k c t
845-- 'unpack', and the list version of transpose, and is thus not very
915-- 'unpack', and the list version of transpose, and is thus not very
846-- efficient.
916-- efficient.
847transpose :: [Text] -> [Text]
917transpose :: [Text] -> [Text]
918+transpose = coerce JSS.transpose
848−transpose ts = P.map pack (L.transpose (P.map unpack ts))
919+-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
850-- -----------------------------------------------------------------------------
921-- -----------------------------------------------------------------------------
851-- * Reducing 'Text's (folds)
922-- * Reducing 'Text's (folds)
@@ -855,23 +926,27 @@ transpose ts = P.map pack (L.transpose (P.map unpack ts))
855-- reduces the 'Text' using the binary operator, from left to right.
926-- reduces the 'Text' using the binary operator, from left to right.
856-- Subject to fusion.
927-- Subject to fusion.
857foldl :: (a -> Char -> a) -> a -> Text -> a
928foldl :: (a -> Char -> a) -> a -> Text -> a
929+foldl f a0 (Text t) = JSS.foldl f a0 t
858−foldl f z t = S.foldl f z (stream t)
930+-- foldl f z t = S.foldl f z (stream t)
859{-# INLINE foldl #-}
931{-# INLINE foldl #-}
861-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
933-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
862foldl' :: (a -> Char -> a) -> a -> Text -> a
934foldl' :: (a -> Char -> a) -> a -> Text -> a
935+foldl' f a0 (Text t) = JSS.foldl' f a0 t
863−foldl' f z t = S.foldl' f z (stream t)
936+-- foldl' f z t = S.foldl' f z (stream t)
864{-# INLINE foldl' #-}
937{-# INLINE foldl' #-}
866-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
939-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
867-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
940-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
868foldl1 :: (Char -> Char -> Char) -> Text -> Char
941foldl1 :: (Char -> Char -> Char) -> Text -> Char
942+foldl1 = coerce JSS.foldl1
869−foldl1 f t = S.foldl1 f (stream t)
943+-- foldl1 f t = S.foldl1 f (stream t)
870{-# INLINE foldl1 #-}
944{-# INLINE foldl1 #-}
872-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
946-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
873foldl1' :: (Char -> Char -> Char) -> Text -> Char
947foldl1' :: (Char -> Char -> Char) -> Text -> Char
948+foldl1' = coerce JSS.foldl1'
874−foldl1' f t = S.foldl1' f (stream t)
949+-- foldl1' f t = S.foldl1' f (stream t)
875{-# INLINE foldl1' #-}
950{-# INLINE foldl1' #-}
877-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
952-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -879,14 +954,16 @@ foldl1' f t = S.foldl1' f (stream t)
879-- reduces the 'Text' using the binary operator, from right to left.
954-- reduces the 'Text' using the binary operator, from right to left.
880-- Subject to fusion.
955-- Subject to fusion.
881foldr :: (Char -> a -> a) -> a -> Text -> a
956foldr :: (Char -> a -> a) -> a -> Text -> a
957+foldr f a0 (Text t) = JSS.foldr f a0 t
882−foldr f z t = S.foldr f z (stream t)
958+-- foldr f z t = S.foldr f z (stream t)
883{-# INLINE foldr #-}
959{-# INLINE foldr #-}
885-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
961-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
886-- and thus must be applied to a non-empty 'Text'. Subject to
962-- and thus must be applied to a non-empty 'Text'. Subject to
887-- fusion.
963-- fusion.
888foldr1 :: (Char -> Char -> Char) -> Text -> Char
964foldr1 :: (Char -> Char -> Char) -> Text -> Char
965+foldr1 = coerce JSS.foldr1
889−foldr1 f t = S.foldr1 f (stream t)
966+-- foldr1 f t = S.foldr1 f (stream t)
890{-# INLINE foldr1 #-}
967{-# INLINE foldr1 #-}
892-- -----------------------------------------------------------------------------
969-- -----------------------------------------------------------------------------
@@ -894,48 +971,54 @@ foldr1 f t = S.foldr1 f (stream t)
895-- | /O(n)/ Concatenate a list of 'Text's.
972-- | /O(n)/ Concatenate a list of 'Text's.
896concat :: [Text] -> Text
973concat :: [Text] -> Text
974+concat = coerce JSS.concat
897−concat ts = case ts' of
975+-- concat ts = case ts' of
898− [] -> empty
976+-- [] -> empty
899− [t] -> t
977+-- [t] -> t
900− _ -> Text (A.run go) 0 len
978+-- _ -> Text (A.run go) 0 len
902− ts' = L.filter (not . null) ts
980+-- ts' = L.filter (not . null) ts
903− len = sumP "concat" $ L.map lengthWord16 ts'
981+-- len = sumP "concat" $ L.map lengthWord16 ts'
904− go :: ST s (A.MArray s)
982+-- go :: ST s (A.MArray s)
905− go = do
983+-- go = do
906− arr <- A.new len
984+-- arr <- A.new len
907− let step i (Text a o l) =
985+-- let step i (Text a o l) =
908− let !j = i + l in A.copyI arr i a o j >> return j
986+-- let !j = i + l in A.copyI arr i a o j >> return j
909− foldM step 0 ts' >> return arr
987+-- foldM step 0 ts' >> return arr
911-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
989-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
912-- concatenate the results.
990-- concatenate the results.
913concatMap :: (Char -> Text) -> Text -> Text
991concatMap :: (Char -> Text) -> Text -> Text
992+concatMap = coerce JSS.concatMap
914−concatMap f = concat . foldr ((:) . f) []
993+-- concatMap f = concat . foldr ((:) . f) []
915{-# INLINE concatMap #-}
994{-# INLINE concatMap #-}
917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
996-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
918-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
997-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
919any :: (Char -> Bool) -> Text -> Bool
998any :: (Char -> Bool) -> Text -> Bool
920−any p t = S.any p (stream t)
1000+-- any p t = S.any p (stream t)
921{-# INLINE any #-}
1001{-# INLINE any #-}
923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
1003-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
924-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
1004-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
925all :: (Char -> Bool) -> Text -> Bool
1005all :: (Char -> Bool) -> Text -> Bool
1006+all = coerce JSS.all
926−all p t = S.all p (stream t)
1007+-- all p t = S.all p (stream t)
927{-# INLINE all #-}
1008{-# INLINE all #-}
929-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
1010-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
930-- must be non-empty. Subject to fusion.
1011-- must be non-empty. Subject to fusion.
931maximum :: Text -> Char
1012maximum :: Text -> Char
1013+maximum = coerce JSS.maximum
932−maximum t = S.maximum (stream t)
1014+-- maximum t = S.maximum (stream t)
933{-# INLINE maximum #-}
1015{-# INLINE maximum #-}
935-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
1017-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
936-- must be non-empty. Subject to fusion.
1018-- must be non-empty. Subject to fusion.
937minimum :: Text -> Char
1019minimum :: Text -> Char
1020+minimum = coerce JSS.minimum
938−minimum t = S.minimum (stream t)
1021+-- minimum t = S.minimum (stream t)
939{-# INLINE minimum #-}
1022{-# INLINE minimum #-}
941-- -----------------------------------------------------------------------------
1024-- -----------------------------------------------------------------------------
@@ -951,8 +1034,9 @@ minimum t = S.minimum (stream t)
952-- > last (scanl f z xs) == foldl f z xs.
1035-- > last (scanl f z xs) == foldl f z xs.
953scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1036scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
1037+scanl = coerce JSS.scanl
954−scanl f z t = unstream (S.scanl g z (stream t))
1038+-- scanl f z t = unstream (S.scanl g z (stream t))
955− where g a b = safe (f a b)
1039+-- where g a b = safe (f a b)
956{-# INLINE scanl #-}
1040{-# INLINE scanl #-}
958-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
1042-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -961,8 +1045,9 @@ scanl f z t = unstream (S.scanl g z (stream t))
962-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1046-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
963scanl1 :: (Char -> Char -> Char) -> Text -> Text
1047scanl1 :: (Char -> Char -> Char) -> Text -> Text
1048+scanl1 = coerce JSS.scanl1
964−scanl1 f t | null t = empty
1049+-- scanl1 f t | null t = empty
965− | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1050+-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
966{-# INLINE scanl1 #-}
1051{-# INLINE scanl1 #-}
968-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
1053-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -970,16 +1055,18 @@ scanl1 f t | null t = empty
971-- > scanr f v == reverse . scanl (flip f) v . reverse
1056-- > scanr f v == reverse . scanl (flip f) v . reverse
972scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1057scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
1058+scanr = coerce JSS.scanr
973−scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1059+-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
974− where g a b = safe (f a b)
1060+-- where g a b = safe (f a b)
975{-# INLINE scanr #-}
1061{-# INLINE scanr #-}
977-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
1063-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
978-- value argument. Subject to fusion. Performs replacement on
1064-- value argument. Subject to fusion. Performs replacement on
979-- invalid scalar values.
1065-- invalid scalar values.
980scanr1 :: (Char -> Char -> Char) -> Text -> Text
1066scanr1 :: (Char -> Char -> Char) -> Text -> Text
1067+scanr1 = coerce JSS.scanr1
981−scanr1 f t | null t = empty
1068+-- scanr1 f t | null t = empty
982− | otherwise = scanr f (last t) (init t)
1069+-- | otherwise = scanr f (last t) (init t)
983{-# INLINE scanr1 #-}
1070{-# INLINE scanr1 #-}
985-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
1072-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -987,8 +1074,11 @@ scanr1 f t | null t = empty
987-- parameter from left to right, and returns a final 'Text'. Performs
1074-- parameter from left to right, and returns a final 'Text'. Performs
988-- replacement on invalid scalar values.
1075-- replacement on invalid scalar values.
989mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1076mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1077+mapAccumL f z0 (Text a) =
1078+ let (a', t) = JSS.mapAccumL f z0 a
990−mapAccumL f z0 = S.mapAccumL g z0 . stream
1080+-- mapAccumL f z0 = S.mapAccumL g z0 . stream
991− where g a b = second safe (f a b)
1081+-- where g a b = second safe (f a b)
992{-# INLINE mapAccumL #-}
1082{-# INLINE mapAccumL #-}
994-- | The 'mapAccumR' function behaves like a combination of 'map' and
1084-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -998,8 +1088,11 @@ mapAccumL f z0 = S.mapAccumL g z0 . stream
998-- 'Text'.
1088-- 'Text'.
999-- Performs replacement on invalid scalar values.
1089-- Performs replacement on invalid scalar values.
1000mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1090mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1091+mapAccumR f z0 (Text a) =
1092+ let (a', t) = JSS.mapAccumR f z0 a
1001−mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1094+-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1002− where g a b = second safe (f a b)
1095+-- where g a b = second safe (f a b)
1003{-# INLINE mapAccumR #-}
1096{-# INLINE mapAccumR #-}
1005-- -----------------------------------------------------------------------------
1098-- -----------------------------------------------------------------------------
@@ -1008,20 +1101,21 @@ mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1008-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1101-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
1009-- @t@ repeated @n@ times.
1102-- @t@ repeated @n@ times.
1010replicate :: Int -> Text -> Text
1103replicate :: Int -> Text -> Text
1104+replicate = coerce JSS.replicate
1011−replicate n t@(Text a o l)
1105+-- replicate n t@(Text a o l)
1012− | n <= 0 || l <= 0 = empty
1106+-- | n <= 0 || l <= 0 = empty
1013− | n == 1 = t
1107+-- | n == 1 = t
1014− | isSingleton t = replicateChar n (unsafeHead t)
1108+-- | isSingleton t = replicateChar n (unsafeHead t)
1015− | otherwise = Text (A.run x) 0 len
1109+-- | otherwise = Text (A.run x) 0 len
1017− len = l `mul` n
1111+-- len = l `mul` n
1018− x :: ST s (A.MArray s)
1112+-- x :: ST s (A.MArray s)
1019− x = do
1113+-- x = do
1020− arr <- A.new len
1114+-- arr <- A.new len
1021− let loop !d !i | i >= n = return arr
1115+-- let loop !d !i | i >= n = return arr
1022− | otherwise = let m = d + l
1116+-- | otherwise = let m = d + l
1023− in A.copyI arr d a o m >> loop m (i+1)
1117+-- in A.copyI arr d a o m >> loop m (i+1)
1024− loop 0 0
1118+-- loop 0 0
1025{-# INLINE [1] replicate #-}
1119{-# INLINE [1] replicate #-}
1027{-# RULES
1121{-# RULES
@@ -1032,8 +1126,19 @@ replicate n t@(Text a o l)
1032-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1126-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
1033-- value of every element. Subject to fusion.
1127-- value of every element. Subject to fusion.
1034replicateChar :: Int -> Char -> Text
1128replicateChar :: Int -> Char -> Text
1035replicateChar n c = unstream (S.replicateCharI n (safe c))
1130replicateChar n c = unstream (S.replicateCharI n (safe c))
1036{-# INLINE replicateChar #-}
1131{-# INLINE replicateChar #-}
1133+replicateChar n c = Text $ js_replicateChar n c
1134+{-# INLINE [1] replicateChar #-}
1136+"JSSTRING replicateChar -> fused" [~1] forall n c.
1137+ replicateChar n c = unstream (S.replicateCharI n (safe c))
1138+"JSSTRING replicateChar -> unfused" [1] forall n c.
1139+ unstream (S.replicateCharI n (safe c)) = replicateChar n c
1038-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1143-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
1039-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
1144-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1043,7 +1148,8 @@ replicateChar n c = unstream (S.replicateCharI n (safe c))
1043-- string, and @b@ is the seed value for further production. Subject
1148-- string, and @b@ is the seed value for further production. Subject
1044-- to fusion. Performs replacement on invalid scalar values.
1149-- to fusion. Performs replacement on invalid scalar values.
1045unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1150unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1151+unfoldr f a = Text $ JSS.unfoldr f a
1046−unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1152+-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1047{-# INLINE unfoldr #-}
1153{-# INLINE unfoldr #-}
1049-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
1155-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1053,7 +1159,8 @@ unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1053-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1159-- correct, otherwise its performance is similar to 'unfoldr'. Subject
1054-- to fusion. Performs replacement on invalid scalar values.
1160-- to fusion. Performs replacement on invalid scalar values.
1055unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1161unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1162+unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1056−unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1163+-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1057{-# INLINE unfoldrN #-}
1164{-# INLINE unfoldrN #-}
1059-- -----------------------------------------------------------------------------
1166-- -----------------------------------------------------------------------------
@@ -1063,18 +1170,19 @@ unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1063-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1170-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
1064-- the length of the Text. Subject to fusion.
1171-- the length of the Text. Subject to fusion.
1065take :: Int -> Text -> Text
1172take :: Int -> Text -> Text
1173+take = coerce JSS.take
1066−take n t@(Text arr off len)
1174+-- take n t@(Text arr off len)
1067− | n <= 0 = empty
1175+-- | n <= 0 = empty
1068− | n >= len = t
1176+-- | n >= len = t
1069− | otherwise = text arr off (iterN n t)
1177+-- | otherwise = text arr off (iterN n t)
1070{-# INLINE [1] take #-}
1178{-# INLINE [1] take #-}
1072−iterN :: Int -> Text -> Int
1180+-- iterN :: Int -> Text -> Int
1073−iterN n t@(Text _arr _off len) = loop 0 0
1181+-- iterN n t@(Text _arr _off len) = loop 0 0
1074− where loop !i !cnt
1182+-- where loop !i !cnt
1075− | i >= len || cnt >= n = i
1183+-- | i >= len || cnt >= n = i
1076− | otherwise = loop (i+d) (cnt+1)
1184+-- | otherwise = loop (i+d) (cnt+1)
1077− where d = iter_ t i
1185+-- where d = iter_ t i
1079{-# RULES
1187{-# RULES
1080"TEXT take -> fused" [~1] forall n t.
1188"TEXT take -> fused" [~1] forall n t.
@@ -1090,29 +1198,31 @@ iterN n t@(Text _arr _off len) = loop 0 0
1091-- > takeEnd 3 "foobar" == "bar"
1199-- > takeEnd 3 "foobar" == "bar"
1092takeEnd :: Int -> Text -> Text
1200takeEnd :: Int -> Text -> Text
1201+takeEnd = coerce JSS.takeEnd
1093−takeEnd n t@(Text arr off len)
1202+-- takeEnd n t@(Text arr off len)
1094− | n <= 0 = empty
1203+-- | n <= 0 = empty
1095− | n >= len = t
1204+-- | n >= len = t
1096− | otherwise = text arr (off+i) (len-i)
1205+-- | otherwise = text arr (off+i) (len-i)
1097− where i = iterNEnd n t
1206+-- where i = iterNEnd n t
1099−iterNEnd :: Int -> Text -> Int
1208+-- iterNEnd :: Int -> Text -> Int
1100−iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1209+-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1101− where loop i !m
1210+-- where loop i !m
1102− | i <= 0 = 0
1211+-- | i <= 0 = 0
1103− | m <= 1 = i
1212+-- | m <= 1 = i
1104− | otherwise = loop (i+d) (m-1)
1213+-- | otherwise = loop (i+d) (m-1)
1105− where d = reverseIter_ t i
1214+-- where d = reverseIter_ t i
1107-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1216-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
1108-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1217-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
1109-- is greater than the length of the 'Text'. Subject to fusion.
1218-- is greater than the length of the 'Text'. Subject to fusion.
1110drop :: Int -> Text -> Text
1219drop :: Int -> Text -> Text
1220+drop = coerce JSS.drop
1111−drop n t@(Text arr off len)
1221+-- drop n t@(Text arr off len)
1112− | n <= 0 = t
1222+-- | n <= 0 = t
1113− | n >= len = empty
1223+-- | n >= len = empty
1114− | otherwise = text arr (off+i) (len-i)
1224+-- | otherwise = text arr (off+i) (len-i)
1115− where i = iterN n t
1225+-- where i = iterN n t
1116{-# INLINE [1] drop #-}
1226{-# INLINE [1] drop #-}
1118{-# RULES
1228{-# RULES
@@ -1129,20 +1239,22 @@ drop n t@(Text arr off len)
1130-- > dropEnd 3 "foobar" == "foo"
1240-- > dropEnd 3 "foobar" == "foo"
1131dropEnd :: Int -> Text -> Text
1241dropEnd :: Int -> Text -> Text
1242+dropEnd = coerce JSS.dropEnd
1132−dropEnd n t@(Text arr off len)
1243+-- dropEnd n t@(Text arr off len)
1133− | n <= 0 = t
1244+-- | n <= 0 = t
1134− | n >= len = empty
1245+-- | n >= len = empty
1135− | otherwise = text arr off (iterNEnd n t)
1246+-- | otherwise = text arr off (iterNEnd n t)
1137-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1248-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
1138-- returns the longest prefix (possibly empty) of elements that
1249-- returns the longest prefix (possibly empty) of elements that
1139-- satisfy @p@. Subject to fusion.
1250-- satisfy @p@. Subject to fusion.
1140takeWhile :: (Char -> Bool) -> Text -> Text
1251takeWhile :: (Char -> Bool) -> Text -> Text
1252+takeWhile = coerce JSS.takeWhile
1141−takeWhile p t@(Text arr off len) = loop 0
1253+-- takeWhile p t@(Text arr off len) = loop 0
1142− where loop !i | i >= len = t
1254+-- where loop !i | i >= len = t
1143− | p c = loop (i+d)
1255+-- | p c = loop (i+d)
1144− | otherwise = text arr off i
1256+-- | otherwise = text arr off i
1145− where Iter c d = iter t i
1257+-- where Iter c d = iter t i
1146{-# INLINE [1] takeWhile #-}
1258{-# INLINE [1] takeWhile #-}
1148{-# RULES
1260{-# RULES
@@ -1159,11 +1271,13 @@ takeWhile p t@(Text arr off len) = loop 0
1160-- > takeWhileEnd (=='o') "foo" == "oo"
1272-- > takeWhileEnd (=='o') "foo" == "oo"
1161takeWhileEnd :: (Char -> Bool) -> Text -> Text
1273takeWhileEnd :: (Char -> Bool) -> Text -> Text
1274+takeWhileEnd = P.error "takeWhileEnd: not implemented."
1275+-- takeWhileEnd :: (Char -> Bool) -> Text -> Text
1162−takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1276+-- takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1163− where loop !i !l | l <= 0 = t
1277+-- where loop !i !l | l <= 0 = t
1164− | p c = loop (i+d) (l+d)
1278+-- | p c = loop (i+d) (l+d)
1165− | otherwise = text arr (off+l) (len-l)
1279+-- | otherwise = text arr (off+l) (len-l)
1166− where (c,d) = reverseIter t i
1280+-- where (c,d) = reverseIter t i
1167{-# INLINE [1] takeWhileEnd #-}
1281{-# INLINE [1] takeWhileEnd #-}
1169{-# RULES
1283{-# RULES
@@ -1176,11 +1290,12 @@ takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1290-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1177-- 'takeWhile' @p@ @t@. Subject to fusion.
1291-- 'takeWhile' @p@ @t@. Subject to fusion.
1178dropWhile :: (Char -> Bool) -> Text -> Text
1292dropWhile :: (Char -> Bool) -> Text -> Text
1293+dropWhile = coerce JSS.dropWhile
1179−dropWhile p t@(Text arr off len) = loop 0 0
1294+-- dropWhile p t@(Text arr off len) = loop 0 0
1180− where loop !i !l | l >= len = empty
1295+-- where loop !i !l | l >= len = empty
1181− | p c = loop (i+d) (l+d)
1296+-- | p c = loop (i+d) (l+d)
1182− | otherwise = Text arr (off+i) (len-l)
1297+-- | otherwise = Text arr (off+i) (len-l)
1183− where Iter c d = iter t i
1298+
1184{-# INLINE [1] dropWhile #-}
1299{-# INLINE [1] dropWhile #-}
1186{-# RULES
1301{-# RULES
@@ -1198,11 +1313,12 @@ dropWhile p t@(Text arr off len) = loop 0 0
1199-- > dropWhileEnd (=='.') "foo..." == "foo"
1314-- > dropWhileEnd (=='.') "foo..." == "foo"
1200dropWhileEnd :: (Char -> Bool) -> Text -> Text
1315dropWhileEnd :: (Char -> Bool) -> Text -> Text
1316+dropWhileEnd = coerce JSS.dropWhileEnd
1201−dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1317+-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1202− where loop !i !l | l <= 0 = empty
1318+-- where loop !i !l | l <= 0 = empty
1203− | p c = loop (i+d) (l+d)
1319+-- | p c = loop (i+d) (l+d)
1204− | otherwise = Text arr off l
1320+-- | otherwise = Text arr off l
1205− where (c,d) = reverseIter t i
1321+-- where (c,d) = reverseIter t i
1206{-# INLINE [1] dropWhileEnd #-}
1322{-# INLINE [1] dropWhileEnd #-}
1208{-# RULES
1324{-# RULES
@@ -1245,45 +1361,49 @@ strip = dropAround isSpace
1245-- prefix of @t@ of length @n@, and whose second is the remainder of
1361-- prefix of @t@ of length @n@, and whose second is the remainder of
1246-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1362-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
1247splitAt :: Int -> Text -> (Text, Text)
1363splitAt :: Int -> Text -> (Text, Text)
1364+splitAt = coerce JSS.splitAt
1248−splitAt n t@(Text arr off len)
1365+-- splitAt n t@(Text arr off len)
1249− | n <= 0 = (empty, t)
1366+-- | n <= 0 = (empty, t)
1250− | n >= len = (t, empty)
1367+-- | n >= len = (t, empty)
1251− | otherwise = let k = iterN n t
1368+-- | otherwise = let k = iterN n t
1252− in (text arr off k, text arr (off+k) (len-k))
1369+-- in (text arr off k, text arr (off+k) (len-k))
1254-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1371-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
1255-- a pair whose first element is the longest prefix (possibly empty)
1372-- a pair whose first element is the longest prefix (possibly empty)
1256-- of @t@ of elements that satisfy @p@, and whose second is the
1373-- of @t@ of elements that satisfy @p@, and whose second is the
1257-- remainder of the list.
1374-- remainder of the list.
1258span :: (Char -> Bool) -> Text -> (Text, Text)
1375span :: (Char -> Bool) -> Text -> (Text, Text)
1376+span = coerce JSS.span
1259−span p t = case span_ p t of
1377+-- span p t = case span_ p t of
1260− (# hd,tl #) -> (hd,tl)
1378+-- (# hd,tl #) -> (hd,tl)
1261{-# INLINE span #-}
1379{-# INLINE span #-}
1263-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1381-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1264-- over elements that fail the predicate @p@.
1382-- over elements that fail the predicate @p@.
1265break :: (Char -> Bool) -> Text -> (Text, Text)
1383break :: (Char -> Bool) -> Text -> (Text, Text)
1384+break = coerce JSS.break
1266−break p = span (not . p)
1385+-- break p = span (not . p)
1267{-# INLINE break #-}
1386{-# INLINE break #-}
1269-- | /O(n)/ Group characters in a string according to a predicate.
1388-- | /O(n)/ Group characters in a string according to a predicate.
1270groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1389groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1390+groupBy = coerce JSS.groupBy
1271−groupBy p = loop
1391+-- groupBy p = loop
1273− loop t@(Text arr off len)
1393+-- loop t@(Text arr off len)
1274− | null t = []
1394+-- | null t = []
1275− | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1395+-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1276− where Iter c d = iter t 0
1396+-- where Iter c d = iter t 0
1277− n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1397+-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1279-- | Returns the /array/ index (in units of 'Word16') at which a
1399-- | Returns the /array/ index (in units of 'Word16') at which a
1280-- character may be found. This is /not/ the same as the logical
1400-- character may be found. This is /not/ the same as the logical
1281-- index returned by e.g. 'findIndex'.
1401-- index returned by e.g. 'findIndex'.
1282−findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1402+-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1283−findAIndexOrEnd q t@(Text _arr _off len) = go 0
1403+-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1284− where go !i | i >= len || q c = i
1404+-- where go !i | i >= len || q c = i
1285− | otherwise = go (i+d)
1405+-- | otherwise = go (i+d)
1286− where Iter c d = iter t i
1406+-- where Iter c d = iter t i
1288-- | /O(n)/ Group characters in a string by equality.
1408-- | /O(n)/ Group characters in a string by equality.
1289group :: Text -> [Text]
1409group :: Text -> [Text]
@@ -1292,15 +1412,17 @@ group = groupBy (==)
1292-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1412-- | /O(n)/ Return all initial segments of the given 'Text', shortest
1293-- first.
1413-- first.
1294inits :: Text -> [Text]
1414inits :: Text -> [Text]
1415+inits = coerce JSS.inits
1295−inits t@(Text arr off len) = loop 0
1416+-- inits t@(Text arr off len) = loop 0
1296− where loop i | i >= len = [t]
1417+-- where loop i | i >= len = [t]
1297− | otherwise = Text arr off i : loop (i + iter_ t i)
1418+-- | otherwise = Text arr off i : loop (i + iter_ t i)
1299-- | /O(n)/ Return all final segments of the given 'Text', longest
1420-- | /O(n)/ Return all final segments of the given 'Text', longest
1300-- first.
1421-- first.
1301tails :: Text -> [Text]
1422tails :: Text -> [Text]
1423+tails = coerce JSS.tails
1302−tails t | null t = [empty]
1424+-- tails t | null t = [empty]
1303− | otherwise = t : tails (unsafeTail t)
1425+-- | otherwise = t : tails (unsafeTail t)
1305-- $split
1427-- $split
@@ -1333,13 +1455,14 @@ splitOn :: Text
1334 -- ^ Input text.
1456 -- ^ Input text.
1335 -> [Text]
1457 -> [Text]
1458+splitOn = coerce JSS.splitOn
1336−splitOn pat@(Text _ _ l) src@(Text arr off len)
1459+-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1337− | l <= 0 = emptyError "splitOn"
1460+-- | l <= 0 = emptyError "splitOn"
1338− | isSingleton pat = split (== unsafeHead pat) src
1461+-- | isSingleton pat = split (== unsafeHead pat) src
1339− | otherwise = go 0 (indices pat src)
1462+-- | otherwise = go 0 (indices pat src)
1341− go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1464+-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1342− go s _ = [text arr (s+off) (len-s)]
1465+-- go s _ = [text arr (s+off) (len-s)]
1343{-# INLINE [1] splitOn #-}
1466{-# INLINE [1] splitOn #-}
1345{-# RULES
1468{-# RULES
@@ -1355,11 +1478,12 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
1355-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1478-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356-- > split (=='a') "" == [""]
1479-- > split (=='a') "" == [""]
1357split :: (Char -> Bool) -> Text -> [Text]
1480split :: (Char -> Bool) -> Text -> [Text]
1481+split = coerce JSS.split
1358−split _ t@(Text _off _arr 0) = [t]
1482+-- split _ t@(Text _off _arr 0) = [t]
1359−split p t = loop t
1483+-- split p t = loop t
1360− where loop s | null s' = [l]
1484+-- where loop s | null s' = [l]
1361− | otherwise = l : loop (unsafeTail s')
1485+-- | otherwise = l : loop (unsafeTail s')
1362− where (# l, s' #) = span_ (not . p) s
1486+-- where (# l, s' #) = span_ (not . p) s
1363{-# INLINE split #-}
1487{-# INLINE split #-}
1365-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1489-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
@@ -1369,11 +1493,12 @@ split p t = loop t
1369-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1493-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1494-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1371chunksOf :: Int -> Text -> [Text]
1495chunksOf :: Int -> Text -> [Text]
1372−chunksOf k = go
1496+chunksOf = coerce JSS.chunksOf
1374− go t = case splitAt k t of
1499+-- go t = case splitAt k t of
1375− (a,b) | null a -> []
1500+-- (a,b) | null a -> []
1376− | otherwise -> a : go b
1501+-- | otherwise -> a : go b
1377{-# INLINE chunksOf #-}
1502{-# INLINE chunksOf #-}
1379-- ----------------------------------------------------------------------------
1504-- ----------------------------------------------------------------------------
@@ -1386,7 +1511,8 @@ chunksOf k = go
1386-- returns the first element matching the predicate, or 'Nothing' if
1511-- returns the first element matching the predicate, or 'Nothing' if
1387-- there is no such element.
1512-- there is no such element.
1388find :: (Char -> Bool) -> Text -> Maybe Char
1513find :: (Char -> Bool) -> Text -> Maybe Char
1514+find = coerce JSS.find
1389−find p t = S.findBy p (stream t)
1515+-- find p t = S.findBy p (stream t)
1390{-# INLINE find #-}
1516{-# INLINE find #-}
1392-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
1518-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1395,14 +1521,16 @@ find p t = S.findBy p (stream t)
1396-- > partition p t == (filter p t, filter (not . p) t)
1522-- > partition p t == (filter p t, filter (not . p) t)
1397partition :: (Char -> Bool) -> Text -> (Text, Text)
1523partition :: (Char -> Bool) -> Text -> (Text, Text)
1524+partition = coerce JSS.partition
1398−partition p t = (filter p t, filter (not . p) t)
1525+-- partition p t = (filter p t, filter (not . p) t)
1399{-# INLINE partition #-}
1526{-# INLINE partition #-}
1401-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1528-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
1402-- returns a 'Text' containing those characters that satisfy the
1529-- returns a 'Text' containing those characters that satisfy the
1403-- predicate.
1530-- predicate.
1404filter :: (Char -> Bool) -> Text -> Text
1531filter :: (Char -> Bool) -> Text -> Text
1532+filter = coerce JSS.filter
1405−filter p t = unstream (S.filter p (stream t))
1533+-- filter p t = unstream (S.filter p (stream t))
1406{-# INLINE filter #-}
1534{-# INLINE filter #-}
1408-- | /O(n+m)/ Find the first instance of @needle@ (which must be
1536-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1427,11 +1555,12 @@ filter p t = unstream (S.filter p (stream t))
1427-- In (unlikely) bad cases, this function's time complexity degrades
1555-- In (unlikely) bad cases, this function's time complexity degrades
1428-- towards /O(n*m)/.
1556-- towards /O(n*m)/.
1429breakOn :: Text -> Text -> (Text, Text)
1557breakOn :: Text -> Text -> (Text, Text)
1558+breakOn = coerce JSS.breakOn
1430−breakOn pat src@(Text arr off len)
1559+-- breakOn pat src@(Text arr off len)
1431− | null pat = emptyError "breakOn"
1560+-- | null pat = emptyError "breakOn"
1432− | otherwise = case indices pat src of
1561+-- | otherwise = case indices pat src of
1433− [] -> (src, empty)
1562+-- [] -> (src, empty)
1434− (x:_) -> (text arr off x, text arr (off+x) (len-x))
1563+-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
1435{-# INLINE breakOn #-}
1564{-# INLINE breakOn #-}
1437-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
1566-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1443,8 +1572,9 @@ breakOn pat src@(Text arr off len)
1444-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1573-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1445breakOnEnd :: Text -> Text -> (Text, Text)
1574breakOnEnd :: Text -> Text -> (Text, Text)
1575+breakOnEnd = coerce JSS.breakOnEnd
1446−breakOnEnd pat src = (reverse b, reverse a)
1576+-- breakOnEnd pat src = (reverse b, reverse a)
1447− where (a,b) = breakOn (reverse pat) (reverse src)
1577+-- where (a,b) = breakOn (reverse pat) (reverse src)
1448{-# INLINE breakOnEnd #-}
1578{-# INLINE breakOnEnd #-}
1450-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1580-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1468,12 +1598,13 @@ breakOnEnd pat src = (reverse b, reverse a)
1468breakOnAll :: Text -- ^ @needle@ to search for
1598breakOnAll :: Text -- ^ @needle@ to search for
1469 -> Text -- ^ @haystack@ in which to search
1599 -> Text -- ^ @haystack@ in which to search
1470 -> [(Text, Text)]
1600 -> [(Text, Text)]
1601+breakOnAll = coerce JSS.breakOnAll
1471−breakOnAll pat src@(Text arr off slen)
1602+-- breakOnAll pat src@(Text arr off slen)
1472− | null pat = emptyError "breakOnAll"
1603+-- | null pat = emptyError "breakOnAll"
1473− | otherwise = L.map step (indices pat src)
1604+-- | otherwise = L.map step (indices pat src)
1475− step x = (chunk 0 x, chunk x (slen-x))
1606+-- step x = (chunk 0 x, chunk x (slen-x))
1476− chunk !n !l = text arr (n+off) l
1607+-- chunk !n !l = text arr (n+off) l
1477{-# INLINE breakOnAll #-}
1608{-# INLINE breakOnAll #-}
1479-------------------------------------------------------------------------------
1610-------------------------------------------------------------------------------
@@ -1497,14 +1628,16 @@ breakOnAll pat src@(Text arr off slen)
1498-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1629-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
1499index :: Text -> Int -> Char
1630index :: Text -> Int -> Char
1631+index = coerce JSS.index
1500−index t n = S.index (stream t) n
1632+-- index t n = S.index (stream t) n
1501{-# INLINE index #-}
1633{-# INLINE index #-}
1503-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1635-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
1504-- and returns the index of the first element in the 'Text' satisfying
1636-- and returns the index of the first element in the 'Text' satisfying
1505-- the predicate. Subject to fusion.
1637-- the predicate. Subject to fusion.
1506findIndex :: (Char -> Bool) -> Text -> Maybe Int
1638findIndex :: (Char -> Bool) -> Text -> Maybe Int
1639+findIndex = coerce JSS.findIndex
1507−findIndex p t = S.findIndex p (stream t)
1640+-- findIndex p t = S.findIndex p (stream t)
1508{-# INLINE findIndex #-}
1641{-# INLINE findIndex #-}
1510-- | /O(n+m)/ The 'count' function returns the number of times the
1643-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1514,10 +1647,11 @@ findIndex p t = S.findIndex p (stream t)
1514-- In (unlikely) bad cases, this function's time complexity degrades
1647-- In (unlikely) bad cases, this function's time complexity degrades
1515-- towards /O(n*m)/.
1648-- towards /O(n*m)/.
1516count :: Text -> Text -> Int
1649count :: Text -> Text -> Int
1650+count = coerce JSS.count
1517−count pat src
1651+-- count pat src
1518− | null pat = emptyError "count"
1652+-- | null pat = emptyError "count"
1519− | isSingleton pat = countChar (unsafeHead pat) src
1653+-- | isSingleton pat = countChar (unsafeHead pat) src
1520− | otherwise = L.length (indices pat src)
1654+-- | otherwise = L.length (indices pat src)
1521{-# INLINE [1] count #-}
1655{-# INLINE [1] count #-}
1523{-# RULES
1657{-# RULES
@@ -1539,42 +1673,46 @@ countChar c t = S.countChar c (stream t)
1539-- excess elements of the longer 'Text' are discarded. This is
1673-- excess elements of the longer 'Text' are discarded. This is
1540-- equivalent to a pair of 'unpack' operations.
1674-- equivalent to a pair of 'unpack' operations.
1541zip :: Text -> Text -> [(Char,Char)]
1675zip :: Text -> Text -> [(Char,Char)]
1676+zip = coerce JSS.zip
1542−zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1677+-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1543{-# INLINE zip #-}
1678{-# INLINE zip #-}
1545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1680-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1546-- given as the first argument, instead of a tupling function.
1681-- given as the first argument, instead of a tupling function.
1547-- Performs replacement on invalid scalar values.
1682-- Performs replacement on invalid scalar values.
1548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1683zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1684+zipWith = coerce JSS.zipWith
1549−zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1685+-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550− where g a b = safe (f a b)
1686+-- where g a b = safe (f a b)
1551{-# INLINE zipWith #-}
1687{-# INLINE zipWith #-}
1553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1689-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1554-- representing white space.
1690-- representing white space.
1555words :: Text -> [Text]
1691words :: Text -> [Text]
1692+words = coerce JSS.words
1556−words t@(Text arr off len) = loop 0 0
1693+-- words t@(Text arr off len) = loop 0 0
1558− loop !start !n
1695+-- loop !start !n
1559− | n >= len = if start == n
1696+-- | n >= len = if start == n
1560− then []
1697+-- then []
1561− else [Text arr (start+off) (n-start)]
1698+-- else [Text arr (start+off) (n-start)]
1562− | isSpace c =
1699+-- | isSpace c =
1563− if start == n
1700+-- if start == n
1564− then loop (start+1) (start+1)
1701+-- then loop (start+1) (start+1)
1565− else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1702+-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1566− | otherwise = loop start (n+d)
1703+-- | otherwise = loop start (n+d)
1567− where Iter c d = iter t n
1704+-- where Iter c d = iter t n
1568{-# INLINE words #-}
1705{-# INLINE words #-}
1570-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1707-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
1571-- newline 'Char's. The resulting strings do not contain newlines.
1708-- newline 'Char's. The resulting strings do not contain newlines.
1572lines :: Text -> [Text]
1709lines :: Text -> [Text]
1710+lines = coerce JSS.lines
1573−lines ps | null ps = []
1711+-- lines ps | null ps = []
1574− | otherwise = h : if null t
1712+-- | otherwise = h : if null t
1575− then []
1713+-- then []
1576− else lines (unsafeTail t)
1714+-- else lines (unsafeTail t)
1577− where (# h,t #) = span_ (/= '\n') ps
1715+-- where (# h,t #) = span_ (/= '\n') ps
1578{-# INLINE lines #-}
1716{-# INLINE lines #-}
@@ -1602,34 +1740,40 @@ lines' ps | null ps = []
1602-- | /O(n)/ Joins lines, after appending a terminating newline to
1740-- | /O(n)/ Joins lines, after appending a terminating newline to
1604unlines :: [Text] -> Text
1742unlines :: [Text] -> Text
1743+unlines = coerce JSS.unlines
1605−unlines = concat . L.map (`snoc` '\n')
1744+-- unlines = concat . L.map (`snoc` '\n')
1606{-# INLINE unlines #-}
1745{-# INLINE unlines #-}
1608-- | /O(n)/ Joins words using single space characters.
1747-- | /O(n)/ Joins words using single space characters.
1609unwords :: [Text] -> Text
1748unwords :: [Text] -> Text
1749+unwords = coerce JSS.unwords
1610−unwords = intercalate (singleton ' ')
1750+-- unwords = intercalate (singleton ' ')
1611{-# INLINE unwords #-}
1751{-# INLINE unwords #-}
1613-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1753-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
1614-- 'True' iff the first is a prefix of the second. Subject to fusion.
1754-- 'True' iff the first is a prefix of the second. Subject to fusion.
1615isPrefixOf :: Text -> Text -> Bool
1755isPrefixOf :: Text -> Text -> Bool
1756+isPrefixOf = coerce JSS.isPrefixOf
1616−isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1757+-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617− alen <= blen && S.isPrefixOf (stream a) (stream b)
1758+-- alen <= blen && S.isPrefixOf (stream a) (stream b)
1618{-# INLINE [1] isPrefixOf #-}
1759{-# INLINE [1] isPrefixOf #-}
1620{-# RULES
1761{-# RULES
1621"TEXT isPrefixOf -> fused" [~1] forall s t.
1762"TEXT isPrefixOf -> fused" [~1] forall s t.
1622 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1763 isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1764+"JSSTRING isPrefixOf -> unfused" [1] forall x y.
1765+ S.isPrefixOf (stream x) (stream y) = isPrefixOf x y
1625-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1768-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
1626-- 'True' iff the first is a suffix of the second.
1769-- 'True' iff the first is a suffix of the second.
1627isSuffixOf :: Text -> Text -> Bool
1770isSuffixOf :: Text -> Text -> Bool
1771+isSuffixOf = coerce JSS.isSuffixOf
1628−isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1772+-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1629− d >= 0 && a == b'
1773+-- d >= 0 && a == b'
1630− where d = blen - alen
1774+-- where d = blen - alen
1631− b' | d == 0 = b
1775+-- b' | d == 0 = b
1632− | otherwise = Text barr (boff+d) alen
1776+-- | otherwise = Text barr (boff+d) alen
1633{-# INLINE isSuffixOf #-}
1777{-# INLINE isSuffixOf #-}
1635-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
1779-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1639,10 +1783,11 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1639-- In (unlikely) bad cases, this function's time complexity degrades
1783-- In (unlikely) bad cases, this function's time complexity degrades
1640-- towards /O(n*m)/.
1784-- towards /O(n*m)/.
1641isInfixOf :: Text -> Text -> Bool
1785isInfixOf :: Text -> Text -> Bool
1786+isInfixOf = coerce JSS.isInfixOf
1642−isInfixOf needle haystack
1787+-- isInfixOf needle haystack
1643− | null needle = True
1788+-- | null needle = True
1644− | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1789+-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1645− | otherwise = not . L.null . indices needle $ haystack
1790+-- | otherwise = not . L.null . indices needle $ haystack
1646{-# INLINE [1] isInfixOf #-}
1791{-# INLINE [1] isInfixOf #-}
1648{-# RULES
1793{-# RULES
@@ -1672,9 +1817,10 @@ isInfixOf needle haystack
1672-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1817-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1673-- > fnordLength _ = -1
1818-- > fnordLength _ = -1
1674stripPrefix :: Text -> Text -> Maybe Text
1819stripPrefix :: Text -> Text -> Maybe Text
1820+stripPrefix = coerce JSS.stripPrefix
1675−stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1821+-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1676− | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1822+-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1677− | otherwise = Nothing
1823+-- | otherwise = Nothing
1679-- | /O(n)/ Find the longest non-empty common prefix of two strings
1825-- | /O(n)/ Find the longest non-empty common prefix of two strings
1680-- and return it, along with the suffixes of each string at which they
1826-- and return it, along with the suffixes of each string at which they
@@ -1689,15 +1835,16 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1689-- > commonPrefixes "veeble" "fetzer" == Nothing
1835-- > commonPrefixes "veeble" "fetzer" == Nothing
1690-- > commonPrefixes "" "baz" == Nothing
1836-- > commonPrefixes "" "baz" == Nothing
1691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1837commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1838+commonPrefixes = coerce JSS.commonPrefixes
1692−commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1839+-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1694− go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1841+-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1695− | i > 0 = Just (Text arr0 off0 i,
1842+-- | i > 0 = Just (Text arr0 off0 i,
1696− text arr0 (off0+i) (len0-i),
1843+-- text arr0 (off0+i) (len0-i),
1697− text arr1 (off1+j) (len1-j))
1844+-- text arr1 (off1+j) (len1-j))
1698− | otherwise = Nothing
1845+-- | otherwise = Nothing
1699− where Iter a d0 = iter t0 i
1846+-- where Iter a d0 = iter t0 i
1700− Iter b d1 = iter t1 j
1847+-- Iter b d1 = iter t1 j
1702-- | /O(n)/ Return the prefix of the second string if its suffix
1849-- | /O(n)/ Return the prefix of the second string if its suffix
1703-- matches the entire first string.
1850-- matches the entire first string.
@@ -1718,21 +1865,22 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1718-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1865-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1719-- > quuxLength _ = -1
1866-- > quuxLength _ = -1
1720stripSuffix :: Text -> Text -> Maybe Text
1867stripSuffix :: Text -> Text -> Maybe Text
1868+stripSuffix = coerce JSS.stripSuffix
1721−stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1869+-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1722− | p `isSuffixOf` t = Just $! text arr off (len-plen)
1870+-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
1723− | otherwise = Nothing
1871+-- | otherwise = Nothing
1725−-- | Add a list of non-negative numbers. Errors out on overflow.
1873+-- -- | Add a list of non-negative numbers. Errors out on overflow.
1726−sumP :: String -> [Int] -> Int
1874+-- sumP :: String -> [Int] -> Int
1727−sumP fun = go 0
1875+-- sumP fun = go 0
1728− where go !a (x:xs)
1876+-- where go !a (x:xs)
1729− | ax >= 0 = go ax xs
1877+-- | ax >= 0 = go ax xs
1730− | otherwise = overflowError fun
1878+-- | otherwise = overflowError fun
1731− where ax = a + x
1879+-- where ax = a + x
1732− go a _ = a
1880+-- go a _ = a
1734−emptyError :: String -> a
1882+-- emptyError :: String -> a
1735−emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1883+-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1737overflowError :: String -> a
1885overflowError :: String -> a
1738overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1886overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1746,10 +1894,12 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
1746-- copy \"breaks the link\" to the original array, allowing it to be
1894-- copy \"breaks the link\" to the original array, allowing it to be
1747-- garbage collected if there are no other live references to it.
1895-- garbage collected if there are no other live references to it.
1748copy :: Text -> Text
1896copy :: Text -> Text
1749−copy (Text arr off len) = Text (A.run go) 0 len
1898+-- copy (Text arr off len) = Text (A.run go) 0 len
1751− go :: ST s (A.MArray s)
1900+-- go :: ST s (A.MArray s)
1752− go = do
1901+-- go = do
1753− marr <- A.new len
1902+-- marr <- A.new len
1754− A.copyI marr 0 arr off len
1903+
1755− return marr
1904+foreign import javascript unsafe
1905+ "h$jsstringReplicateChar" js_replicateChar :: Int -> Char -> JSString