Comment stripper with string literals

Write codeHard35 min

Your task

Strip comments from a source string while respecting string literals. A line comment starts with '//' and runs to the end of the line (the newline itself is kept). A block comment starts with '/' and runs to the next '/' (which may span lines); block comments do not nest. However, '//', '/', and '/' that appear inside a double-quoted string literal are NOT comment markers. String literals are delimited by double quotes and may contain a backslash-escaped quote ('"') which does not close the string. Comment markers are never split across a string boundary in valid input. Remove all comment text (replace it with nothing) and return the resulting string. Assume every string literal and block comment is properly closed.

Examples

strip_comments(src: str) -> str
Example 1
Inputstrip_comments("int x = 5; // set x\nx++;")
Output"int x = 5; \nx++;"