[project @ 2005-07-19 16:44:50 by simonpj]
[ghc-hetmet.git] / ghc / compiler / utils / StringBuffer.lhs
index 1a7020b..e53dbc8 100644 (file)
@@ -8,9 +8,10 @@ Buffers for scanning string input stored in external arrays.
 \begin{code}
 module StringBuffer
        (
-        StringBuffer,
+        StringBuffer(..),
+       -- non-abstract for vs\/HaskellService
 
-        -- * Creation/destruction
+        -- * Creation\/destruction
         hGetStringBuffer,     -- :: FilePath     -> IO StringBuffer
        stringToStringBuffer, -- :: String       -> IO StringBuffer
 
@@ -23,9 +24,12 @@ module StringBuffer
        -- * Moving
        stepOn, stepOnBy,
 
-         -- * Conversion
+        -- * Conversion
         lexemeToString,     -- :: StringBuffer -> Int -> String
         lexemeToFastString, -- :: StringBuffer -> Int -> FastString
+
+        -- * Parsing integers
+       parseInteger,
        ) where
 
 #include "HsVersions.h"
@@ -45,7 +49,8 @@ import GHC.IOBase
 import GHC.IO          ( slurpFile )
 #endif
 
-import IO                      ( openFile, hFileSize, IOMode(ReadMode) )
+import IO                      ( openFile, hFileSize, IOMode(ReadMode),
+                                 hClose )
 #if __GLASGOW_HASKELL__ >= 601
 import System.IO               ( openBinaryFile )
 #else
@@ -97,8 +102,9 @@ hGetStringBuffer fname = do
    r <- hGetBufBA h arr size_i
 #else
    arr <- newArray_ (0,size_i-1)
-   r <- hGetArray h arr size_i
+   r <- if size_i == 0 then return 0 else hGetArray h arr size_i
 #endif
+   hClose h
    if (r /= size_i)
        then ioError (userError "short read of file")
        else do
@@ -173,4 +179,13 @@ lexemeToFastString :: StringBuffer -> Int -> FastString
 lexemeToFastString _ 0 = mkFastString ""
 lexemeToFastString (StringBuffer fo _ current#) (I# len) =
     mkFastSubStringBA# fo current# len
+
+-- -----------------------------------------------------------------------------
+-- Parsing integer strings in various bases
+
+parseInteger :: StringBuffer -> Int -> Integer -> (Char->Int) -> Integer
+parseInteger buf len radix to_int 
+  = go 0 0
+  where go i x | i == len  = x
+              | otherwise = go (i+1) (x * radix + toInteger (to_int (lookAhead buf i)))
 \end{code}