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

37
LINES

< > BotCompany Repo | #1000812 // match2 function

JavaX fragment (include)

1  
// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)
2  
3  
static S[] match2(L<S> pat, L<S> tok) {
4  
  // standard case (no ...)
5  
  int i = pat.indexOf("...");
6  
  if (i < 0) return match2_match(pat, tok);
7  
  
8  
  pat = new ArrayList<S>(pat); // We're modifying it, so copy first
9  
  pat.set(i, "*");
10  
  while (pat.size() < tok.size()) {
11  
    pat.add(i, "*");
12  
    pat.add(i+1, ""); // doesn't matter
13  
  }
14  
  
15  
  return match2_match(pat, tok);
16  
}
17  
18  
static S[] match2_match(L<S> pat, L<S> tok) {
19  
  L<S> result = new ArrayList<S>();
20  
  if (pat.size() != tok.size()) {
21  
    ifdef match2_debug
22  
      print("match2: Size mismatch: " + structure(pat) + " vs " + structure(tok));
23  
    endifdef
24  
    return null;
25  
  }
26  
  for (int i = 1; i < pat.size(); i += 2) {
27  
    S p = pat.get(i), t = tok.get(i);
28  
    ifdef match2_debug
29  
      print("match2: Checking " + p + " against " + t);
30  
    endifdef
31  
    if (eq(p, "*"))
32  
      result.add(t);
33  
    else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now. TODO: should remove
34  
      return null;
35  
  }
36  
  return result.toArray(new S[result.size()]);
37  
}

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: 811 / 7631
Version history: 2 change(s)
Referenced in: [show references]