def _message_length(word):
"""Return a message length."""
length = 0
for char in word:
width = east_asian_width(char)
if width in ('W', 'F', 'A'):
length += 2
elif width in ('Na', 'H'):
length += 1
return length
python类east_asian_width()的实例源码
def column_width(s):
text = char_decode(s)
if isinstance(text, unicode):
width = 0
for c in text:
width += east_asian_widths[unicodedata.east_asian_width(c)]
return width
else:
return len(text)
def str_width(s):
"""Return the width of the string.
Takes the width of East Asian characters into account
"""
return sum([2 if unicodedata.east_asian_width(c) == 'W' else 1 for c in s])
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width
def strwidth(string):
"""Return displayed width of `string`, considering wide characters"""
return len(string) + sum(1 for char in string
if unicodedata.east_asian_width(char) in 'FW')
def strcrop(string, width, tail=None):
"""Return `string` cropped to `width`, considering wide characters
If `tail` is not None, it must be a string that is appended to the cropped
string.
"""
def widechar_indexes(s):
for i,c in enumerate(s):
if unicodedata.east_asian_width(c) in 'FW':
yield i
if strwidth(string) <= width:
return string # string is already short enough
if tail is not None:
width -= strwidth(tail) # Account for tail in final width
indexes = list(widechar_indexes(string)) + [len(string)]
if not indexes:
return string[:width] # No wide chars, regular cropping is ok
parts = []
start = 0
end = 0
currwidth = strwidth(''.join(parts))
while indexes and currwidth < width and end < len(string):
end = indexes.pop(0)
if end > 0:
parts.append(string[start:end])
currwidth = strwidth(''.join(parts))
start = end
if currwidth > width:
excess = currwidth - width
parts[-1] = parts[-1][:-excess]
if tail is not None:
parts.append(tail)
return ''.join(parts)
def _message_length(message):
"""
???????????
"""
length = 0
for char in message:
width = east_asian_width(char)
if width in ('W', 'F', 'A'):
length += 2
elif width in ('Na', 'H'):
length += 1
return length
__init__.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def east_asian_len(data, encoding=None, ambiguous_width=1):
"""
Calculate display width considering unicode East Asian Width
"""
if isinstance(data, text_type):
return sum([_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data])
else:
return len(data)
__init__.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def east_asian_len(data, encoding=None, ambiguous_width=1):
"""
Calculate display width considering unicode East Asian Width
"""
if isinstance(data, text_type):
try:
data = data.decode(encoding)
except UnicodeError:
pass
return sum([_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data])
else:
return len(data)
def width(s):
w = 0
for c in s.decode(locale.getpreferredencoding()):
if unicodedata.east_asian_width(c) in ("W", "F"):
w += 2
else:
w += 1
return w
def is_wide(char):
"""is_wide(unicode_char) -> boolean
Return True if unicode_char is Fullwidth or Wide, False otherwise.
Fullwidth and Wide CJK chars are double-width.
"""
return unicodedata.east_asian_width(char) in ('F', 'W')
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width
def string_width(string):
"""Get the visible width of a unicode string.
Some CJK unicode characters are more than one byte unlike ASCII and latin unicode characters.
From: https://github.com/Robpol86/terminaltables/pull/9
:param str string: String to measure.
:return: String's width.
:rtype: int
"""
# Colorclass instance.
if hasattr(string, 'value_no_colors'):
string = string.value_no_colors
# Convert to unicode.
try:
decoded = string.decode('u8')
except (AttributeError, UnicodeEncodeError):
decoded = string
width = 0
for char in decoded:
if unicodedata.east_asian_width(char) in ('F', 'W'):
width += 2
else:
width += 1
return width
def strwidth_ucs_4(width_data, string):
return sum(((
(
0
) if combining(symbol) else (
width_data[east_asian_width(symbol)]
)
) for symbol in string))
def strwidth_ucs_2(width_data, string):
return sum(((
(
width_data[east_asian_width(string[i - 1] + symbol)]
) if 0xDC00 <= ord(symbol) <= 0xDFFF else (
0
) if combining(symbol) or 0xD800 <= ord(symbol) <= 0xDBFF else (
width_data[east_asian_width(symbol)]
)
) for i, symbol in enumerate(string)))
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width
def str_len(value):
result = 0
for val in value:
if(unicodedata.east_asian_width(val) in (r'A',r'F',r'W')):
result += 2
else:
result += 1
return result
def unicode_width(string):
def char_width(char):
# ('F', 'W', 'A', 'H', 'N', 'Na')
# Ref: http://www.unicode.org/reports/tr11/tr11-14.html
w2 = ('F', 'W', 'A')
w1 = ('H', 'Na')
w = unicodedata.east_asian_width(char)
if w in w2:
return 2
elif w in w1:
return 1
else:
return 0
length = sum([char_width(c) for c in string])
return length
def _charwidth(c):
return 2 if unicodedata.east_asian_width(c) in ['F', 'W', 'A'] else 1
__init__.py 文件源码
项目:tf_aws_ecs_instance_draining_on_scale_in
作者: terraform-community-modules
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width