[project @ 2004-08-13 13:04:50 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / StringBuffer.lhs
index 1a7020b..61a0321 100644 (file)
@@ -8,7 +8,8 @@ Buffers for scanning string input stored in external arrays.
 \begin{code}
 module StringBuffer
        (
-        StringBuffer,
+        StringBuffer(..),
+       -- non-abstract for vs/HaskellService
 
         -- * Creation/destruction
         hGetStringBuffer,     -- :: FilePath     -> IO StringBuffer
@@ -26,6 +27,9 @@ module StringBuffer
          -- * Conversion
         lexemeToString,     -- :: StringBuffer -> Int -> String
         lexemeToFastString, -- :: StringBuffer -> Int -> FastString
+
+        -- * Parsing integers
+        parseInteger,
        ) where
 
 #include "HsVersions.h"
@@ -97,7 +101,7 @@ 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
    if (r /= size_i)
        then ioError (userError "short read of file")
@@ -173,4 +177,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}