on master help sign in
Remove duplicate work from string parser
84ced15Sean
Data/Aeson/Parser/Internal.hs+15−14
@@ -203,21 +203,22 @@ jstring = A.word8 DOUBLE_QUOTE *> jstring_
203-- | Parse a string without a leading quote.
203-- | Parse a string without a leading quote.
204jstring_ :: Parser Text
204jstring_ :: Parser Text
205jstring_ = {-# SCC "jstring_" #-} do
205jstring_ = {-# SCC "jstring_" #-} do
206− s <- A.scan False $ \s c -> if s then Just False
206+ (s, (_, escaped)) <- A.runScanner (False, False) go
207− else if c == DOUBLE_QUOTE
207+ _ <- A.anyWord8
208− then Nothing
208+ s1 <- if escaped
209− else Just (c == BACKSLASH)
210− _ <- A.word8 DOUBLE_QUOTE
211− s1 <- if BACKSLASH `B.elem` s
212− then case unescape s of
209+ then case unescape s of
213− Right r -> return r
210+ Right r -> return r
214− Left err -> fail err
211+ Left err -> fail err
215− else return s
212+ else return s
216−
217 case decodeUtf8' s1 of
213 case decodeUtf8' s1 of
218− Right r -> return r
214+ Right r -> return r
219− Left err -> fail $ show err
215+ Left err -> fail $ show err
220−
216+ where go (a, b) c
217+ | a = Just (False, b)
218+ | c == DOUBLE_QUOTE = Nothing
219+ | otherwise = let a' = c == backSlash
220+ in Just (a', a' || b)
221+ where backSlash = BACKSLASH
221{-# INLINE jstring_ #-}
222{-# INLINE jstring_ #-}
222
223
223unescape :: ByteString -> Either String ByteString
224unescape :: ByteString -> Either String ByteString
aeson.cabal+1−1
@@ -84,7 +84,7 @@ library
84 Data.Aeson.Types.Internal
84 Data.Aeson.Types.Internal
85
85
86 build-depends:
86 build-depends:
87− attoparsec >= 0.13.0.0,
87+ attoparsec >= 0.13.0.1,
88 base >= 4.5 && < 5,
88 base >= 4.5 && < 5,
89 bytestring >= 0.10.4.0,
89 bytestring >= 0.10.4.0,
90 containers,
90 containers,
benchmarks/aeson-benchmarks.cabal+1−1
@@ -32,7 +32,7 @@ library
32 Data.Aeson.Types.Internal
32 Data.Aeson.Types.Internal
33
33
34 build-depends:
34 build-depends:
35− attoparsec >= 0.13.0.0,
35+ attoparsec >= 0.13.0.1,
36 base == 4.*,
36 base == 4.*,
37 bytestring >= 0.10.4.0,
37 bytestring >= 0.10.4.0,
38 containers,
38 containers,