framing.py 文件源码

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

项目:py-bson-rpc 作者: seprich 项目源码 文件源码
def extract_message(cls, raw_bytes):
        if len(raw_bytes) < 2:
            return None, raw_bytes
        if six.byte2int(raw_bytes) != 123:
            raise FramingError(
                'Broken state. Expected JSON Object, got: %s' % raw_bytes)
        stack = [123]
        uniesc = 0
        poppers = {91: [93], 123: [125], 34: [34]}
        adders = {91: [34, 91, 123], 123: [34, 91, 123], 34: [92], 92: [117]}
        for idx in range(1, len(raw_bytes)):
            cbyte = six.indexbytes(raw_bytes, idx)
            if cbyte in poppers.get(stack[-1], []):
                stack.pop()
            elif cbyte in adders.get(stack[-1], []):
                stack.append(cbyte)
            elif stack[-1] == 92:
                stack.pop()
            elif stack[-1] == 117:
                uniesc += 1
                if uniesc >= 4:
                    stack = stack[:-2]
                    uniesc = 0
            if not stack:
                return raw_bytes[:idx + 1], raw_bytes[idx + 1:]
        return None, raw_bytes
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号