@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, BangPatterns #-}
22
33module Network.Wai.Handler.Warp.PackInt where
44
@@ -7,6 +7,11 @@ import Data.ByteString.Internal (ByteString(..), unsafeCreate)
77import Data.Word8 (Word8)
88import Foreign.Ptr (Ptr, plusPtr)
99import Foreign.Storable (poke)
10+import qualified Network.HTTP.Types as H
11+
12+-- $setup
13+-- >>> import Data.ByteString.Char8 as B
14+-- >>> import Test.QuickCheck (Large(..))
1015
1116-- |
1217--
@@ -29,3 +34,23 @@ packIntegral n = unsafeCreate len go0
2934
3035{-# SPECIALIZE packIntegral :: Int -> ByteString #-}
3136{-# SPECIALIZE packIntegral :: Integer -> ByteString #-}
37+
38+-- |
39+--
40+-- >>> packStatus H.status200
41+-- "200"
42+-- >>> packStatus H.preconditionFailed412
43+-- "412"
44+
45+packStatus :: H.Status -> ByteString
46+packStatus status = unsafeCreate 3 $ \p -> do
47+ poke p (toW8 r2)
48+ poke (p `plusPtr` 1) (toW8 r1)
49+ poke (p `plusPtr` 2) (toW8 r0)
50+ where
51+ toW8 :: Int -> Word8
52+ toW8 n = 48 + fromIntegral n
53+ !s = fromIntegral $ H.statusCode status
54+ (!q0,!r0) = s `divMod` 10
55+ (!q1,!r1) = q0 `divMod` 10
56+ !r2 = q1 `mod` 10