]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Generalize the word breaking code.
authorBrian Wellington <bwelling@xbill.org>
Thu, 9 Jul 2020 17:59:12 +0000 (10:59 -0700)
committerBrian Wellington <bwelling@xbill.org>
Thu, 9 Jul 2020 17:59:12 +0000 (10:59 -0700)
Refactor common code from _base64ify and _hexify, and also add support
for _hexify to skip word breaks.

dns/rdata.py

index fd9bea99712ebb114dbfa943d5dfc474eafd1497..17636afad90694e2ddf7648462ee82e37b215c8f 100644 (file)
@@ -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'"\\'