kbd_firmware/lib/python/qmk/comment_remover.py
foostan d18d94e139 Squashed 'qmk/qmk_firmware/' content from commit b7468f4785
git-subtree-dir: qmk/qmk_firmware
git-subtree-split: b7468f47857ad20a031906ccbd654541222a0d26
2024-01-28 19:36:44 +09:00

21 lines
538 B
Python

"""Removes C/C++ style comments from text.
Gratefully adapted from https://stackoverflow.com/a/241506
"""
import re
comment_pattern = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE)
def _comment_stripper(match):
"""Removes C/C++ style comments from a regex match.
"""
s = match.group(0)
return ' ' if s.startswith('/') else s
def comment_remover(text):
"""Remove C/C++ style comments from text.
"""
return re.sub(comment_pattern, _comment_stripper, text)