Created
June 19, 2020 22:44
-
-
Save james4388/6ab6a5c2069f2345067b2c54fd0ff469 to your computer and use it in GitHub Desktop.
Clean up sql comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def clean_comment(sql: str) -> str: | |
lines = sql.split('\n') | |
output = [] | |
for line in lines: | |
escape = False | |
quote = False | |
i = 0 | |
buffer = [] | |
n = len(line) | |
while i < n: | |
if line[i] == '\\': | |
escape = not escape | |
elif not escape and line[i] == "'": | |
quote = not quote | |
elif (not escape and not quote) and line[i: i + 2] == '--': | |
break | |
else: | |
escape = False | |
buffer.append(line[i]) | |
i += 1 | |
output.append(''.join(buffer)) | |
return '\n'.join(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment