module HaskellWorks.Data.Conduit.Tokenize.Attoparsec.Offset
( Offset (..)
) where
import Control.Exception (Exception)
import Control.Monad.Trans.Resource (MonadThrow)
import qualified Data.Attoparsec.Types as A
import qualified Data.ByteString as B
import Data.Conduit
import Data.Int
import qualified Data.Text as T
import HaskellWorks.Data.Conduit.Tokenize.Attoparsec.Internal
import Prelude hiding (lines)
data Offset = Offset
{ pos :: !Int64
}
deriving (Eq, Ord)
instance Show Offset where
show (Offset c) = show c
instance Exception (ParseError Offset)
instance Show (ParseDelta Offset) where
show (ParseDelta s e) = show s ++ '-' : show e
instance AttoparsecState B.ByteString Offset where
getState = B.foldl' f (Offset 0)
where
f (Offset c) _ = Offset (c + 1)
modState x (Offset cols) = cols' `seq` Offset cols'
where
Offset dcols = getState x
cols' = cols + dcols
instance AttoparsecState T.Text Offset where
getState = T.foldl' f (Offset 0)
where
f (Offset c) _ = Offset (c + 1)
modState x (Offset cols) =
cols' `seq` Offset cols'
where
Offset dcols = getState x
cols' = cols + dcols