forked from micropython/micropython-lib
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre.py
206 lines (166 loc) · 5.98 KB
/
re.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
importsys
importffilib
importarray
importuctypes
pcre2=ffilib.open("libpcre2-8")
# pcre2_code *pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE length,
# uint32_t options, int *errorcode, PCRE2_SIZE *erroroffset,
# pcre2_compile_context *ccontext);
pcre2_compile=pcre2.func("p", "pcre2_compile_8", "siippp")
# int pcre2_match(const pcre2_code *code, PCRE2_SPTR subject,
# PCRE2_SIZE length, PCRE2_SIZE startoffset, uint32_t options,
# pcre2_match_data *match_data, pcre2_match_context *mcontext);
pcre2_match=pcre2.func("i", "pcre2_match_8", "Psiiipp")
# int pcre2_pattern_info(const pcre2_code *code, uint32_t what,
# void *where);
pcre2_pattern_info=pcre2.func("i", "pcre2_pattern_info_8", "Pip")
# PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *match_data);
pcre2_get_ovector_pointer=pcre2.func("p", "pcre2_get_ovector_pointer_8", "p")
# pcre2_match_data *pcre2_match_data_create_from_pattern(const pcre2_code *code,
# pcre2_general_context *gcontext);
pcre2_match_data_create_from_pattern=pcre2.func(
"p", "pcre2_match_data_create_from_pattern_8", "Pp"
)
# PCRE2_SIZE that is of type size_t.
# Use ULONG as type to support both 32bit and 64bit.
PCRE2_SIZE_SIZE=uctypes.sizeof({"field": 0|uctypes.ULONG})
PCRE2_SIZE_TYPE="L"
# Real value in pcre2.h is 0xFFFFFFFF for 32bit and
# 0x0xFFFFFFFFFFFFFFFF for 64bit that is equivalent
# to -1
PCRE2_ZERO_TERMINATED=-1
IGNORECASE=I=0x8
MULTILINE=M=0x400
DOTALL=S=0x20
VERBOSE=X=0x80
PCRE2_ANCHORED=0x80000000
# TODO. Note that Python3 has unicode by default
ASCII=A=0
UNICODE=U=0
PCRE2_INFO_CAPTURECOUNT=0x4
classPCREMatch:
def__init__(self, s, num_matches, offsets):
self.s=s
self.num=num_matches
self.offsets=offsets
defgroup(self, *n):
ifnotn:
returnself.s[self.offsets[0] : self.offsets[1]]
iflen(n) ==1:
returnself.s[self.offsets[n[0] *2] : self.offsets[n[0] *2+1]]
returntuple(self.s[self.offsets[i*2] : self.offsets[i*2+1]] foriinn)
defgroups(self, default=None):
assertdefaultisNone
returntuple(self.group(i+1) foriinrange(self.num-1))
defstart(self, n=0):
returnself.offsets[n*2]
defend(self, n=0):
returnself.offsets[n*2+1]
defspan(self, n=0):
returnself.offsets[n*2], self.offsets[n*2+1]
classPCREPattern:
def__init__(self, compiled_ptn):
self.obj=compiled_ptn
defsearch(self, s, pos=0, endpos=-1, _flags=0):
assertendpos==-1, "pos: %d, endpos: %d"% (pos, endpos)
buf=array.array("i", [0])
pcre2_pattern_info(self.obj, PCRE2_INFO_CAPTURECOUNT, buf)
cap_count=buf[0]
match_data=pcre2_match_data_create_from_pattern(self.obj, None)
num=pcre2_match(self.obj, s, len(s), pos, _flags, match_data, None)
ifnum==-1:
# No match
returnNone
ov_ptr=pcre2_get_ovector_pointer(match_data)
# pcre2_get_ovector_pointer return PCRE2_SIZE
ov_buf=uctypes.bytearray_at(ov_ptr, PCRE2_SIZE_SIZE* (cap_count+1) *2)
ov=array.array(PCRE2_SIZE_TYPE, ov_buf)
# We don't care how many matching subexpressions we got, we
# care only about total # of capturing ones (including empty)
returnPCREMatch(s, cap_count+1, ov)
defmatch(self, s, pos=0, endpos=-1):
returnself.search(s, pos, endpos, PCRE2_ANCHORED)
defsub(self, repl, s, count=0):
ifnotcallable(repl):
assert"\\"notinrepl, "Backrefs not implemented"
res=""
whiles:
m=self.search(s)
ifnotm:
returnres+s
beg, end=m.span()
res+=s[:beg]
ifcallable(repl):
res+=repl(m)
else:
res+=repl
s=s[end:]
ifcount!=0:
count-=1
ifcount==0:
returnres+s
returnres
defsplit(self, s, maxsplit=0):
res= []
whileTrue:
m=self.search(s)
g=None
ifm:
g=m.group(0)
ifnotmornotg:
res.append(s)
returnres
beg, end=m.span(0)
res.append(s[:beg])
ifm.num>1:
res.extend(m.groups())
s=s[end:]
ifmaxsplit>0:
maxsplit-=1
ifmaxsplit==0:
res.append(s)
returnres
deffindall(self, s):
res= []
start=0
whileTrue:
m=self.search(s, start)
ifnotm:
returnres
ifm.num==1:
res.append(m.group(0))
elifm.num==2:
res.append(m.group(1))
else:
res.append(m.groups())
beg, end=m.span(0)
start=end
defcompile(pattern, flags=0):
errcode=bytes(4)
erroffset=bytes(4)
regex=pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, flags, errcode, erroffset, None)
assertregex
returnPCREPattern(regex)
defsearch(pattern, string, flags=0):
r=compile(pattern, flags)
returnr.search(string)
defmatch(pattern, string, flags=0):
r=compile(pattern, flags|PCRE2_ANCHORED)
returnr.search(string)
defsub(pattern, repl, s, count=0, flags=0):
r=compile(pattern, flags)
returnr.sub(repl, s, count)
defsplit(pattern, s, maxsplit=0, flags=0):
r=compile(pattern, flags)
returnr.split(s, maxsplit)
deffindall(pattern, s, flags=0):
r=compile(pattern, flags)
returnr.findall(s)
defescape(s):
res=""
forcins:
if"0"<=c<="9"or"A"<=c<="Z"or"a"<=c<="z"orc=="_":
res+=c
else:
res+="\\"+c
returnres