on master help sign in
Resimplify the parsing of escaped text
commit1bc5cf9c1ab37b997cb2c943c70339a2e9c65cbb authorBryan O'Sullivan <bos@serpentine.com> dateFri 11 Feb 2011, 20:20 (16 years ago) parent183a450
2 files changed+10−8
Data/Aeson/Parser.hs+9−7 aeson.cabal+1−1
Data/Aeson/Parser.hs+9−7
@@ -102,8 +102,8 @@ reparse p s = case parse p s `feed` "" of
102102 _ -> fail "unexpected failure"
103103
104104unescape :: Parser ByteString
105−unescape = toByteString <$> go (Right mempty) where
106− go (Right acc) = do
105+unescape = toByteString <$> go mempty where
106+ go acc = do
107107 h <- A.takeWhile (/=backslash)
108108 let rest = do
109109 start <- A.take 2
@@ -113,9 +113,9 @@ unescape = toByteString <$> go (Right mempty) where
113113 Just i -> i
114114 _ -> 255
115115 if slash /= backslash || escape == 255
116− then go (Left "invalid JSON escape sequence")
117− else flip (<|>) (go (Left "invalid Unicode escape sequence")) $ do
118− let cont m = go (Right (acc `mappend` fromByteString h `mappend` m))
116+ then fail "invalid JSON escape sequence"
117+ else do
118+ let cont m = go (acc `mappend` fromByteString h `mappend` m)
119119 {-# INLINE cont #-}
120120 if t /= 117 -- 'u'
121121 then cont (fromWord8 (B.unsafeIndex mapping escape))
@@ -130,8 +130,10 @@ unescape = toByteString <$> go (Right mempty) where
130130 (b - 0xdc00) + 0x10000
131131 in cont (fromChar (chr c))
132132 else fail "invalid UTF-16 surrogates"
133− rest <|> return (acc `mappend` fromByteString h)
134− go (Left err) = fail err
133+ done <- atEnd
134+ if done
135+ then return (acc `mappend` fromByteString h)
136+ else rest
135137 mapping = "\"\\/\n\t\b\r\f"
136138
137139hexQuad :: Parser Int
aeson.cabal+1−1
@@ -72,7 +72,7 @@ library
7272 Data.Aeson.Functions
7373
7474 build-depends:
75− attoparsec >= 0.8.4.0,
75+ attoparsec >= 0.8.5.0,
7676 base == 4.*,
7777 blaze-builder >= 0.2.1.4,
7878 bytestring,