@@ -1,4 +1,6 @@
1−{-# LANGUAGE OverloadedStrings, MagicHash, BangPatterns #-}
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE MagicHash #-}
3+{-# LANGUAGE BangPatterns #-}
24
35-- Copyright : Erik de Castro Lopo <erikd@mega-nerd.com>
46-- License : BSD3
@@ -12,11 +14,11 @@ module Network.Wai.Handler.Warp.ReadInt (
1214-- poorly with the CPP pragma.
1315
1416import Data.ByteString (ByteString)
15−import qualified Data.ByteString.Char8 as B
16−import qualified Data.Char as C
17+import qualified Data.ByteString as S
1718import Data.Int (Int64)
1819import GHC.Prim
1920import GHC.Types
21+import GHC.Word
2022
2123{-# INLINE readInt #-}
2224readInt :: Integral a => ByteString -> a
@@ -32,15 +34,14 @@ readInt bs = fromIntegral $ readInt64 bs
3234
3335{- NOINLINE readInt64MH #-}
3436readInt64 :: ByteString -> Int64
35−readInt64 bs =
36− B.foldl' (\i c -> i * 10 + fromIntegral (mhDigitToInt c)) 0
37− $ B.takeWhile C.isDigit bs
37+readInt64 bs = S.foldl' (\ !i !c -> i * 10 + fromIntegral (mhDigitToInt c)) 0
38+ $ S.takeWhile isDigit bs
3839
3940data Table = Table !Addr#
4041
4142{- NOINLINE mhDigitToInt #-}
42−mhDigitToInt :: Char -> Int
43−mhDigitToInt (C# i) = I# (word2Int# (indexWord8OffAddr# addr (ord# i)))
43+mhDigitToInt :: Word8 -> Int
44+mhDigitToInt (W8# i) = I# (word2Int# (indexWord8OffAddr# addr (word2Int# i)))
4445 where
4546 !(Table addr) = table
4647 table :: Table
@@ -62,3 +63,5 @@ mhDigitToInt (C# i) = I# (word2Int# (indexWord8OffAddr# addr (ord# i)))
6263 \\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
6364 \\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"#
6465
66+isDigit :: Word8 -> Bool
67+isDigit w = w >= 48 && w <= 57