Last active
February 9, 2022 09:58
-
-
Save kkew3/8bb9aa225a6c82ae5e1a0fa609c9a65a to your computer and use it in GitHub Desktop.
Python `str.ljust` and `str.rjust` for CJK characters mixed with ASCII characters string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__all__ = [ | |
'cjkljust', | |
'cjkrjust', | |
'cjkcenter', | |
] | |
import unicodedata | |
#def cjklen(string): | |
# return sum(2 if unicodedata.east_asian_width(char) in 'FW' else 1 | |
# for char in string) | |
def count_cjk_chars(string): | |
return sum(unicodedata.east_asian_width(char) in 'FW' for char in string) | |
def cjkljust(string, width, fillbyte=' '): | |
""" | |
>>> cjkljust('hello', 10, '*') | |
'hello*****' | |
>>> cjkljust('你好world', 10, '*') | |
'你好world*' | |
>>> cjkljust('你好world', 1, '*') | |
'你好world' | |
""" | |
#return string.ljust(len(string) + width - cjklen(string), fillbyte) | |
return string.ljust(width - count_cjk_chars(string), fillbyte) | |
def cjkrjust(string, width, fillbyte=' '): | |
#return string.rjust(len(string) + width - cjklen(string), fillbyte) | |
return string.rjust(width - count_cjk_chars(string), fillbyte) | |
def cjkcenter(string, width, fillbyte=' '): | |
return string.center(width - count_cjk_chars(string), fillbyte) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment