@@ -102,11 +102,36 @@ def _substitute_path(path, regex_iter):
102
102
continue
103
103
if not content :
104
104
raise UnicodeDecodeError ('Unable to decode with any encoding: %s' % path )
105
+
106
+ # list that will contain the comments
107
+ comments = []
108
+ replace_comments = (path .suffix == '.java' or path .suffix == '.cc' or path .suffix == '.h'
109
+ or path .suffix == '.js' or path .suffix == '.cpp' or path .suffix == '.c' )
110
+ if replace_comments :
111
+ content = re .sub (
112
+ r'(\/\*.*?\*/\n|\s\/\/[^\n]+)' ,
113
+ lambda match : _replace_comments (comments , match .group (0 )),
114
+ content ,
115
+ flags = re .DOTALL )
116
+ if path .suffix == '.py' or path .suffix == '.gn' :
117
+ content = re .sub (
118
+ # docstrings not supported
119
+ r'\s\#[^\n]+' ,
120
+ lambda match : _replace_comments (comments , match .group (0 )),
121
+ content ,
122
+ flags = re .DOTALL )
123
+ replace_comments = True
124
+
105
125
file_subs = 0
106
126
for regex_pair in regex_iter :
107
127
content , sub_count = regex_pair .pattern .subn (regex_pair .replacement , content )
108
128
file_subs += sub_count
109
129
if file_subs > 0 :
130
+ if replace_comments :
131
+ # restore comments
132
+ content = re .sub (PLACE_HOLDER + r'(\d+):' ,
133
+ lambda match : _restore_comments (comments , match .group (1 )), content )
134
+
110
135
substituted_content = content .encode (encoding )
111
136
input_file .seek (0 )
112
137
input_file .write (content .encode (encoding ))
@@ -115,6 +140,19 @@ def _substitute_path(path, regex_iter):
115
140
return (None , None )
116
141
117
142
143
+ # use a randomized placeholder for comment replacements
144
+ PLACE_HOLDER = ':C7yae7ozv:'
145
+
146
+
147
+ def _replace_comments (comments , comment ):
148
+ comments .append (comment )
149
+ return PLACE_HOLDER + str (len (comments )) + ':'
150
+
151
+
152
+ def _restore_comments (comments , index ):
153
+ return comments [int (index ) - 1 ]
154
+
155
+
118
156
def _validate_file_index (index_file , resolved_tree , cache_index_files ):
119
157
"""
120
158
Validation of file index and hashes against the source tree.
0 commit comments