-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxypcre.xyi
306 lines (287 loc) · 11.9 KB
/
xypcre.xyi
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* XYplorerPCRE aka xypcre v1.3.1
Alternative to default RegExMatch() and RegExReplace()
url:http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=14569
allows XYplorer to use a PCRE-compatible RegularExpression
engine instead of the limited Visual Basic implementation.
This is achieved by offloading regexp operations to an Au3
utility, xypcre.exe (AutoIt3 uses the PCRE regexp library)
Therefore, the bundled xypcre.exe must be available to the
the functions. Place xypcre at <xyscripts>\xypcre.exe. But
the functions can download xypcre.exe to the expected path
at runtime. See the xypcrefind() function for details. */
NAMESPACE xypcre
/*CORE :pcrematch(),pcrereplace(),pcrecapture(),pcresplit()
**DEPEND:xypcrefind(),xypcrewaiter()
**HELPER:pcretoken()
*/
/*
pcrematch()
Finds regexp pattern match(es) in a given string.
$string String to work on (haystack).
$pattern The RegExp pattern to match in string (needle).
$sep Separator between matches. Should be at least two
chars wide to account for strings that may already
contain $sep characters.
This parameter is irrelevant if $format == 2.
$index 1-based index of a single match to return.
No effect if < 1. Returns last match if > total.
$format 0 simple $sep-separated tokens.
1 Escape tokens against $sep character(s).
Escaped as [$][s][e][p]
2 create a special tokenlist in this format:
token1length,token2length|token1token2
Default value is 2. Values cannot be combined.
Return: list of matches.
*/
FUNCTION pcrematch($string, $pattern, $sep='||', $index=0, $format=2) {
//locate and validate the xypcre utility
$xypcre = xypcrefind();
//setup the interapp-flow-synchronizer variable
$IFS = '$P_UDF_pcre_IFS'; $k = 0;
//find a unique variable name
while (isset(*$IFS)) { $IFS = '$P_UDF_pcre_IFS'.$k; $k++; }
unset $k; perm *$IFS;
//now $IFS points to this session's IFS var as *$IFS
$op = 0; //operation mode
//run xypcre, and provide XY hwnd and the IFS var name
//xypcre will return it's gui and main hwnd in *$IFS
run $xypcre.' '.<hwnd>.' '.quote($IFS);
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 0) == 1) { return; }
$xypcreHwnd = gettoken(*$IFS, 1, '|'); //hwnd to send msg
$killHwnd = gettoken(*$IFS, 2, '|'); //main hwnd to kill xypcre
set *$IFS, '';
// send parameters
// xypcre acknowledeges reception by sending loop-breaker (by touching *$IFS)
// also keep time, and opt to stop if taking too long (xypcre hang/crash)
copydata $xypcreHwnd, $op, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $string, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $pattern, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $sep, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $index, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $format, 0;
//all params sent, wait till final return, IDed by $op prefix
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 2, $op) == 1) { return; }
$return = *$IFS; unset *$IFS; //result is passed into *$IFS
return $return;
}
/*
pcrecapture()
Finds captured groups of a regexp pattern in a given string.
$string String to work on (haystack).
$pattern The RegExp pattern with capturing group to match.
$index 1-based index of the captured group to return.
Returns 1st group if < 1 and last one if > total.
Pass named groups by their ordinal index.
$sep same as in pcrematch().
$format same as in pcrematch().
Return: List of strings matching specified capturing group.
*/
FUNCTION pcrecapture($string, $pattern, $index=1, $sep='||', $format=2) {
$xypcre = xypcrefind();
if ($xypcre == '') || ($xypcre == 'xypcrefind()') { return; }
$IFS = '$P_UDF_pcre_IFS'; $k = 0;
while (isset(*$IFS)) { $IFS = '$P_UDF_pcre_IFS'.$k; $k++; }
unset $k; perm *$IFS;
$op = 1;
run $xypcre.' '.<hwnd>.' '.quote($IFS);
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 0) == 1) { return; }
$xypcreHwnd = gettoken(*$IFS, 1, '|');
$killHwnd = gettoken(*$IFS, 2, '|');
set *$IFS, '';
copydata $xypcreHwnd, $op, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $string, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $pattern, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $index, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $sep, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $format, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 2, $op) == 1) { return; }
$return = *$IFS; unset *$IFS;
return $return;
}
/*
pcrereplace()
Replaces parts of a string, using a regexp pattern.
$string String to work on (haystack).
$pattern The RegExp pattern to match in string (needle).
$replace The string or pattern to replace with.
Return: resulting string after replacement.
*/
FUNCTION pcrereplace($string, $pattern, $replace) {
$xypcre = xypcrefind();
if ($xypcre == '') || ($xypcre == 'xypcrefind()') { return; }
$IFS = '$P_UDF_pcre_IFS'; $k = 0;
while (isset(*$IFS)) { $IFS = '$P_UDF_pcre_IFS'.$k; $k++; }
unset $k; perm *$IFS;
$op = 2;
run $xypcre.' '.<hwnd>.' '.quote($IFS);
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 0) == 1) { return; }
$xypcreHwnd = gettoken(*$IFS, 1, '|');
$killHwnd = gettoken(*$IFS, 2, '|');
set *$IFS, '';
copydata $xypcreHwnd, $op, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $string, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $pattern, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $replace, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 2, $op) == 1) { return; }
$return = *$IFS; unset *$IFS;
return $return;
}
/*pcresplit()
Splits a string into substrings at each position where
the given regexp pattern matches.
$string String to work on.
$pattern The RegExp pattern to match.
The matching region is removed while splitting.
$sep token separator. See pcrematch().
$format format of return. See pcrematch().
Returns: split substrings.
*/
FUNCTION pcresplit($string, $pattern, $sep='||', $format=2) {
$xypcre = xypcrefind();
if ($xypcre == '') || ($xypcre == 'xypcrefind()') { return; }
$IFS = '$P_UDF_pcre_IFS'; $k = 0;
while (isset(*$IFS)) { $IFS = '$P_UDF_pcre_IFS'.$k; $k++; }
unset $k; perm *$IFS;
$op = 3;
run $xypcre.' '.<hwnd>.' '.quote($IFS);
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 0) == 1) { return; }
$xypcreHwnd = gettoken(*$IFS, 1, '|');
$killHwnd = gettoken(*$IFS, 2, '|');
set *$IFS, '';
copydata $xypcreHwnd, $op, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $string, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $pattern, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $sep, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd) == 1) { return; }
copydata $xypcreHwnd, $format, 0;
if (xypcrewaiter($IFS, $xypcre, $killHwnd, 2, $op) == 1) { return; }
$return = *$IFS; unset *$IFS;
return $return;
}
/*====== EXTRA FUNCTIONS ======*/
/*
pcretoken()
Extracts a match from formatted xypcre returns.
$matches pcre UDF formatted source matchlist
$index 'count': return total match count
integer: 1-based match index to return.
$format format of return. See pcrematch().
$sep token separator. See pcrematch().
Return: specified token or token count.
*/
FUNCTION pcretoken($matches, $index=1, $format=2, $sep='||') {
//make sure $index is valid
if ($index != 'count') && ($index*1 == 0) { $index = 1; }
if ($format == 0) { //simple tokenlist
$return = gettoken($matches, $index, $sep);
} elseif ($format == 1) { //escaped tokenlist
$return = gettoken($matches, $index, $sep);
if ($index == 'count') { return $return; }
$uniq = ''; //uniq char container, save loops
$len = strlen($sep);
while ($c++ < $len) {
$chr = substr($sep, $c-1, 1);
if (strpos($uniq, $chr,, 1) == -1) {
$uniq = $uniq.$chr;
$return = replace($return, '['.$chr.']', $chr, 1);
}
}
} elseif ($format == 2) { //xypcre-format tokenlist
$xlen = gettoken($matches, 1, '|'); //all lengths
if ($index == 'count') { return gettoken($xlen, 'count', '+'); }
$matches = substr($matches, strlen($xlen)+1); //extract matches
$len = gettoken($xlen, $index, '+', 't'); //return token legth
$xlen = gettoken($xlen, $index-1, '+', 't', 1); //lengths before ret.
$xlen = eval($xlen) + 0; //calc ret.token start
$return = substr($matches, $xlen, $len);
} else {
$return = '';
}
return $return;
}
/*====== HELPER FUNCTIONS ======*/
/*core functions will not work without these*/
FUNCTION xypcrewaiter(&$IFS, &$xypcre, &$killHwnd, $phase=1, $op='.') {
//Wait till getting next msg in *$IFS. May touch *$IFS, $xypcre
//Return 0:OK/CONTINUE,1:BAD/EXIT
//phase 0: initial only stop udf on abort
// 1: interim stop xypcre + udf on abort
// 2: stopper last msg step check for $op in data
$msg = "RegExp engine Nonresponsive. Abort with empty return?"; //abort msg
$fmt = 'yyyy-mm-dd hh:nn:ss'; //time format
$wait = 9; //waiting period in seconds before abort notice
$time = now($fmt);
if ($phase == 2) {
//in last step, xypcre send result in copydata as string
while (substr(get('CopiedData', 3), 0, 1) != $op) {
if (datediff($time,, 's') > $wait) {
if (confirm($msg,, 2)) {
run $xypcre.' abort '.$killHwnd;
unset *$IFS;
return 1;
} else {
$time = now($fmt);
}
}
wait 1; continue;
}
set *$IFS, substr(gettoken(get('CopiedData'), 3, '|',, 2), 1);
} else {
//otherwise xypcre resets *$IFS
while (*$IFS == '') {
if (datediff($time,, 's') > $wait) { //ask to abort if $wait time gone
if (confirm($msg,, 2)) {
//clean up before aborting
if ($phase!=0){ run $xypcre.' abort '.$killHwnd; }
unset *$IFS;
return 1;
} else {
$time = now($fmt);
}
}
wait 1; continue;
}
if ($phase == 1) { set *$IFS, ''; }
//$IFS value left as is in phase 0
//if ($phase == 0) { }
}
return 0; //all OK!
}
FUNCTION xypcrefind() {
// tries to find xypcre in paths $l1,$l2,$l3
$l1 = "<xyscripts>\xypcre.exe";
$l2 = "<xydata>\xypcre.exe";
$l3 = "<xypath>\xypcre.exe";
if (isset($P_UDF_pcre_xypcre) == 1) { $xypcre = $P_UDF_pcre_xypcre, 'r'; }
elseif (exists($l1) == 1) { $xypcre = $l1; }
elseif (exists($l2) == 1) { $xypcre = $l2; }
elseif (exists($l3) == 1) { $xypcre = $l3; }
else {
echo "Downloading xypcre to<crlf>$l1", "XYPCRE not found";
download 'https://github.com/bdeshi/xypcre/releases/download/v1.3.1/xypcre.exe', $l1, 'o';
$xypcre = $l1;
}
if !(strpos(gpc($xypcre, 'ext'), 'exe')) {
$identity = property('System.Software.ProductName', $xypcre);
} else {
$identity = gettoken(runret($xypcre), 1, <crlf>);
}
assert $identity == 'XYplorerPCRE', "Cannot find xypcre: $xypcre";
return $xypcre;
}