[utils] Fix js_to_json
authorPhilipp Hagemeister <phihag@phihag.de>
Wed, 18 Feb 2015 22:59:50 +0000 (23:59 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Wed, 18 Feb 2015 22:59:51 +0000 (23:59 +0100)
Previously, the runtime could be atrocious for longer inputs.

test/test_utils.py
youtube_dl/utils.py

index 1c29d0889b97991e20beed3fa9840bb4eb43a6da..c7373af1e2f64b8bc0bf0003d162b4e4b697eb87 100644 (file)
@@ -370,6 +370,10 @@ class TestUtil(unittest.TestCase):
             "playlist":[{"controls":{"all":null}}]
         }''')
 
+        inp = '"SAND Number: SAND 2013-7800P\\nPresenter: Tom Russo\\nHabanero Software Training - Xyce Software\\nXyce, Sandia\\u0027s"'
+        json_code = js_to_json(inp)
+        self.assertEqual(json.loads(json_code), json.loads(inp))
+
     def test_js_to_json_edgecases(self):
         on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}")
         self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"})
index 3eb6bc6d48bbe55f60aa8cd07a03d67937c1985e..4358137a0ae7f1d7367d965949997381b47e613f 100644 (file)
@@ -1560,8 +1560,8 @@ def js_to_json(code):
         return '"%s"' % v
 
     res = re.sub(r'''(?x)
-        "(?:[^"\\]*(?:\\\\|\\")?)*"|
-        '(?:[^'\\]*(?:\\\\|\\')?)*'|
+        "(?:[^"\\]*(?:\\\\|\\['"nu]))*[^"\\]*"|
+        '(?:[^'\\]*(?:\\\\|\\['"nu]))*[^'\\]*'|
         [a-zA-Z_][.a-zA-Z_0-9]*
         ''', fix_kv, code)
     res = re.sub(r',(\s*\])', lambda m: m.group(1), res)