Commit a243fa79 authored by ian@well-typed.com's avatar ian@well-typed.com
Browse files

Use ByteString rather than FastBytes in Binary

parent 80a3a156
......@@ -74,7 +74,9 @@ import BasicTypes
import Foreign
import Data.Array
import qualified Data.ByteString.Unsafe as BS
import Data.ByteString (ByteString)
import qualified Data.ByteString.Internal as BS
import qualified Data.ByteString.Unsafe as BS
import Data.IORef
import Data.Char ( ord, chr )
import Data.Time
......@@ -714,14 +716,14 @@ type SymbolTable = Array Int Name
---------------------------------------------------------
putFS :: BinHandle -> FastString -> IO ()
putFS bh fs = putFB bh $ fastStringToFastBytes fs
putFS bh fs = putBS bh $ fastStringToFastBytes fs
getFS :: BinHandle -> IO FastString
getFS bh = do fb <- getFB bh
mkFastStringFastBytes fb
getFS bh = do bs <- getBS bh
mkFastStringFastBytes bs
putFB :: BinHandle -> FastBytes -> IO ()
putFB bh bs =
putBS :: BinHandle -> ByteString -> IO ()
putBS bh bs =
BS.unsafeUseAsCStringLen bs $ \(ptr, l) -> do
put_ bh l
let
......@@ -733,19 +735,19 @@ putFB bh bs =
go 0
{- -- possible faster version, not quite there yet:
getFB bh@BinMem{} = do
getBS bh@BinMem{} = do
(I# l) <- get bh
arr <- readIORef (arr_r bh)
off <- readFastMutInt (off_r bh)
return $! (mkFastSubBytesBA# arr off l)
-}
getFB :: BinHandle -> IO FastBytes
getFB bh = do
getBS :: BinHandle -> IO ByteString
getBS bh = do
l <- get bh
fp <- mallocForeignPtrBytes l
withForeignPtr fp $ \ptr -> do
let
go n | n == l = return $ foreignPtrToFastBytes fp l
go n | n == l = return $ BS.fromForeignPtr fp 0 l
| otherwise = do
b <- getByte bh
pokeElemOff ptr n b
......@@ -753,9 +755,9 @@ getFB bh = do
--
go 0
instance Binary FastBytes where
put_ bh f = putFB bh f
get bh = getFB bh
instance Binary ByteString where
put_ bh f = putBS bh f
get bh = getBS bh
instance Binary FastString where
put_ bh f =
......
......@@ -29,7 +29,6 @@ module FastString
-- * FastBytes
FastBytes,
mkFastStringFastBytes,
foreignPtrToFastBytes,
fastStringToFastBytes,
fastZStringToByteString,
unsafeMkFastBytesString,
......@@ -132,9 +131,6 @@ import GHC.Base ( unpackCString# )
type FastBytes = ByteString
foreignPtrToFastBytes :: ForeignPtr Word8 -> Int -> FastBytes
foreignPtrToFastBytes fp len = BS.fromForeignPtr fp 0 len
mkFastStringFastBytes :: FastBytes -> IO FastString
mkFastStringFastBytes bs = mkFastStringByteString bs
......@@ -146,21 +142,7 @@ fastZStringToByteString (FastZString bs) = bs
-- This will drop information if any character > '\xFF'
unsafeMkFastBytesString :: String -> FastBytes
unsafeMkFastBytesString str =
inlinePerformIO $ do
let l = Prelude.length str
buf <- mallocForeignPtrBytes l
withForeignPtr buf $ \ptr -> do
pokeCAString (castPtr ptr) str
return $ foreignPtrToFastBytes buf l
pokeCAString :: Ptr CChar -> String -> IO ()
pokeCAString ptr str =
let
go [] !_ = return ()
go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
in
go str 0
unsafeMkFastBytesString = BSC.pack
hashByteString :: ByteString -> Int
hashByteString bs
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment