Skip to content

Instantly share code, notes, and snippets.

@rrader
Last active December 15, 2015 07:19
Show Gist options
  • Save rrader/5223087 to your computer and use it in GitHub Desktop.
Save rrader/5223087 to your computer and use it in GitHub Desktop.
Блочный алгоритм удаления комментариев из файла
from StringIO import StringIO
import re
import itertools
s = 'text1 text2 /* asdasd */ text3 /* /*asdasd */ text4'
BLOCK = 6 # even number!
OPEN = 1
CLOSE = 2
another = {OPEN: CLOSE, CLOSE: OPEN}
def blocked(f, position=False):
while True:
block = f.read(BLOCK)
if not block:
break
if position:
yield (block, f.pos - BLOCK)
else:
yield block
def finder(block):
block, pos = block
comments = [(m.start() + pos, OPEN) for m in re.finditer('/\*', block)] + \
[(m.end() + pos, CLOSE) for m in re.finditer('\*/', block)]
return comments
def reducer(datafile):
def do_reduce(data, item, waiting=[OPEN]):
if waiting[0] == item[1]:
waiting[0] = another[waiting[0]]
new_data = datafile.read(item[0] - datafile.pos)
if item[1] == OPEN:
data.append(new_data)
return data
else:
return data
else:
return data
return do_reduce
datafile = StringIO(s)
comments = itertools.chain([[]], *map(finder, blocked(datafile, position=True)))
datafile.seek(0)
result = ''.join(reduce(reducer(datafile), comments)) + datafile.read()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment