c_preproc.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码
def parse_char(txt):
    """
    Parse a c character

    :param txt: character to parse
    :type txt: string
    :return: a character literal
    :rtype: string
    """

    if not txt: raise PreprocError("attempted to parse a null char")
    if txt[0] != '\\':
        return ord(txt)
    c = txt[1]
    if c == 'x':
        if len(txt) == 4 and txt[3] in string.hexdigits: return int(txt[2:], 16)
        return int(txt[2:], 16)
    elif c.isdigit():
        if c == '0' and len(txt)==2: return 0
        for i in 3, 2, 1:
            if len(txt) > i and txt[1:1+i].isdigit():
                return (1+i, int(txt[1:1+i], 8))
    else:
        try: return chr_esc[c]
        except KeyError: raise PreprocError("could not parse char literal '%s'" % txt)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号