From: Brian Wellington Date: Thu, 9 Jul 2020 17:59:12 +0000 (-0700) Subject: Generalize the word breaking code. X-Git-Tag: v2.0.0~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bac5775b07f7e10c1a593533a8f1786f8b2ea40b;p=thirdparty%2Fdnspython.git Generalize the word breaking code. Refactor common code from _base64ify and _hexify, and also add support for _hexify to skip word breaks. --- diff --git a/dns/rdata.py b/dns/rdata.py index fd9bea99..17636afa 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -31,33 +31,34 @@ import dns.rdataclass import dns.rdatatype import dns.tokenizer -_hex_chunksize = 32 +_chunksize = 32 -def _hexify(data, chunksize=_hex_chunksize): - """Convert a binary string into its hex encoding, broken up into chunks - of chunksize characters separated by a space. +def _wordbreak(line, chunksize=_chunksize): + """Break a string into chunks of chunksize characters separated by a space. """ - line = binascii.hexlify(data) + if not chunksize: + return line return b' '.join([line[i:i + chunksize] for i in range(0, len(line), chunksize)]).decode() -_base64_chunksize = 32 + +def _hexify(data, chunksize=_chunksize): + """Convert a binary string into its hex encoding, broken up into chunks + of chunksize characters separated by a space. + """ + + return _wordbreak(binascii.hexlify(data), chunksize) -def _base64ify(data, chunksize=_base64_chunksize): +def _base64ify(data, chunksize=_chunksize): """Convert a binary string into its base64 encoding, broken up into chunks of chunksize characters separated by a space. """ - line = base64.b64encode(data) - if not chunksize: - return line - return b' '.join([line[i:i + chunksize] - for i - in range(0, len(line), chunksize)]).decode() + return _wordbreak(base64.b64encode(data), chunksize) __escaped = b'"\\'