| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
package com.stevesoft.pat; |
| 9 |
|
|
| 10 |
|
import java.util.Enumeration; |
| 11 |
|
import java.util.Hashtable; |
| 12 |
|
import java.util.Vector; |
| 13 |
|
|
| 14 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 5 |
Complexity Density: 1.67 |
|
| 15 |
|
class FastChar extends oneChar |
| 16 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 17 |
1186 |
FastChar(char c)... |
| 18 |
|
{ |
| 19 |
1186 |
super(c); |
| 20 |
|
} |
| 21 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 3 |
Complexity Density: 3 |
|
| 22 |
161188 |
public int matchInternal(int p, Pthings pt)... |
| 23 |
|
{ |
| 24 |
161188 |
return (p < pt.src.length() && pt.src.charAt(p) == c) |
| 25 |
|
? nextMatch(p + 1, pt) |
| 26 |
|
: -1; |
| 27 |
|
} |
| 28 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 29 |
852 |
Pattern clone1(Hashtable h)... |
| 30 |
|
{ |
| 31 |
852 |
return new FastChar(c); |
| 32 |
|
} |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| |
|
| 62.4% |
Uncovered Elements: 56 (149) |
Complexity: 38 |
Complexity Density: 0.39 |
|
| 40 |
|
class Branch extends Pattern |
| 41 |
|
{ |
| 42 |
|
Hashtable h = new Hashtable(); |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
Vector keys = new Vector(); |
| 52 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 53 |
813 |
Branch()... |
| 54 |
|
{ |
| 55 |
|
} |
| 56 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 57 |
213 |
Pattern clone1(Hashtable x)... |
| 58 |
|
{ |
| 59 |
213 |
Branch b = new Branch(); |
| 60 |
213 |
b.keys = (Vector) keys.clone(); |
| 61 |
213 |
x.put(this, b); |
| 62 |
213 |
x.put(b, b); |
| 63 |
|
|
| 64 |
639 |
for (int i = 0; i < keys.size(); i++) |
| 65 |
|
{ |
| 66 |
426 |
Pattern p = (Pattern) h.get(keys.elementAt(i)); |
| 67 |
426 |
b.h.put(keys.elementAt(i), p.clone(x)); |
| 68 |
|
} |
| 69 |
213 |
return b; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| |
|
| 26.3% |
Uncovered Elements: 14 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
| 73 |
95 |
final Pattern reduce(boolean ignoreCase, boolean dontMinQ)... |
| 74 |
|
{ |
| 75 |
95 |
if (h.size() == 1) |
| 76 |
|
{ |
| 77 |
0 |
Enumeration e = h.keys(); |
| 78 |
0 |
Character c = (Character) e.nextElement(); |
| 79 |
0 |
Pattern oc; |
| 80 |
0 |
if (ignoreCase || dontMinQ) |
| 81 |
|
{ |
| 82 |
0 |
oc = new oneChar(c.charValue()); |
| 83 |
|
} |
| 84 |
|
else |
| 85 |
|
{ |
| 86 |
0 |
oc = new FastChar(c.charValue()); |
| 87 |
|
} |
| 88 |
0 |
oc.next = (Pattern) h.get(c); |
| 89 |
0 |
oc.add(next); |
| 90 |
0 |
return oc; |
| 91 |
|
} |
| 92 |
95 |
else if (h.size() == 0) |
| 93 |
|
{ |
| 94 |
0 |
return null; |
| 95 |
|
} |
| 96 |
95 |
return this; |
| 97 |
|
} |
| 98 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 99 |
187 |
public patInt maxChars()... |
| 100 |
|
{ |
| 101 |
187 |
Enumeration e = h.keys(); |
| 102 |
187 |
patInt count = new patInt(0); |
| 103 |
561 |
while (e.hasMoreElements()) |
| 104 |
|
{ |
| 105 |
374 |
Object key = e.nextElement(); |
| 106 |
374 |
Pattern pa = (Pattern) h.get(key); |
| 107 |
374 |
patInt pi = pa.maxChars(); |
| 108 |
374 |
pi.inc(); |
| 109 |
374 |
count.maxeq(pi); |
| 110 |
|
} |
| 111 |
187 |
return count; |
| 112 |
|
} |
| 113 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 114 |
187 |
public patInt minChars()... |
| 115 |
|
{ |
| 116 |
187 |
Enumeration e = h.keys(); |
| 117 |
187 |
patInt count = new patInt(0); |
| 118 |
561 |
while (e.hasMoreElements()) |
| 119 |
|
{ |
| 120 |
374 |
Object key = e.nextElement(); |
| 121 |
374 |
Pattern pa = (Pattern) h.get(key); |
| 122 |
374 |
patInt pi = pa.minChars(); |
| 123 |
374 |
pi.inc(); |
| 124 |
374 |
count.mineq(pi); |
| 125 |
|
} |
| 126 |
187 |
return count; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| |
|
| 52.6% |
Uncovered Elements: 9 (19) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 130 |
202 |
void addc(oneChar o, boolean ignoreCase, boolean dontMinQ)... |
| 131 |
|
{ |
| 132 |
202 |
Pattern n = o.next; |
| 133 |
202 |
if (n == null) |
| 134 |
|
{ |
| 135 |
184 |
n = new NullPattern(); |
| 136 |
|
} |
| 137 |
|
else |
| 138 |
|
{ |
| 139 |
18 |
n = RegOpt.opt(n, ignoreCase, dontMinQ); |
| 140 |
|
} |
| 141 |
202 |
n.setParent(this); |
| 142 |
202 |
set(Character.valueOf(o.c), n, ignoreCase, dontMinQ); |
| 143 |
202 |
if (ignoreCase) |
| 144 |
|
{ |
| 145 |
0 |
if (o.c != o.altc) |
| 146 |
|
{ |
| 147 |
0 |
set(Character.valueOf(o.altc), n, ignoreCase, dontMinQ); |
| 148 |
|
} |
| 149 |
0 |
if (o.c != o.altc2 && o.altc != o.altc2) |
| 150 |
|
{ |
| 151 |
0 |
set(Character.valueOf(o.altc2), n, ignoreCase, dontMinQ); |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 73.2% |
Uncovered Elements: 11 (41) |
Complexity: 12 |
Complexity Density: 0.41 |
|
| 156 |
202 |
void set(Character c, Pattern n, boolean igc, boolean dontMinQ)... |
| 157 |
|
{ |
| 158 |
202 |
Pattern p = (Pattern) h.get(c); |
| 159 |
202 |
next = null; |
| 160 |
|
|
| 161 |
|
|
| 162 |
202 |
if (p == null) |
| 163 |
|
{ |
| 164 |
190 |
if (n instanceof Or) |
| 165 |
|
{ |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
6 |
NullPattern np = new NullPattern(); |
| 172 |
6 |
np.add(n); |
| 173 |
6 |
h.put(c, np); |
| 174 |
|
} |
| 175 |
|
else |
| 176 |
|
{ |
| 177 |
184 |
h.put(c, n); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
190 |
keys.addElement(c); |
| 183 |
|
} |
| 184 |
12 |
else if (p instanceof Or) |
| 185 |
|
{ |
| 186 |
9 |
((Or) p).addOr(n); |
| 187 |
|
} |
| 188 |
3 |
else if (p instanceof oneChar && n instanceof oneChar |
| 189 |
|
&& ((oneChar) p).c != ((oneChar) n).c) |
| 190 |
|
{ |
| 191 |
0 |
Branch b = new Branch(); |
| 192 |
0 |
b.addc((oneChar) p, igc, dontMinQ); |
| 193 |
0 |
b.addc((oneChar) n, igc, dontMinQ); |
| 194 |
0 |
h.put(c, b); |
| 195 |
0 |
b.setParent(this); |
| 196 |
|
} |
| 197 |
3 |
else if (p instanceof Branch && n instanceof oneChar) |
| 198 |
|
{ |
| 199 |
0 |
((Branch) p).addc((oneChar) n, igc, dontMinQ); |
| 200 |
0 |
n.setParent(p); |
| 201 |
|
} |
| 202 |
|
else |
| 203 |
|
{ |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
3 |
Or o = new Or(); |
| 209 |
3 |
o.setParent(this); |
| 210 |
|
|
| 211 |
|
|
| 212 |
3 |
if (p instanceof NullPattern && p.parent == null && p.next != null) |
| 213 |
|
{ |
| 214 |
3 |
o.addOr(p.next); |
| 215 |
|
} |
| 216 |
|
else |
| 217 |
|
{ |
| 218 |
0 |
o.addOr(p); |
| 219 |
|
} |
| 220 |
3 |
o.addOr(n); |
| 221 |
|
|
| 222 |
3 |
Pattern optpat = RegOpt.opt(o, igc, dontMinQ); |
| 223 |
3 |
h.put(c, optpat); |
| 224 |
3 |
optpat.setParent(this); |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 228 |
0 |
public String toString()... |
| 229 |
|
{ |
| 230 |
0 |
StringBuffer sb = new StringBuffer(); |
| 231 |
|
|
| 232 |
0 |
sb.append("(?:(?#branch)"); |
| 233 |
0 |
for (int i = 0; i < keys.size(); i++) |
| 234 |
|
{ |
| 235 |
0 |
Character c = (Character) keys.elementAt(i); |
| 236 |
0 |
sb.append(c); |
| 237 |
0 |
sb.append(h.get(c)); |
| 238 |
0 |
if (i + 1 < keys.size()) |
| 239 |
|
{ |
| 240 |
0 |
sb.append("|"); |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
0 |
sb.append(")"); |
| 244 |
0 |
sb.append(nextString()); |
| 245 |
0 |
return sb.toString(); |
| 246 |
|
} |
| 247 |
|
|
| |
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
| 248 |
421 |
public int matchInternal(int pos, Pthings pt)... |
| 249 |
|
{ |
| 250 |
421 |
if (pos >= pt.src.length()) |
| 251 |
|
{ |
| 252 |
216 |
return -1; |
| 253 |
|
} |
| 254 |
205 |
Pattern n = (Pattern) h.get(Character.valueOf(pt.src.charAt(pos))); |
| 255 |
205 |
if (n == null) |
| 256 |
|
{ |
| 257 |
205 |
return -1; |
| 258 |
|
} |
| 259 |
0 |
if (pt.cbits != null && pt.cbits.get(pos)) |
| 260 |
|
{ |
| 261 |
0 |
return -1; |
| 262 |
|
} |
| 263 |
0 |
return n.matchInternal(pos + 1, pt); |
| 264 |
|
} |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| |
|
| 74.1% |
Uncovered Elements: 36 (139) |
Complexity: 39 |
Complexity Density: 0.46 |
|
| 272 |
|
public class RegOpt |
| 273 |
|
{ |
| |
|
| 71.1% |
Uncovered Elements: 26 (90) |
Complexity: 26 |
Complexity Density: 0.43 |
|
| 274 |
2328 |
static Pattern opt(Pattern p, boolean ignoreCase, boolean dontMinQ)... |
| 275 |
|
{ |
| 276 |
2328 |
if (p == null) |
| 277 |
|
{ |
| 278 |
0 |
return p; |
| 279 |
|
} |
| 280 |
2328 |
if (p instanceof Bracket) |
| 281 |
|
{ |
| 282 |
530 |
Bracket b = (Bracket) p; |
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
530 |
p = FastBracket.process(b, ignoreCase); |
| 287 |
|
|
| 288 |
|
|
| 289 |
530 |
p.next = b.next; |
| 290 |
530 |
p.parent = b.parent; |
| 291 |
|
} |
| 292 |
1798 |
else if (p instanceof oneChar && !ignoreCase && !dontMinQ) |
| 293 |
|
{ |
| 294 |
334 |
oneChar o = (oneChar) p; |
| 295 |
334 |
p = new FastChar(o.c); |
| 296 |
334 |
p.next = o.next; |
| 297 |
334 |
p.parent = o.parent; |
| 298 |
|
} |
| 299 |
1464 |
else if (p instanceof Or && ((Or) p).leftForm().equals("(?:") |
| 300 |
|
&& ((Or) p).v.size() == 1) |
| 301 |
|
{ |
| 302 |
0 |
Or o = (Or) p; |
| 303 |
0 |
p = (Pattern) o.v.elementAt(0); |
| 304 |
0 |
p.setParent(null); |
| 305 |
0 |
p = RegOpt.opt(p, ignoreCase, dontMinQ); |
| 306 |
0 |
p.add(o.next); |
| 307 |
|
} |
| 308 |
1464 |
else if (p instanceof Or) |
| 309 |
|
{ |
| 310 |
600 |
Or o = (Or) p; |
| 311 |
600 |
o.pv = null; |
| 312 |
600 |
Vector v = o.v; |
| 313 |
600 |
o.v = new Vector(); |
| 314 |
600 |
Branch b = new Branch(); |
| 315 |
600 |
b.parent = o.parent; |
| 316 |
1322 |
for (int i = 0; i < v.size(); i++) |
| 317 |
|
{ |
| 318 |
722 |
Pattern pp = (Pattern) v.elementAt(i); |
| 319 |
|
|
| 320 |
|
|
| 321 |
722 |
if (pp instanceof oneChar && (b.h.size() >= 1 || (i + 1 < v.size() |
| 322 |
|
&& v.elementAt(i + 1) instanceof oneChar))) |
| 323 |
|
{ |
| 324 |
202 |
b.addc((oneChar) pp, ignoreCase, dontMinQ); |
| 325 |
|
} |
| 326 |
|
else |
| 327 |
|
{ |
| 328 |
520 |
if (b.keys.size() > 0) |
| 329 |
|
{ |
| 330 |
0 |
Pattern p2 = (Pattern) b.reduce(ignoreCase, dontMinQ); |
| 331 |
0 |
if (p2 != null) |
| 332 |
|
{ |
| 333 |
0 |
o.addOr(p2); |
| 334 |
0 |
b = new Branch(); |
| 335 |
0 |
b.parent = o.parent; |
| 336 |
|
} |
| 337 |
|
} |
| 338 |
520 |
o.addOr(opt(pp, ignoreCase, dontMinQ)); |
| 339 |
|
} |
| 340 |
|
} |
| 341 |
600 |
if (b.keys.size() > 0) |
| 342 |
|
{ |
| 343 |
95 |
Pattern p2 = (Pattern) b.reduce(ignoreCase, dontMinQ); |
| 344 |
95 |
if (p2 != null) |
| 345 |
|
{ |
| 346 |
95 |
o.addOr(p2); |
| 347 |
|
} |
| 348 |
|
} |
| 349 |
600 |
if (o.v.size() == 1 && o.leftForm().equals("(?:")) |
| 350 |
|
{ |
| 351 |
3 |
p = (Pattern) o.v.elementAt(0); |
| 352 |
3 |
p.setParent(null); |
| 353 |
3 |
p = RegOpt.opt(p, ignoreCase, dontMinQ); |
| 354 |
3 |
p.add(o.next); |
| 355 |
|
} |
| 356 |
|
} |
| 357 |
864 |
else if (p instanceof FastMulti) |
| 358 |
|
{ |
| 359 |
619 |
PatternSub ps = (PatternSub) p; |
| 360 |
619 |
ps.sub = RegOpt.opt(ps.sub, ignoreCase, dontMinQ); |
| 361 |
|
} |
| 362 |
245 |
else if (p instanceof Multi && safe4fm(((PatternSub) p).sub)) |
| 363 |
|
{ |
| 364 |
0 |
Multi m = (Multi) p; |
| 365 |
0 |
FastMulti fm = null; |
| 366 |
0 |
try |
| 367 |
|
{ |
| 368 |
0 |
fm = new FastMulti(m.a, m.b, opt(m.sub, ignoreCase, dontMinQ)); |
| 369 |
|
} catch (RegSyntax rs) |
| 370 |
|
{ |
| 371 |
|
} |
| 372 |
0 |
fm.parent = m.parent; |
| 373 |
0 |
fm.matchFewest = m.matchFewest; |
| 374 |
0 |
fm.next = m.next; |
| 375 |
0 |
p = fm; |
| 376 |
|
} |
| 377 |
2328 |
if (p.next != null) |
| 378 |
|
{ |
| 379 |
840 |
p.next = opt(p.next, ignoreCase, dontMinQ); |
| 380 |
|
} |
| 381 |
2328 |
return p; |
| 382 |
|
} |
| 383 |
|
|
| |
|
| 78.7% |
Uncovered Elements: 10 (47) |
Complexity: 13 |
Complexity Density: 0.52 |
|
| 384 |
4692 |
final static boolean safe4fm(Pattern x)... |
| 385 |
|
{ |
| 386 |
9431 |
while (x != null) |
| 387 |
|
{ |
| 388 |
4747 |
if (x instanceof Bracket) |
| 389 |
|
{ |
| 390 |
4545 |
; |
| 391 |
|
} |
| 392 |
202 |
else if (x instanceof Range) |
| 393 |
|
{ |
| 394 |
112 |
; |
| 395 |
|
} |
| 396 |
90 |
else if (x instanceof oneChar) |
| 397 |
|
{ |
| 398 |
71 |
; |
| 399 |
|
} |
| 400 |
19 |
else if (x instanceof Any) |
| 401 |
|
{ |
| 402 |
0 |
; |
| 403 |
|
} |
| 404 |
19 |
else if (x instanceof Custom |
| 405 |
|
&& ((Custom) x).v instanceof UniValidator) |
| 406 |
|
{ |
| 407 |
0 |
; |
| 408 |
|
} |
| 409 |
19 |
else if (x instanceof Or) |
| 410 |
|
{ |
| 411 |
19 |
Or o = (Or) x; |
| 412 |
19 |
if (!o.leftForm().equals("(?:")) |
| 413 |
|
{ |
| 414 |
8 |
return false; |
| 415 |
|
} |
| 416 |
11 |
patInt lo = o.countMinChars(); |
| 417 |
11 |
patInt hi = o.countMaxChars(); |
| 418 |
11 |
if (!lo.equals(hi)) |
| 419 |
|
{ |
| 420 |
0 |
return false; |
| 421 |
|
} |
| 422 |
22 |
for (int i = 0; i < o.v.size(); i++) |
| 423 |
|
{ |
| 424 |
11 |
if (!safe4fm((Pattern) o.v.elementAt(i))) |
| 425 |
|
{ |
| 426 |
0 |
return false; |
| 427 |
|
} |
| 428 |
|
} |
| 429 |
|
} |
| 430 |
|
else |
| 431 |
|
{ |
| 432 |
0 |
return false; |
| 433 |
|
} |
| 434 |
4739 |
x = x.next; |
| 435 |
|
} |
| 436 |
4684 |
return true; |
| 437 |
|
} |
| 438 |
|
|
| 439 |
|
|
| 440 |
|
|
| 441 |
|
|
| 442 |
|
|
| 443 |
|
|
| 444 |
|
|
| 445 |
|
|
| 446 |
|
|
| 447 |
|
|
| 448 |
|
|
| 449 |
|
|
| 450 |
|
} |