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

37
LINES

< > BotCompany Repo | #1000812 // match2 function

JavaX fragment (include)

// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)

static S[] match2(L<S> pat, L<S> tok) {
  // standard case (no ...)
  int i = pat.indexOf("...");
  if (i < 0) return match2_match(pat, tok);
  
  pat = new ArrayList<S>(pat); // We're modifying it, so copy first
  pat.set(i, "*");
  while (pat.size() < tok.size()) {
    pat.add(i, "*");
    pat.add(i+1, ""); // doesn't matter
  }
  
  return match2_match(pat, tok);
}

static S[] match2_match(L<S> pat, L<S> tok) {
  L<S> result = new ArrayList<S>();
  if (pat.size() != tok.size()) {
    ifdef match2_debug
      print("match2: Size mismatch: " + structure(pat) + " vs " + structure(tok));
    endifdef
    return null;
  }
  for (int i = 1; i < pat.size(); i += 2) {
    S p = pat.get(i), t = tok.get(i);
    ifdef match2_debug
      print("match2: Checking " + p + " against " + t);
    endifdef
    if (eq(p, "*"))
      result.add(t);
    else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now. TODO: should remove
      return null;
  }
  return result.toArray(new S[result.size()]);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000812
Snippet name: match2 function
Eternal ID of this version: #1000812/3
Text MD5: 0b0758a39429d9a2d40ff1a52eb19724
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-09 20:00:50
Source code size: 1201 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 872 / 7698
Version history: 2 change(s)
Referenced in: [show references]