Skip to content

Commit

Permalink
Remove duplicate work from string parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanRBurton committed Jul 22, 2015
1 parent 94a4edc commit 84ced15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions Data/Aeson/Parser/Internal.hs
Expand Up @@ -203,21 +203,22 @@ jstring = A.word8 DOUBLE_QUOTE *> jstring_
-- | Parse a string without a leading quote.
jstring_ :: Parser Text
jstring_ = {-# SCC "jstring_" #-} do
s <- A.scan False $ \s c -> if s then Just False
else if c == DOUBLE_QUOTE
then Nothing
else Just (c == BACKSLASH)
_ <- A.word8 DOUBLE_QUOTE
s1 <- if BACKSLASH `B.elem` s
then case unescape s of
Right r -> return r
Left err -> fail err
else return s

(s, (_, escaped)) <- A.runScanner (False, False) go
_ <- A.anyWord8
s1 <- if escaped
then case unescape s of
Right r -> return r
Left err -> fail err
else return s
case decodeUtf8' s1 of
Right r -> return r
Left err -> fail $ show err

Right r -> return r
Left err -> fail $ show err
where go (a, b) c
| a = Just (False, b)
| c == DOUBLE_QUOTE = Nothing
| otherwise = let a' = c == backSlash
in Just (a', a' || b)
where backSlash = BACKSLASH
{-# INLINE jstring_ #-}

unescape :: ByteString -> Either String ByteString
Expand Down
2 changes: 1 addition & 1 deletion aeson.cabal
Expand Up @@ -84,7 +84,7 @@ library
Data.Aeson.Types.Internal

build-depends:
attoparsec >= 0.13.0.0,
attoparsec >= 0.13.0.1,
base >= 4.5 && < 5,
bytestring >= 0.10.4.0,
containers,
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/aeson-benchmarks.cabal
Expand Up @@ -32,7 +32,7 @@ library
Data.Aeson.Types.Internal

build-depends:
attoparsec >= 0.13.0.0,
attoparsec >= 0.13.0.1,
base == 4.*,
bytestring >= 0.10.4.0,
containers,
Expand Down

0 comments on commit 84ced15

Please sign in to comment.