Navigation Menu

Skip to content

Commit

Permalink
Port util.filenaming to py3
Browse files Browse the repository at this point in the history
  • Loading branch information
samj1912 committed Apr 7, 2017
1 parent 5ad260a commit 0a1ded3
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions picard/util/filenaming.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0a1ded3

Please sign in to comment.