-- * Conversion
lexemeToString, -- :: StringBuffer -> Int -> String
lexemeToFastString, -- :: StringBuffer -> Int -> FastString
+
+ -- * Parsing integers
+ parseInteger,
) where
#include "HsVersions.h"
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
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
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}