Added missing Data.Static

Ignore-this: 1bea98c16d73c055f608e8e2ea7b99d0

darcs-hash:20090813050306-a4fee-8fa3d698cffaff03b58cd867a29d7d08d078fd42
This commit is contained in:
Henning Guenther 2009-08-12 22:03:06 -07:00
parent 7f1a89a45a
commit 6f3c147f8d

46
Data/Static.hs Normal file
View File

@ -0,0 +1,46 @@
{-# LANGUAGE MagicHash,FlexibleInstances #-}
module Data.Static where
import GHC.Exts
import GHC.Prim
import GHC.Word
import Data.Word
import Data.Bits
import Data.Char
class StaticElement e where
extract :: Addr# -> Int# -> e
gen :: e -> [Word8]
instance StaticElement Word8 where
extract addr i = W8# (indexWord8OffAddr# addr i)
gen w = [w]
instance StaticElement Word16 where
extract addr i = W16# (indexWord16OffAddr# addr i)
gen w = let r1 = fromIntegral w
r2 = fromIntegral $ w `shiftR` 8
in [r1,r2]
instance StaticElement Word32 where
extract addr i = W32# (indexWord32OffAddr# addr i)
gen w = let r1 = fromIntegral w
r2 = fromIntegral $ w `shiftR` 8
r3 = fromIntegral $ w `shiftR` 16
r4 = fromIntegral $ w `shiftR` 24
in [r1,r2,r3,r4]
instance StaticElement Char where
extract addr i = C# (indexWideCharOffAddr# addr i)
gen c = gen (fromIntegral (ord c)::Word32)
instance StaticElement (Maybe Char) where
extract addr i = let v = indexWord32OffAddr# addr i
in if eqWord# v (int2Word# 4294967295#) -- -1 in Word32
then Nothing
else (if (I# (word2Int# v)) > 0x10FFFF
then error (show (I# (word2Int# v))++" is not a valid char ("++show (I# i)++")")
else Just (chr (I# (word2Int# v)))
)
gen Nothing = gen (-1::Word32)
gen (Just c) = gen (fromIntegral (ord c)::Word32)