Skip to content

Instantly share code, notes, and snippets.

@abhillman
Created November 24, 2014 03:03
Show Gist options
  • Save abhillman/51e0710ecea6b226d850 to your computer and use it in GitHub Desktop.
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
#!/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