From 0a1ded3f4afcb28abc2b0cdc87cecaa64180bc7c Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 6 Apr 2017 17:16:03 +0530 Subject: [PATCH] Port util.filenaming to py3 --- picard/util/filenaming.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/picard/util/filenaming.py b/picard/util/filenaming.py index 782241e50..21ba158a8 100644 --- a/picard/util/filenaming.py +++ b/picard/util/filenaming.py @@ -29,7 +29,7 @@ def _get_utf16_length(text): """Returns the number of code points used by a unicode object in its UTF-16 representation. """ - if isinstance(text, str): + if isinstance(text, bytes): return len(text) # if this is a narrow Python build, len will in fact return exactly # what we're looking for @@ -97,7 +97,7 @@ def _shorten_to_bytes_length(text, length): i = length # a UTF-8 intermediate byte starts with the bits 10xxxxxx, # so ord(char) & 0b11000000 = 0b10000000 - while i > 0 and (ord(raw[i]) & 0xC0) == 0x80: + while i > 0 and (raw[i] & 0xC0) == 0x80: i -= 1 return decode_filename(raw[:i]) # finally, a brute force approach @@ -117,7 +117,7 @@ def shorten_filename(filename, length, mode): """Truncates a filename to the given number of thingies, as implied by `mode`. """ - if isinstance(filename, str): + if isinstance(filename, bytes): return filename[:length] if mode == SHORTEN_BYTES: return _shorten_to_bytes_length(filename, length) @@ -148,7 +148,7 @@ def _shorten_to_utf16_ratio(text, ratio): """Shortens the string to the given ratio (and strips it).""" length = _get_utf16_length(text) limit = max(1, int(math.floor(length / ratio))) - if isinstance(text, str): + if isinstance(text, bytes): return text[:limit].strip() else: return _shorten_to_utf16_length(text, limit).strip()