1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
package com.stevesoft.pat; |
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
@see |
15 |
|
|
|
|
| 0% |
Uncovered Elements: 294 (294) |
Complexity: 86 |
Complexity Density: 0.45 |
|
16 |
|
class parsePerl |
17 |
|
{ |
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
18 |
0 |
final static char close(char c)... |
19 |
|
{ |
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
0 |
if (c == '<') |
28 |
|
{ |
29 |
0 |
return '>'; |
30 |
|
} |
31 |
0 |
if (c == '[') |
32 |
|
{ |
33 |
0 |
return ']'; |
34 |
|
} |
35 |
0 |
if (c == '(') |
36 |
|
{ |
37 |
0 |
return ')'; |
38 |
|
} |
39 |
0 |
if (c == '{') |
40 |
|
{ |
41 |
0 |
return '}'; |
42 |
|
} |
43 |
0 |
return c; |
44 |
|
} |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
46 |
0 |
final public static String codify(String s, boolean keepbs)... |
47 |
|
{ |
48 |
0 |
return codify(s, 0, s.length(), keepbs); |
49 |
|
} |
50 |
|
|
|
|
| 0% |
Uncovered Elements: 79 (79) |
Complexity: 20 |
Complexity Density: 0.36 |
|
51 |
0 |
final public static String codify(String s, int i0, int iN,... |
52 |
|
boolean keepbs) |
53 |
|
{ |
54 |
0 |
StringBuffer sb = new StringBuffer(); |
55 |
0 |
boolean ucmode = false, lcmode = false, litmode = false; |
56 |
0 |
boolean uc1 = false, lc1 = false; |
57 |
0 |
boolean modified = false; |
58 |
0 |
for (int i = i0; i < iN; i++) |
59 |
|
{ |
60 |
0 |
char c = s.charAt(i); |
61 |
0 |
boolean mf = true, app = true; |
62 |
0 |
if (c == '\\') |
63 |
|
{ |
64 |
0 |
app = false; |
65 |
0 |
i++; |
66 |
0 |
if (i < s.length()) |
67 |
|
{ |
68 |
0 |
char c2 = s.charAt(i); |
69 |
0 |
switch (c2) |
70 |
|
{ |
71 |
0 |
case 'Q': |
72 |
0 |
litmode = true; |
73 |
0 |
break; |
74 |
0 |
case 'U': |
75 |
0 |
ucmode = true; |
76 |
0 |
break; |
77 |
0 |
case 'L': |
78 |
0 |
lcmode = true; |
79 |
0 |
break; |
80 |
0 |
case 'u': |
81 |
0 |
uc1 = true; |
82 |
0 |
break; |
83 |
0 |
case 'l': |
84 |
0 |
lc1 = true; |
85 |
0 |
break; |
86 |
0 |
case 'E': |
87 |
0 |
uc1 = lc1 = ucmode = lcmode = litmode = false; |
88 |
0 |
break; |
89 |
0 |
default: |
90 |
0 |
if (keepbs) |
91 |
|
{ |
92 |
0 |
sb.append('\\'); |
93 |
|
} |
94 |
0 |
c = c2; |
95 |
0 |
if (keepbs) |
96 |
|
{ |
97 |
0 |
mf = false; |
98 |
|
} |
99 |
0 |
app = true; |
100 |
0 |
break; |
101 |
|
} |
102 |
0 |
modified |= mf; |
103 |
|
} |
104 |
|
} |
105 |
0 |
if (app) |
106 |
|
{ |
107 |
0 |
if (lc1) |
108 |
|
{ |
109 |
0 |
c = lc(c); |
110 |
0 |
lc1 = false; |
111 |
|
} |
112 |
0 |
else if (uc1) |
113 |
|
{ |
114 |
0 |
c = uc(c); |
115 |
0 |
uc1 = false; |
116 |
|
} |
117 |
0 |
else if (ucmode) |
118 |
|
{ |
119 |
0 |
c = uc(c); |
120 |
|
} |
121 |
0 |
else if (lcmode) |
122 |
|
{ |
123 |
0 |
c = lc(c); |
124 |
|
} |
125 |
0 |
if (litmode && needbs(c)) |
126 |
|
{ |
127 |
0 |
sb.append('\\'); |
128 |
|
} |
129 |
0 |
sb.append(c); |
130 |
|
} |
131 |
|
} |
132 |
0 |
return modified ? sb.toString() : s; |
133 |
|
} |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
0 |
final static char uc(char c)... |
136 |
|
{ |
137 |
0 |
return CaseMgr.toUpperCase(c); |
138 |
|
} |
139 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
0 |
final static char lc(char c)... |
141 |
|
{ |
142 |
0 |
return CaseMgr.toLowerCase(c); |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 8 |
Complexity Density: 0.89 |
|
145 |
0 |
final static boolean needbs(char c)... |
146 |
|
{ |
147 |
0 |
if (c >= 'a' && c <= 'z') |
148 |
|
{ |
149 |
0 |
return false; |
150 |
|
} |
151 |
0 |
if (c >= 'A' && c <= 'Z') |
152 |
|
{ |
153 |
0 |
return false; |
154 |
|
} |
155 |
0 |
if (c >= '0' && c <= '9') |
156 |
|
{ |
157 |
0 |
return false; |
158 |
|
} |
159 |
0 |
if (c == '_') |
160 |
|
{ |
161 |
0 |
return false; |
162 |
|
} |
163 |
0 |
return true; |
164 |
|
} |
165 |
|
|
|
|
| 0% |
Uncovered Elements: 142 (142) |
Complexity: 43 |
Complexity Density: 0.43 |
|
166 |
0 |
final static Regex parse(String s)... |
167 |
|
{ |
168 |
0 |
boolean igncase = false, optim = false, gFlag = false; |
169 |
0 |
boolean sFlag = false, mFlag = false, xFlag = false; |
170 |
|
|
171 |
0 |
StringBuffer s1 = new StringBuffer(); |
172 |
0 |
StringBuffer s2 = new StringBuffer(); |
173 |
0 |
int i = 0, count = 0; |
174 |
0 |
char mode, delim = '/', cdelim = '/'; |
175 |
0 |
if (s.length() >= 3 && s.charAt(0) == 's') |
176 |
|
{ |
177 |
0 |
mode = 's'; |
178 |
0 |
delim = s.charAt(1); |
179 |
0 |
cdelim = close(delim); |
180 |
0 |
i = 2; |
181 |
|
} |
182 |
0 |
else if (s.length() >= 2 && s.charAt(0) == 'm') |
183 |
|
{ |
184 |
0 |
mode = 'm'; |
185 |
0 |
delim = s.charAt(1); |
186 |
0 |
cdelim = close(delim); |
187 |
0 |
i = 2; |
188 |
|
} |
189 |
0 |
else if (s.length() >= 1 && s.charAt(0) == '/') |
190 |
|
{ |
191 |
0 |
mode = 'm'; |
192 |
0 |
i = 1; |
193 |
|
} |
194 |
|
else |
195 |
|
{ |
196 |
0 |
try |
197 |
|
{ |
198 |
0 |
RegSyntaxError.endItAll("Regex.perlCode should be of the " |
199 |
|
+ "form s/// or m// or //"); |
200 |
|
} catch (RegSyntax rs) |
201 |
|
{ |
202 |
|
} |
203 |
0 |
return null; |
204 |
|
} |
205 |
0 |
for (; i < s.length(); i++) |
206 |
|
{ |
207 |
0 |
if (s.charAt(i) == '\\') |
208 |
|
{ |
209 |
0 |
s1.append('\\'); |
210 |
0 |
i++; |
211 |
|
} |
212 |
0 |
else if (s.charAt(i) == cdelim && count == 0) |
213 |
|
{ |
214 |
0 |
i++; |
215 |
0 |
break; |
216 |
|
} |
217 |
0 |
else if (s.charAt(i) == delim && cdelim != delim) |
218 |
|
{ |
219 |
0 |
count++; |
220 |
|
} |
221 |
0 |
else if (s.charAt(i) == cdelim && cdelim != delim) |
222 |
|
{ |
223 |
0 |
count--; |
224 |
|
} |
225 |
0 |
s1.append(s.charAt(i)); |
226 |
|
} |
227 |
0 |
if (mode == 's' && cdelim != delim) |
228 |
|
{ |
229 |
0 |
while (i < s.length() && Prop.isWhite(s.charAt(i))) |
230 |
|
{ |
231 |
0 |
i++; |
232 |
|
} |
233 |
0 |
if (i >= s.length()) |
234 |
|
{ |
235 |
0 |
try |
236 |
|
{ |
237 |
0 |
RegSyntaxError.endItAll("" + mode + delim + " needs " + cdelim); |
238 |
|
} catch (RegSyntax rs) |
239 |
|
{ |
240 |
|
} |
241 |
0 |
return null; |
242 |
|
} |
243 |
0 |
cdelim = close(delim = s.charAt(i)); |
244 |
0 |
i++; |
245 |
|
} |
246 |
0 |
count = 0; |
247 |
0 |
if (mode == 's') |
248 |
|
{ |
249 |
0 |
for (; i < s.length(); i++) |
250 |
|
{ |
251 |
0 |
if (s.charAt(i) == '\\') |
252 |
|
{ |
253 |
0 |
s2.append('\\'); |
254 |
0 |
i++; |
255 |
|
} |
256 |
0 |
else if (s.charAt(i) == cdelim && count == 0) |
257 |
|
{ |
258 |
0 |
i++; |
259 |
0 |
break; |
260 |
|
} |
261 |
0 |
else if (s.charAt(i) == delim && cdelim != delim) |
262 |
|
{ |
263 |
0 |
count++; |
264 |
|
} |
265 |
0 |
else if (s.charAt(i) == cdelim && cdelim != delim) |
266 |
|
{ |
267 |
0 |
count--; |
268 |
|
} |
269 |
0 |
s2.append(s.charAt(i)); |
270 |
|
} |
271 |
|
} |
272 |
0 |
for (; i < s.length(); i++) |
273 |
|
{ |
274 |
0 |
char c = s.charAt(i); |
275 |
0 |
switch (c) |
276 |
|
{ |
277 |
0 |
case 'x': |
278 |
0 |
xFlag = true; |
279 |
0 |
break; |
280 |
0 |
case 'i': |
281 |
0 |
igncase = true; |
282 |
0 |
break; |
283 |
0 |
case 'o': |
284 |
0 |
optim = true; |
285 |
0 |
break; |
286 |
0 |
case 's': |
287 |
0 |
sFlag = true; |
288 |
0 |
break; |
289 |
0 |
case 'm': |
290 |
0 |
mFlag = true; |
291 |
0 |
break; |
292 |
0 |
case 'g': |
293 |
0 |
gFlag = true; |
294 |
0 |
break; |
295 |
0 |
default: |
296 |
|
|
297 |
|
|
298 |
0 |
try |
299 |
|
{ |
300 |
0 |
RegSyntaxError.endItAll("Illegal flag to pattern: " + c); |
301 |
|
} catch (RegSyntax rs) |
302 |
|
{ |
303 |
|
} |
304 |
0 |
return null; |
305 |
|
} |
306 |
|
} |
307 |
0 |
Regex r = new Regex(); |
308 |
0 |
try |
309 |
|
{ |
310 |
0 |
String pat = s1.toString(), reprul = s2.toString(); |
311 |
0 |
if (xFlag) |
312 |
|
{ |
313 |
0 |
pat = strip(pat); |
314 |
0 |
reprul = strip(reprul); |
315 |
|
} |
316 |
0 |
r.compile(pat); |
317 |
0 |
r.ignoreCase |= igncase; |
318 |
0 |
r.gFlag |= gFlag; |
319 |
0 |
r.sFlag |= sFlag; |
320 |
0 |
r.mFlag |= mFlag; |
321 |
0 |
if (optim) |
322 |
|
{ |
323 |
0 |
r.optimize(); |
324 |
|
} |
325 |
0 |
if (delim == '\'') |
326 |
|
{ |
327 |
0 |
r.setReplaceRule(new StringRule(reprul)); |
328 |
|
} |
329 |
|
else |
330 |
|
{ |
331 |
0 |
r.setReplaceRule(ReplaceRule.perlCode(reprul)); |
332 |
|
} |
333 |
|
} catch (RegSyntax rs) |
334 |
|
{ |
335 |
0 |
r = null; |
336 |
|
} |
337 |
0 |
return r; |
338 |
|
} |
339 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 7 |
Complexity Density: 0.44 |
|
340 |
0 |
static String strip(String s)... |
341 |
|
{ |
342 |
0 |
StringBuffer sb = new StringBuffer(); |
343 |
0 |
for (int i = 0; i < s.length(); i++) |
344 |
|
{ |
345 |
0 |
char c = s.charAt(i); |
346 |
0 |
if (Prop.isWhite(c)) |
347 |
|
{ |
348 |
0 |
; |
349 |
|
} |
350 |
0 |
else if (c == '#') |
351 |
|
{ |
352 |
0 |
i++; |
353 |
0 |
while (i < s.length()) |
354 |
|
{ |
355 |
0 |
if (s.charAt(i) == '\n') |
356 |
|
{ |
357 |
0 |
break; |
358 |
|
} |
359 |
0 |
i++; |
360 |
|
} |
361 |
|
} |
362 |
0 |
else if (c == '\\') |
363 |
|
{ |
364 |
0 |
sb.append(c); |
365 |
0 |
sb.append(s.charAt(++i)); |
366 |
|
} |
367 |
|
else |
368 |
|
{ |
369 |
0 |
sb.append(c); |
370 |
|
} |
371 |
|
} |
372 |
0 |
return sb.toString(); |
373 |
|
} |
374 |
|
} |