Created
November 24, 2014 03:03
-
-
Save abhillman/51e0710ecea6b226d850 to your computer and use it in GitHub Desktop.
Little program to remove c-like comments given from input via standard in
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
#!/usr/bin/python | |
# Little program to remove c-like comments from input given via standard in | |
import fileinput | |
from sys import stdout | |
COMMENT_OPEN = False | |
OPEN_COMMENT = '/*' | |
CLOSE_COMMENT = '*/' | |
OPEN_COMMENT_IDX = 0 | |
CLOSE_COMMENT_IDX = 0 | |
for line in fileinput.input(): | |
for char in line: | |
if COMMENT_OPEN: | |
if char == CLOSE_COMMENT[0]: | |
CLOSE_COMMENT_IDX = 1 | |
elif char == CLOSE_COMMENT[1]: | |
COMMENT_OPEN = False | |
continue | |
if char == OPEN_COMMENT[0]: | |
OPEN_COMMENT_IDX = 1 | |
continue | |
elif char == OPEN_COMMENT[1]: | |
if OPEN_COMMENT_IDX == 1: | |
COMMENT_OPEN = True | |
continue | |
else: | |
stdout.write(OPEN_COMMENT[0]) | |
stdout.write(char) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment