Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

109
LINES

< > BotCompany Repo | #1007975 // flexMatchIC2_multi - flexMatch (ignore case, with (a|b), multiple outs, dev.)

JavaX fragment (include)

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  
  ret flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m);
9  
}
10  
11  
static bool flexMatchIC2(S pat, S s, Matches m, bool joinBrackets) {
12  
  ret flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets);
13  
}
14  
15  
static bool flexMatchIC2(L<S> tokpat, L<S> tokfull, Matches m) {
16  
  ret flexMatchIC2(tokpat, tokfull, m, true);
17  
}
18  
19  
static bool flexMatchIC2(L<S> tokpat, L<S> tokfull, Matches m, bool joinBrackets) {
20  
  tokpat = codeTokens(joinBrackets ? joinBrackets(tokpat) : tokpat);
21  
  for (int i = 0; i < l(tokpat); i++)
22  
    if (eq(tokpat.get(i), "*"))
23  
      tokpat.add(i++, "!*"); // insert single-token wildcard in front to avoid empty matches
24  
  if (joinBrackets) tokfull = joinBrackets(tokfull);
25  
  L<S> tok = codeTokens(tokfull);
26  
  new BitSet bla;
27  
  new BitSet bla2;
28  
  if (!flexMatchIC2_impl(tokpat, 0, tok, 0, bla, bla2)) ret false;
29  
  if (m != null) {
30  
    new L<S> l;
31  
    for (int i = 1; i < l(tokfull); i += 2) {
32  
      if (bla.get(i/2)) {
33  
        int j = i;
34  
        while (j < l(tokfull) && bla.get(j/2)) j += 2;
35  
        l.add(join(subList(tokfull, i, j-1)));
36  
        i = j-2;
37  
      } else if (bla2.get(i/2))
38  
        l.add(tokfull.get(i));
39  
    }
40  
    m.m = toStringArray(l);
41  
  }
42  
  ret true;
43  
}
44  
45  
static void flexMatchIC2_impl(Collector<Matches> out, L<S> pat, int ipat, L<S> tok, int itok, BitSet bla, BitSet bla2) {
46  
  if (flexMatchIC2_debug)
47  
    print("flexMatchIC2 pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla));
48  
  if (ipat >= l(pat))
49  
    ret itok >= l(tok);
50  
  S t = pat.get(ipat);
51  
  
52  
  if (eq(t, "*")) { // the flex wildcard (0 or more tokens)
53  
    if (flexMatchIC2_debug) print("Trying zero tokens");
54  
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok, bla, bla2)) {
55  
      if (flexMatchIC2_debug) print("Success!");
56  
      ret true;
57  
    }
58  
    
59  
    bla.set(itok);
60  
    if (itok < l(tok)) {
61  
      if (flexMatchIC2_debug) print("Trying one or more tokens");
62  
      if (flexMatchIC2_impl(pat, ipat, tok, itok+1, bla, bla2)) {
63  
        if (flexMatchIC2_debug) print("Success!");
64  
        ret true; // success, leave mark
65  
      }
66  
    }
67  
    
68  
    if (flexMatchIC2_debug) print("Failed * matching");
69  
    bla.clear(itok); // fail, undo marking
70  
    ret false;
71  
  }
72  
  if (itok >= l(tok)) {
73  
    if (flexMatchIC2_debug)
74  
      print("too much pattern");
75  
    ret false;
76  
  }
77  
  if (eq(t, "!*")) { // the single-token wildcard
78  
    bla.set(itok);
79  
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
80  
      ret true; // success, leave mark
81  
    bla.clear(itok); // fail, undo marking
82  
    ret false;
83  
  }
84  
  S realt = tok.get(itok);
85  
  if (t.startsWith("(") && t.endsWith(")")) {
86  
    // quick pre-check
87  
    if (flexMatchIC2_debug)
88  
      print("flexMatchIC2 precheck " + t + " " + realt);
89  
    if (!containsIgnoreCase(t, realt)) false;
90  
    // real check
91  
    L<S> list = splitAt(dropFirstAndLast(t), "|");
92  
    if (flexMatchIC2_debug)
93  
      print("flexMatchIC2 real check " + struct(list));
94  
    if (!containsIgnoreCase(list, realt)) false;
95  
    bla2.set(itok);
96  
  } else if (neqic(realt, t)) {
97  
    if (flexMatchIC2_debug)
98  
      print("mismatch");
99  
    ret false;
100  
  }
101  
  
102  
  // it is a token match. consume and proceed
103  
  if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
104  
    true;
105  
  else {
106  
    bla2.clear(itok);
107  
    false;
108  
  }
109  
}

Author comment

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: #1007975
Snippet name: flexMatchIC2_multi - flexMatch (ignore case, with (a|b), multiple outs, dev.)
Eternal ID of this version: #1007975/1
Text MD5: 36269b6169926c8fb7ac38095f72056b
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-17 22:26:36
Source code size: 3535 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 414 / 413
Referenced in: [show references]