static int findCodeTokens_optimized(L tok, S... tokens) { ret findCodeTokens_optimized(tok, 1, false, tokens); } static int findCodeTokens_optimized(L tok, boolean ignoreCase, S... tokens) { ret findCodeTokens_optimized(tok, 1, ignoreCase, tokens); } static int findCodeTokens_optimized(L tok, int startIdx, boolean ignoreCase, S... tokens) { ret findCodeTokens_optimized(tok, startIdx, ignoreCase, tokens, null); } static HashSet findCodeTokens_optimized_specials = lithashset("*", "", "", "", "\\*"); static int findCodeTokens_optimized_bails, findCodeTokens_optimized_nonbails; sinterface findCodeTokens_optimized_Matcher { bool get(S token); } static int findCodeTokens_optimized(L tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) { int end = tok.size()-tokens.length*2+2, nTokens = tokens.length; int i = startIdx | 1; // bail out early if first token not found (works great with IndexedList) S firstToken = tokens[0]; if (!ignoreCase && !findCodeTokens_optimized_specials.contains(firstToken)) { ifclass IndexedList2 if (tok instanceof IndexedList2 && !tok.contains(firstToken)) { ++findCodeTokens_optimized_bails; ret -1; } ++findCodeTokens_optimized_nonbails; endif // quickly scan for first token while (i < end && !firstToken.equals(tok.get(i))) i += 2; } findCodeTokens_optimized_Matcher[] matchers = new[nTokens]; for j to nTokens: { S p = tokens[j]; findCodeTokens_optimized_Matcher matcher; if (p.equals("*")) matcher = t -> true; else if (p.equals("")) matcher = t -> isQuoted(t); else if (p.equals("")) matcher = t -> isIdentifier(t); else if (p.equals("")) matcher = t -> isInteger(t); else if (p.equals("\\*")) matcher = t -> t.equals("*"); else if (ignoreCase) matcher = t -> eqic(p, t); else matcher = t -> t.equals(p); matchers[j] = matcher; } outer: for (; i < end; i += 2) { for (int j = 0; j < nTokens; j++) if (!matchers[j].get(tok.get(i+j*2))) continue outer; if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index return i; } return -1; }