module Data.JSString
    ( JSString
    -- * Creation and elimination
    , pack
    , unpack
    , unpack'
    , singleton
    , empty
    -- * Basic interface
    , cons
    , snoc
    , append
    , uncons
    , head
    , last
    , tail
    , init
    , null
    , length
    , compareLength
    -- * Transformations
    , map
    , intercalate
    , intersperse
    , transpose
    , reverse
    , replace
    -- ** Case conversion
    , toCaseFold
    , toLower
    , toUpper
    , toTitle
                       -- ** Justification
    , justifyLeft
    , justifyRight
    , center
                       -- * Folds
    , foldl
    , foldl'
    , foldl1
    , foldl1'
    , foldr
    , foldr1
                       -- ** Special folds
    , concat
    , concatMap
    , any
    , all
    , maximum
    , minimum
                       -- * Construction
                       -- ** Scans
    , scanl
    , scanl1
    , scanr
    , scanr1
                       -- ** Accumulating maps
    , mapAccumL
    , mapAccumR
                       -- ** Generation and unfolding
    , replicate
    , unfoldr
    , unfoldrN
                       -- * Substrings
                       -- ** Breaking strings
    , take
    , takeEnd
    , drop
    , dropEnd
    , takeWhile
    , dropWhile
    , dropWhileEnd
    , dropAround
    , strip
    , stripStart
    , stripEnd
    , splitAt
    , breakOn
    , breakOnEnd
    , break
    , span
    , group
    , group'
    , groupBy
    , inits
    , tails
                       -- ** Breaking into many substrings
    , splitOn
    , splitOn'
    , split
    , chunksOf
    , chunksOf'
    -- ** Breaking into lines and words
    , lines
    , lines'
    , words
    , words'
    , unlines
    , unwords
    -- * Predicates
    , isPrefixOf
    , isSuffixOf
    , isInfixOf
    -- ** View patterns
    , stripPrefix
    , stripSuffix
    , commonPrefixes
    -- * Searching
    , filter
    , breakOnAll
    , breakOnAll'
    , find
    , partition
    -- * Indexing
    , index
    , findIndex
    , count
    -- * Zipping
    , zip
    , zipWith
    ) where


import qualified Data.Text as T

import           Data.JSString.Internal.Type
import           GHC.Exts as Exts

import Prelude
       (Maybe, Bool, String, Ordering, (.),
        ($),fmap, (<$>))

-- | /O(1)/ The empty 'JSString'.
empty :: JSString
empty = JSString (T.empty)
{-# INLINE [1] empty #-}

getJSString :: JSString -> T.Text
getJSString (JSString txt) = txt

-- -----------------------------------------------------------------------------
-- * Conversion to/from 'JSString'

-- | /O(n)/ Convert a 'String' into a 'JSString'.  Subject to
-- fusion.
pack :: String -> JSString
pack = JSString . T.pack
{-# INLINE [1] pack #-}

-- | /O(n)/ Convert a 'JSString' into a 'String'.  Subject to fusion.
unpack :: JSString -> String
unpack = T.unpack . getJSString
{-# INLINE [1] unpack #-}

unpack' :: JSString -> String
unpack' = unpack
{-# INLINE unpack' #-}

-- | /O(1)/ Convert a character into a 'JSString'.  Subject to fusion.
-- Performs replacement on invalid scalar values.
singleton :: Char -> JSString
singleton = JSString . T.singleton
{-# INLINE [1] singleton #-}

-- -----------------------------------------------------------------------------
-- * Basic functions

-- | /O(n)/ Adds a character to the front of a 'JSString'.  This function
-- is more costly than its 'List' counterpart because it requires
-- copying a new array.  Subject to fusion.  Performs replacement on
-- invalid scalar values.
cons :: Char -> JSString -> JSString
cons c (JSString x) = JSString $ T.cons c x
{-# INLINE [1] cons #-}

infixr 5 `cons`

-- | /O(n)/ Adds a character to the end of a 'JSString'.  This copies the
-- entire array in the process, unless fused.  Subject to fusion.
-- Performs replacement on invalid scalar values.
snoc :: JSString -> Char -> JSString
snoc (JSString x) c = JSString $ T.snoc x c
  -- unstream (S.snoc (stream t) (safe c))
{-# INLINE [1] snoc #-}

-- | /O(n)/ Appends one 'JSString' to the other by copying both of them
-- into a new 'JSString'.  Subject to fusion.
append :: JSString -> JSString -> JSString
append (JSString x) (JSString y) = JSString $ T.append x y
{-# INLINE [1] append #-}

-- | /O(1)/ Returns the first character of a 'JSString', which must be
-- non-empty.  Subject to fusion.
head :: JSString -> Char
head (JSString x) = T.head x
{-# INLINE [1] head #-}


-- | /O(1)/ Returns the first character and rest of a 'JSString', or
-- 'Nothing' if empty. Subject to fusion.
uncons :: JSString -> Maybe (Char, JSString)
uncons (JSString x) = (fmap JSString) <$> T.uncons x
{-# INLINE [1] uncons #-}

-- | /O(1)/ Returns the last character of a 'JSString', which must be
-- non-empty.  Subject to fusion.
last :: JSString -> Char
last (JSString x) = T.last x
{-# INLINE [1] last #-}

-- | /O(1)/ Returns all characters after the head of a 'JSString', which
-- must be non-empty.  Subject to fusion.
tail :: JSString -> JSString
tail (JSString x) = JSString $ T.tail x
{-# INLINE [1] tail #-}

-- | /O(1)/ Returns all but the last character of a 'JSString', which must
-- be non-empty.  Subject to fusion.
init :: JSString -> JSString
init (JSString x) = JSString $ T.init x
{-# INLINE [1] init #-}

-- | /O(1)/ Tests whether a 'JSString' is empty or not.  Subject to
-- fusion.
null :: JSString -> Bool
null (JSString x) = T.null x
{-# INLINE [1] null #-}

-- | /O(n)/ Returns the number of characters in a 'JSString'.
-- Subject to fusion.
length :: JSString -> Int
length (JSString x) = T.length x
{-# INLINE [1] length #-}
-- | /O(n)/ Compare the count of characters in a 'JSString' to a number.
-- Subject to fusion.
--
-- This function gives the same answer as comparing against the result
-- of 'length', but can short circuit if the count of characters is
-- greater than the number, and hence be more efficient.
compareLength :: JSString -> Int -> Ordering
compareLength (JSString t) n = T.compareLength t n
{-# INLINE [1] compareLength #-}

-- -----------------------------------------------------------------------------
-- * Transformations
-- | /O(n)/ 'map' @f@ @t@ is the 'JSString' obtained by applying @f@ to
-- each element of @t@.  Subject to fusion.  Performs replacement on
-- invalid scalar values.
map :: (Char -> Char) -> JSString -> JSString
map f (JSString t) = JSString $ T.map f t
{-# INLINE [1] map #-}

-- | /O(n)/ The 'intercalate' function takes a 'JSString' and a list of
-- 'JSString's and concatenates the list after interspersing the first
-- argument between each element of the list.
intercalate :: JSString -> [JSString] -> JSString
intercalate (JSString i) xs = JSString $ T.intercalate i (getJSString <$> xs)
{-# INLINE [1] intercalate #-}

-- | /O(n)/ The 'intersperse' function takes a character and places it
-- between the characters of a 'JSString'.  Subject to fusion.  Performs
-- replacement on invalid scalar values.
intersperse     :: Char -> JSString -> JSString
intersperse c (JSString x) = JSString $ T.intersperse c x
{-# INLINE [1] intersperse #-}

-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
reverse :: JSString -> JSString
reverse = JSString . T.reverse . getJSString
{-# INLINE [1] reverse #-}

-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
-- @haystack@ with @replacement@.
--
-- This function behaves as though it was defined as follows:
--
-- @
-- replace needle replacement haystack =
--   'intercalate' replacement ('splitOn' needle haystack)
-- @
--
-- As this suggests, each occurrence is replaced exactly once.  So if
-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
-- be replaced recursively:
--
-- > replace "oo" "foo" "oo" == "foo"
--
-- In cases where several instances of @needle@ overlap, only the
-- first one will be replaced:
--
-- > replace "ofo" "bar" "ofofo" == "barfo"
--
-- In (unlikely) bad cases, this function's time complexity degrades
-- towards /O(n*m)/.
replace :: JSString
        -- ^ @needle@ to search for.  If this string is empty, an
        -- error will occur.
        -> JSString
        -- ^ @replacement@ to replace @needle@ with.
        -> JSString
        -- ^ @haystack@ in which to search.
        -> JSString
replace (JSString needle) (JSString replacement) (JSString haystack) =
    JSString $ T.replace needle replacement haystack
{-# INLINE replace #-}

-- ----------------------------------------------------------------------------
-- ** Case conversions (folds)

-- $case
--
-- When case converting 'JSString' values, do not use combinators like
-- @map toUpper@ to case convert each character of a string
-- individually, as this gives incorrect results according to the
-- rules of some writing systems.  The whole-string case conversion
-- functions from this module, such as @toUpper@, obey the correct
-- case conversion rules.  As a result, these functions may map one
-- input character to two or three output characters. For examples,
-- see the documentation of each function.
--
-- /Note/: In some languages, case conversion is a locale- and
-- context-dependent operation. The case conversion functions in this
-- module are /not/ locale sensitive. Programs that require locale
-- sensitivity should use appropriate versions of the
-- <http://hackage.haskell.org/package/text-icu-0.6.3.7/docs/Data-Text-ICU.html#g:4 case mapping functions from the text-icu package >.

-- | /O(n)/ Convert a string to folded case.  Subject to fusion.
--
-- This function is mainly useful for performing caseless (also known
-- as case insensitive) string comparisons.
--
-- A string @x@ is a caseless match for a string @y@ if and only if:
--
-- @toCaseFold x == toCaseFold y@
--
-- The result string may be longer than the input string, and may
-- differ from applying 'toLower' to the input string.  For instance,
-- the Armenian small ligature \"&#xfb13;\" (men now, U+FB13) is case
-- folded to the sequence \"&#x574;\" (men, U+0574) followed by
-- \"&#x576;\" (now, U+0576), while the Greek \"&#xb5;\" (micro sign,
-- U+00B5) is case folded to \"&#x3bc;\" (small letter mu, U+03BC)
-- instead of itself.
toCaseFold :: JSString -> JSString
toCaseFold = JSString . T.toCaseFold . getJSString
{-# INLINE [0] toCaseFold #-}

-- | /O(n)/ Convert a string to lower case, using simple case
-- conversion.  Subject to fusion.
--
-- The result string may be longer than the input string.  For
-- instance, \"&#x130;\" (Latin capital letter I with dot above,
-- U+0130) maps to the sequence \"i\" (Latin small letter i, U+0069)
-- followed by \" &#x307;\" (combining dot above, U+0307).
toLower :: JSString -> JSString
toLower = JSString . T.toLower . getJSString
{-# INLINE [1] toLower #-}
-- | /O(n)/ Convert a string to upper case, using simple case
-- conversion.  Subject to fusion.
--
-- The result string may be longer than the input string.  For
-- instance, the German \"&#xdf;\" (eszett, U+00DF) maps to the
-- two-letter sequence \"SS\".
toUpper :: JSString -> JSString
toUpper = JSString . T.toUpper . getJSString
{-# INLINE [1] toUpper #-}

-- | /O(n)/ Convert a string to title case, using simple case
-- conversion. Subject to fusion.
--
-- The first letter of the input is converted to title case, as is
-- every subsequent letter that immediately follows a non-letter.
-- Every letter that immediately follows another letter is converted
-- to lower case.
--
-- The result string may be longer than the input string. For example,
-- the Latin small ligature &#xfb02; (U+FB02) is converted to the
-- sequence Latin capital letter F (U+0046) followed by Latin small
-- letter l (U+006C).
--
-- /Note/: this function does not take language or culture specific
-- rules into account. For instance, in English, different style
-- guides disagree on whether the book name \"The Hill of the Red
-- Fox\" is correctly title cased&#x2014;but this function will
-- capitalize /every/ word.
toTitle :: JSString -> JSString
toTitle = JSString . T.toTitle . getJSString
{-# INLINE toTitle #-}

-- | /O(n)/ Left-justify a string to the given length, using the
-- specified fill character on the right. Subject to fusion.
-- Performs replacement on invalid scalar values.
--
-- Examples:
--
-- > justifyLeft 7 'x' "foo"    == "fooxxxx"
-- > justifyLeft 3 'x' "foobar" == "foobar"
justifyLeft :: Int -> Char -> JSString -> JSString
justifyLeft k c (JSString t) = JSString $ T.justifyLeft k c t
{-# INLINE [1] justifyLeft #-}

-- | /O(n)/ Right-justify a string to the given length, using the
-- specified fill character on the left.  Performs replacement on
-- invalid scalar values.
--
-- Examples:
--
-- > justifyRight 7 'x' "bar"    == "xxxxbar"
-- > justifyRight 3 'x' "foobar" == "foobar"
justifyRight :: Int -> Char -> JSString -> JSString
justifyRight k c (JSString t) = JSString $ T.justifyRight k c t
{-# INLINE justifyRight #-}

-- | /O(n)/ Center a string to the given length, using the specified
-- fill character on either side.  Performs replacement on invalid
-- scalar values.
--
-- Examples:
--
-- > center 8 'x' "HS" = "xxxHSxxx"
center :: Int -> Char -> JSString -> JSString
center k c (JSString t) = JSString $ T.center k c t
{-# INLINE center #-}

-- | /O(n)/ The 'transpose' function transposes the rows and columns
-- of its 'JSString' argument.  Note that this function uses 'pack',
-- 'unpack', and the list version of transpose, and is thus not very
-- efficient.
transpose :: [JSString] -> [JSString]
transpose ts = JSString <$> T.transpose (getJSString <$> ts)

-- -----------------------------------------------------------------------------
-- * Reducing 'JSString's (folds)

-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
-- (typically the left-identity of the operator), and a 'JSString',
-- reduces the 'JSString' using the binary operator, from left to right.
-- Subject to fusion.
foldl :: (a -> Char -> a) -> a -> JSString -> a
foldl f z (JSString t) = T.foldl f z t
{-# INLINE foldl #-}

-- | /O(n)/ A strict version of 'foldl'.  Subject to fusion.
foldl' :: (a -> Char -> a) -> a -> JSString -> a
foldl' f z (JSString t) = T.foldl' f z t
{-# INLINE foldl' #-}

-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
-- and thus must be applied to a non-empty 'JSString'.  Subject to fusion.
foldl1 :: (Char -> Char -> Char) -> JSString -> Char
foldl1 f (JSString t) = T.foldl1 f t
{-# INLINE foldl1 #-}

-- | /O(n)/ A strict version of 'foldl1'.  Subject to fusion.
foldl1' :: (Char -> Char -> Char) -> JSString -> Char
foldl1' f (JSString t) = T.foldl1' f t
{-# INLINE foldl1' #-}

-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
-- (typically the right-identity of the operator), and a 'JSString',
-- reduces the 'JSString' using the binary operator, from right to left.
-- Subject to fusion.
foldr :: (Char -> a -> a) -> a -> JSString -> a
foldr f z (JSString t) = T.foldr f z t
{-# INLINE foldr #-}

-- | /O(n)/ A variant of 'foldr' that has no starting value argument,
-- and thus must be applied to a non-empty 'JSString'.  Subject to
-- fusion.
foldr1 :: (Char -> Char -> Char) -> JSString -> Char
foldr1 f (JSString t) = T.foldr1 f t
{-# INLINE foldr1 #-}

-- -----------------------------------------------------------------------------
-- ** Special folds

-- | /O(n)/ Concatenate a list of 'JSString's.
concat :: [JSString] -> JSString
concat xs = JSString $ T.concat $ getJSString <$> xs

-- | /O(n)/ Map a function over a 'JSString' that results in a 'JSString', and
-- concatenate the results.
concatMap :: (Char -> JSString) -> JSString -> JSString
concatMap f (JSString t) = JSString $ T.concatMap (getJSString . f) t
{-# INLINE concatMap #-}

-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
-- 'JSString' @t@ satisifes the predicate @p@. Subject to fusion.
any :: (Char -> Bool) -> JSString -> Bool
any p (JSString t) = T.any p t
{-# INLINE any #-}

-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
-- 'JSString' @t@ satisify the predicate @p@. Subject to fusion.
all :: (Char -> Bool) -> JSString -> Bool
all p (JSString t) = T.all p t
{-# INLINE all #-}

-- | /O(n)/ 'maximum' returns the maximum value from a 'JSString', which
-- must be non-empty. Subject to fusion.
maximum :: JSString -> Char
maximum (JSString t) = T.maximum t
{-# INLINE maximum #-}

-- | /O(n)/ 'minimum' returns the minimum value from a 'JSString', which
-- must be non-empty. Subject to fusion.
minimum :: JSString -> Char
minimum (JSString t) = T.minimum t
{-# INLINE minimum #-}

-- -----------------------------------------------------------------------------
-- * Building 'JSString's

-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
-- successive reduced values from the left. Subject to fusion.
-- Performs replacement on invalid scalar values.
--
-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
--
-- Note that
--
-- > last (scanl f z xs) == foldl f z xs.
scanl :: (Char -> Char -> Char) -> Char -> JSString -> JSString
scanl f z (JSString t) = JSString $ T.scanl f z t
{-# INLINE scanl #-}

-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
-- value argument.  Subject to fusion.  Performs replacement on
-- invalid scalar values.
--
-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
scanl1 :: (Char -> Char -> Char) -> JSString -> JSString
scanl1 f (JSString x) = JSString $ T.scanl1 f x
{-# INLINE scanl1 #-}

-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'.  Performs
-- replacement on invalid scalar values.
--
-- > scanr f v == reverse . scanl (flip f) v . reverse
scanr :: (Char -> Char -> Char) -> Char -> JSString -> JSString
scanr f c (JSString z) = JSString $ T.scanr f c z
{-# INLINE scanr #-}

-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
-- value argument.  Subject to fusion.  Performs replacement on
-- invalid scalar values.
scanr1 :: (Char -> Char -> Char) -> JSString -> JSString
scanr1 f (JSString t) = JSString $ T.scanr1 f t
{-# INLINE scanr1 #-}

-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
-- function to each element of a 'JSString', passing an accumulating
-- parameter from left to right, and returns a final 'JSString'.  Performs
-- replacement on invalid scalar values.
mapAccumL :: (a -> Char -> (a,Char)) -> a -> JSString -> (a, JSString)
mapAccumL f z (JSString t) = JSString <$> T.mapAccumL f z t
{-# INLINE mapAccumL #-}

-- | The 'mapAccumR' function behaves like a combination of 'map' and
-- a strict 'foldr'; it applies a function to each element of a
-- 'JSString', passing an accumulating parameter from right to left, and
-- returning a final value of this accumulator together with the new
-- 'JSString'.
-- Performs replacement on invalid scalar values.
mapAccumR :: (a -> Char -> (a,Char)) -> a -> JSString -> (a, JSString)
mapAccumR f z (JSString t) = JSString <$> T.mapAccumR f z t
{-# INLINE mapAccumR #-}

-- -----------------------------------------------------------------------------
-- ** Generating and unfolding 'JSString's

-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'JSString' consisting of the input
-- @t@ repeated @n@ times.
replicate :: Int -> JSString -> JSString
replicate n (JSString t) = JSString $ T.replicate n t
{-# INLINE [1] replicate #-}

-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
-- 'JSString' from a seed value. The function takes the element and
-- returns 'Nothing' if it is done producing the 'JSString', otherwise
-- 'Just' @(a,b)@.  In this case, @a@ is the next 'Char' in the
-- string, and @b@ is the seed value for further production. Subject
-- to fusion.  Performs replacement on invalid scalar values.
unfoldr     :: (a -> Maybe (Char,a)) -> a -> JSString
unfoldr f s = JSString $ T.unfoldr f s
{-# INLINE unfoldr #-}

-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a 'JSString' from a seed
-- value. However, the length of the result should be limited by the
-- first argument to 'unfoldrN'. This function is more efficient than
-- 'unfoldr' when the maximum length of the result is known and
-- correct, otherwise its performance is similar to 'unfoldr'. Subject
-- to fusion.  Performs replacement on invalid scalar values.
unfoldrN     :: Int -> (a -> Maybe (Char,a)) -> a -> JSString
unfoldrN n f s = JSString $ T.unfoldrN n f s
{-# INLINE unfoldrN #-}

-- -----------------------------------------------------------------------------
-- * Substrings

-- | /O(n)/ 'take' @n@, applied to a 'JSString', returns the prefix of the
-- 'JSString' of length @n@, or the 'JSString' itself if @n@ is greater than
-- the length of the JSString. Subject to fusion.
take :: Int -> JSString -> JSString
take n (JSString t) = JSString $ T.take n t
{-           t@(Text arr off len)
    | n <= 0    = empty
    | n >= len  = t
    | otherwise = text arr off (iterN n t) -}
{-# INLINE [1] take #-}
{-
iterN :: Int -> JSString -> Int
iterN n t@(Text _arr _off len) = loop 0 0
  where loop !i !cnt
            | i >= len || cnt >= n = i
            | otherwise            = loop (i+d) (cnt+1)
          where d = iter_ t i
-}

-- | /O(n)/ 'takeEnd' @n@ @t@ returns the suffix remaining after
-- taking @n@ characters from the end of @t@.
--
-- Examples:
--
-- > takeEnd 3 "foobar" == "bar"
takeEnd :: Int -> JSString -> JSString
takeEnd n (JSString t) = JSString $ T.takeEnd n t
{-
iterNEnd :: Int -> JSString -> Int
iterNEnd n t@(Text _arr _off len) = loop (len-1) n
  where loop i !m
          | i <= 0    = 0
          | m <= 1    = i
          | otherwise = loop (i+d) (m-1)
          where d = reverseIter_ t i
-}
-- | /O(n)/ 'drop' @n@, applied to a 'JSString', returns the suffix of the
-- 'JSString' after the first @n@ characters, or the empty 'JSString' if @n@
-- is greater than the length of the 'JSString'. Subject to fusion.
drop :: Int -> JSString -> JSString
drop n (JSString t) = JSString $ T.drop n t
{-# INLINE [1] drop #-}

-- | /O(n)/ 'dropEnd' @n@ @t@ returns the prefix remaining after
-- dropping @n@ characters from the end of @t@.
--
-- Examples:
--
-- > dropEnd 3 "foobar" == "foo"
dropEnd :: Int -> JSString -> JSString
dropEnd n (JSString x) = JSString $ T.dropEnd n x

-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'JSString',
-- returns the longest prefix (possibly empty) of elements that
-- satisfy @p@.  Subject to fusion.
takeWhile :: (Char -> Bool) -> JSString -> JSString
takeWhile p (JSString x) = JSString $ T.takeWhile p x
{-# INLINE [1] takeWhile #-}

-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
-- 'takeWhile' @p@ @t@. Subject to fusion.
dropWhile :: (Char -> Bool) -> JSString -> JSString
dropWhile p (JSString x) = JSString $ T.dropWhile p x
{-# INLINE [1] dropWhile #-}

-- | /O(n)/ 'dropWhileEnd' @p@ @t@ returns the prefix remaining after
-- dropping characters that fail the predicate @p@ from the end of
-- @t@.  Subject to fusion.
-- Examples:
--
-- > dropWhileEnd (=='.') "foo..." == "foo"
dropWhileEnd :: (Char -> Bool) -> JSString -> JSString
dropWhileEnd p (JSString x) = JSString $ T.dropWhileEnd p x
{-# INLINE [1] dropWhileEnd #-}

-- | /O(n)/ 'dropAround' @p@ @t@ returns the substring remaining after
-- dropping characters that fail the predicate @p@ from both the
-- beginning and end of @t@.  Subject to fusion.
dropAround :: (Char -> Bool) -> JSString -> JSString
dropAround p (JSString t) = JSString $ T.dropAround p t
{-# INLINE [1] dropAround #-}

-- | /O(n)/ Remove leading white space from a string.  Equivalent to:
--
-- > dropWhile isSpace
stripStart :: JSString -> JSString
stripStart = JSString . T.stripStart . getJSString
{-# INLINE [1] stripStart #-}

-- | /O(n)/ Remove trailing white space from a string.  Equivalent to:
--
-- > dropWhileEnd isSpace
stripEnd :: JSString -> JSString
stripEnd = JSString . T.stripEnd . getJSString
{-# INLINE [1] stripEnd #-}

-- | /O(n)/ Remove leading and trailing white space from a string.
-- Equivalent to:
--
-- > dropAround isSpace
strip :: JSString -> JSString
strip = JSString . T.strip . getJSString
{-# INLINE [1] strip #-}

-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
-- prefix of @t@ of length @n@, and whose second is the remainder of
-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
splitAt :: Int -> JSString -> (JSString, JSString)
splitAt n (JSString t) = let (x, y) = T.splitAt n t in (JSString x, JSString y)
{-# INLINE splitAt #-}

-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
-- a pair whose first element is the longest prefix (possibly empty)
-- of @t@ of elements that satisfy @p@, and whose second is the
-- remainder of the list.
span :: (Char -> Bool) -> JSString -> (JSString, JSString)
span p (JSString t) = let (x, y) = T.span p t in (JSString x, JSString y)
{-# INLINE span #-}

-- | /O(n)/ 'break' is like 'span', but the prefix returned is
-- over elements that fail the predicate @p@.
break :: (Char -> Bool) -> JSString -> (JSString, JSString)
break p (JSString t) = let (x, y) = T.break p t in (JSString x, JSString y)
{-# INLINE break #-}

-- | /O(n)/ Group characters in a string according to a predicate.
groupBy :: (Char -> Char -> Bool) -> JSString -> [JSString]
groupBy p (JSString x) = JSString <$> T.groupBy p x

-- | /O(n)/ Group characters in a string by equality.
group :: JSString -> [JSString]
group (JSString x) = JSString <$> T.group x
{-# INLINE group #-}

group' :: JSString -> [JSString]
group' = group
{-# INLINE group' #-}

-- | /O(n^2)/ Return all initial segments of the given 'JSString', shortest
-- first.
inits :: JSString -> [JSString]
inits (JSString x) = JSString <$> T.inits x

-- | /O(n^2)/ Return all final segments of the given 'JSString', longest
-- first.
tails :: JSString -> [JSString]
tails (JSString x) = JSString <$> T.tails x

-- $split
--
-- Splitting functions in this library do not perform character-wise
-- copies to create substrings; they just construct new 'JSString's that
-- are slices of the original.

-- | /O(m+n)/ Break a 'JSString' into pieces separated by the first 'JSString'
-- argument (which cannot be empty), consuming the delimiter. An empty
-- delimiter is invalid, and will cause an error to be raised.
--
-- Examples:
--
-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
-- > splitOn "aaa"  "aaaXaaaXaaaXaaa"  == ["","X","X","X",""]
-- > splitOn "x"    "x"                == ["",""]
--
-- and
--
-- > intercalate s . splitOn s         == id
-- > splitOn (singleton c)             == split (==c)
--
-- (Note: the string @s@ to split on above cannot be empty.)
--
-- In (unlikely) bad cases, this function's time complexity degrades
-- towards /O(n*m)/.
splitOn :: JSString
        -- ^ String to split on. If this string is empty, an error
        -- will occur.
        -> JSString
        -- ^ Input text.
        -> [JSString]
splitOn (JSString pat) (JSString src) = JSString <$> T.splitOn pat src
{-# INLINE [1] splitOn #-}

splitOn' :: JSString
         -- ^ String to split on. If this string is empty, an error
         -- will occur.
         -> JSString
         -- ^ Input text.
         -> [JSString]
splitOn' = splitOn
{-# NOINLINE splitOn' #-}
--- {-# INLINE [1] splitOn' #-}

-- | /O(n)/ Splits a 'JSString' into components delimited by separators,
-- where the predicate returns True for a separator element.  The
-- resulting components do not contain the separators.  Two adjacent
-- separators result in an empty component in the output.  eg.
--
-- > split (=='a') "aabbaca" == ["","","bb","c",""]
-- > split (=='a') ""        == [""]
split :: (Char -> Bool) -> JSString -> [JSString]
split p (JSString x) = JSString <$> T.split p x
{-# INLINE split #-}

-- | /O(n)/ Splits a 'JSString' into components of length @k@.  The last
-- element may be shorter than the other chunks, depending on the
-- length of the input. Examples:
--
-- > chunksOf 3 "foobarbaz"   == ["foo","bar","baz"]
-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
chunksOf :: Int -> JSString -> [JSString]
chunksOf n (JSString x) = JSString <$> T.chunksOf n x
{-# INLINE chunksOf #-}

-- | /O(n)/ Splits a 'JSString' into components of length @k@.  The last
-- element may be shorter than the other chunks, depending on the
-- length of the input. Examples:
--
-- > chunksOf 3 "foobarbaz"   == ["foo","bar","baz"]
-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
chunksOf' :: Int -> JSString -> [JSString]
chunksOf' = chunksOf
{-# INLINE chunksOf' #-}

-- ----------------------------------------------------------------------------
-- * Searching

-------------------------------------------------------------------------------
-- ** Searching with a predicate

-- | /O(n)/ The 'find' function takes a predicate and a 'JSString', and
-- returns the first element matching the predicate, or 'Nothing' if
-- there is no such element.
find :: (Char -> Bool) -> JSString -> Maybe Char
find p (JSString t) = T.find p t
{-# INLINE find #-}

-- | /O(n)/ The 'partition' function takes a predicate and a 'JSString',
-- and returns the pair of 'JSString's with elements which do and do not
-- satisfy the predicate, respectively; i.e.
--
-- > partition p t == (filter p t, filter (not . p) t)
partition :: (Char -> Bool) -> JSString -> (JSString, JSString)
partition p (JSString t) = let (x, y) = T.partition p t in (JSString x, JSString y)
{-# INLINE partition #-}

-- | /O(n)/ 'filter', applied to a predicate and a 'JSString',
-- returns a 'JSString' containing those characters that satisfy the
-- predicate.
filter :: (Char -> Bool) -> JSString -> JSString
filter p (JSString t) = JSString $ T.filter p t
{-# INLINE filter #-}

-- | /O(n+m)/ Find the first instance of @needle@ (which must be
-- non-'null') in @haystack@.  The first element of the returned tuple
-- is the prefix of @haystack@ before @needle@ is matched.  The second
-- is the remainder of @haystack@, starting with the match.
--
-- Examples:
--
-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
-- > breakOn "/" "foobar"   ==> ("foobar", "")
--
-- Laws:
--
-- > append prefix match == haystack
-- >   where (prefix, match) = breakOn needle haystack
--
-- If you need to break a string by a substring repeatedly (e.g. you
-- want to break on every instance of a substring), use 'breakOnAll'
-- instead, as it has lower startup overhead.
--
-- In (unlikely) bad cases, this function's time complexity degrades
-- towards /O(n*m)/.
breakOn :: JSString -> JSString -> (JSString, JSString)
breakOn (JSString pat) (JSString src) = let (x, y) = T.breakOn pat src in (JSString x, JSString y)
{-# INLINE breakOn #-}

-- | /O(n+m)/ Similar to 'breakOn', but searches from the end of the
-- string.
--
-- The first element of the returned tuple is the prefix of @haystack@
-- up to and including the last match of @needle@.  The second is the
-- remainder of @haystack@, following the match.
--
-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
breakOnEnd :: JSString -> JSString -> (JSString, JSString)
breakOnEnd (JSString pat) (JSString src) = let (x, y) = T.breakOnEnd pat src in (JSString x, JSString y)
{-# INLINE breakOnEnd #-}

-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
-- @haystack@.  Each element of the returned list consists of a pair:
--
-- * The entire string prior to the /k/th match (i.e. the prefix)
--
-- * The /k/th match, followed by the remainder of the string
--
-- Examples:
--
-- > breakOnAll "::" ""
-- > ==> []
-- > breakOnAll "/" "a/b/c/"
-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
--
-- In (unlikely) bad cases, this function's time complexity degrades
-- towards /O(n*m)/.
--
-- The @needle@ parameter may not be empty.
breakOnAll :: JSString              -- ^ @needle@ to search for
           -> JSString              -- ^ @haystack@ in which to search
           -> [(JSString, JSString)]
breakOnAll (JSString pat) (JSString src) = (\(x, y) -> (JSString x, JSString y)) <$> T.breakOnAll pat src
{-# INLINE breakOnAll #-}

breakOnAll' :: JSString              -- ^ @needle@ to search for
            -> JSString              -- ^ @haystack@ in which to search
            -> [(JSString, JSString)]
breakOnAll' = breakOnAll
{-# INLINE breakOnAll' #-}

-------------------------------------------------------------------------------
-- ** Indexing 'JSString's

-- $index
--
-- If you think of a 'JSString' value as an array of 'Char' values (which
-- it is not), you run the risk of writing inefficient code.
--
-- An idiom that is common in some languages is to find the numeric
-- offset of a character or substring, then use that number to split
-- or trim the searched string.  With a 'JSString' value, this approach
-- would require two /O(n)/ operations: one to perform the search, and
-- one to operate from wherever the search ended.
--
-- For example, suppose you have a string that you want to split on
-- the substring @\"::\"@, such as @\"foo::bar::quux\"@. Instead of
-- searching for the index of @\"::\"@ and taking the substrings
-- before and after that index, you would instead use @breakOnAll \"::\"@.

-- | /O(n)/ 'JSString' index (subscript) operator, starting from 0.
index :: JSString -> Int -> Char
index (JSString t) n = T.index t n
{-# INLINE index #-}

-- | /O(n)/ The 'findIndex' function takes a predicate and a 'JSString'
-- and returns the index of the first element in the 'JSString' satisfying
-- the predicate. Subject to fusion.
findIndex :: (Char -> Bool) -> JSString -> Maybe Int
findIndex p (JSString t) = T.findIndex p t
{-# INLINE findIndex #-}

-- | /O(n+m)/ The 'count' function returns the number of times the
-- query string appears in the given 'JSString'. An empty query string is
-- invalid, and will cause an error to be raised.
--
-- In (unlikely) bad cases, this function's time complexity degrades
-- towards /O(n*m)/.
count :: JSString -> JSString -> Int
count (JSString pat) (JSString src) = T.count pat src
{-# INLINE [1] count #-}

-------------------------------------------------------------------------------
-- * Zipping

-- | /O(n)/ 'zip' takes two 'JSString's and returns a list of
-- corresponding pairs of bytes. If one input 'JSString' is short,
-- excess elements of the longer 'JSString' are discarded. This is
-- equivalent to a pair of 'unpack' operations.
zip :: JSString -> JSString -> [(Char,Char)]
zip (JSString a) (JSString b) = T.zip a b
{-# INLINE [0] zip #-}

-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
-- given as the first argument, instead of a tupling function.
-- Performs replacement on invalid scalar values.
zipWith :: (Char -> Char -> Char) -> JSString -> JSString -> JSString
zipWith f (JSString t1) (JSString t2) = JSString $ T.zipWith f t1 t2
{-# INLINE [0] zipWith #-}

-- | /O(n)/ Breaks a 'JSString' up into a list of words, delimited by 'Char's
-- representing white space.
words :: JSString -> [JSString]
words (JSString x) = JSString <$> T.words x
{-# INLINE words #-}

-- fixme: strict words' that allocates the whole list in one go
words' :: JSString -> [JSString]
words' = words
{-# INLINE words' #-}

-- | /O(n)/ Breaks a 'JSString' up into a list of 'JSString's at
-- newline 'Char's. The resulting strings do not contain newlines.
lines :: JSString -> [JSString]
lines (JSString ps) = JSString <$> T.lines ps
{-# INLINE lines #-}

lines' :: JSString -> [JSString]
lines' = lines
{-# INLINE lines' #-}

{-
-- | /O(n)/ Portably breaks a 'JSString' up into a list of 'JSString's at line
-- boundaries.
--
-- A line boundary is considered to be either a line feed, a carriage
-- return immediately followed by a line feed, or a carriage return.
-- This accounts for both Unix and Windows line ending conventions,
-- and for the old convention used on Mac OS 9 and earlier.
lines' :: Text -> [Text]
lines' ps | null ps   = []
          | otherwise = h : case uncons t of
                              Nothing -> []
                              Just (c,t')
                                  | c == '\n' -> lines t'
                                  | c == '\r' -> case uncons t' of
                                                   Just ('\n',t'') -> lines t''
                                                   _               -> lines t'
    where (h,t)    = span notEOL ps
          notEOL c = c /= '\n' && c /= '\r'

-}

-- | /O(n)/ Joins lines, after appending a terminating newline to
-- each.
unlines :: [JSString] -> JSString
unlines xs = JSString $ T.unlines $ getJSString <$> xs
{-# INLINE unlines #-}

-- | /O(n)/ Joins words using single space characters.
unwords :: [JSString] -> JSString
unwords xs = JSString $ T.unwords $ getJSString <$> xs
{-# INLINE unwords #-}

-- | /O(n)/ The 'isPrefixOf' function takes two 'JSString's and returns
-- 'True' iff the first is a prefix of the second.  Subject to fusion.
isPrefixOf :: JSString -> JSString -> Bool
isPrefixOf (JSString x) (JSString y) = T.isPrefixOf x y
{-# INLINE [1] isPrefixOf #-}

-- | /O(n)/ The 'isSuffixOf' function takes two 'JSString's and returns
-- 'True' iff the first is a suffix of the second.
isSuffixOf :: JSString -> JSString -> Bool
isSuffixOf (JSString x) (JSString y) = T.isSuffixOf x y
{-# INLINE isSuffixOf #-}

-- | The 'isInfixOf' function takes two 'JSString's and returns
-- 'True' iff the first is contained, wholly and intact, anywhere
-- within the second.
--
-- Complexity depends on how the JavaScript engine implements
-- String.prototype.find.
isInfixOf :: JSString -> JSString -> Bool
isInfixOf (JSString needle) (JSString haystack) = T.isInfixOf needle haystack
{-# INLINE [1] isInfixOf #-}

-------------------------------------------------------------------------------
-- * View patterns

-- | /O(n)/ Return the suffix of the second string if its prefix
-- matches the entire first string.
--
-- Examples:
--
-- > stripPrefix "foo" "foobar" == Just "bar"
-- > stripPrefix ""    "baz"    == Just "baz"
-- > stripPrefix "foo" "quux"   == Nothing
--
-- This is particularly useful with the @ViewPatterns@ extension to
-- GHC, as follows:
--
-- > {-# LANGUAGE ViewPatterns #-}
-- > import Data.Text as T
-- >
-- > fnordLength :: JSString -> Int
-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
-- > fnordLength _                                 = -1
stripPrefix :: JSString -> JSString -> Maybe JSString
stripPrefix (JSString x) (JSString y) = JSString <$> T.stripPrefix x y
{-# INLINE stripPrefix #-}

-- | /O(n)/ Find the longest non-empty common prefix of two strings
-- and return it, along with the suffixes of each string at which they
-- no longer match.
--
-- If the strings do not have a common prefix or either one is empty,
-- this function returns 'Nothing'.
--
-- Examples:
--
-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
-- > commonPrefixes "veeble" "fetzer"  == Nothing
-- > commonPrefixes "" "baz"           == Nothing
commonPrefixes :: JSString -> JSString -> Maybe (JSString,JSString,JSString)
commonPrefixes (JSString x) (JSString y) = (\(a, b, c) -> (JSString a, JSString b, JSString c)) <$> T.commonPrefixes x y
{-# INLINE commonPrefixes #-}

-- | /O(n)/ Return the prefix of the second string if its suffix
-- matches the entire first string.
--
-- Examples:
--
-- > stripSuffix "bar" "foobar" == Just "foo"
-- > stripSuffix ""    "baz"    == Just "baz"
-- > stripSuffix "foo" "quux"   == Nothing
--
-- This is particularly useful with the @ViewPatterns@ extension to
-- GHC, as follows:
--
-- > {-# LANGUAGE ViewPatterns #-}
-- > import Data.Text as T
-- >
-- > quuxLength :: Text -> Int
-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
-- > quuxLength _                                = -1
stripSuffix :: JSString -> JSString -> Maybe JSString
stripSuffix (JSString x) (JSString y) = JSString <$> T.stripSuffix x y
{-# INLINE stripSuffix #-}

-- -----------------------------------------------------------------------------