on master help sign in
Merge pull request #599 from Lysxia/decodeHex

Refactor decodeHex in UnescapePure

commitf701f73db6ed73e0145c7f6e606f76f143a38154 authorAdam Bergmark <adam@bergmark.nl> dateSun 29 Oct 2017, 17:14 (9 years ago) parent6bc3b7da01478e
2 files changed+18−30
benchmarks/Escape.hs+13−7 pure/Data/Aeson/Parser/UnescapePure.hs+5−23
benchmarks/Escape.hs+13−7
@@ -11,15 +11,21 @@ import qualified "aeson-benchmarks" Data.Aeson.Parser.UnescapeFFI as FFI
1111import qualified "aeson-benchmarks" Data.Aeson.Parser.UnescapePure as Pure
1212
1313import qualified Data.ByteString.Char8 as BS
14−
15−n :: Int
16−n = 10000
17−
18−input :: BS.ByteString
19−input = BS.concat $ replicate n $ BS.pack "\\\""
14+import System.Environment (getArgs, withArgs)
2015
2116main :: IO ()
22−main = defaultMain
17+main = do
18+ args_ <- getArgs
19+ let (args, p, n) =
20+ case args_ of
21+ "--pattern" : p : args_ -> k p args_
22+ _ -> k "\\\"" args_
23+ k p args_ =
24+ case args_ of
25+ "--repeat" : n : args_ -> (args_, p, read n)
26+ args_ -> (args_, p, 10000)
27+ input = BS.concat $ replicate n $ BS.pack p
28+ withArgs args $ defaultMain
2329 [ bench "ffi" $ whnf FFI.unescapeText input
2430 , bench "pure" $ whnf Pure.unescapeText input
2531 ]
pure/Data/Aeson/Parser/UnescapePure.hs+5−23
@@ -120,29 +120,11 @@ decode UtfTail1 point word = case word of
120120 _ -> throwDecodeError
121121
122122decodeHex :: Word8 -> Word16
123−decodeHex 48 = 0 -- '0'
124−decodeHex 49 = 1 -- '1'
125−decodeHex 50 = 2 -- '2'
126−decodeHex 51 = 3 -- '3'
127−decodeHex 52 = 4 -- '4'
128−decodeHex 53 = 5 -- '5'
129−decodeHex 54 = 6 -- '6'
130−decodeHex 55 = 7 -- '7'
131−decodeHex 56 = 8 -- '8'
132−decodeHex 57 = 9 -- '9'
133−decodeHex 65 = 10 -- 'A'
134−decodeHex 97 = 10 -- 'a'
135−decodeHex 66 = 11 -- 'B'
136−decodeHex 98 = 11 -- 'b'
137−decodeHex 67 = 12 -- 'C'
138−decodeHex 99 = 12 -- 'c'
139−decodeHex 68 = 13 -- 'D'
140−decodeHex 100 = 13 -- 'd'
141−decodeHex 69 = 14 -- 'E'
142−decodeHex 101 = 14 -- 'e'
143−decodeHex 70 = 15 -- 'F'
144−decodeHex 102 = 15 -- 'f'
145−decodeHex _ = throwDecodeError
123+decodeHex x
124+ | 48 <= x && x <= 57 = fromIntegral x - 48 -- 0-9
125+ | 65 <= x && x <= 70 = fromIntegral x - 55 -- A-F
126+ | 97 <= x && x <= 102 = fromIntegral x - 87 -- a-f
127+ | otherwise = throwDecodeError
146128
147129unescapeText' :: ByteString -> Text
148130unescapeText' bs = runText $ \done -> do