def remove_comments()

in mysql-connector-python/lib/mysql/connector/_scripting.py [0:0]


    def remove_comments(code: bytes) -> bytes:
        """Remove MySQL comments which include `--`-style, `#`-style
        and `C`-style comments.

        A `--`-style comment spans from `--` to the end of the line.
        It requires the second dash to be
        followed by at least one whitespace or control character
        (such as a space, tab, newline, and so on).

        A `#`-style comment spans from `#` to the end of the line.

        A C-style comment spans from a `/*` sequence to the following `*/`
        sequence, as in the C programming language. This syntax enables a
        comment to extend over multiple lines because the beginning and
        closing sequences need not be on the same line.

        **NOTE: Only C-style comments representing MySQL extensions or
        optimizer hints are preserved**. E.g.,

        ```
        /*! MySQL-specific code */

        /*+ MySQL-specific code */
        ```

        *For Reference Manual- MySQL Comments*, see
        https://dev.mysql.com/doc/refman/en/comments.html.
        """

        def is_dash_style(b_str: bytes, b_char: int) -> bool:
            return b_str == b"--" and (
                MySQLScriptSplitter.is_control_char(b_char)
                or MySQLScriptSplitter.is_white_space_char(b_char)
            )

        def is_hash_style(b_str: bytes) -> bool:
            return b_str == b"#"

        def is_c_style(b_str: bytes, b_char: int) -> bool:
            return b_str == b"/*" and b_char not in {ord("!"), ord("+")}

        buf = bytearray(b"")
        i, literal_ctx = 0, None
        line_break, single_quote, double_quote = ord("\n"), ord("'"), ord('"')
        while i < len(code):
            if literal_ctx is None:
                style = None
                if is_dash_style(buf[-2:], code[i]):
                    style = "--"
                elif is_hash_style(buf[-1:]):
                    style = "#"
                elif is_c_style(buf[-2:], code[i]):
                    style = "/*"
                if style is not None:
                    if style in ("--", "#"):
                        while i < len(code) and code[i] != line_break:
                            i += 1
                    else:
                        while i + 1 < len(code) and code[i : i + 2] != b"*/":
                            i += 1
                        i += 2

                    for _ in range(len(style)):
                        buf.pop()

                    while buf and (
                        MySQLScriptSplitter.is_control_char(buf[-1])
                        or MySQLScriptSplitter.is_white_space_char(buf[-1])
                    ):
                        buf.pop()

                    continue

            if literal_ctx is None and code[i] in [single_quote, double_quote]:
                literal_ctx = code[i]
            elif literal_ctx is not None and code[i] in {literal_ctx, line_break}:
                literal_ctx = None

            buf.append(code[i])
            i += 1

        return bytes(buf)