on master help sign in
Resimplify the parsing of escaped text
1bc5cf9Bryan O'Sullivan
Data/Aeson/Parser.hs+9−7
@@ -102,8 +102,8 @@ reparse p s = case parse p s `feed` "" of
102 _ -> fail "unexpected failure"
102 _ -> fail "unexpected failure"
103
103
104unescape :: Parser ByteString
104unescape :: Parser ByteString
105−unescape = toByteString <$> go (Right mempty) where
105+unescape = toByteString <$> go mempty where
106− go (Right acc) = do
106+ go acc = do
107 h <- A.takeWhile (/=backslash)
107 h <- A.takeWhile (/=backslash)
108 let rest = do
108 let rest = do
109 start <- A.take 2
109 start <- A.take 2
@@ -113,9 +113,9 @@ unescape = toByteString <$> go (Right mempty) where
113 Just i -> i
113 Just i -> i
114 _ -> 255
114 _ -> 255
115 if slash /= backslash || escape == 255
115 if slash /= backslash || escape == 255
116− then go (Left "invalid JSON escape sequence")
116+ then fail "invalid JSON escape sequence"
117− else flip (<|>) (go (Left "invalid Unicode escape sequence")) $ do
117+ else do
118− let cont m = go (Right (acc `mappend` fromByteString h `mappend` m))
118+ let cont m = go (acc `mappend` fromByteString h `mappend` m)
119 {-# INLINE cont #-}
119 {-# INLINE cont #-}
120 if t /= 117 -- 'u'
120 if t /= 117 -- 'u'
121 then cont (fromWord8 (B.unsafeIndex mapping escape))
121 then cont (fromWord8 (B.unsafeIndex mapping escape))
@@ -130,8 +130,10 @@ unescape = toByteString <$> go (Right mempty) where
130 (b - 0xdc00) + 0x10000
130 (b - 0xdc00) + 0x10000
131 in cont (fromChar (chr c))
131 in cont (fromChar (chr c))
132 else fail "invalid UTF-16 surrogates"
132 else fail "invalid UTF-16 surrogates"
133+ done <- atEnd
134+ if done
133− rest <|> return (acc `mappend` fromByteString h)
135+ then return (acc `mappend` fromByteString h)
134− go (Left err) = fail err
136+ else rest
135 mapping = "\"\\/\n\t\b\r\f"
137 mapping = "\"\\/\n\t\b\r\f"
136
138
137hexQuad :: Parser Int
139hexQuad :: Parser Int
aeson.cabal+1−1
@@ -72,7 +72,7 @@ library
72 Data.Aeson.Functions
72 Data.Aeson.Functions
73
73
74 build-depends:
74 build-depends:
75− attoparsec >= 0.8.4.0,
75+ attoparsec >= 0.8.5.0,
76 base == 4.*,
76 base == 4.*,
77 blaze-builder >= 0.2.1.4,
77 blaze-builder >= 0.2.1.4,
78 bytestring,
78 bytestring,