-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcre.py
executable file
·231 lines (197 loc) · 6.79 KB
/
pcre.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
#!usr/bin/env python
# pypcre.py
#
# Copyright 2009 ahmed youssef <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
from defs import *
class Regex(object):
def __init__(self, pattern, flags=PCRE_ANCHORED):
self.reg = self.compile(pattern, flags)
self.flags = flags
self.capcount = captured_count(self.reg)
self.flags = 0
def compile(self, pattern, flags):
return compile(pattern, flags)
def match(self, against, flags=None):
if flags is None:
flags = self.flags
self._lastmatchagainst = against
res = exec_match(self.reg, against, flags)
if res:
return Match(self.reg, against, flags, *res)
return None
def isearch(self, s, flags=None):
x = 0
while x < len(s):
m = self.match(s[x:], flags)
if m:
# s=s[m.end():]
yield m
x += m.end()
else:
x += 1
def ifindall(self, s, flags=None):
x = 0
while x < len(s):
m = self.match(s[x:], flags)
if m:
yield m.group()
x += m.end()
else:
x += 1
def isplit(self, s, flags=None):
x = 0
lastafter = None
while x < len(s):
m = self.match(s[x:], flags)
firsttime = True
if m:
before = s[:x + m.start()]
after = s[x + m.end():]
lastafter = after
if firsttime:
yield before
firsttime = False
else:
yield after
s = s[x + m.end():]
else:
x += 1
yield lastafter
def sub(self, repl, s, flags=None):
x = 0
while x < len(s):
m = self.match(s[x:], flags)
if m:
sbeforematch = s[:x]
sinmatch = s[x + m.start():x + m.end()]
safter = s[x + m.end():]
s = sbeforematch + repl + safter
x = m.end()
else:
x += 1
return s
replace = sub
def subn(self, repl, s, flags=None, count=10):
x = 0
while x < len(s) and count != 0:
m = self.match(s[x:], flags)
if m:
sbeforematch = s[:x]
sinmatch = s[x + m.start():x + m.end()]
safter = s[x + m.end():]
s = sbeforematch + repl + safter
count -= 1 # dec
x = m.end()
else:
x += 1
return s
replacen = subn
def expand(self):
raise NotImplementedError
class Match(object):
def __init__(self, reg, against, flags, rc, ovec):
self.reg = reg
self.against = bytes(against, encoding='utf8')
self.flags = flags
self._rc = rc
self._ovec = ovec
self.capcount = captured_count(self.reg)
self._groupdict = {}
def groups(self):
for i in range(self.capcount+1):
if self.group(i):
yield self.group(i)
#FIXME: build index here not just fetch the names
## TOO SLOW.
def get_name_table(self):
p = c_char_p()
namecount = captured_count(self.reg)
entrysize = c_int()
table = c_char_p()
capcount = captured_count(self.reg)
pcre_fullinfo(self.reg, None, PCRE_INFO_NAMEENTRYSIZE, byref(entrysize))
pcre_fullinfo(self.reg, None, PCRE_INFO_NAMETABLE, byref(table))
tbl = cast(table, POINTER(c_char))
#WIP
groups = []
for i in range(namecount):
# key starts from tbl[2]
gname = b""
idx = 2
while tbl[idx] != b"\x00":
gname += (tbl[idx])
idx += 1
groups.append(gname.decode('utf8'))
# import pdb; pdb.set_trace()
void_p = cast(tbl, c_voidp).value+entrysize.value
tbl = cast(void_p, POINTER(c_char))
return groups
def groupdict(self):
for gname in self.get_name_table():
gname = str(gname)
self._groupdict[gname] = self.group(gname)
return self._groupdict
def group(self, n=0):
# n: int or n:string
if isinstance(n, int):
# groupIndex
if n > self.capcount:
raise IndexError('groupIndex<%d> must be < %d' %
(n, self.capcount))
i = n * 2
if self._ovec[i] < 0:
return None
return self.against[self._ovec[i]: self._ovec[i + 1]]
elif isinstance(n, str):
# groupByName
gidx = self._group_index_by_name(n)
return self.group(gidx)
# EXPERIMENTAL
# int pcre_get_named_substring(const pcre *code,
# const char *subject, int *ovector,
# int stringcount, const char *stringname,
# const char **stringptr);
def group_by_name(self, n):
n = bytes(n, encoding='utf8')
p = c_char_p()
r = pcre_get_named_substring(self.reg, c_char_p(
self.against), self._ovec, self._rc, c_char_p(n), byref(p))
return p.value
def _group_index_by_name(self, n):
n = bytes(n, encoding='utf8')
return pcre_get_stringnumber(self.reg, c_char_p(n))
def span(self, n=0):
return self.start(n), self.end(n) # tuple
def start(self, n=0):
if isinstance(n, str):
n = self._group_index_by_name(n)
return self.start(n)
elif isinstance(n, int):
return self._ovec[n * 2]
else:
raise ValueError
def end(self, n=0):
if isinstance(n, str):
n = self._group_index_by_name(n)
return self.end(n)
elif isinstance(n, int):
return self._ovec[n * 2 + 1]
else:
raise ValueError
def __str__(self):
return "pcre.Match<{_id}> span: {span}".format(_id=id(self), span=self.span())