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

56
LINES

< > BotCompany Repo | #1006801 // rfindCodeTokens - scan from end, returns C index

JavaX fragment (include)

1  
static int rfindCodeTokens(L<S> tok, S... tokens) {
2  
  ret rfindCodeTokens(tok, 1, false, tokens);
3  
}
4  
5  
static int rfindCodeTokens(L<S> tok, boolean ignoreCase, S... tokens) {
6  
  ret rfindCodeTokens(tok, 1, ignoreCase, tokens);
7  
}
8  
9  
static int rfindCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
10  
  ret rfindCodeTokens(tok, startIdx, ignoreCase, tokens, null);
11  
}
12  
13  
static L<S> rfindCodeTokens_specials = litlist("*", "<quoted>", "<id>", "<int>", "\\*");
14  
static bool rfindCodeTokens_debug;
15  
static int rfindCodeTokens_indexed, rfindCodeTokens_unindexed;
16  
static int rfindCodeTokens_bails, rfindCodeTokens_nonbails;
17  
18  
static int rfindCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
19  
  ret rfindCodeTokens(tok, startIdx, l(tok), ignoreCase, tokens, condition);
20  
}
21  
22  
static int rfindCodeTokens(L<S> tok, int startIdx, int endIndex, bool ignoreCase, S[] tokens, O condition) {
23  
  if (rfindCodeTokens_debug) {
24  
    if (eq(getClassName(tok), "main$IndexedList2"))
25  
      rfindCodeTokens_indexed++;
26  
    else
27  
      rfindCodeTokens_unindexed++;
28  
  }
29  
  // bail out early if first token not found (works great with IndexedList)
30  
  if (!rfindCodeTokens_specials.contains(tokens[0])
31  
    && !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) {
32  
      ++rfindCodeTokens_bails;
33  
      ret -1;
34  
    }
35  
  ++rfindCodeTokens_nonbails;
36  
  
37  
  outer: for (int i = min(endIndex, tok.size()-tokens.length*2) | 1; i >= startIdx; i -= 2) {
38  
    for (int j = 0; j < tokens.length; j++) {
39  
      S p = tokens[j], t = tok.get(i+j*2);
40  
      boolean match;
41  
      if (eq(p, "*")) match = true;
42  
      else if (eq(p, "<quoted>")) match = isQuoted(t);
43  
      else if (eq(p, "<id>")) match = isIdentifier(t);
44  
      else if (eq(p, "<int>")) match = isInteger(t);
45  
      else if (eq(p, "\\*")) match = eq("*", t);
46  
      else match = ignoreCase ? eqic(p, t) : eq(p, t);
47  
      
48  
      if (!match)
49  
        continue outer;
50  
    }
51  
    
52  
    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
53  
      return i;
54  
  }
55  
  return -1;
56  
}

Author comment

Began life as a copy of #1001193

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: #1006801
Snippet name: rfindCodeTokens - scan from end, returns C index
Eternal ID of this version: #1006801/3
Text MD5: 7a61c681b7f41146bdca40d6fb8cf1d6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-01 16:06:35
Source code size: 2131 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 429 / 467
Version history: 2 change(s)
Referenced in: [show references]