-- | Unitary formats.
module Data.Repa.Convert.Format.Unit
        ( UnitAsc       (..))
where
import Data.Repa.Convert.Internal.Format
import Data.Repa.Convert.Internal.Packable
import Data.Repa.Convert.Format.String
import GHC.Exts
import Data.Word
import Prelude hiding (fail)
#include "repa-convert.h"


------------------------------------------------------------------------------------------- UnitAsc
-- | A particular ASCII string.
data UnitAsc    = UnitAsc String        deriving (UnitAsc -> UnitAsc -> Bool
(UnitAsc -> UnitAsc -> Bool)
-> (UnitAsc -> UnitAsc -> Bool) -> Eq UnitAsc
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnitAsc -> UnitAsc -> Bool
== :: UnitAsc -> UnitAsc -> Bool
$c/= :: UnitAsc -> UnitAsc -> Bool
/= :: UnitAsc -> UnitAsc -> Bool
Eq, Int -> UnitAsc -> ShowS
[UnitAsc] -> ShowS
UnitAsc -> String
(Int -> UnitAsc -> ShowS)
-> (UnitAsc -> String) -> ([UnitAsc] -> ShowS) -> Show UnitAsc
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnitAsc -> ShowS
showsPrec :: Int -> UnitAsc -> ShowS
$cshow :: UnitAsc -> String
show :: UnitAsc -> String
$cshowList :: [UnitAsc] -> ShowS
showList :: [UnitAsc] -> ShowS
Show)
instance Format UnitAsc                 where
 type Value UnitAsc        = ()
 fieldCount :: UnitAsc -> Int
fieldCount UnitAsc
_              = Int
1
 minSize :: UnitAsc -> Int
minSize    (UnitAsc String
s)    = String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
 fixedSize :: UnitAsc -> Maybe Int
fixedSize  (UnitAsc String
s)    = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
 packedSize :: UnitAsc -> Value UnitAsc -> Maybe Int
packedSize (UnitAsc String
s) () = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
 {-# INLINE fieldCount #-}
 {-# INLINE minSize    #-}
 {-# INLINE fixedSize  #-}
 {-# INLINE packedSize #-}


instance Packable UnitAsc where
 packer :: UnitAsc
-> Value UnitAsc -> Addr# -> IO () -> (Addr# -> IO ()) -> IO ()
packer    (UnitAsc String
s)              () Addr#
start IO ()
k
  = FixChars
-> Value FixChars -> Addr# -> IO () -> (Addr# -> IO ()) -> IO ()
forall format.
Packable format =>
format
-> Value format -> Addr# -> IO () -> (Addr# -> IO ()) -> IO ()
packer (Int -> FixChars
FixChars (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s)) String
Value FixChars
s  Addr#
start IO ()
k
 {-# INLINE packer #-}


instance Unpackable UnitAsc where
 unpacker :: UnitAsc
-> Addr#
-> Addr#
-> (Word8 -> Bool)
-> IO ()
-> (Addr# -> Value UnitAsc -> IO ())
-> IO ()
unpacker  (UnitAsc String
str) Addr#
start Addr#
end Word8 -> Bool
stop IO ()
fail Addr# -> Value UnitAsc -> IO ()
eat
  = do  (Ptr Addr#
ptr, String
str') <- Ptr Word8 -> Ptr Word8 -> (Word8 -> Bool) -> IO (Ptr Word8, String)
unpackCharList (Addr# -> Ptr Word8
pw8 Addr#
start) (Addr# -> Ptr Word8
pw8 Addr#
end) Word8 -> Bool
stop
        if String
str String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
str'
         then Addr# -> Value UnitAsc -> IO ()
eat Addr#
ptr ()
         else IO ()
fail
 {-# NOINLINE unpacker #-}



pw8 :: Addr# -> Ptr Word8
pw8 :: Addr# -> Ptr Word8
pw8 Addr#
addr = Addr# -> Ptr Word8
forall a. Addr# -> Ptr a
Ptr Addr#
addr
{-# INLINE pw8 #-}