1# Copyright (C) 2018 and later: Unicode, Inc. and others. 2# License & terms of use: http://www.unicode.org/copyright.html 3 4import io 5 6class CommentStripper(object): 7 """Removes lines starting with "//" from a file stream.""" 8 9 def __init__(self, f): 10 self.f = f 11 self.state = 0 12 13 def read(self, size=-1): 14 bytes = self.f.read(size) 15 # TODO: Do we need to read more bytes if comments were stripped 16 # in order to obey the size request? 17 return "".join(self._strip_comments(bytes)) 18 19 def _strip_comments(self, bytes): 20 for byte in bytes: 21 if self.state == 0: 22 # state 0: start of a line 23 if byte == "/": 24 self.state = 1 25 elif byte == "\n": 26 self.state = 0 27 yield byte 28 else: 29 self.state = 2 30 yield byte 31 elif self.state == 1: 32 # state 1: read a single '/' 33 if byte == "/": 34 self.state = 3 35 elif byte == "\n": 36 self.state = 0 37 yield "/" # the one that was skipped 38 yield "\n" 39 else: 40 self.state = 2 41 yield "/" # the one that was skipped 42 yield byte 43 elif self.state == 2: 44 # state 2: middle of a line, no comment 45 if byte == "\n": 46 self.state = 0 47 yield byte 48 elif self.state == 3: 49 # state 3: inside a comment 50 if byte == "\n": 51 self.state = 0 52