-
Notifications
You must be signed in to change notification settings - Fork 4
/
shortencpp.py
executable file
·285 lines (256 loc) · 9.39 KB
/
shortencpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
import re
import sys
IDENTIFIER_REGEX = "[a-zA-Z_][a-zA-Z0-9_]+"
CPP_QUALIFIED_NAME_REGEX = "%s(::%s)*" % (IDENTIFIER_REGEX,
IDENTIFIER_REGEX)
string_replace = [
("std::__1", "std"),
("std::basic_string<char, std::char_traits<char>, std::allocator<char> >", "std::string"),
("std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >", "std::basic_ostringstream"),
("std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >", "std::stringbuf"),
("std::basic_istream<char, std::char_traits<char> >", "std::istream"),
("std::basic_ostream<char, std::char_traits<char> >", "std::ostream"),
("std::basic_iostream<char, std::char_traits<char> >", "std::iostream"),
]
pairs = [(ord('<'), ord('>')), (ord('('), ord(')')), (ord('['), ord(']')),
(ord('{'), ord('}'))]
def dump_string_as_array_of_chars(s):
for (i, c) in enumerate(s):
print '[%u] %s' % (i, c)
class string_range:
def __init__(self, start, end):
self.start = start
self.end = end
def get_amount_and_index(ch):
global pairs
for (i, cp) in enumerate(pairs):
if ch == cp[0]:
return (+1, i)
if ch == cp[1]:
return (-1, i)
return (0, 0)
def find_matching_char(s, pos):
'''Given a string and a position that points to a starting character in
"char_pairs", find the matching terminating character of the character
pair'''
ai = get_amount_and_index(ord(s[pos]))
if ai[0] == +1:
counts = list()
for i in range(len(pairs)):
counts.append(0)
counts[ai[1]] += ai[0]
for i in range(pos + 1, len(s)):
ai = get_amount_and_index(ord(s[i]))
if ai[0]:
counts[ai[1]] += ai[0]
done = True
for c in counts:
if c != 0:
done = False
if done:
return i
return -1
def find_cpp_arg_end(s, pos, end_pos):
global pairs
while pos < end_pos:
pos_adjusted = False
ch = ord(s[pos])
if ch == ord(','):
return pos
for cp in pairs:
if ch == cp[0]:
pos = find_matching_char(s, pos)
if pos == -1:
return -1
pos += 1
pos_adjusted = True
break
if ch == cp[1]:
print 'unexpected %c character at index %u of "%s"' % (ch, pos,
s)
return -1
if not pos_adjusted:
pos += 1
return pos
class template_splitter:
def __init__(self, s):
self.s = s
self.name = None
self.params = list()
self.template_start = -1
self.template_end = -1
template_start_re = re.compile("(%s)(<)" % (CPP_QUALIFIED_NAME_REGEX))
m = template_start_re.search(s)
if m:
self.template_start = m.start(3)
self.template_end = find_matching_char(s, self.template_start)
if self.template_end > 0:
self.name = string_range(m.start(1), m.end(1))
arg_start = self.template_start + 1
while arg_start < self.template_end:
arg_end = find_cpp_arg_end(s, arg_start, self.template_end)
if arg_end == -1:
print 'error: unexpected arg end not found'
while s[arg_start].isspace():
arg_start += 1
while s[arg_end-1].isspace():
arg_end -= 1
self.params.append(string_range(arg_start, arg_end))
arg_start = arg_end + 1
def dump(self, f=sys.stdout):
name = self.get_name()
if name:
print 'name: "%s"' % (name)
for idx in range(self.get_num_params()):
num = idx + 1
name = self.get_param(num)
print 'param%u = "%s"' % (num, name)
def get_substr(self, range):
return self.s[range.start:range.end]
def get_name(self):
if self.name is None:
return None
return self.get_substr(self.name)
def get_num_params(self):
return len(self.params)
def get_param(self, number):
'''Parameter number starts at 1.'''
idx = number - 1
if idx >= 0 and idx < len(self.params):
return self.get_substr(self.params[idx])
return None
@classmethod
def shorten(cls, s):
debug = False
remaining = s
result = None
while True:
if debug:
print 'remaining = "%s"' % (remaining)
t = template_splitter(remaining)
if t.name is None:
processed = remaining
remaining = None
else:
if debug:
t.dump()
(processed, remaining) = t.__shorten_template()
if debug:
print 'processed = "%s"' % (processed)
if result:
result += processed
else:
result = processed
if remaining is None or len(remaining) == 0:
return result
def __shorten_template(self):
default_allocator_2 = ['std::vector', 'std::__vector_base', 'std::__split_buffer']
name = self.get_name()
result = None
if name == 'std::map' or name == 'std::multimap':
result = self.__shorten_std_map()
if name in default_allocator_2:
result = self.__shorten_template_with_default_allocator()
if result is None:
return (self.s[0:self.template_end+1], self.s[self.template_end+1:])
else:
return result
def __shorten_template_with_default_allocator(self):
'''If the template is a template with a second argument that is an
allocator, we can remove the allocator. For example:
std::vector<std::string, std::allocator<std::string > >::~vector()
Can be shortened to:
std::vector<std::string>::~vector()
'''
debug = False
if debug:
print 'template with default allocator as arg2: "%s"' % (self.s)
num_params = self.get_num_params()
if num_params != 2:
return None
allocator = template_splitter(self.get_param(2))
if debug:
allocator.dump()
if allocator.get_name() != 'std::allocator':
return None
if self.get_param(1) != allocator.get_param(1):
return None
remove_start = self.params[0].end
remove_end = self.params[1].end
if self.s[remove_end] == ' ':
remove_end += 1
end = self.template_end+1
short = self.s[0:remove_start] + self.s[remove_end:end]
if debug:
print 'default allocator can be shortened: "%s"' % (short)
return (short, self.s[end:])
def __shorten_std_map(self):
'''If the template is a std::map or std::multimap and can be shortened
return a shortened string, else return None'''
debug = False
if debug:
print 'std::map template: "%s"' % (self.s)
num_params = self.get_num_params()
if num_params != 4:
return None
comparison = template_splitter(self.get_param(3))
if comparison.get_name() != "std::less":
return None
if debug:
comparison.dump()
if comparison.get_num_params() != 1:
return None
param1 = self.get_param(1)
if comparison.get_param(1) != param1:
return None
if debug:
print 'std::less is default'
allocator = template_splitter(self.get_param(4))
if debug:
allocator.dump()
if allocator.get_name() != 'std::allocator':
return None
pair = template_splitter(allocator.get_param(1))
if debug:
pair.dump()
pair_first = pair.get_param(1)
if not (pair_first == param1 or pair_first == param1 + " const"):
return None
pair_second = pair.get_param(2)
if pair_second != self.get_param(2):
return None
remove_start = self.params[1].end
remove_end = self.params[3].end+1
end = self.template_end+1
short = self.s[0:remove_start] + self.s[remove_end:end]
if debug:
print 'std::map can be shortened: "%s"' % (short)
return (short, self.s[end:])
def shorten_string(s):
global string_replace
for r in string_replace:
s = s.replace(r[0], r[1])
short = template_splitter.shorten(s)
if short:
return short
return s
def main():
# strs = ["FUNC a260 33 0 std::__split_buffer<AA, std::allocator<AA>&>::clear()", "FUNC a260 33 0 std::__split_buffer<AA, std::allocator<AA> >::clear()"]
# for s in strs:
# print 'orig = "%s"' % (s)
# s = shorten_string(s)
# print 'sstr = "%s"' % (s)
# short = template_splitter.shorten(s)
# print 'tmpl = "%s"' % (short)
# return
paths = sys.argv[1:]
for path in paths:
f = open(path, 'r')
lines = f.readlines()
n_lines = len(lines)
for i in range(n_lines):
line = shorten_string(lines[i])
print line,
if __name__ == '__main__':
main()