Commit 0fd3d360 authored by Tamar Christina's avatar Tamar Christina Committed by Marge Bot

winio: fixed bytestring reading interface.

parent 6f0243ae
......@@ -1017,10 +1017,13 @@ hGetBufSome h !ptr count
| otherwise =
wantReadableHandle_ "hGetBufSome" h $ \ h_@Handle__{..} -> do
flushCharReadBuffer h_
buf@Buffer{ bufSize=sz } <- readIORef haByteBuffer
buf@Buffer{ bufSize=sz, bufOffset=offset } <- readIORef haByteBuffer
if isEmptyBuffer buf
then case count > sz of -- large read? optimize it with a little special case:
True -> RawIO.read haDevice (castPtr ptr) 0 count
True -> do bytes <- RawIO.read haDevice (castPtr ptr) offset count
-- Update buffer with actual bytes written.
writeIORef haByteBuffer $! bufferAddOffset bytes buf
return bytes
_ -> do (r,buf') <- Buffered.fillReadBuffer haDevice buf
if r == 0
then return 0
......@@ -1074,7 +1077,9 @@ bufReadNBEmpty h_@Handle__{..}
m <- RawIO.readNonBlocking haDevice ptr offset count
case m of
Nothing -> return so_far
Just n -> return (so_far + n)
Just n -> do -- Update buffer with actual bytes written.
writeIORef haByteBuffer $! bufferAddOffset n buf
return (so_far + n)
| otherwise = do
-- buf <- readIORef haByteBuffer
......
......@@ -149,3 +149,4 @@ test('T17414',
high_memory_usage],
compile_and_run, [''])
test('T17510', expect_broken(17510), compile_and_run, [''])
test('bytestringread001', extra_run_opts('test.data'), compile_and_run, [''])
import System.Environment
import qualified Data.ByteString.Lazy as BL
import Data.Word
fold_tailrec :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec _ acc [] =
acc
fold_tailrec foldFun acc (x : xs) =
fold_tailrec foldFun (foldFun acc x) xs
fold_tailrec' :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec' _ acc [] =
acc
fold_tailrec' foldFun acc (x : xs) =
let acc' = foldFun acc x
in seq acc' (fold_tailrec' foldFun acc' xs)
main :: IO ()
main =
do
args <- getArgs
let filename = head args
-- generate file
let dt = replicate (65 * 1024) 'a'
writeFile filename dt
byteString <- BL.readFile filename
let wordsList = BL.unpack byteString
-- wordsList is supposed to be lazy (bufferized)
let bytesCount = fold_tailrec (\acc word -> acc + 1) 0 wordsList
print ("Total bytes in " ++ filename ++ ": "
++ (show bytesCount))
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