-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.py
127 lines (104 loc) · 3.2 KB
/
Core.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
import re
def remove_everything_between(subs1, subs2, line):
regex = re.compile(subs1 + r'.*' + subs2)
return regex.sub('', line)
def remove_everything_before(subs, line):
regex = re.compile(r'.*' + subs)
return regex.sub('', line)
def remove_everything_past(subs, line):
regex = re.compile(subs + r'.*')
return regex.sub('', line)
def is_in_property_set():
return in_property_set
def enter_property_set():
global in_property_set
in_property_set = True
return
def exit_property_set():
global in_property_set
in_property_set = False
return
def has_data():
return has_data
def remove_blank_lines(lines):
replacedLines = []
for i,line in enumerate(lines):
if line != '\n':
replacedLines.append(line)
return replacedLines
def replace_tabs(lines):
replacedLines = []
for line in lines:
result = str()
for c in line:
if c == '\t':
result += ' ';
else:
result += c
replacedLines.append(result)
return replacedLines
def remove_trailing_white_spaces(lines):
stripped_lines = []
in_line = False
for line in lines:
result = str()
in_line = False
for c in line:
if c == ' ' and in_line == True:
result+=c
if c != ' ':
in_line = True
result+=c
stripped_lines.append(result)
return stripped_lines
def remove_comments(lines):
start, end = '/*', '*/'
escaped_start, escaped_end = '/\*', '\*/'
in_comment = False
newlines = []
for line in lines:
if not in_comment:
start_pos = line.find(start)
if start_pos != -1:
in_comment = True
end_pos = line.find(end)
# inline multiline comment
if start_pos < end_pos:
line = remove_everything_between(escaped_start, escaped_end, line)
in_comment = False
else:
line = remove_everything_past(escaped_start, line)
else:
end_pos = line.find(end)
if end_pos != -1:
line = remove_everything_before(escaped_end, line)
in_comment = False
start_pos = line.find(start)
# start of another comment on the same line
if start_pos != -1:
line = remove_everything_past(escaped_start, line)
in_comment = True
else:
line = ''
newlines.append(line)
return newlines
def optimize_lines(lines):
result = []
for line in lines:
line = re.sub(r'[ ]*[{]', "{", line)
line = re.sub(r'[ ]*[}]', "}", line)
line = re.sub(r'[ ]*[:]', ":", line)
line = re.sub(r'[:][ ]*', ":", line)
line = re.sub(r'[ ]*[;]$]', ";", line)
line = re.sub(r'[ ]*[\n]$]', "\n", line)
result.append(line)
return result
def condense_lines(lines):
final = []
for line in lines:
result = str()
for c in line:
if c != '\n':
result+= c
final.append(result)
return final