1 | static bool flexMatchIC2_debug; |
2 | |
3 | static bool flexMatchIC2(S pat, S s) { |
4 | ret flexMatchIC2(pat, s, null); |
5 | } |
6 | |
7 | static bool flexMatchIC2(S pat, S s, Matches m) { |
8 | assertNotNull("Pattern is null", pat); |
9 | if (s == null) false; |
10 | ret flexMatchIC2(javaTok(pat), javaTok(s), m; |
11 | } |
12 | |
13 | static bool flexMatchIC2(L<S> tokpat, L<S> tokfull, Matches m) { |
14 | tokpat = codeTokens(joinBrackets(tokpat)); |
15 | for (int i = 0; i < l(tokpat); i++) |
16 | if (eq(tokpat.get(i), "*")) |
17 | tokpat.add(i++, "!*"); // insert single-token wildcard in front to avoid empty matches |
18 | tokfull = joinBrackets(tokfull); |
19 | L<S> tok = codeTokens(tokfull); |
20 | new BitSet bla; // bla marks long matches |
21 | new BitSet bla2; // bla2 marks (a|b) matches and start of long matches |
22 | if (!flexMatchIC2_impl(tokpat, 0, tok, 0, bla, bla2)) ret false; |
23 | if (m != null) { |
24 | new L<S> l; |
25 | for (int i = 1; i < l(tokfull); i += 2) { |
26 | if (bla.get(i/2)) { |
27 | int j = i; |
28 | while (j < l(tokfull) && bla.get(j/2) && !bla2.get(j/2+1)) j += 2; |
29 | l.add(join(subList(tokfull, i, j-1))); |
30 | i = j-2; |
31 | } else if (bla2.get(i/2)) |
32 | l.add(tokfull.get(i)); |
33 | } |
34 | m.m = toStringArray(l); |
35 | } |
36 | ret true; |
37 | } |
38 | |
39 | static bool flexMatchIC2_impl(L<S> pat, int ipat, L<S> tok, int itok, BitSet bla, BitSet bla2) { |
40 | if (flexMatchIC2_debug) |
41 | print("flexMatchIC2 pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla)); |
42 | if (ipat >= l(pat)) |
43 | ret itok >= l(tok); |
44 | S t = pat.get(ipat); |
45 | |
46 | if (eq(t, "*")) { // the flex wildcard (0 or more tokens) |
47 | if (flexMatchIC2_debug) print("Trying zero tokens"); |
48 | if (flexMatchIC2_impl(pat, ipat+1, tok, itok, bla, bla2)) { |
49 | if (flexMatchIC2_debug) print("Success!"); |
50 | ret true; |
51 | } |
52 | |
53 | bla.set(itok); |
54 | if (itok < l(tok)) { |
55 | if (flexMatchIC2_debug) print("Trying one or more tokens"); |
56 | if (flexMatchIC2_impl(pat, ipat, tok, itok+1, bla, bla2)) { |
57 | if (flexMatchIC2_debug) print("Success!"); |
58 | ret true; // success, leave mark |
59 | } |
60 | } |
61 | |
62 | if (flexMatchIC2_debug) print("Failed * matching"); |
63 | bla.clear(itok); // fail, undo marking |
64 | ret false; |
65 | } |
66 | if (itok >= l(tok)) { |
67 | if (flexMatchIC2_debug) |
68 | print("too much pattern"); |
69 | ret false; |
70 | } |
71 | if (eq(t, "!*")) { // the single-token wildcard |
72 | bla.set(itok); |
73 | bla2.set(itok); |
74 | if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2)) |
75 | ret true; // success, leave mark |
76 | bla.clear(itok); // fail, undo marking |
77 | bla2.clear(itok); |
78 | ret false; |
79 | } |
80 | S realt = tok.get(itok); |
81 | if (t.startsWith("(") && t.endsWith(")")) { |
82 | // quick pre-check |
83 | if (flexMatchIC2_debug) |
84 | print("flexMatchIC2 precheck " + t + " " + realt); |
85 | if (!containsIgnoreCase(t, realt)) false; |
86 | // real check |
87 | L<S> list = splitAt(dropFirstAndLast(t), "|"); |
88 | if (flexMatchIC2_debug) |
89 | print("flexMatchIC2 real check " + struct(list)); |
90 | if (!containsIgnoreCase(list, realt)) false; |
91 | bla2.set(itok); |
92 | } else if (neqic(realt, t)) { |
93 | if (flexMatchIC2_debug) |
94 | print("mismatch"); |
95 | ret false; |
96 | } |
97 | |
98 | // it is a token match. consume and proceed |
99 | if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2)) |
100 | true; |
101 | else { |
102 | bla2.clear(itok); |
103 | false; |
104 | } |
105 | } |
Began life as a copy of #1005501
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1007444 |
Snippet name: | Changing flexMatchIC2 - flexMatch (ignore case, with (a|b)) |
Eternal ID of this version: | #1007444/1 |
Text MD5: | 940f6f42d07e092b186763a2668d076d |
Author: | stefan |
Category: | javax / parsing |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-03-23 17:15:23 |
Source code size: | 3373 bytes / 105 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 547 / 544 |
Referenced in: | [show references] |