Bryan O'Sullivan avatar Bryan O'Sullivan committed 5b5da24

Resimplify the parsing of escaped text

Comments (0)

Files changed (2)


                 _            -> fail "unexpected failure"
 unescape :: Parser ByteString
-unescape = toByteString <$> go (Right mempty) where
-  go (Right acc) = do
+unescape = toByteString <$> go mempty where
+  go acc = do
     h <- A.takeWhile (/=backslash)
     let rest = do
           start <- A.take 2
                          Just i -> i
                          _      -> 255
           if slash /= backslash || escape == 255
-            then go (Left "invalid JSON escape sequence")
-            else flip (<|>) (go (Left "invalid Unicode escape sequence")) $ do
-            let cont m = go (Right (acc `mappend` fromByteString h `mappend` m))
+            then fail "invalid JSON escape sequence"
+            else do
+            let cont m = go (acc `mappend` fromByteString h `mappend` m)
                 {-# INLINE cont #-}
             if t /= 117 -- 'u'
               then cont (fromWord8 (B.unsafeIndex mapping escape))
                                        (b - 0xdc00) + 0x10000
                               in cont (fromChar (chr c))
                          else fail "invalid UTF-16 surrogates"
-    rest <|> return (acc `mappend` fromByteString h)
-  go (Left err) = fail err
+    done <- atEnd
+    if done
+      then return (acc `mappend` fromByteString h)
+      else rest
   mapping = "\"\\/\n\t\b\r\f"
 hexQuad :: Parser Int
-    attoparsec >=,
+    attoparsec >=,
     base == 4.*,
     blaze-builder >=,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.