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

64
LINES

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

JavaX fragment (include)

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

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

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

static HashSet<S> findCodeTokens_specials = lithashset("*", "<quoted>", "<id>", "<int>", "\\*");
static bool findCodeTokens_debug;
static int findCodeTokens_indexed, findCodeTokens_unindexed;
static int findCodeTokens_bails, findCodeTokens_nonbails;

static int findCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
  if (findCodeTokens_debug) {
    if (eq(getClassName(tok), "main$IndexedList2"))
      findCodeTokens_indexed++;
    else
      findCodeTokens_unindexed++;
  }
  
  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_specials.contains(firstToken)) {
    ifclass IndexedList2
    if (tok instanceof IndexedList2 && !tok.contains(firstToken)) {
      ++findCodeTokens_bails;
      ret -1;
    }
    ++findCodeTokens_nonbails;
    endif
    
    // quickly scan for first token
    while (i < end && !firstToken.equals(tok.get(i)))
      i += 2;
  }
  
  outer: for (; i < end; i += 2) {
    for (int j = 0; j < nTokens; j++) {
      S p = tokens[j], t = tok.get(i+j*2);
      boolean match;
      if (eq(p, "*")) match = true;
      else if (eq(p, "<quoted>")) match = isQuoted(t);
      else if (eq(p, "<id>")) match = isIdentifier(t);
      else if (eq(p, "<int>")) match = isInteger(t);
      else if (eq(p, "\\*")) match = eq("*", t);
      else match = ignoreCase ? eqic(p, t) : eq(p, t);
      
      if (!match)
        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: #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: 206 / 234
Referenced in: