1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 12:12:34 +01:00

implement 'parse_unicode_escapes()'

This commit is contained in:
Mike Fährmann 2019-06-16 21:46:26 +02:00
parent 3a36a0fa1e
commit b171befa87
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
4 changed files with 26 additions and 1 deletions

View File

@ -1,5 +1,7 @@
# Changelog
## Unreleased
## 1.8.6 - 2019-06-14
### Additions
- Support for

View File

@ -174,6 +174,17 @@ def extract_from(txt, pos=0, default=""):
return extr
def parse_unicode_escapes(txt):
"""Convert JSON Unicode escapes in 'txt' into actual characters"""
if "\\u" in txt:
return re.sub(r"\\u([0-9a-fA-F]{4})", _hex_to_char, txt)
return txt
def _hex_to_char(match):
return chr(int(match.group(1), 16))
def parse_bytes(value, default=0, suffixes="bkmgtp"):
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
try:

View File

@ -6,4 +6,4 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
__version__ = "1.8.6"
__version__ = "1.8.7-dev"

View File

@ -271,6 +271,18 @@ class TestText(unittest.TestCase):
self.assertEqual(e("[", "]"), "END")
self.assertEqual(e("[", "]"), "END")
def test_parse_unicode_escapes(self, f=text.parse_unicode_escapes):
self.assertEqual(f(""), "")
self.assertEqual(f("foobar"), "foobar")
self.assertEqual(f("foobar"), "foobar")
self.assertEqual(f("foo\\u2019bar"), "foobar")
self.assertEqual(f("foo\\u201bar"), "fooar")
self.assertEqual(f("foo\\u201zar"), "foo\\u201zar")
self.assertEqual(
f("\\u2018foo\\u2019\\u2020bar\\u00ff"),
"foo†barÿ",
)
def test_parse_bytes(self, f=text.parse_bytes):
self.assertEqual(f("0"), 0)
self.assertEqual(f("50"), 50)