Skip to content

Commit f701f73

Browse files
authored
Merge pull request #599 from Lysxia/decodeHex
Refactor decodeHex in UnescapePure
2 parents 6bc3b7d + a01478e commit f701f73

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

benchmarks/Escape.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ import qualified "aeson-benchmarks" Data.Aeson.Parser.UnescapeFFI as FFI
1111
import qualified "aeson-benchmarks" Data.Aeson.Parser.UnescapePure as Pure
1212

1313
import 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

2116
main :: 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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,11 @@ decode UtfTail1 point word = case word of
120120
_ -> throwDecodeError
121121

122122
decodeHex :: 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

147129
unescapeText' :: ByteString -> Text
148130
unescapeText' bs = runText $ \done -> do

0 commit comments

Comments
 (0)