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

64
LINES

< > BotCompany Repo | #1025241 // findCodeTokens (old)

JavaX fragment (include)

1  
static int findCodeTokens(L<S> tok, S... tokens) {
2  
  ret findCodeTokens(tok, 1, false, tokens);
3  
}
4  
5  
static int findCodeTokens(L<S> tok, boolean ignoreCase, S... tokens) {
6  
  ret findCodeTokens(tok, 1, ignoreCase, tokens);
7  
}
8  
9  
static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
10  
  ret findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
11  
}
12  
13  
static HashSet<S> findCodeTokens_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
14  
static bool findCodeTokens_debug;
15  
static int findCodeTokens_indexed, findCodeTokens_unindexed;
16  
static int findCodeTokens_bails, findCodeTokens_nonbails;
17  
18  
static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
19  
  if (findCodeTokens_debug) {
20  
    if (eq(getClassName(tok), "main$IndexedList2"))
21  
      findCodeTokens_indexed++;
22  
    else
23  
      findCodeTokens_unindexed++;
24  
  }
25  
  
26  
  int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
27  
  int i = startIdx | 1;
28  
  
29  
  // bail out early if first token not found (works great with IndexedList)
30  
  S firstToken = tokens[0];
31  
  if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) {
32  
    ifclass IndexedList2
33  
    if (tok instanceof IndexedList2 && !tok.contains(firstToken)) {
34  
      ++findCodeTokens_bails;
35  
      ret -1;
36  
    }
37  
    ++findCodeTokens_nonbails;
38  
    endif
39  
    
40  
    // quickly scan for first token
41  
    while (i < end && !firstToken.equals(tok.get(i)))
42  
      i += 2;
43  
  }
44  
  
45  
  outer: for (; i < end; i += 2) {
46  
    for (int j = 0; j < nTokens; j++) {
47  
      S p = tokens[j], t = tok.get(i+j*2);
48  
      boolean match;
49  
      if (eq(p, "*")) match = true;
50  
      else if (eq(p, "<quoted>")) match = isQuoted(t);
51  
      else if (eq(p, "<id>")) match = isIdentifier(t);
52  
      else if (eq(p, "<int>")) match = isInteger(t);
53  
      else if (eq(p, "\\*")) match = eq("*", t);
54  
      else match = ignoreCase ? eqic(p, t) : eq(p, t);
55  
      
56  
      if (!match)
57  
        continue outer;
58  
    }
59  
    
60  
    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
61  
      return i;
62  
  }
63  
  return -1;
64  
}

Author comment

Began life as a copy of #1001193

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025241
Snippet name: findCodeTokens (old)
Eternal ID of this version: #1025241/1
Text MD5: de1a79525be8563b09fd76768d529173
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-15 02:05:31
Source code size: 2138 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 207 / 236
Referenced in: [show references]