@@ -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