@@ -6,7 +6,7 @@
66#if __GLASGOW_HASKELL__ >= 708
77{-# LANGUAGE TypeFamilies #-}
88#endif
9−
9+{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
1010-- |
1111-- Module : Data.Text
1212-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
@@ -223,16 +223,16 @@ import qualified Data.Text.Internal.Fusion as S
223223import qualified Data.Text.Internal.Fusion.Common as S
224224import Data.Text.Encoding (decodeUtf8', encodeUtf8)
225225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
226−import Data.Text.Internal.Private (span_)
226+-- import Data.Text.Internal.Private (span_)
227227import 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#)
229229import qualified Prelude as P
230230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
231231 reverseIter_, unsafeHead, unsafeTail)
232232import Data.Text.Internal.Unsafe.Char (unsafeChr)
233233import qualified Data.Text.Internal.Functions as F
234234import qualified Data.Text.Internal.Encoding.Utf16 as U16
235−import Data.Text.Internal.Search (indices)
235+-- import Data.Text.Internal.Search (indices)
236236#if defined(__HADDOCK__)
237237import Data.ByteString (ByteString)
238238import qualified Data.Text.Lazy as L
@@ -246,6 +246,28 @@ import qualified GHC.Exts as Exts
246246import Text.Printf (PrintfArg, formatArg, formatString)
247247#endif
248248
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
254+import Data.Coerce
255+import GHC.Exts (Addr#)
256+
257+unpack :: Text -> String
258+unpack = coerce JSS.unpack
259+
260+singleton :: Char -> Text
261+singleton = coerce JSS.singleton
262+{-# INLINE [1] singleton #-}
263+
264+
265+-- | /O(n)/ Convert a literal string into a JSString. Subject to fusion.
266+unpackCString# :: Addr# -> Text
267+unpackCString# addr# = Text $ SJS.unstream (SJS.streamCString# addr#)
268+{-# NOINLINE unpackCString# #-}
269+---------------------------------------------------------------------
270+
249271-- $strict
250272--
251273-- This package provides both strict and lazy 'Text' types. The
@@ -321,17 +343,17 @@ import Text.Printf (PrintfArg, formatArg, formatString)
321343-- Functions that can be fused by the compiler are documented with the
322344-- phrase \"Subject to fusion\".
323345
324−instance Eq Text where
325− Text arrA offA lenA == Text arrB offB lenB
326− | lenA == lenB = A.equal arrA offA arrB offB lenA
327− | otherwise = False
328− {-# INLINE (==) #-}
346+-- instance Eq Text where
347+-- Text arrA offA lenA == Text arrB offB lenB
348+-- | lenA == lenB = A.equal arrA offA arrB offB lenA
349+-- | otherwise = False
350+-- {-# INLINE (==) #-}
329351
330−instance Ord Text where
331− compare = compareText
352+-- instance Ord Text where
353+-- compare = compareText
332354
333−instance Read Text where
334− readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
355+-- instance Read Text where
356+-- readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
335357
336358#if MIN_VERSION_base(4,9,0)
337359-- Semigroup orphan instances for older GHCs are provided by
@@ -341,17 +363,17 @@ instance Semigroup Text where
341363 (<>) = append
342364#endif
343365
344−instance Monoid Text where
345− mempty = empty
346−#if MIN_VERSION_base(4,9,0)
347− mappend = (<>) -- future-proof definition
348−#else
349− mappend = append
350−#endif
351− mconcat = concat
366+-- instance Monoid Text where
367+-- mempty = empty
368+-- #if MIN_VERSION_base(4,9,0)
369+-- mappend = (<>) -- future-proof definition
370+-- #else
371+-- mappend = append
372+-- #endif
373+-- mconcat = concat
352374
353−instance IsString Text where
354− fromString = pack
375+-- instance IsString Text where
376+-- fromString = pack
355377
356378#if __GLASGOW_HASKELL__ >= 708
357379instance Exts.IsList Text where
@@ -387,18 +409,18 @@ instance Binary Text where
387409-- and 'Data.Map.Map' is archived here:
388410-- <http://markmail.org/message/trovdc6zkphyi3cr#query:+page:1+mid:a46der3iacwjcf6n+state:results Proposal: Allow gunfold for Data.Map, ... >
389411
390−instance Data Text where
391− gfoldl f z txt = z pack `f` (unpack txt)
392− toConstr _ = packConstr
393− gunfold k z c = case constrIndex c of
394− 1 -> k (z pack)
395− _ -> P.error "gunfold"
396− dataTypeOf _ = textDataType
412+-- instance Data Text where
413+-- gfoldl f z txt = z pack `f` (unpack txt)
414+-- toConstr _ = packConstr
415+-- gunfold k z c = case constrIndex c of
416+-- 1 -> k (z pack)
417+-- _ -> P.error "gunfold"
418+-- dataTypeOf _ = textDataType
397419
398420#if MIN_VERSION_base(4,7,0)
399421-- | Only defined for @base-4.7.0.0@ and later
400422instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
423+ formatArg t = formatString $ unpack t
402424#endif
403425
404426packConstr :: Constr
@@ -407,19 +429,19 @@ packConstr = mkConstr textDataType "pack" [] Prefix
407429textDataType :: DataType
408430textDataType = mkDataType "Data.Text.Text" [packConstr]
409431
410−-- | /O(n)/ Compare two 'Text' values lexicographically.
411−compareText :: Text -> Text -> Ordering
412−compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
413− | lenA == 0 && lenB == 0 = EQ
414− | otherwise = go 0 0
415− where
416− go !i !j
417− | i >= lenA || j >= lenB = compare lenA lenB
418− | a < b = LT
419− | a > b = GT
420− | otherwise = go (i+di) (j+dj)
421− where Iter a di = iter ta i
422− Iter b dj = iter tb j
432+-- -- | /O(n)/ Compare two 'Text' values lexicographically.
433+-- compareText :: Text -> Text -> Ordering
434+-- compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
435+-- | lenA == 0 && lenB == 0 = EQ
436+-- | otherwise = go 0 0
437+-- where
438+-- go !i !j
439+-- | i >= lenA || j >= lenB = compare lenA lenB
440+-- | a < b = LT
441+-- | a > b = GT
442+-- | otherwise = go (i+di) (j+dj)
443+-- where Iter a di = iter ta i
444+-- Iter b dj = iter tb j
423445
424446-- -----------------------------------------------------------------------------
425447-- * Conversion to/from 'Text'
@@ -427,7 +449,8 @@ compareText ta@(Text _arrA _offA lenA) tb@(Text _arrB _offB lenB)
427449-- | /O(n)/ Convert a 'String' into a 'Text'. Subject to
428450-- fusion. Performs replacement on invalid scalar values.
429451pack :: String -> Text
430−pack = unstream . S.map safe . S.streamList
452+pack = coerce JSS.pack
453+-- pack = unstream . S.map safe . S.streamList
431454{-# INLINE [1] pack #-}
432455
433456-- -----------------------------------------------------------------------------
@@ -438,7 +461,8 @@ pack = unstream . S.map safe . S.streamList
438461-- copying a new array. Subject to fusion. Performs replacement on
439462-- invalid scalar values.
440463cons :: Char -> Text -> Text
441−cons c t = unstream (S.cons (safe c) (stream t))
464+cons = coerce JSS.cons
465+-- cons c t = unstream (S.cons (safe c) (stream t))
442466{-# INLINE cons #-}
443467
444468infixr 5 `cons`
@@ -447,47 +471,51 @@ infixr 5 `cons`
447471-- entire array in the process, unless fused. Subject to fusion.
448472-- Performs replacement on invalid scalar values.
449473snoc :: Text -> Char -> Text
450−snoc t c = unstream (S.snoc (stream t) (safe c))
474+snoc = coerce JSS.snoc
475+-- snoc t c = unstream (S.snoc (stream t) (safe c))
451476{-# INLINE snoc #-}
452477
453478-- | /O(n)/ Appends one 'Text' to the other by copying both of them
454479-- into a new 'Text'. Subject to fusion.
455480append :: Text -> Text -> Text
456−append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
457− | len1 == 0 = b
458− | len2 == 0 = a
459− | len > 0 = Text (A.run x) 0 len
460− | otherwise = overflowError "append"
461− where
462− len = len1+len2
463− x :: ST s (A.MArray s)
464− x = do
465− arr <- A.new len
466− A.copyI arr 0 arr1 off1 len1
467− A.copyI arr len1 arr2 off2 len
468− return arr
481+append = coerce JSS.append
482+-- append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
483+-- | len1 == 0 = b
484+-- | len2 == 0 = a
485+-- | len > 0 = Text (A.run x) 0 len
486+-- | otherwise = overflowError "append"
487+-- where
488+-- len = len1+len2
489+-- x :: ST s (A.MArray s)
490+-- x = do
491+-- arr <- A.new len
492+-- A.copyI arr 0 arr1 off1 len1
493+-- A.copyI arr len1 arr2 off2 len
494+-- return arr
469495{-# NOINLINE append #-}
470496
471−{-# RULES
472−"TEXT append -> fused" [~1] forall t1 t2.
473− append t1 t2 = unstream (S.append (stream t1) (stream t2))
474−"TEXT append -> unfused" [1] forall t1 t2.
475− unstream (S.append (stream t1) (stream t2)) = append t1 t2
476− #-}
497+-- {-# RULES
498+-- "TEXT append -> fused" [~1] forall t1 t2.
499+-- append t1 t2 = unstream (S.append (stream t1) (stream t2))
500+-- "TEXT append -> unfused" [1] forall t1 t2.
501+-- unstream (S.append (stream t1) (stream t2)) = append t1 t2
502+-- #-}
477503
478504-- | /O(1)/ Returns the first character of a 'Text', which must be
479505-- non-empty. Subject to fusion.
480506head :: Text -> Char
481−head t = S.head (stream t)
507+head = coerce JSS.head
508+-- head t = S.head (stream t)
482509{-# INLINE head #-}
483510
484511-- | /O(1)/ Returns the first character and rest of a 'Text', or
485512-- 'Nothing' if empty. Subject to fusion.
486513uncons :: Text -> Maybe (Char, Text)
487−uncons t@(Text arr off len)
488− | len <= 0 = Nothing
489− | otherwise = Just $ let !(Iter c d) = iter t 0
490− in (c, text arr (off+d) (len-d))
514+uncons = coerce JSS.uncons
515+-- uncons t@(Text arr off len)
516+-- | len <= 0 = Nothing
517+-- | otherwise = Just $ let !(Iter c d) = iter t 0
518+-- in (c, text arr (off+d) (len-d))
491519{-# INLINE [1] uncons #-}
492520
493521-- | Lifted from Control.Arrow and specialized.
@@ -497,81 +525,86 @@ second f (a, b) = (a, f b)
497525-- | /O(1)/ Returns the last character of a 'Text', which must be
498526-- non-empty. Subject to fusion.
499527last :: Text -> Char
500−last (Text arr off len)
501− | len <= 0 = emptyError "last"
502− | n < 0xDC00 || n > 0xDFFF = unsafeChr n
503− | otherwise = U16.chr2 n0 n
504− where n = A.unsafeIndex arr (off+len-1)
505− n0 = A.unsafeIndex arr (off+len-2)
528+last = coerce JSS.last
529+-- last (Text arr off len)
530+-- | len <= 0 = emptyError "last"
531+-- | n < 0xDC00 || n > 0xDFFF = unsafeChr n
532+-- | otherwise = U16.chr2 n0 n
533+-- where n = A.unsafeIndex arr (off+len-1)
534+-- n0 = A.unsafeIndex arr (off+len-2)
506535{-# INLINE [1] last #-}
507536
508−{-# RULES
509−"TEXT last -> fused" [~1] forall t.
510− last t = S.last (stream t)
511−"TEXT last -> unfused" [1] forall t.
512− S.last (stream t) = last t
513− #-}
537+-- {-# RULES
538+-- "TEXT last -> fused" [~1] forall t.
539+-- last t = S.last (stream t)
540+-- "TEXT last -> unfused" [1] forall t.
541+-- S.last (stream t) = last t
542+-- #-}
514543
515544-- | /O(1)/ Returns all characters after the head of a 'Text', which
516545-- must be non-empty. Subject to fusion.
517546tail :: Text -> Text
518−tail t@(Text arr off len)
519− | len <= 0 = emptyError "tail"
520− | otherwise = text arr (off+d) (len-d)
521− where d = iter_ t 0
547+tail = coerce JSS.tail
548+-- tail t@(Text arr off len)
549+-- | len <= 0 = emptyError "tail"
550+-- | otherwise = text arr (off+d) (len-d)
551+-- where d = iter_ t 0
522552{-# INLINE [1] tail #-}
523553
524−{-# RULES
525−"TEXT tail -> fused" [~1] forall t.
526− tail t = unstream (S.tail (stream t))
527−"TEXT tail -> unfused" [1] forall t.
528− unstream (S.tail (stream t)) = tail t
529− #-}
554+-- {-# RULES
555+-- "TEXT tail -> fused" [~1] forall t.
556+-- tail t = unstream (S.tail (stream t))
557+-- "TEXT tail -> unfused" [1] forall t.
558+-- unstream (S.tail (stream t)) = tail t
559+-- #-}
530560
531561-- | /O(1)/ Returns all but the last character of a 'Text', which must
532562-- be non-empty. Subject to fusion.
533563init :: Text -> Text
534−init (Text arr off len) | len <= 0 = emptyError "init"
535− | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
536− | otherwise = text arr off (len-1)
537− where
538− n = A.unsafeIndex arr (off+len-1)
564+init = coerce JSS.init
565+-- init (Text arr off len) | len <= 0 = emptyError "init"
566+-- | n >= 0xDC00 && n <= 0xDFFF = text arr off (len-2)
567+-- | otherwise = text arr off (len-1)
568+-- where
569+-- n = A.unsafeIndex arr (off+len-1)
539570{-# INLINE [1] init #-}
540571
541−{-# RULES
542−"TEXT init -> fused" [~1] forall t.
543− init t = unstream (S.init (stream t))
544−"TEXT init -> unfused" [1] forall t.
545− unstream (S.init (stream t)) = init t
546− #-}
572+-- {-# RULES
573+-- "TEXT init -> fused" [~1] forall t.
574+-- init t = unstream (S.init (stream t))
575+-- "TEXT init -> unfused" [1] forall t.
576+-- unstream (S.init (stream t)) = init t
577+-- #-}
547578
548579-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
549580-- fusion.
550581null :: Text -> Bool
551−null (Text _arr _off len) =
552−#if defined(ASSERTS)
553− assert (len >= 0) $
554−#endif
555− len <= 0
582+null = coerce JSS.null
583+-- null (Text _arr _off len) =
584+-- #if defined(ASSERTS)
585+-- assert (len >= 0) $
586+-- #endif
587+-- len <= 0
556588{-# INLINE [1] null #-}
557589
558−{-# RULES
559−"TEXT null -> fused" [~1] forall t.
560− null t = S.null (stream t)
561−"TEXT null -> unfused" [1] forall t.
562− S.null (stream t) = null t
563− #-}
590+-- {-# RULES
591+-- "TEXT null -> fused" [~1] forall t.
592+-- null t = S.null (stream t)
593+-- "TEXT null -> unfused" [1] forall t.
594+-- S.null (stream t) = null t
595+-- #-}
564596
565597-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
566598-- Subject to fusion.
567−isSingleton :: Text -> Bool
568−isSingleton = S.isSingleton . stream
569−{-# INLINE isSingleton #-}
599+-- isSingleton :: Text -> Bool
600+-- isSingleton = S.isSingleton . stream
601+-- {-# INLINE isSingleton #-}
570602
571603-- | /O(n)/ Returns the number of characters in a 'Text'.
572604-- Subject to fusion.
573605length :: Text -> Int
574−length t = S.length (stream t)
606+length = coerce JSS.length
607+-- length t = S.length (stream t)
575608{-# INLINE [0] length #-}
576609-- length needs to be phased after the compareN/length rules otherwise
577610-- it may inline before the rules have an opportunity to fire.
@@ -583,43 +616,44 @@ length t = S.length (stream t)
583616-- of 'length', but can short circuit if the count of characters is
584617-- greater than the number, and hence be more efficient.
585618compareLength :: Text -> Int -> Ordering
586−compareLength t n = S.compareLengthI (stream t) n
587−{-# INLINE [1] compareLength #-}
588−
589−{-# RULES
590−"TEXT compareN/length -> compareLength" [~1] forall t n.
591− compare (length t) n = compareLength t n
592− #-}
593−
594−{-# RULES
595−"TEXT ==N/length -> compareLength/==EQ" [~1] forall t n.
596− eqInt (length t) n = compareLength t n == EQ
597− #-}
598−
599−{-# RULES
600−"TEXT /=N/length -> compareLength//=EQ" [~1] forall t n.
601− neInt (length t) n = compareLength t n /= EQ
602− #-}
603−
604−{-# RULES
605−"TEXT <N/length -> compareLength/==LT" [~1] forall t n.
606− ltInt (length t) n = compareLength t n == LT
607− #-}
608−
609−{-# RULES
610−"TEXT <=N/length -> compareLength//=GT" [~1] forall t n.
611− leInt (length t) n = compareLength t n /= GT
612− #-}
613−
614−{-# RULES
615−"TEXT >N/length -> compareLength/==GT" [~1] forall t n.
616− gtInt (length t) n = compareLength t n == GT
617− #-}
618−
619−{-# RULES
620−"TEXT >=N/length -> compareLength//=LT" [~1] forall t n.
621− geInt (length t) n = compareLength t n /= LT
622− #-}
619+compareLength t n = compare (length t) n
620+-- compareLength t n = S.compareLengthI (stream t) n
621+-- {-# INLINE [1] compareLength #-}
622+--
623+-- {-# RULES
624+-- "TEXT compareN/length -> compareLength" [~1] forall t n.
625+-- compare (length t) n = compareLength t n
626+-- #-}
627+--
628+-- {-# RULES
629+-- "TEXT ==N/length -> compareLength/==EQ" [~1] forall t n.
630+-- eqInt (length t) n = compareLength t n == EQ
631+-- #-}
632+--
633+-- {-# RULES
634+-- "TEXT /=N/length -> compareLength//=EQ" [~1] forall t n.
635+-- neInt (length t) n = compareLength t n /= EQ
636+-- #-}
637+--
638+-- {-# RULES
639+-- "TEXT <N/length -> compareLength/==LT" [~1] forall t n.
640+-- ltInt (length t) n = compareLength t n == LT
641+-- #-}
642+--
643+-- {-# RULES
644+-- "TEXT <=N/length -> compareLength//=GT" [~1] forall t n.
645+-- leInt (length t) n = compareLength t n /= GT
646+-- #-}
647+--
648+-- {-# RULES
649+-- "TEXT >N/length -> compareLength/==GT" [~1] forall t n.
650+-- gtInt (length t) n = compareLength t n == GT
651+-- #-}
652+--
653+-- {-# RULES
654+-- "TEXT >=N/length -> compareLength//=LT" [~1] forall t n.
655+-- geInt (length t) n = compareLength t n /= LT
656+-- #-}
623657
624658-- -----------------------------------------------------------------------------
625659-- * Transformations
@@ -627,26 +661,30 @@ compareLength t n = S.compareLengthI (stream t) n
627661-- each element of @t@. Subject to fusion. Performs replacement on
628662-- invalid scalar values.
629663map :: (Char -> Char) -> Text -> Text
630−map f t = unstream (S.map (safe . f) (stream t))
664+map = coerce JSS.map
665+-- map f t = unstream (S.map (safe . f) (stream t))
631666{-# INLINE [1] map #-}
632667
633668-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
634669-- 'Text's and concatenates the list after interspersing the first
635670-- argument between each element of the list.
636671intercalate :: Text -> [Text] -> Text
637−intercalate t = concat . (F.intersperse t)
672+intercalate = coerce JSS.intercalate
673+-- intercalate t = concat . (F.intersperse t)
638674{-# INLINE intercalate #-}
639675
640676-- | /O(n)/ The 'intersperse' function takes a character and places it
641677-- between the characters of a 'Text'. Subject to fusion. Performs
642678-- replacement on invalid scalar values.
643679intersperse :: Char -> Text -> Text
644−intersperse c t = unstream (S.intersperse (safe c) (stream t))
680+intersperse = coerce JSS.intersperse
681+-- intersperse c t = unstream (S.intersperse (safe c) (stream t))
645682{-# INLINE intersperse #-}
646683
647684-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
648685reverse :: Text -> Text
649−reverse t = S.reverse (stream t)
686+reverse = coerce JSS.reverse
687+-- reverse t = S.reverse (stream t)
650688{-# INLINE reverse #-}
651689
652690-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
@@ -680,28 +718,29 @@ replace :: Text
680718 -> Text
681719 -- ^ @haystack@ in which to search.
682720 -> Text
683−replace needle@(Text _ _ neeLen)
684− (Text repArr repOff repLen)
685− haystack@(Text hayArr hayOff hayLen)
686− | neeLen == 0 = emptyError "replace"
687− | L.null ixs = haystack
688− | len > 0 = Text (A.run x) 0 len
689− | otherwise = empty
690− where
691− ixs = indices needle haystack
692− len = hayLen - (neeLen - repLen) `mul` L.length ixs
693− x :: ST s (A.MArray s)
694− x = do
695− marr <- A.new len
696− let loop (i:is) o d = do
697− let d0 = d + i - o
698− d1 = d0 + repLen
699− A.copyI marr d hayArr (hayOff+o) d0
700− A.copyI marr d0 repArr repOff d1
701− loop is (i + neeLen) d1
702− loop [] o d = A.copyI marr d hayArr (hayOff+o) len
703− loop ixs 0 0
704− return marr
721+replace = coerce JSS.replace
722+-- replace needle@(Text _ _ neeLen)
723+-- (Text repArr repOff repLen)
724+-- haystack@(Text hayArr hayOff hayLen)
725+-- | neeLen == 0 = emptyError "replace"
726+-- | L.null ixs = haystack
727+-- | len > 0 = Text (A.run x) 0 len
728+-- | otherwise = empty
729+-- where
730+-- ixs = indices needle haystack
731+-- len = hayLen - (neeLen - repLen) `mul` L.length ixs
732+-- x :: ST s (A.MArray s)
733+-- x = do
734+-- marr <- A.new len
735+-- let loop (i:is) o d = do
736+-- let d0 = d + i - o
737+-- d1 = d0 + repLen
738+-- A.copyI marr d hayArr (hayOff+o) d0
739+-- A.copyI marr d0 repArr repOff d1
740+-- loop is (i + neeLen) d1
741+-- loop [] o d = A.copyI marr d hayArr (hayOff+o) len
742+-- loop ixs 0 0
743+-- return marr
705744
706745-- ----------------------------------------------------------------------------
707746-- ** Case conversions (folds)
@@ -740,7 +779,8 @@ replace needle@(Text _ _ neeLen)
740779-- U+00B5) is case folded to \"μ\" (small letter mu, U+03BC)
741780-- instead of itself.
742781toCaseFold :: Text -> Text
743−toCaseFold t = unstream (S.toCaseFold (stream t))
782+-- toCaseFold t = unstream (S.toCaseFold (stream t))
783+toCaseFold = coerce JSS.toCaseFold
744784{-# INLINE toCaseFold #-}
745785
746786-- | /O(n)/ Convert a string to lower case, using simple case
@@ -751,7 +791,8 @@ toCaseFold t = unstream (S.toCaseFold (stream t))
751791-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
752792-- followed by \" ̇\" (combining dot above, U+0307).
753793toLower :: Text -> Text
754−toLower t = unstream (S.toLower (stream t))
794+-- toLower t = unstream (S.toLower (stream t))
795+toLower = coerce JSS.toLower
755796{-# INLINE toLower #-}
756797
757798-- | /O(n)/ Convert a string to upper case, using simple case
@@ -761,7 +802,8 @@ toLower t = unstream (S.toLower (stream t))
761802-- instance, the German \"ß\" (eszett, U+00DF) maps to the
762803-- two-letter sequence \"SS\".
763804toUpper :: Text -> Text
764−toUpper t = unstream (S.toUpper (stream t))
805+-- toUpper t = unstream (S.toUpper (stream t))
806+toUpper = coerce JSS.toUpper
765807{-# INLINE toUpper #-}
766808
767809-- | /O(n)/ Convert a string to title case, using simple case
@@ -783,7 +825,8 @@ toUpper t = unstream (S.toUpper (stream t))
783825-- Fox\" is correctly title cased—but this function will
784826-- capitalize /every/ word.
785827toTitle :: Text -> Text
786−toTitle t = unstream (S.toTitle (stream t))
828+toTitle = coerce JSS.toTitle
829+-- toTitle t = unstream (S.toTitle (stream t))
787830{-# INLINE toTitle #-}
788831
789832-- | /O(n)/ Left-justify a string to the given length, using the
@@ -795,18 +838,19 @@ toTitle t = unstream (S.toTitle (stream t))
795838-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796839-- > justifyLeft 3 'x' "foobar" == "foobar"
797840justifyLeft :: Int -> Char -> Text -> Text
798−justifyLeft k c t
799− | len >= k = t
800− | otherwise = t `append` replicateChar (k-len) c
801− where len = length t
841+justifyLeft = coerce JSS.justifyLeft
842+-- justifyLeft k c t
843+-- | len >= k = t
844+-- | otherwise = t `append` replicateChar (k-len) c
845+-- where len = length t
802846{-# INLINE [1] justifyLeft #-}
803847
804−{-# RULES
805−"TEXT justifyLeft -> fused" [~1] forall k c t.
806− justifyLeft k c t = unstream (S.justifyLeftI k c (stream t))
807−"TEXT justifyLeft -> unfused" [1] forall k c t.
808− unstream (S.justifyLeftI k c (stream t)) = justifyLeft k c t
809− #-}
848+-- {-# RULES
849+-- "TEXT justifyLeft -> fused" [~1] forall k c t.
850+-- justifyLeft k c t = unstream (S.justifyLeftI k c (stream t))
851+-- "TEXT justifyLeft -> unfused" [1] forall k c t.
852+-- unstream (S.justifyLeftI k c (stream t)) = justifyLeft k c t
853+-- #-}
810854
811855-- | /O(n)/ Right-justify a string to the given length, using the
812856-- specified fill character on the left. Performs replacement on
@@ -817,10 +861,11 @@ justifyLeft k c t
817861-- > justifyRight 7 'x' "bar" == "xxxxbar"
818862-- > justifyRight 3 'x' "foobar" == "foobar"
819863justifyRight :: Int -> Char -> Text -> Text
820−justifyRight k c t
821− | len >= k = t
822− | otherwise = replicateChar (k-len) c `append` t
823− where len = length t
864+justifyRight = coerce JSS.justifyRight
865+-- justifyRight k c t
866+-- | len >= k = t
867+-- | otherwise = replicateChar (k-len) c `append` t
868+-- where len = length t
824869{-# INLINE justifyRight #-}
825870
826871-- | /O(n)/ Center a string to the given length, using the specified
@@ -831,13 +876,14 @@ justifyRight k c t
831876--
832877-- > center 8 'x' "HS" = "xxxHSxxx"
833878center :: Int -> Char -> Text -> Text
834−center k c t
835− | len >= k = t
836− | otherwise = replicateChar l c `append` t `append` replicateChar r c
837− where len = length t
838− d = k - len
839− r = d `quot` 2
840− l = d - r
879+center = coerce JSS.center
880+-- center k c t
881+-- | len >= k = t
882+-- | otherwise = replicateChar l c `append` t `append` replicateChar r c
883+-- where len = length t
884+-- d = k - len
885+-- r = d `quot` 2
886+-- l = d - r
841887{-# INLINE center #-}
842888
843889-- | /O(n)/ The 'transpose' function transposes the rows and columns
@@ -845,7 +891,8 @@ center k c t
845891-- 'unpack', and the list version of transpose, and is thus not very
846892-- efficient.
847893transpose :: [Text] -> [Text]
848−transpose ts = P.map pack (L.transpose (P.map unpack ts))
894+transpose = coerce JSS.transpose
895+-- transpose ts = P.map pack (L.transpose (P.map unpack ts))
849896
850897-- -----------------------------------------------------------------------------
851898-- * Reducing 'Text's (folds)
@@ -855,23 +902,27 @@ transpose ts = P.map pack (L.transpose (P.map unpack ts))
855902-- reduces the 'Text' using the binary operator, from left to right.
856903-- Subject to fusion.
857904foldl :: (a -> Char -> a) -> a -> Text -> a
858−foldl f z t = S.foldl f z (stream t)
905+foldl f a0 (Text t) = JSS.foldl f a0 t
906+-- foldl f z t = S.foldl f z (stream t)
859907{-# INLINE foldl #-}
860908
861909-- | /O(n)/ A strict version of 'foldl'. Subject to fusion.
862910foldl' :: (a -> Char -> a) -> a -> Text -> a
863−foldl' f z t = S.foldl' f z (stream t)
911+foldl' f a0 (Text t) = JSS.foldl' f a0 t
912+-- foldl' f z t = S.foldl' f z (stream t)
864913{-# INLINE foldl' #-}
865914
866915-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
867916-- and thus must be applied to a non-empty 'Text'. Subject to fusion.
868917foldl1 :: (Char -> Char -> Char) -> Text -> Char
869−foldl1 f t = S.foldl1 f (stream t)
918+foldl1 = coerce JSS.foldl1
919+-- foldl1 f t = S.foldl1 f (stream t)
870920{-# INLINE foldl1 #-}
871921
872922-- | /O(n)/ A strict version of 'foldl1'. Subject to fusion.
873923foldl1' :: (Char -> Char -> Char) -> Text -> Char
874−foldl1' f t = S.foldl1' f (stream t)
924+foldl1' = coerce JSS.foldl1'
925+-- foldl1' f t = S.foldl1' f (stream t)
875926{-# INLINE foldl1' #-}
876927
877928-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
@@ -879,14 +930,16 @@ foldl1' f t = S.foldl1' f (stream t)
879930-- reduces the 'Text' using the binary operator, from right to left.
880931-- Subject to fusion.
881932foldr :: (Char -> a -> a) -> a -> Text -> a
882−foldr f z t = S.foldr f z (stream t)
933+foldr f a0 (Text t) = JSS.foldr f a0 t
934+-- foldr f z t = S.foldr f z (stream t)
883935{-# INLINE foldr #-}
884936
885937-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
886938-- and thus must be applied to a non-empty 'Text'. Subject to
887939-- fusion.
888940foldr1 :: (Char -> Char -> Char) -> Text -> Char
889−foldr1 f t = S.foldr1 f (stream t)
941+foldr1 = coerce JSS.foldr1
942+-- foldr1 f t = S.foldr1 f (stream t)
890943{-# INLINE foldr1 #-}
891944
892945-- -----------------------------------------------------------------------------
@@ -894,48 +947,54 @@ foldr1 f t = S.foldr1 f (stream t)
894947
895948-- | /O(n)/ Concatenate a list of 'Text's.
896949concat :: [Text] -> Text
897−concat ts = case ts' of
898− [] -> empty
899− [t] -> t
900− _ -> Text (A.run go) 0 len
901− where
902− ts' = L.filter (not . null) ts
903− len = sumP "concat" $ L.map lengthWord16 ts'
904− go :: ST s (A.MArray s)
905− go = do
906− arr <- A.new len
907− let step i (Text a o l) =
908− let !j = i + l in A.copyI arr i a o j >> return j
909− foldM step 0 ts' >> return arr
950+concat = coerce JSS.concat
951+-- concat ts = case ts' of
952+-- [] -> empty
953+-- [t] -> t
954+-- _ -> Text (A.run go) 0 len
955+-- where
956+-- ts' = L.filter (not . null) ts
957+-- len = sumP "concat" $ L.map lengthWord16 ts'
958+-- go :: ST s (A.MArray s)
959+-- go = do
960+-- arr <- A.new len
961+-- let step i (Text a o l) =
962+-- let !j = i + l in A.copyI arr i a o j >> return j
963+-- foldM step 0 ts' >> return arr
910964
911965-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
912966-- concatenate the results.
913967concatMap :: (Char -> Text) -> Text -> Text
914−concatMap f = concat . foldr ((:) . f) []
968+concatMap = coerce JSS.concatMap
969+-- concatMap f = concat . foldr ((:) . f) []
915970{-# INLINE concatMap #-}
916971
917972-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
918973-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
919974any :: (Char -> Bool) -> Text -> Bool
920−any p t = S.any p (stream t)
975+any = coerce JSS.any
976+-- any p t = S.any p (stream t)
921977{-# INLINE any #-}
922978
923979-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
924980-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
925981all :: (Char -> Bool) -> Text -> Bool
926−all p t = S.all p (stream t)
982+all = coerce JSS.all
983+-- all p t = S.all p (stream t)
927984{-# INLINE all #-}
928985
929986-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
930987-- must be non-empty. Subject to fusion.
931988maximum :: Text -> Char
932−maximum t = S.maximum (stream t)
989+maximum = coerce JSS.maximum
990+-- maximum t = S.maximum (stream t)
933991{-# INLINE maximum #-}
934992
935993-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
936994-- must be non-empty. Subject to fusion.
937995minimum :: Text -> Char
938−minimum t = S.minimum (stream t)
996+minimum = coerce JSS.minimum
997+-- minimum t = S.minimum (stream t)
939998{-# INLINE minimum #-}
940999
9411000-- -----------------------------------------------------------------------------
@@ -951,8 +1010,9 @@ minimum t = S.minimum (stream t)
9511010--
9521011-- > last (scanl f z xs) == foldl f z xs.
9531012scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
954−scanl f z t = unstream (S.scanl g z (stream t))
955− where g a b = safe (f a b)
1013+scanl = coerce JSS.scanl
1014+-- scanl f z t = unstream (S.scanl g z (stream t))
1015+-- where g a b = safe (f a b)
9561016{-# INLINE scanl #-}
9571017
9581018-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
@@ -961,8 +1021,9 @@ scanl f z t = unstream (S.scanl g z (stream t))
9611021--
9621022-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
9631023scanl1 :: (Char -> Char -> Char) -> Text -> Text
964−scanl1 f t | null t = empty
965− | otherwise = scanl f (unsafeHead t) (unsafeTail t)
1024+scanl1 = coerce JSS.scanl1
1025+-- scanl1 f t | null t = empty
1026+-- | otherwise = scanl f (unsafeHead t) (unsafeTail t)
9661027{-# INLINE scanl1 #-}
9671028
9681029-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'. Performs
@@ -970,16 +1031,18 @@ scanl1 f t | null t = empty
9701031--
9711032-- > scanr f v == reverse . scanl (flip f) v . reverse
9721033scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
973−scanr f z = S.reverse . S.reverseScanr g z . reverseStream
974− where g a b = safe (f a b)
1034+scanr = coerce JSS.scanr
1035+-- scanr f z = S.reverse . S.reverseScanr g z . reverseStream
1036+-- where g a b = safe (f a b)
9751037{-# INLINE scanr #-}
9761038
9771039-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
9781040-- value argument. Subject to fusion. Performs replacement on
9791041-- invalid scalar values.
9801042scanr1 :: (Char -> Char -> Char) -> Text -> Text
981−scanr1 f t | null t = empty
982− | otherwise = scanr f (last t) (init t)
1043+scanr1 = coerce JSS.scanr1
1044+-- scanr1 f t | null t = empty
1045+-- | otherwise = scanr f (last t) (init t)
9831046{-# INLINE scanr1 #-}
9841047
9851048-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
@@ -987,8 +1050,11 @@ scanr1 f t | null t = empty
9871050-- parameter from left to right, and returns a final 'Text'. Performs
9881051-- replacement on invalid scalar values.
9891052mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
990−mapAccumL f z0 = S.mapAccumL g z0 . stream
991− where g a b = second safe (f a b)
1053+mapAccumL f z0 (Text a) =
1054+ let (a', t) = JSS.mapAccumL f z0 a
1055+ in (a', Text t)
1056+-- mapAccumL f z0 = S.mapAccumL g z0 . stream
1057+-- where g a b = second safe (f a b)
9921058{-# INLINE mapAccumL #-}
9931059
9941060-- | The 'mapAccumR' function behaves like a combination of 'map' and
@@ -998,8 +1064,11 @@ mapAccumL f z0 = S.mapAccumL g z0 . stream
9981064-- 'Text'.
9991065-- Performs replacement on invalid scalar values.
10001066mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
1001−mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1002− where g a b = second safe (f a b)
1067+mapAccumR f z0 (Text a) =
1068+ let (a', t) = JSS.mapAccumR f z0 a
1069+ in (a', Text t)
1070+-- mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
1071+-- where g a b = second safe (f a b)
10031072{-# INLINE mapAccumR #-}
10041073
10051074-- -----------------------------------------------------------------------------
@@ -1008,32 +1077,33 @@ mapAccumR f z0 = second reverse . S.mapAccumL g z0 . reverseStream
10081077-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
10091078-- @t@ repeated @n@ times.
10101079replicate :: Int -> Text -> Text
1011−replicate n t@(Text a o l)
1012− | n <= 0 || l <= 0 = empty
1013− | n == 1 = t
1014− | isSingleton t = replicateChar n (unsafeHead t)
1015− | otherwise = Text (A.run x) 0 len
1016− where
1017− len = l `mul` n
1018− x :: ST s (A.MArray s)
1019− x = do
1020− arr <- A.new len
1021− let loop !d !i | i >= n = return arr
1022− | otherwise = let m = d + l
1023− in A.copyI arr d a o m >> loop m (i+1)
1024− loop 0 0
1080+replicate = coerce JSS.replicate
1081+-- replicate n t@(Text a o l)
1082+-- | n <= 0 || l <= 0 = empty
1083+-- | n == 1 = t
1084+-- | isSingleton t = replicateChar n (unsafeHead t)
1085+-- | otherwise = Text (A.run x) 0 len
1086+-- where
1087+-- len = l `mul` n
1088+-- x :: ST s (A.MArray s)
1089+-- x = do
1090+-- arr <- A.new len
1091+-- let loop !d !i | i >= n = return arr
1092+-- | otherwise = let m = d + l
1093+-- in A.copyI arr d a o m >> loop m (i+1)
1094+-- loop 0 0
10251095{-# INLINE [1] replicate #-}
10261096
1027−{-# RULES
1028−"TEXT replicate/singleton -> replicateChar" [~1] forall n c.
1029− replicate n (singleton c) = replicateChar n c
1030− #-}
1097+-- {-# RULES
1098+-- "TEXT replicate/singleton -> replicateChar" [~1] forall n c.
1099+-- replicate n (singleton c) = replicateChar n c
1100+-- #-}
10311101
10321102-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
10331103-- value of every element. Subject to fusion.
1034−replicateChar :: Int -> Char -> Text
1035−replicateChar n c = unstream (S.replicateCharI n (safe c))
1036−{-# INLINE replicateChar #-}
1104+-- replicateChar :: Int -> Char -> Text
1105+-- replicateChar n c = unstream (S.replicateCharI n (safe c))
1106+-- {-# INLINE replicateChar #-}
10371107
10381108-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
10391109-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
@@ -1043,7 +1113,8 @@ replicateChar n c = unstream (S.replicateCharI n (safe c))
10431113-- string, and @b@ is the seed value for further production. Subject
10441114-- to fusion. Performs replacement on invalid scalar values.
10451115unfoldr :: (a -> Maybe (Char,a)) -> a -> Text
1046−unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
1116+unfoldr f a = Text $ JSS.unfoldr f a
1117+-- unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10471118{-# INLINE unfoldr #-}
10481119
10491120-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'Text' from a seed
@@ -1053,7 +1124,8 @@ unfoldr f s = unstream (S.unfoldr (firstf safe . f) s)
10531124-- correct, otherwise its performance is similar to 'unfoldr'. Subject
10541125-- to fusion. Performs replacement on invalid scalar values.
10551126unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Text
1056−unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
1127+unfoldrN n f a0 = Text $ JSS.unfoldrN n f a0
1128+-- unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10571129{-# INLINE unfoldrN #-}
10581130
10591131-- -----------------------------------------------------------------------------
@@ -1063,25 +1135,26 @@ unfoldrN n f s = unstream (S.unfoldrN n (firstf safe . f) s)
10631135-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
10641136-- the length of the Text. Subject to fusion.
10651137take :: Int -> Text -> Text
1066−take n t@(Text arr off len)
1067− | n <= 0 = empty
1068− | n >= len = t
1069− | otherwise = text arr off (iterN n t)
1138+take = coerce JSS.take
1139+-- take n t@(Text arr off len)
1140+-- | n <= 0 = empty
1141+-- | n >= len = t
1142+-- | otherwise = text arr off (iterN n t)
10701143{-# INLINE [1] take #-}
10711144
1072−iterN :: Int -> Text -> Int
1073−iterN n t@(Text _arr _off len) = loop 0 0
1074− where loop !i !cnt
1075− | i >= len || cnt >= n = i
1076− | otherwise = loop (i+d) (cnt+1)
1077− where d = iter_ t i
1145+-- iterN :: Int -> Text -> Int
1146+-- iterN n t@(Text _arr _off len) = loop 0 0
1147+-- where loop !i !cnt
1148+-- | i >= len || cnt >= n = i
1149+-- | otherwise = loop (i+d) (cnt+1)
1150+-- where d = iter_ t i
10781151
1079−{-# RULES
1080−"TEXT take -> fused" [~1] forall n t.
1081− take n t = unstream (S.take n (stream t))
1082−"TEXT take -> unfused" [1] forall n t.
1083− unstream (S.take n (stream t)) = take n t
1084− #-}
1152+-- {-# RULES
1153+-- "TEXT take -> fused" [~1] forall n t.
1154+-- take n t = unstream (S.take n (stream t))
1155+-- "TEXT take -> unfused" [1] forall n t.
1156+-- unstream (S.take n (stream t)) = take n t
1157+-- #-}
10851158
10861159-- | /O(n)/ 'takeEnd' @n@ @t@ returns the suffix remaining after
10871160-- taking @n@ characters from the end of @t@.
@@ -1090,37 +1163,39 @@ iterN n t@(Text _arr _off len) = loop 0 0
10901163--
10911164-- > takeEnd 3 "foobar" == "bar"
10921165takeEnd :: Int -> Text -> Text
1093−takeEnd n t@(Text arr off len)
1094− | n <= 0 = empty
1095− | n >= len = t
1096− | otherwise = text arr (off+i) (len-i)
1097− where i = iterNEnd n t
1098−
1099−iterNEnd :: Int -> Text -> Int
1100−iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1101− where loop i !m
1102− | i <= 0 = 0
1103− | m <= 1 = i
1104− | otherwise = loop (i+d) (m-1)
1105− where d = reverseIter_ t i
1166+takeEnd = coerce JSS.takeEnd
1167+-- takeEnd n t@(Text arr off len)
1168+-- | n <= 0 = empty
1169+-- | n >= len = t
1170+-- | otherwise = text arr (off+i) (len-i)
1171+-- where i = iterNEnd n t
1172+
1173+-- iterNEnd :: Int -> Text -> Int
1174+-- iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1175+-- where loop i !m
1176+-- | i <= 0 = 0
1177+-- | m <= 1 = i
1178+-- | otherwise = loop (i+d) (m-1)
1179+-- where d = reverseIter_ t i
11061180
11071181-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
11081182-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
11091183-- is greater than the length of the 'Text'. Subject to fusion.
11101184drop :: Int -> Text -> Text
1111−drop n t@(Text arr off len)
1112− | n <= 0 = t
1113− | n >= len = empty
1114− | otherwise = text arr (off+i) (len-i)
1115− where i = iterN n t
1185+drop = coerce JSS.drop
1186+-- drop n t@(Text arr off len)
1187+-- | n <= 0 = t
1188+-- | n >= len = empty
1189+-- | otherwise = text arr (off+i) (len-i)
1190+-- where i = iterN n t
11161191{-# INLINE [1] drop #-}
11171192
1118−{-# RULES
1119−"TEXT drop -> fused" [~1] forall n t.
1120− drop n t = unstream (S.drop n (stream t))
1121−"TEXT drop -> unfused" [1] forall n t.
1122− unstream (S.drop n (stream t)) = drop n t
1123− #-}
1193+-- {-# RULES
1194+-- "TEXT drop -> fused" [~1] forall n t.
1195+-- drop n t = unstream (S.drop n (stream t))
1196+-- "TEXT drop -> unfused" [1] forall n t.
1197+-- unstream (S.drop n (stream t)) = drop n t
1198+-- #-}
11241199
11251200-- | /O(n)/ 'dropEnd' @n@ @t@ returns the prefix remaining after
11261201-- dropping @n@ characters from the end of @t@.
@@ -1129,28 +1204,30 @@ drop n t@(Text arr off len)
11291204--
11301205-- > dropEnd 3 "foobar" == "foo"
11311206dropEnd :: Int -> Text -> Text
1132−dropEnd n t@(Text arr off len)
1133− | n <= 0 = t
1134− | n >= len = empty
1135− | otherwise = text arr off (iterNEnd n t)
1207+dropEnd = coerce JSS.dropEnd
1208+-- dropEnd n t@(Text arr off len)
1209+-- | n <= 0 = t
1210+-- | n >= len = empty
1211+-- | otherwise = text arr off (iterNEnd n t)
11361212
11371213-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
11381214-- returns the longest prefix (possibly empty) of elements that
11391215-- satisfy @p@. Subject to fusion.
11401216takeWhile :: (Char -> Bool) -> Text -> Text
1141−takeWhile p t@(Text arr off len) = loop 0
1142− where loop !i | i >= len = t
1143− | p c = loop (i+d)
1144− | otherwise = text arr off i
1145− where Iter c d = iter t i
1217+takeWhile = coerce JSS.takeWhile
1218+-- takeWhile p t@(Text arr off len) = loop 0
1219+-- where loop !i | i >= len = t
1220+-- | p c = loop (i+d)
1221+-- | otherwise = text arr off i
1222+-- where Iter c d = iter t i
11461223{-# INLINE [1] takeWhile #-}
11471224
1148−{-# RULES
1149−"TEXT takeWhile -> fused" [~1] forall p t.
1150− takeWhile p t = unstream (S.takeWhile p (stream t))
1151−"TEXT takeWhile -> unfused" [1] forall p t.
1152− unstream (S.takeWhile p (stream t)) = takeWhile p t
1153− #-}
1225+-- {-# RULES
1226+-- "TEXT takeWhile -> fused" [~1] forall p t.
1227+-- takeWhile p t = unstream (S.takeWhile p (stream t))
1228+-- "TEXT takeWhile -> unfused" [1] forall p t.
1229+-- unstream (S.takeWhile p (stream t)) = takeWhile p t
1230+-- #-}
11541231
11551232-- | /O(n)/ 'takeWhileEnd', applied to a predicate @p@ and a 'Text',
11561233-- returns the longest suffix (possibly empty) of elements that
@@ -1159,36 +1236,39 @@ takeWhile p t@(Text arr off len) = loop 0
11591236--
11601237-- > takeWhileEnd (=='o') "foo" == "oo"
11611238takeWhileEnd :: (Char -> Bool) -> Text -> Text
1162−takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1163− where loop !i !l | l <= 0 = t
1164− | p c = loop (i+d) (l+d)
1165− | otherwise = text arr (off+l) (len-l)
1166− where (c,d) = reverseIter t i
1239+takeWhileEnd = P.undefined -- TODO
1240+-- takeWhileEnd :: (Char -> Bool) -> Text -> Text
1241+-- takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1242+-- where loop !i !l | l <= 0 = t
1243+-- | p c = loop (i+d) (l+d)
1244+-- | otherwise = text arr (off+l) (len-l)
1245+-- where (c,d) = reverseIter t i
11671246{-# INLINE [1] takeWhileEnd #-}
11681247
1169−{-# RULES
1170−"TEXT takeWhileEnd -> fused" [~1] forall p t.
1171− takeWhileEnd p t = S.reverse (S.takeWhile p (S.reverseStream t))
1172−"TEXT takeWhileEnd -> unfused" [1] forall p t.
1173− S.reverse (S.takeWhile p (S.reverseStream t)) = takeWhileEnd p t
1174− #-}
1248+-- {-# RULES
1249+-- "TEXT takeWhileEnd -> fused" [~1] forall p t.
1250+-- takeWhileEnd p t = S.reverse (S.takeWhile p (S.reverseStream t))
1251+-- "TEXT takeWhileEnd -> unfused" [1] forall p t.
1252+-- S.reverse (S.takeWhile p (S.reverseStream t)) = takeWhileEnd p t
1253+-- #-}
11751254
11761255-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
11771256-- 'takeWhile' @p@ @t@. Subject to fusion.
11781257dropWhile :: (Char -> Bool) -> Text -> Text
1179−dropWhile p t@(Text arr off len) = loop 0 0
1180− where loop !i !l | l >= len = empty
1181− | p c = loop (i+d) (l+d)
1182− | otherwise = Text arr (off+i) (len-l)
1183− where Iter c d = iter t i
1258+dropWhile = coerce JSS.dropWhile
1259+-- dropWhile p t@(Text arr off len) = loop 0 0
1260+-- where loop !i !l | l >= len = empty
1261+-- | p c = loop (i+d) (l+d)
1262+-- | otherwise = Text arr (off+i) (len-l)
1263+
11841264{-# INLINE [1] dropWhile #-}
11851265
1186−{-# RULES
1187−"TEXT dropWhile -> fused" [~1] forall p t.
1188− dropWhile p t = unstream (S.dropWhile p (stream t))
1189−"TEXT dropWhile -> unfused" [1] forall p t.
1190− unstream (S.dropWhile p (stream t)) = dropWhile p t
1191− #-}
1266+-- {-# RULES
1267+-- "TEXT dropWhile -> fused" [~1] forall p t.
1268+-- dropWhile p t = unstream (S.dropWhile p (stream t))
1269+-- "TEXT dropWhile -> unfused" [1] forall p t.
1270+-- unstream (S.dropWhile p (stream t)) = dropWhile p t
1271+-- #-}
11921272
11931273-- | /O(n)/ 'dropWhileEnd' @p@ @t@ returns the prefix remaining after
11941274-- dropping characters that satisfy the predicate @p@ from the end of
@@ -1198,19 +1278,20 @@ dropWhile p t@(Text arr off len) = loop 0 0
11981278--
11991279-- > dropWhileEnd (=='.') "foo..." == "foo"
12001280dropWhileEnd :: (Char -> Bool) -> Text -> Text
1201−dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1202− where loop !i !l | l <= 0 = empty
1203− | p c = loop (i+d) (l+d)
1204− | otherwise = Text arr off l
1205− where (c,d) = reverseIter t i
1281+dropWhileEnd = coerce JSS.dropWhileEnd
1282+-- dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1283+-- where loop !i !l | l <= 0 = empty
1284+-- | p c = loop (i+d) (l+d)
1285+-- | otherwise = Text arr off l
1286+-- where (c,d) = reverseIter t i
12061287{-# INLINE [1] dropWhileEnd #-}
12071288
1208−{-# RULES
1209−"TEXT dropWhileEnd -> fused" [~1] forall p t.
1210− dropWhileEnd p t = S.reverse (S.dropWhile p (S.reverseStream t))
1211−"TEXT dropWhileEnd -> unfused" [1] forall p t.
1212− S.reverse (S.dropWhile p (S.reverseStream t)) = dropWhileEnd p t
1213− #-}
1289+-- {-# RULES
1290+-- "TEXT dropWhileEnd -> fused" [~1] forall p t.
1291+-- dropWhileEnd p t = S.reverse (S.dropWhile p (S.reverseStream t))
1292+-- "TEXT dropWhileEnd -> unfused" [1] forall p t.
1293+-- S.reverse (S.dropWhile p (S.reverseStream t)) = dropWhileEnd p t
1294+-- #-}
12141295
12151296-- | /O(n)/ 'dropAround' @p@ @t@ returns the substring remaining after
12161297-- dropping characters that satisfy the predicate @p@ from both the
@@ -1245,45 +1326,49 @@ strip = dropAround isSpace
12451326-- prefix of @t@ of length @n@, and whose second is the remainder of
12461327-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
12471328splitAt :: Int -> Text -> (Text, Text)
1248−splitAt n t@(Text arr off len)
1249− | n <= 0 = (empty, t)
1250− | n >= len = (t, empty)
1251− | otherwise = let k = iterN n t
1252− in (text arr off k, text arr (off+k) (len-k))
1329+splitAt = coerce JSS.splitAt
1330+-- splitAt n t@(Text arr off len)
1331+-- | n <= 0 = (empty, t)
1332+-- | n >= len = (t, empty)
1333+-- | otherwise = let k = iterN n t
1334+-- in (text arr off k, text arr (off+k) (len-k))
12531335
12541336-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
12551337-- a pair whose first element is the longest prefix (possibly empty)
12561338-- of @t@ of elements that satisfy @p@, and whose second is the
12571339-- remainder of the list.
12581340span :: (Char -> Bool) -> Text -> (Text, Text)
1259−span p t = case span_ p t of
1260− (# hd,tl #) -> (hd,tl)
1341+span = coerce JSS.span
1342+-- span p t = case span_ p t of
1343+-- (# hd,tl #) -> (hd,tl)
12611344{-# INLINE span #-}
12621345
12631346-- | /O(n)/ 'break' is like 'span', but the prefix returned is
12641347-- over elements that fail the predicate @p@.
12651348break :: (Char -> Bool) -> Text -> (Text, Text)
1266−break p = span (not . p)
1349+break = coerce JSS.break
1350+-- break p = span (not . p)
12671351{-# INLINE break #-}
12681352
12691353-- | /O(n)/ Group characters in a string according to a predicate.
12701354groupBy :: (Char -> Char -> Bool) -> Text -> [Text]
1271−groupBy p = loop
1272− where
1273− loop t@(Text arr off len)
1274− | null t = []
1275− | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1276− where Iter c d = iter t 0
1277− n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
1355+groupBy = coerce JSS.groupBy
1356+-- groupBy p = loop
1357+-- where
1358+-- loop t@(Text arr off len)
1359+-- | null t = []
1360+-- | otherwise = text arr off n : loop (text arr (off+n) (len-n))
1361+-- where Iter c d = iter t 0
1362+-- n = d + findAIndexOrEnd (not . p c) (Text arr (off+d) (len-d))
12781363
12791364-- | Returns the /array/ index (in units of 'Word16') at which a
12801365-- character may be found. This is /not/ the same as the logical
12811366-- index returned by e.g. 'findIndex'.
1282−findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1283−findAIndexOrEnd q t@(Text _arr _off len) = go 0
1284− where go !i | i >= len || q c = i
1285− | otherwise = go (i+d)
1286− where Iter c d = iter t i
1367+-- findAIndexOrEnd :: (Char -> Bool) -> Text -> Int
1368+-- findAIndexOrEnd q t@(Text _arr _off len) = go 0
1369+-- where go !i | i >= len || q c = i
1370+-- | otherwise = go (i+d)
1371+-- where Iter c d = iter t i
12871372
12881373-- | /O(n)/ Group characters in a string by equality.
12891374group :: Text -> [Text]
@@ -1292,15 +1377,17 @@ group = groupBy (==)
12921377-- | /O(n)/ Return all initial segments of the given 'Text', shortest
12931378-- first.
12941379inits :: Text -> [Text]
1295−inits t@(Text arr off len) = loop 0
1296− where loop i | i >= len = [t]
1297− | otherwise = Text arr off i : loop (i + iter_ t i)
1380+inits = coerce JSS.inits
1381+-- inits t@(Text arr off len) = loop 0
1382+-- where loop i | i >= len = [t]
1383+-- | otherwise = Text arr off i : loop (i + iter_ t i)
12981384
12991385-- | /O(n)/ Return all final segments of the given 'Text', longest
13001386-- first.
13011387tails :: Text -> [Text]
1302−tails t | null t = [empty]
1303− | otherwise = t : tails (unsafeTail t)
1388+tails = coerce JSS.tails
1389+-- tails t | null t = [empty]
1390+-- | otherwise = t : tails (unsafeTail t)
13041391
13051392-- $split
13061393--
@@ -1333,19 +1420,20 @@ splitOn :: Text
13331420 -> Text
13341421 -- ^ Input text.
13351422 -> [Text]
1336−splitOn pat@(Text _ _ l) src@(Text arr off len)
1337− | l <= 0 = emptyError "splitOn"
1338− | isSingleton pat = split (== unsafeHead pat) src
1339− | otherwise = go 0 (indices pat src)
1340− where
1341− go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1342− go s _ = [text arr (s+off) (len-s)]
1423+splitOn = coerce JSS.splitOn
1424+-- splitOn pat@(Text _ _ l) src@(Text arr off len)
1425+-- | l <= 0 = emptyError "splitOn"
1426+-- | isSingleton pat = split (== unsafeHead pat) src
1427+-- | otherwise = go 0 (indices pat src)
1428+-- where
1429+-- go !s (x:xs) = text arr (s+off) (x-s) : go (x+l) xs
1430+-- go s _ = [text arr (s+off) (len-s)]
13431431{-# INLINE [1] splitOn #-}
13441432
1345−{-# RULES
1346−"TEXT splitOn/singleton -> split/==" [~1] forall c t.
1347− splitOn (singleton c) t = split (==c) t
1348− #-}
1433+-- {-# RULES
1434+-- "TEXT splitOn/singleton -> split/==" [~1] forall c t.
1435+-- splitOn (singleton c) t = split (==c) t
1436+-- #-}
13491437
13501438-- | /O(n)/ Splits a 'Text' into components delimited by separators,
13511439-- where the predicate returns True for a separator element. The
@@ -1355,11 +1443,12 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
13551443-- > split (=='a') "aabbaca" == ["","","bb","c",""]
13561444-- > split (=='a') "" == [""]
13571445split :: (Char -> Bool) -> Text -> [Text]
1358−split _ t@(Text _off _arr 0) = [t]
1359−split p t = loop t
1360− where loop s | null s' = [l]
1361− | otherwise = l : loop (unsafeTail s')
1362− where (# l, s' #) = span_ (not . p) s
1446+split = coerce JSS.split
1447+-- split _ t@(Text _off _arr 0) = [t]
1448+-- split p t = loop t
1449+-- where loop s | null s' = [l]
1450+-- | otherwise = l : loop (unsafeTail s')
1451+-- where (# l, s' #) = span_ (not . p) s
13631452{-# INLINE split #-}
13641453
13651454-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
@@ -1369,11 +1458,12 @@ split p t = loop t
13691458-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
13701459-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
13711460chunksOf :: Int -> Text -> [Text]
1372−chunksOf k = go
1373− where
1374− go t = case splitAt k t of
1375− (a,b) | null a -> []
1376− | otherwise -> a : go b
1461+chunksOf = coerce JSS.chunksOf
1462+-- chunksOf k = go
1463+-- where
1464+-- go t = case splitAt k t of
1465+-- (a,b) | null a -> []
1466+-- | otherwise -> a : go b
13771467{-# INLINE chunksOf #-}
13781468
13791469-- ----------------------------------------------------------------------------
@@ -1386,7 +1476,8 @@ chunksOf k = go
13861476-- returns the first element matching the predicate, or 'Nothing' if
13871477-- there is no such element.
13881478find :: (Char -> Bool) -> Text -> Maybe Char
1389−find p t = S.findBy p (stream t)
1479+find = coerce JSS.find
1480+-- find p t = S.findBy p (stream t)
13901481{-# INLINE find #-}
13911482
13921483-- | /O(n)/ The 'partition' function takes a predicate and a 'Text',
@@ -1395,14 +1486,16 @@ find p t = S.findBy p (stream t)
13951486--
13961487-- > partition p t == (filter p t, filter (not . p) t)
13971488partition :: (Char -> Bool) -> Text -> (Text, Text)
1398−partition p t = (filter p t, filter (not . p) t)
1489+partition = coerce JSS.partition
1490+-- partition p t = (filter p t, filter (not . p) t)
13991491{-# INLINE partition #-}
14001492
14011493-- | /O(n)/ 'filter', applied to a predicate and a 'Text',
14021494-- returns a 'Text' containing those characters that satisfy the
14031495-- predicate.
14041496filter :: (Char -> Bool) -> Text -> Text
1405−filter p t = unstream (S.filter p (stream t))
1497+filter = coerce JSS.filter
1498+-- filter p t = unstream (S.filter p (stream t))
14061499{-# INLINE filter #-}
14071500
14081501-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1427,11 +1520,12 @@ filter p t = unstream (S.filter p (stream t))
14271520-- In (unlikely) bad cases, this function's time complexity degrades
14281521-- towards /O(n*m)/.
14291522breakOn :: Text -> Text -> (Text, Text)
1430−breakOn pat src@(Text arr off len)
1431− | null pat = emptyError "breakOn"
1432− | otherwise = case indices pat src of
1433− [] -> (src, empty)
1434− (x:_) -> (text arr off x, text arr (off+x) (len-x))
1523+breakOn = coerce JSS.breakOn
1524+-- breakOn pat src@(Text arr off len)
1525+-- | null pat = emptyError "breakOn"
1526+-- | otherwise = case indices pat src of
1527+-- [] -> (src, empty)
1528+-- (x:_) -> (text arr off x, text arr (off+x) (len-x))
14351529{-# INLINE breakOn #-}
14361530
14371531-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
@@ -1443,8 +1537,9 @@ breakOn pat src@(Text arr off len)
14431537--
14441538-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
14451539breakOnEnd :: Text -> Text -> (Text, Text)
1446−breakOnEnd pat src = (reverse b, reverse a)
1447− where (a,b) = breakOn (reverse pat) (reverse src)
1540+breakOnEnd = coerce JSS.breakOnEnd
1541+-- breakOnEnd pat src = (reverse b, reverse a)
1542+-- where (a,b) = breakOn (reverse pat) (reverse src)
14481543{-# INLINE breakOnEnd #-}
14491544
14501545-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
@@ -1468,12 +1563,13 @@ breakOnEnd pat src = (reverse b, reverse a)
14681563breakOnAll :: Text -- ^ @needle@ to search for
14691564 -> Text -- ^ @haystack@ in which to search
14701565 -> [(Text, Text)]
1471−breakOnAll pat src@(Text arr off slen)
1472− | null pat = emptyError "breakOnAll"
1473− | otherwise = L.map step (indices pat src)
1474− where
1475− step x = (chunk 0 x, chunk x (slen-x))
1476− chunk !n !l = text arr (n+off) l
1566+breakOnAll = coerce JSS.breakOnAll
1567+-- breakOnAll pat src@(Text arr off slen)
1568+-- | null pat = emptyError "breakOnAll"
1569+-- | otherwise = L.map step (indices pat src)
1570+-- where
1571+-- step x = (chunk 0 x, chunk x (slen-x))
1572+-- chunk !n !l = text arr (n+off) l
14771573{-# INLINE breakOnAll #-}
14781574
14791575-------------------------------------------------------------------------------
@@ -1497,14 +1593,16 @@ breakOnAll pat src@(Text arr off slen)
14971593
14981594-- | /O(n)/ 'Text' index (subscript) operator, starting from 0.
14991595index :: Text -> Int -> Char
1500−index t n = S.index (stream t) n
1596+index = coerce JSS.index
1597+-- index t n = S.index (stream t) n
15011598{-# INLINE index #-}
15021599
15031600-- | /O(n)/ The 'findIndex' function takes a predicate and a 'Text'
15041601-- and returns the index of the first element in the 'Text' satisfying
15051602-- the predicate. Subject to fusion.
15061603findIndex :: (Char -> Bool) -> Text -> Maybe Int
1507−findIndex p t = S.findIndex p (stream t)
1604+findIndex = coerce JSS.findIndex
1605+-- findIndex p t = S.findIndex p (stream t)
15081606{-# INLINE findIndex #-}
15091607
15101608-- | /O(n+m)/ The 'count' function returns the number of times the
@@ -1514,22 +1612,23 @@ findIndex p t = S.findIndex p (stream t)
15141612-- In (unlikely) bad cases, this function's time complexity degrades
15151613-- towards /O(n*m)/.
15161614count :: Text -> Text -> Int
1517−count pat src
1518− | null pat = emptyError "count"
1519− | isSingleton pat = countChar (unsafeHead pat) src
1520− | otherwise = L.length (indices pat src)
1615+count = coerce JSS.count
1616+-- count pat src
1617+-- | null pat = emptyError "count"
1618+-- | isSingleton pat = countChar (unsafeHead pat) src
1619+-- | otherwise = L.length (indices pat src)
15211620{-# INLINE [1] count #-}
15221621
1523−{-# RULES
1524−"TEXT count/singleton -> countChar" [~1] forall c t.
1525− count (singleton c) t = countChar c t
1526− #-}
1622+-- {-# RULES
1623+-- "TEXT count/singleton -> countChar" [~1] forall c t.
1624+-- count (singleton c) t = countChar c t
1625+-- #-}
15271626
15281627-- | /O(n)/ The 'countChar' function returns the number of times the
15291628-- query element appears in the given 'Text'. Subject to fusion.
1530−countChar :: Char -> Text -> Int
1531−countChar c t = S.countChar c (stream t)
1532−{-# INLINE countChar #-}
1629+-- countChar :: Char -> Text -> Int
1630+-- countChar c t = S.countChar c (stream t)
1631+-- {-# INLINE countChar #-}
15331632
15341633-------------------------------------------------------------------------------
15351634-- * Zipping
@@ -1539,42 +1638,46 @@ countChar c t = S.countChar c (stream t)
15391638-- excess elements of the longer 'Text' are discarded. This is
15401639-- equivalent to a pair of 'unpack' operations.
15411640zip :: Text -> Text -> [(Char,Char)]
1542−zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1641+zip = coerce JSS.zip
1642+-- zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
15431643{-# INLINE zip #-}
15441644
15451645-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
15461646-- given as the first argument, instead of a tupling function.
15471647-- Performs replacement on invalid scalar values.
15481648zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1549−zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550− where g a b = safe (f a b)
1649+zipWith = coerce JSS.zipWith
1650+-- zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1651+-- where g a b = safe (f a b)
15511652{-# INLINE zipWith #-}
15521653
15531654-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
15541655-- representing white space.
15551656words :: Text -> [Text]
1556−words t@(Text arr off len) = loop 0 0
1557− where
1558− loop !start !n
1559− | n >= len = if start == n
1560− then []
1561− else [Text arr (start+off) (n-start)]
1562− | isSpace c =
1563− if start == n
1564− then loop (start+1) (start+1)
1565− else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1566− | otherwise = loop start (n+d)
1567− where Iter c d = iter t n
1657+words = coerce JSS.words
1658+-- words t@(Text arr off len) = loop 0 0
1659+-- where
1660+-- loop !start !n
1661+-- | n >= len = if start == n
1662+-- then []
1663+-- else [Text arr (start+off) (n-start)]
1664+-- | isSpace c =
1665+-- if start == n
1666+-- then loop (start+1) (start+1)
1667+-- else Text arr (start+off) (n-start) : loop (n+d) (n+d)
1668+-- | otherwise = loop start (n+d)
1669+-- where Iter c d = iter t n
15681670{-# INLINE words #-}
15691671
15701672-- | /O(n)/ Breaks a 'Text' up into a list of 'Text's at
15711673-- newline 'Char's. The resulting strings do not contain newlines.
15721674lines :: Text -> [Text]
1573−lines ps | null ps = []
1574− | otherwise = h : if null t
1575− then []
1576− else lines (unsafeTail t)
1577− where (# h,t #) = span_ (/= '\n') ps
1675+lines = coerce JSS.lines
1676+-- lines ps | null ps = []
1677+-- | otherwise = h : if null t
1678+-- then []
1679+-- else lines (unsafeTail t)
1680+-- where (# h,t #) = span_ (/= '\n') ps
15781681{-# INLINE lines #-}
15791682
15801683{-
@@ -1602,34 +1705,38 @@ lines' ps | null ps = []
16021705-- | /O(n)/ Joins lines, after appending a terminating newline to
16031706-- each.
16041707unlines :: [Text] -> Text
1605−unlines = concat . L.map (`snoc` '\n')
1708+unlines = coerce JSS.unlines
1709+-- unlines = concat . L.map (`snoc` '\n')
16061710{-# INLINE unlines #-}
16071711
16081712-- | /O(n)/ Joins words using single space characters.
16091713unwords :: [Text] -> Text
1610−unwords = intercalate (singleton ' ')
1714+unwords = coerce JSS.unwords
1715+-- unwords = intercalate (singleton ' ')
16111716{-# INLINE unwords #-}
16121717
16131718-- | /O(n)/ The 'isPrefixOf' function takes two 'Text's and returns
16141719-- 'True' iff the first is a prefix of the second. Subject to fusion.
16151720isPrefixOf :: Text -> Text -> Bool
1616−isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1617− alen <= blen && S.isPrefixOf (stream a) (stream b)
1721+isPrefixOf = coerce JSS.isPrefixOf
1722+-- isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
1723+-- alen <= blen && S.isPrefixOf (stream a) (stream b)
16181724{-# INLINE [1] isPrefixOf #-}
16191725
1620−{-# RULES
1621−"TEXT isPrefixOf -> fused" [~1] forall s t.
1622− isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1623− #-}
1726+-- {-# RULES
1727+-- "TEXT isPrefixOf -> fused" [~1] forall s t.
1728+-- isPrefixOf s t = S.isPrefixOf (stream s) (stream t)
1729+-- #-}
16241730
16251731-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
16261732-- 'True' iff the first is a suffix of the second.
16271733isSuffixOf :: Text -> Text -> Bool
1628−isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1629− d >= 0 && a == b'
1630− where d = blen - alen
1631− b' | d == 0 = b
1632− | otherwise = Text barr (boff+d) alen
1734+isSuffixOf = coerce JSS.isSuffixOf
1735+-- isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1736+-- d >= 0 && a == b'
1737+-- where d = blen - alen
1738+-- b' | d == 0 = b
1739+-- | otherwise = Text barr (boff+d) alen
16331740{-# INLINE isSuffixOf #-}
16341741
16351742-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
@@ -1639,16 +1746,17 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
16391746-- In (unlikely) bad cases, this function's time complexity degrades
16401747-- towards /O(n*m)/.
16411748isInfixOf :: Text -> Text -> Bool
1642−isInfixOf needle haystack
1643− | null needle = True
1644− | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1645− | otherwise = not . L.null . indices needle $ haystack
1749+isInfixOf = coerce JSS.isInfixOf
1750+-- isInfixOf needle haystack
1751+-- | null needle = True
1752+-- | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
1753+-- | otherwise = not . L.null . indices needle $ haystack
16461754{-# INLINE [1] isInfixOf #-}
16471755
1648−{-# RULES
1649−"TEXT isInfixOf/singleton -> S.elem/S.stream" [~1] forall n h.
1650− isInfixOf (singleton n) h = S.elem n (S.stream h)
1651− #-}
1756+-- {-# RULES
1757+-- "TEXT isInfixOf/singleton -> S.elem/S.stream" [~1] forall n h.
1758+-- isInfixOf (singleton n) h = S.elem n (S.stream h)
1759+-- #-}
16521760
16531761-------------------------------------------------------------------------------
16541762-- * View patterns
@@ -1672,9 +1780,10 @@ isInfixOf needle haystack
16721780-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
16731781-- > fnordLength _ = -1
16741782stripPrefix :: Text -> Text -> Maybe Text
1675−stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1676− | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1677− | otherwise = Nothing
1783+stripPrefix = coerce JSS.stripPrefix
1784+-- stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1785+-- | p `isPrefixOf` t = Just $! text arr (off+plen) (len-plen)
1786+-- | otherwise = Nothing
16781787
16791788-- | /O(n)/ Find the longest non-empty common prefix of two strings
16801789-- and return it, along with the suffixes of each string at which they
@@ -1689,15 +1798,16 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
16891798-- > commonPrefixes "veeble" "fetzer" == Nothing
16901799-- > commonPrefixes "" "baz" == Nothing
16911800commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1692−commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1693− where
1694− go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1695− | i > 0 = Just (Text arr0 off0 i,
1696− text arr0 (off0+i) (len0-i),
1697− text arr1 (off1+j) (len1-j))
1698− | otherwise = Nothing
1699− where Iter a d0 = iter t0 i
1700− Iter b d1 = iter t1 j
1801+commonPrefixes = coerce JSS.commonPrefixes
1802+-- commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1803+-- where
1804+-- go !i !j | i < len0 && j < len1 && a == b = go (i+d0) (j+d1)
1805+-- | i > 0 = Just (Text arr0 off0 i,
1806+-- text arr0 (off0+i) (len0-i),
1807+-- text arr1 (off1+j) (len1-j))
1808+-- | otherwise = Nothing
1809+-- where Iter a d0 = iter t0 i
1810+-- Iter b d1 = iter t1 j
17011811
17021812-- | /O(n)/ Return the prefix of the second string if its suffix
17031813-- matches the entire first string.
@@ -1718,21 +1828,22 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
17181828-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
17191829-- > quuxLength _ = -1
17201830stripSuffix :: Text -> Text -> Maybe Text
1721−stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1722− | p `isSuffixOf` t = Just $! text arr off (len-plen)
1723− | otherwise = Nothing
1724−
1725−-- | Add a list of non-negative numbers. Errors out on overflow.
1726−sumP :: String -> [Int] -> Int
1727−sumP fun = go 0
1728− where go !a (x:xs)
1729− | ax >= 0 = go ax xs
1730− | otherwise = overflowError fun
1731− where ax = a + x
1732− go a _ = a
1733−
1734−emptyError :: String -> a
1735−emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1831+stripSuffix = coerce JSS.stripSuffix
1832+-- stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1833+-- | p `isSuffixOf` t = Just $! text arr off (len-plen)
1834+-- | otherwise = Nothing
1835+
1836+-- -- | Add a list of non-negative numbers. Errors out on overflow.
1837+-- sumP :: String -> [Int] -> Int
1838+-- sumP fun = go 0
1839+-- where go !a (x:xs)
1840+-- | ax >= 0 = go ax xs
1841+-- | otherwise = overflowError fun
1842+-- where ax = a + x
1843+-- go a _ = a
1844+
1845+-- emptyError :: String -> a
1846+-- emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
17361847
17371848overflowError :: String -> a
17381849overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
@@ -1746,10 +1857,10 @@ overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"
17461857-- copy \"breaks the link\" to the original array, allowing it to be
17471858-- garbage collected if there are no other live references to it.
17481859copy :: Text -> Text
1749−copy (Text arr off len) = Text (A.run go) 0 len
1750− where
1751− go :: ST s (A.MArray s)
1752− go = do
1753− marr <- A.new len
1754− A.copyI marr 0 arr off len
1755− return marr
1860+copy = P.id -- TODO
1861+-- copy (Text arr off len) = Text (A.run go) 0 len
1862+-- where
1863+-- go :: ST s (A.MArray s)
1864+-- go = do
1865+-- marr <- A.new len
1866+