{-# LANGUAGE BangPatterns, CPP, Rank2Types, UnboxedTuples #-}

-- |
-- Module      : Data.Text.Internal.Private
-- Copyright   : (c) 2011 Bryan O'Sullivan
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC

module Data.Text.Internal.Private
    (
      runText
    , span_
    ) where

import Control.Monad.ST (ST, runST)
import Data.Text.Internal (Text(..), text)
import Data.Text.Unsafe (Iter(..), iter)
import qualified Data.Text.Array as A

#if defined(ASSERTS)
import GHC.Stack (HasCallStack)
#endif

span_ :: (Char -> Bool) -> Text -> (# Text, Text #)
span_ :: (Char -> Bool) -> Text -> (# Text, Text #)
span_ Char -> Bool
p t :: Text
t@(Text Array
arr Int
off Int
len) = (# Text
hd,Text
tl #)
  where hd :: Text
hd = Array -> Int -> Int -> Text
text Array
arr Int
off Int
k
        tl :: Text
tl = Array -> Int -> Int -> Text
text Array
arr (Int
offforall a. Num a => a -> a -> a
+Int
k) (Int
lenforall a. Num a => a -> a -> a
-Int
k)
        !k :: Int
k = Int -> Int
loop Int
0
        loop :: Int -> Int
loop !Int
i | Int
i forall a. Ord a => a -> a -> Bool
< Int
len Bool -> Bool -> Bool
&& Char -> Bool
p Char
c = Int -> Int
loop (Int
iforall a. Num a => a -> a -> a
+Int
d)
                | Bool
otherwise      = Int
i
            where Iter Char
c Int
d       = Text -> Int -> Iter
iter Text
t Int
i
{-# INLINE span_ #-}

runText ::
#if defined(ASSERTS)
  HasCallStack =>
#endif
  (forall s. (A.MArray s -> Int -> ST s Text) -> ST s Text) -> Text
runText :: (forall s. (MArray s -> Int -> ST s Text) -> ST s Text) -> Text
runText forall s. (MArray s -> Int -> ST s Text) -> ST s Text
act = forall a. (forall s. ST s a) -> a
runST (forall s. (MArray s -> Int -> ST s Text) -> ST s Text
act forall a b. (a -> b) -> a -> b
$ \ !MArray s
marr !Int
len -> do
                             Array
arr <- forall s. MArray s -> ST s Array
A.unsafeFreeze MArray s
marr
                             forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! Array -> Int -> Int -> Text
text Array
arr Int
0 Int
len)
{-# INLINE runText #-}