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

70
LINES

< > BotCompany Repo | #1025240 // findCodeTokens_optimized [dev.]

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

Libraryless. Click here for Pure Java version (2214L/14K).

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

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: #1025240
Snippet name: findCodeTokens_optimized [dev.]
Eternal ID of this version: #1025240/5
Text MD5: 1600c4b0b9b9f529744fbee789986c94
Transpilation MD5: 0a8ea9364ed0f473aa6fc85d70c870bf
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:00:31
Source code size: 2331 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 147 / 231
Version history: 4 change(s)
Referenced in: [show references]