From 508da722af5da04d9a375b56c6876b65454540ae Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 16 Mar 2018 18:38:41 +0900 Subject: [PATCH] Fix string length calculate for double byte characters --- bin/reverse_geolocate.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bin/reverse_geolocate.py b/bin/reverse_geolocate.py index f23a00b..eeee9e6 100755 --- a/bin/reverse_geolocate.py +++ b/bin/reverse_geolocate.py @@ -383,11 +383,18 @@ def shortenString(string, width, placeholder='..'): string_len_cjk = stringLenCJK(str(string)) # if double byte width is too big if string_len_cjk > width: - # substract the place holder - # subtract difference between double byte and character lenght - width -= ((string_len_cjk - len(str(string))) + len(placeholder)) - # return string width new width - return "{}{}".format(str(string)[:width], placeholder) + # set current length and output string + cur_len = 0 + out_string = '' + # loop through each character + for char in str(string): + # set the current length if we add the character + cur_len += 2 if unicodedata.east_asian_width(char) in "WF" else 1 + # if the new length is smaller than the output length to shorten too add the char + if cur_len <= (width - len(placeholder)): + out_string += char + # return string with new width and placeholder + return "{}{}".format(out_string, placeholder) else: return str(string)