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

113
LINES

< > BotCompany Repo | #1005501 // flexMatchIC2 - flexMatch (ignore case, with (a|b), does not drop punctuation)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3798L/24K).

static bool flexMatchIC2_debug;

static bool flexMatchIC2(S pat, S s) {
  ret flexMatchIC2(pat, s, null);
}

static bool flexMatchIC2(S pat, S s, Matches m) {
  ret flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m);
}

static bool flexMatchIC2(S pat, S s, Matches m, bool joinBrackets) {
  ret flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets);
}

sbool flexMatchIC2(S pat, LS tokfull, Matches m, bool joinBrackets) {
  ret flexMatchIC2(javaTok(pat), tokfull, m, joinBrackets);
}

static bool flexMatchIC2(L<S> tokpat, L<S> tokfull, Matches m) {
  ret flexMatchIC2(tokpat, tokfull, m, true);
}

static bool flexMatchIC2(L<S> tokpat, L<S> tokfull, Matches m, bool joinBrackets) {
  tokpat = codeTokens(joinBrackets ? joinBrackets(tokpat) : tokpat);
  for (int i = 0; i < l(tokpat); i++)
    if (eq(tokpat.get(i), "*"))
      tokpat.add(i++, "!*"); // insert single-token wildcard in front to avoid empty matches
  if (joinBrackets) tokfull = joinBrackets(tokfull);
  L<S> tok = codeTokens(tokfull);
  new BitSet bla;
  new BitSet bla2;
  if (!flexMatchIC2_impl(tokpat, 0, tok, 0, bla, bla2)) ret false;
  if (m != null) {
    new L<S> l;
    for (int i = 1; i < l(tokfull); i += 2) {
      if (bla.get(i/2)) {
        int j = i;
        while (j < l(tokfull) && bla.get(j/2)) j += 2;
        l.add(join(subList(tokfull, i, j-1)));
        i = j-2;
      } else if (bla2.get(i/2))
        l.add(tokfull.get(i));
    }
    m.m = toStringArray(l);
  }
  ret true;
}

static bool flexMatchIC2_impl(L<S> pat, int ipat, L<S> tok, int itok, BitSet bla, BitSet bla2) {
  if (flexMatchIC2_debug)
    print("flexMatchIC2 pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla));
  if (ipat >= l(pat))
    ret itok >= l(tok);
  S t = pat.get(ipat);
  
  if (eq(t, "*")) { // the flex wildcard (0 or more tokens)
    if (flexMatchIC2_debug) print("Trying zero tokens");
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok, bla, bla2)) {
      if (flexMatchIC2_debug) print("Success!");
      ret true;
    }
    
    bla.set(itok);
    if (itok < l(tok)) {
      if (flexMatchIC2_debug) print("Trying one or more tokens");
      if (flexMatchIC2_impl(pat, ipat, tok, itok+1, bla, bla2)) {
        if (flexMatchIC2_debug) print("Success!");
        ret true; // success, leave mark
      }
    }
    
    if (flexMatchIC2_debug) print("Failed * matching");
    bla.clear(itok); // fail, undo marking
    ret false;
  }
  if (itok >= l(tok)) {
    if (flexMatchIC2_debug)
      print("too much pattern");
    ret false;
  }
  if (eq(t, "!*")) { // the single-token wildcard
    bla.set(itok);
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
      ret true; // success, leave mark
    bla.clear(itok); // fail, undo marking
    ret false;
  }
  S realt = tok.get(itok);
  if (t.startsWith("(") && t.endsWith(")")) {
    // quick pre-check
    if (flexMatchIC2_debug)
      print("flexMatchIC2 precheck " + t + " " + realt);
    if (!containsIgnoreCase(t, realt)) false;
    // real check
    L<S> list = splitAt(dropFirstAndLast(t), "|");
    if (flexMatchIC2_debug)
      print("flexMatchIC2 real check " + struct(list));
    if (!containsIgnoreCase(list, realt)) false;
    bla2.set(itok);
  } else if (neqic(realt, t)) {
    if (flexMatchIC2_debug)
      print("mismatch");
    ret false;
  }
  
  // it is a token match. consume and proceed
  if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
    true;
  else {
    bla2.clear(itok);
    false;
  }
}

Author comment

Began life as a copy of #1005459

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005501
Snippet name: flexMatchIC2 - flexMatch (ignore case, with (a|b), does not drop punctuation)
Eternal ID of this version: #1005501/14
Text MD5: bd35ec1d3a4f12f224966db6f88bd039
Transpilation MD5: 3f30e2edbb0c7377603238b836b6366d
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-05 21:38:57
Source code size: 3648 bytes / 113 lines
Pitched / IR pitched: No / No
Views / Downloads: 689 / 734
Version history: 13 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007444 - Changing flexMatchIC2 - flexMatch (ignore case, with (a|b))
#1007975 - flexMatchIC2_multi - flexMatch (ignore case, with (a|b), multiple outs, dev.)
#1007988 - flexMatchIC2_left - flexMatch (ignore case, with (a|b), left-associative)