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

56
LINES

< > BotCompany Repo | #1006801 // rfindCodeTokens - scan from end, returns C index

JavaX fragment (include)

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

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

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

static L<S> rfindCodeTokens_specials = litlist("*", "<quoted>", "<id>", "<int>", "\\*");
static bool rfindCodeTokens_debug;
static int rfindCodeTokens_indexed, rfindCodeTokens_unindexed;
static int rfindCodeTokens_bails, rfindCodeTokens_nonbails;

static int rfindCodeTokens(L<S> tok, int startIdx, boolean ignoreCase, S[] tokens, O condition) {
  ret rfindCodeTokens(tok, startIdx, l(tok), ignoreCase, tokens, condition);
}

static int rfindCodeTokens(L<S> tok, int startIdx, int endIndex, bool ignoreCase, S[] tokens, O condition) {
  if (rfindCodeTokens_debug) {
    if (eq(getClassName(tok), "main$IndexedList2"))
      rfindCodeTokens_indexed++;
    else
      rfindCodeTokens_unindexed++;
  }
  // bail out early if first token not found (works great with IndexedList)
  if (!rfindCodeTokens_specials.contains(tokens[0])
    && !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) {
      ++rfindCodeTokens_bails;
      ret -1;
    }
  ++rfindCodeTokens_nonbails;
  
  outer: for (int i = min(endIndex, tok.size()-tokens.length*2) | 1; i >= startIdx; i -= 2) {
    for (int j = 0; j < tokens.length; 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 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006801
Snippet name: rfindCodeTokens - scan from end, returns C index
Eternal ID of this version: #1006801/3
Text MD5: 7a61c681b7f41146bdca40d6fb8cf1d6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-01 16:06:35
Source code size: 2131 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 493 / 532
Version history: 2 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)