/
sipclean.py
61 lines (45 loc) · 1.35 KB
/
sipclean.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
from re import compile
import re
repls = [
('comments', (compile('\s+//.*'), '')),
('wxunused', (compile('WXUNUSED\(\s*(\w+)\s*\)'), '$1')),
('inline', (compile('inline'), ''))
]
def bigrepl(s):
for name, (pattern, repl) in repls:
s, num_subs = pattern.subn(repl, s)
return s
def test_repl():
s = \
'''
// test
WXUNUSED(4)
'''
s2 = \
'''
4
'''
assert bigrepl(s) == s2
def matchhelp(pattern, string, repl='_._'):
r = re.compile(pattern)
m = r.search(string)
if m:
print('a match!')
i = 0
while m:
m_start = m.start()
m_end = m.end()
i += 1
print( ('%d) start: %d, end: %d, str: %s') %
(i, m_start, m_end, string[m_start:m_end]) )
if m.groups(): # capturing groups
print(' groups: ' + str(m.groups()))
if m_end == len(string): # infinite loop if
break # m_start == m_end == len(string)
elif m_start == m_end: # zero-width match;
m_end += 1 # keep things moving along
m = r.search(string, m_end)
print( ('global replace (%s):\n%s') %
(repl, re.sub(pattern, repl, string)) )
else:
print('not a match')