use LANGUAGE instead of OPTIONS_GHC
[ghc-base.git] / GHC / IO / Handle / Text.hs
index 0bd3550..47cc307 100644 (file)
@@ -1,8 +1,7 @@
-{-# OPTIONS_GHC -XNoImplicitPrelude -#include "HsBase.h" #-}
-{-# OPTIONS_GHC -XRecordWildCards -XBangPatterns #-}
 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
 {-# OPTIONS_GHC -fno-warn-unused-matches #-}
 {-# OPTIONS_HADDOCK hide #-}
+{-# LANGUAGE NoImplicitPrelude, RecordWildCards, BangPatterns #-}
 
 -----------------------------------------------------------------------------
 -- |
@@ -73,6 +72,7 @@ import GHC.List
 -- This operation may fail with:
 --
 --  * 'isEOFError' if the end of file has been reached.
+--
 --  * a decoding error, if the input begins with an invalid byte sequence
 --    in this Handle's encoding.
 --
@@ -641,7 +641,7 @@ commitBuffer' raw sz@(I# _) count@(I# _) flush release
                 -- just copy the data in and update bufR.
             then do withRawBuffer raw     $ \praw ->
                       copyToRawBuffer old_raw (w*charSize)
-                                      praw (fromIntegral (count*charSize))
+                                      praw (count*charSize)
                     writeIORef ref old_buf{ bufR = w + count }
                     return (emptyBuffer raw sz WriteBuffer)
 
@@ -760,7 +760,7 @@ bufWrite h_@Handle__{..} ptr count can_block =
         -- There's enough room in the buffer:
         -- just copy the data in and update bufR.
         then do debugIO ("hPutBuf: copying to buffer, w=" ++ show w)
-                copyToRawBuffer old_raw w ptr (fromIntegral count)
+                copyToRawBuffer old_raw w ptr count
                 writeIORef haByteBuffer old_buf{ bufR = w + count }
                 return count
 
@@ -835,7 +835,7 @@ bufReadNonEmpty h_@Handle__{..}
                 return (so_far + count)
            else do
   
-        copyFromRawBuffer ptr raw (fromIntegral r) (fromIntegral avail)
+        copyFromRawBuffer ptr raw r avail
         let buf' = buf{ bufR=0, bufL=0 }
         writeIORef haByteBuffer buf'
         let remaining = count - avail
@@ -860,9 +860,9 @@ bufReadEmpty h_@Handle__{..}
                 bufReadNonEmpty h_ buf' ptr so_far count
  where
   loop :: FD -> Int -> Int -> IO Int
-  loop fd off bytes | bytes <= 0 = return off
+  loop fd off bytes | bytes <= 0 = return (so_far + off)
   loop fd off bytes = do
-    r <- RawIO.read (fd::FD) (ptr `plusPtr` off) (fromIntegral bytes)
+    r <- RawIO.read (fd::FD) (ptr `plusPtr` off) bytes
     if r == 0
         then return (so_far + off)
         else loop fd (off + r) (bytes - r)
@@ -890,9 +890,9 @@ bufReadEmpty h_@Handle__{..}
 hGetBufSome :: Handle -> Ptr a -> Int -> IO Int
 hGetBufSome h ptr count
   | count == 0 = return 0
-  | count <  0 = illegalBufferSize h "hGetBuf" count
+  | count <  0 = illegalBufferSize h "hGetBufSome" count
   | otherwise =
-      wantReadableHandle_ "hGetBuf" h $ \ h_@Handle__{..} -> do
+      wantReadableHandle_ "hGetBufSome" h $ \ h_@Handle__{..} -> do
          flushCharReadBuffer h_
          buf@Buffer{ bufSize=sz } <- readIORef haByteBuffer
          if isEmptyBuffer buf
@@ -902,7 +902,10 @@ hGetBufSome h ptr count
                             if r == 0
                                then return 0
                                else do writeIORef haByteBuffer buf'
-                                       bufReadNBNonEmpty h_ buf' (castPtr ptr) 0 count
+                                       bufReadNBNonEmpty h_ buf' (castPtr ptr) 0 (min r count)
+                                        -- new count is  (min r count), so
+                                        -- that bufReadNBNonEmpty will not
+                                        -- issue another read.
             else
               bufReadNBEmpty h_ buf (castPtr ptr) 0 count
 
@@ -948,7 +951,7 @@ bufReadNBEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
 bufReadNBEmpty   h_@Handle__{..}
                  buf@Buffer{ bufRaw=raw, bufR=w, bufL=r, bufSize=sz }
                  ptr so_far count
-  | count > sz, False,
+  | count > sz,
     Just fd <- cast haDevice = do
        m <- RawIO.readNonBlocking (fd::FD) ptr count
        case m of
@@ -983,7 +986,7 @@ bufReadNBNonEmpty h_@Handle__{..}
                 return (so_far + count)
            else do
 
-        copyFromRawBuffer ptr raw (fromIntegral r) (fromIntegral avail)
+        copyFromRawBuffer ptr raw r avail
         let buf' = buf{ bufR=0, bufL=0 }
         writeIORef haByteBuffer buf'
         let remaining = count - avail