python类east_asian_width()的实例源码

bot_utils.py 文件源码 项目:chalice-linebot 作者: c-bata 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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
asciidoc.py 文件源码 项目:asciidoc3 作者: asciidoc 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
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)
utils.py 文件源码 项目:tmux2html 作者: tweekmonster 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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])
__init__.py 文件源码 项目:chalktalk_docs 作者: loremIpsum1771 项目源码 文件源码 阅读 22 收藏 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
__init__.py 文件源码 项目:stig 作者: rndusr 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
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')
__init__.py 文件源码 项目:stig 作者: rndusr 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
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)
suddendeath.py 文件源码 项目:pyconjpbot 作者: pyconjp 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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)
ripstuff.py 文件源码 项目:jack 作者: jack-cli-cd-ripper 项目源码 文件源码 阅读 56 收藏 0 点赞 0 评论 0
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
cjkwrap.py 文件源码 项目:cjkwrap 作者: fgallaire 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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')
__init__.py 文件源码 项目:blackmamba 作者: zrzka 项目源码 文件源码 阅读 44 收藏 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
width_and_alignment.py 文件源码 项目:linux_terminal 作者: BillWang139967 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
unicode.py 文件源码 项目:dotfiles 作者: gbraad 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def strwidth_ucs_4(width_data, string):
    return sum(((
        (
            0
        ) if combining(symbol) else (
            width_data[east_asian_width(symbol)]
        )
    ) for symbol in string))
unicode.py 文件源码 项目:dotfiles 作者: gbraad 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)))
__init__.py 文件源码 项目:RST-vscode 作者: tht13 项目源码 文件源码 阅读 21 收藏 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
util.py 文件源码 项目:TornadoWeb 作者: VxCoder 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
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
autotagger.py 文件源码 项目:autotagger 作者: reorx 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
aligner.py 文件源码 项目:table-converter 作者: tenshiPure 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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


问题


面经


文章

微信
公众号

扫码关注公众号