From 483b6de38b8163658881612644c62cc6db79510c Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 14 Apr 2017 11:54:13 -0400 Subject: [PATCH] Parse binaries and UTF strings with leading whitespace --- don/string.py | 6 ++++-- test_don.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/don/string.py b/don/string.py index f77eab7..0a324cb 100644 --- a/don/string.py +++ b/don/string.py @@ -136,6 +136,7 @@ def _binary64_parser(s): _BINARY_MATCHER = re.compile(r'"([\da-f]*)"b') +@_consume_leading_whitespace def _binary_parser(s): match = _BINARY_MATCHER.match(s) @@ -151,7 +152,8 @@ def _binary_parser(s): def _make_utf_parser(encoding): matcher = re.compile(r'"(.*?)"' + encoding) - def parser(s): + @_consume_leading_whitespace + def utf_parser(s): match = matcher.match(s) if match: @@ -163,7 +165,7 @@ def _make_utf_parser(encoding): return _shared._FAILED_PARSE_RESULT - return parser + return utf_parser def _prefix_with_comma(parser): def wrapped(s): diff --git a/test_don.py b/test_don.py index bdcc8dd..cc04df3 100644 --- a/test_don.py +++ b/test_don.py @@ -260,6 +260,12 @@ class TestStringDeserialize(unittest.TestCase): string.deserialize('"deadbeef"b'), ) + def test_deserializes_binary_with_leading_whitespace(self): + self.assertEqual( + b'\xde\xad\xbe\xef', + string.deserialize(' \t\n"deadbeef"b'), + ) + def test_deserializes_utf8(self): self.assertEqual( "Hello, world", @@ -278,6 +284,24 @@ class TestStringDeserialize(unittest.TestCase): string.deserialize('"Hello, world"utf32'), ) + def test_deserializes_utf8_with_leading_whitespace(self): + self.assertEqual( + "Hello, world", + string.deserialize(' \t\n"Hello, world"utf8'), + ) + + def test_deserializes_utf16_with_leading_whitespace(self): + self.assertEqual( + "Hello, world", + string.deserialize(' \t\n"Hello, world"utf16'), + ) + + def test_deserializes_utf32_with_leading_whitespace(self): + self.assertEqual( + "Hello, world", + string.deserialize(' \t\n"Hello, world"utf32'), + ) + def test_deserializes_list(self): self.assertEqual( [1,2,3,4,5], -- 2.20.1