mirror of
https://github.com/fergalmoran/picard.git
synced 2026-02-25 09:03:59 +00:00
Port util.filenaming to py3
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user