on master help sign in
Backed out changeset daedec26ae9d
commit3fbe8dae85b3e0afeece04ee0f1ee346bfac36eb authorBryan O'Sullivan <bos@serpentine.com> dateTue 25 Jan 2011, 22:04 (16 years ago) parent4e05b90
1 file changed+9−13
Data/Aeson/Parser.hs+9−13
Data/Aeson/Parser.hs+9−13
@@ -6,19 +6,15 @@ module Data.Aeson.Parser
66 , value
77 ) where
88
9−import Blaze.ByteString.Builder
10−import Blaze.ByteString.Builder.Char.Utf8
11−import Data.Monoid (Monoid(..))
129import Control.Applicative as A
1310import Data.Aeson.Types (Value(..))
1411import Data.Attoparsec.Char8
1512import Data.Bits (shiftL)
1613import Data.ByteString as B
17−import Data.ByteString.Unsafe as B
1814import Data.Char (chr)
1915import Data.Map as Map
2016import Data.Text as T
21−import Data.Text.Encoding (decodeUtf8)
17+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
2218import Data.Vector as Vector hiding ((++))
2319import Data.Word (Word8)
2420import qualified Data.Attoparsec as A
@@ -71,7 +67,7 @@ jstring_ = do
7167 then Nothing
7268 else Just (c == backslash)
7369 _ <- A.word8 doubleQuote
74− decodeUtf8 <$> reparse unescape s
70+ (decodeUtf8 . B.concat) <$> reparse unescape s
7571
7672reparse :: Parser a -> ByteString -> Parser a
7773reparse p s = case (case parse p s of {Partial k -> k ""; r -> r}) of
@@ -79,24 +75,24 @@ reparse p s = case (case parse p s of {Partial k -> k ""; r -> r}) of
7975 Fail _ _ msg -> fail msg
8076 _ -> fail "unexpected failure"
8177
82−unescape :: Parser ByteString
83−unescape = toByteString <$> go mempty where
84− go bld = do
78+unescape :: Parser [ByteString]
79+unescape = Prelude.reverse <$> go [] where
80+ go acc = do
8581 let backslash = 92
8682 h <- A.takeWhile (/=backslash)
8783 let rest = do
8884 w <- A.word8 backslash *> A.satisfy (`B.elem` "\"\\/ntbrfu")
8985 case B.findIndex (==w) "\"\\/ntbrf" of
90− Just i -> go (bld `mappend` fromByteString h `mappend` fromWord8 (B.unsafeIndex "\"\\/\n\t\b\r\f" i))
86+ Just i -> go (B.singleton (B.index "\"\\/\n\t\b\r\f" i):h:acc)
9187 Nothing -> do
9288 a <- reparse hexadecimal =<< A.take 4
9389 if a < 0xd800 || a > 0xdfff
94− then go (bld `mappend` fromByteString h `mappend` fromChar (chr a))
90+ then go (encodeUtf8 (T.singleton . chr $ a):h:acc)
9591 else do
9692 b <- string "\\u" *> (reparse hexadecimal =<< A.take 4)
9793 if a <= 0xdbff && b >= 0xdc00 && b <= 0xdfff
9894 then let c = ((a - 0xd800) `shiftL` 10) + (b - 0xdc00) +
9995 0x10000
100− in go (bld `mappend` fromByteString h `mappend` fromChar (chr c))
96+ in go (encodeUtf8 (T.singleton . chr $ c):h:acc)
10197 else fail "invalid UTF-16 surrogates"
102− rest <|> return (bld `mappend` fromByteString h)
98+ rest <|> return (h:acc)