in atr/tasks/checks/license.py [0:0]
def strip_comments(content: bytes, file_ext: str) -> bytes:
"""Strip comment prefixes from the content based on the file extension."""
if file_ext not in COMMENT_STYLES:
return content
comment_style = COMMENT_STYLES[file_ext]
lines = content.split(b"\n")
cleaned_lines = []
# Get comment markers as bytes
multi_start = comment_style.get("multi_start", "").encode()
multi_end = comment_style.get("multi_end", "").encode()
single = comment_style.get("single", "").encode()
# State tracking
in_multiline = False
is_c_style = (multi_start == b"/*") and (multi_end == b"*/")
for line in lines:
line = line.strip()
# Handle start of multi-line comment
if not in_multiline and multi_start and multi_start in line:
# Get content after multi-start
line = line[line.find(multi_start) + len(multi_start) :].strip()
in_multiline = True
# Handle end of multi-line comment
elif in_multiline and multi_end and multi_end in line:
# Get content before multi-end
line = line[: line.find(multi_end)].strip()
in_multiline = False
# Handle single-line comments
elif not in_multiline and single and line.startswith(single):
line = line[len(single) :].strip()
# For C style comments, strip leading asterisk if present
elif is_c_style and in_multiline and line.startswith(b"*"):
line = line[1:].strip()
# Only add non-empty lines
if line:
cleaned_lines.append(line)
return b"\n".join(cleaned_lines)