dwidth = 'W' else: raise ValueError('ambiwidth 取值为 1 或者 2') import unicodedata count = 0 for i in s: if unicodedata.east_asian_width(i) in dwidth: count += 2 continue count += 1 return count try: from ctypes import * _w = myutils.loadso('_wchar.so') _w.width.argtypes = (c_wchar_p,) _w.width.restype = c_size_t def strwidth(s, ambiwidth=1): ''' ambiwidth is ignored This is over one time quicker than `strwidth_py' in Python ''' return _w.width(s) except ImportError: strwidth = strwidth_py def _CJK_align(字符串, 对齐宽度, 方向='左', 填充=' '): '''对齐字符串,考虑字符宽度,不检测是否是ASCII字符串''' if len(填充) != 1:
""" X11 相关工具 """ from ctypes import * from myutils import loadso _xlib = loadso("_xlib.so") _xlib.test_display.argtypes = (c_char_p,) def test_display(name): return bool(_xlib.test_display(name.encode("utf-8")))
else: raise ValueError('ambiwidth 取值为 1 或者 2') import unicodedata count = 0 for i in 字符串: if unicodedata.east_asian_width(i) in 双宽度: count += 2 continue count += 1 return count try: from ctypes import * _w = myutils.loadso('_wchar.so') _w.width.argtypes = (c_wchar_p, ) _w.width.restype = c_size_t def 宽度(字符串, ambiwidth=1): ''' ambiwidth 被忽略 这样比纯 Python 的 `宽度_py' 速度要快一倍以上 ''' return _w.width(字符串) except ImportError: 宽度 = 宽度_py width = 宽度
''' ard 解码 来源 http://www.wowstory.com/ 2011年1月12日 ''' from ctypes import * from myutils import loadso import sys _ard = loadso('_ard.so') _ard.ard.argtypes = (c_char_p,) * 2 _ard.ard.restype = c_char_p def ard(str1, str2): return _ard.ard(str1.encode('utf-8'), str2.encode('utf-8')).decode('utf-8') if __name__ == '__main__': print(ard(sys.argv[1], sys.argv[2]))
''' ard 解码 来源 http://www.wowstory.com/ 2011年1月12日 ''' from ctypes import * from myutils import loadso import sys _ard = loadso('_ard.so') _ard.ard.argtypes = (c_char_p, ) * 2 _ard.ard.restype = c_char_p def ard(str1, str2): return _ard.ard(str1.encode('utf-8'), str2.encode('utf-8')).decode('utf-8') if __name__ == '__main__': print(ard(sys.argv[1], sys.argv[2]))