Skip to content

Instantly share code, notes, and snippets.

@kkew3
Last active February 9, 2022 09:58
Show Gist options
  • Save kkew3/8bb9aa225a6c82ae5e1a0fa609c9a65a to your computer and use it in GitHub Desktop.
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.
__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