def py_scanstring()

in src/cfnlint/decode/cfn_json.py [0:0]


def py_scanstring(s, end, strict=True,
                  _b=BACKSLASH, _m=STRINGCHUNK.match):
    """Scan the string s for a JSON string. End is the index of the
    character in s after the quote that started the JSON string.
    Unescapes all valid JSON string escape sequences and raises ValueError
    on attempt to decode an invalid string. If strict is False then literal
    control characters are allowed in the string.
    Returns a tuple of the decoded string and the index of the character in s
    after the end quote."""
    chunks = []
    _append = chunks.append
    begin = end - 1
    while 1:
        chunk = _m(s, end)
        if chunk is None:
            raise JSONDecodeError(
                doc=s,
                pos=begin,
                errors=[
                    build_match(
                        message='Unterminated string starting at',
                        doc=s,
                        pos=begin,
                    ),
                ]
            )
        end = chunk.end()
        content, terminator = chunk.groups()
        # Content is contains zero or more unescaped string characters
        if content:
            _append(content)
        # Terminator is the end of string, a literal control character,
        # or a backslash denoting that an escape sequence follows
        if terminator == '"':
            break
        if terminator != '\\':
            if strict:
                msg = 'Invalid control character {0!r} at'.format(terminator)
                raise JSONDecodeError(
                    doc=s,
                    pos=end,
                    errors=[
                        build_match(
                            message=msg,
                            doc=s,
                            pos=end,
                        ),
                    ]
                )
            _append(terminator)
            continue
        try:
            esc = s[end]
        except IndexError:
            raise JSONDecodeError(
                doc=s,
                pos=begin,
                errors=[
                    build_match(
                        message='Unterminated string starting at',
                        doc=s,
                        pos=begin,
                    ),
                ]
            )
        # If not a unicode escape sequence, must be in the lookup table
        if esc != 'u':
            try:
                char = _b[esc]
            except KeyError:
                msg = 'Invalid \\escape: {0!r}'.format(esc)
                raise JSONDecodeError(
                    doc=s,
                    pos=end,
                    errors=[
                        build_match(
                            message=msg,
                            doc=s,
                            pos=end,
                        ),
                    ]
                )
            end += 1
        else:
            uni = _decode_uXXXX(s, end)
            end += 5
            if 0xd800 <= uni <= 0xdbff and s[end:end + 2] == '\\u':
                uni2 = _decode_uXXXX(s, end + 1)
                if 0xdc00 <= uni2 <= 0xdfff:
                    uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00))
                    end += 6
            char = chr(uni)
        _append(char)
    return ''.join(chunks), end