implement 'parse_unicode_escapes()'
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
## 1.8.6 - 2019-06-14
|
## 1.8.6 - 2019-06-14
|
||||||
### Additions
|
### Additions
|
||||||
- Support for
|
- Support for
|
||||||
|
|||||||
@@ -174,6 +174,17 @@ def extract_from(txt, pos=0, default=""):
|
|||||||
return extr
|
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"):
|
def parse_bytes(value, default=0, suffixes="bkmgtp"):
|
||||||
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
|
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
# it under the terms of the GNU General Public License version 2 as
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
# published by the Free Software Foundation.
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
__version__ = "1.8.6"
|
__version__ = "1.8.7-dev"
|
||||||
|
|||||||
@@ -271,6 +271,18 @@ class TestText(unittest.TestCase):
|
|||||||
self.assertEqual(e("[", "]"), "END")
|
self.assertEqual(e("[", "]"), "END")
|
||||||
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("foo’bar"), "foo’bar")
|
||||||
|
self.assertEqual(f("foo\\u2019bar"), "foo’bar")
|
||||||
|
self.assertEqual(f("foo\\u201bar"), "foo‛ar")
|
||||||
|
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):
|
def test_parse_bytes(self, f=text.parse_bytes):
|
||||||
self.assertEqual(f("0"), 0)
|
self.assertEqual(f("0"), 0)
|
||||||
self.assertEqual(f("50"), 50)
|
self.assertEqual(f("50"), 50)
|
||||||
|
|||||||
Reference in New Issue
Block a user