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).

static int findCodeTokens_optimized(L<S> tok, S... tokens) {
  ret findCodeTokens_optimized(tok, 1, false, tokens);
}

static int findCodeTokens_optimized(L<S> tok, boolean ignoreCase, S... tokens) {
  ret findCodeTokens_optimized(tok, 1, ignoreCase, tokens);
}

static int findCodeTokens_optimized(L<S> tok, int startIdx, boolean ignoreCase, S... tokens) {
  ret findCodeTokens_optimized(tok, startIdx, ignoreCase, tokens, null);
}

static HashSet<S> findCodeTokens_optimized_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
static int findCodeTokens_optimized_bails, findCodeTokens_optimized_nonbails;

sinterface findCodeTokens_optimized_Matcher {
  bool get(S token);
}

static int findCodeTokens_optimized(L<S> 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("<quoted>"))
      matcher = t -> isQuoted(t);
    else if (p.equals("<id>"))
      matcher = t -> isIdentifier(t);
    else if (p.equals("<int>"))
      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;
}

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: 212 / 314
Version history: 4 change(s)
Referenced in: [show references]