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

45
LINES

< > BotCompany Repo | #1032249 // jreplace_dyn_allowNull - null stands for "don't replace"

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

Transpiled version (3785L) is out of date.

// goes over input only once (doesn't start again at 1 like jreplace_dyn)

static S jreplace_dyn_allowNull(S s, S in, TokReplacer replacer, ITokCondition condition default null) {
  L<S> tok = javaTok(s);
  jreplace_dyn_allowNull(tok, in, replacer, condition);
  ret join(tok);
}

sbool jreplace_dyn_allowNull(LS tok, S in, TokReplacer replacer, ITokCondition condition default null) {
  ret jreplace_dyn_allowNull(tok, in, replacer, condition, false, true);
}

sbool jreplace_dyn_allowNull(LS tok, S in, TokReplacer replacer, ITokCondition condition, bool ignoreCase, bool reTok) {
  LS tokin = javaTok(in);
  jfind_preprocess(tokin);
  S[] toks = toStringArray(codeTokensOnly(tokin));  

  bool anyChange;
  int i = 0;
  for safety to 10000: {
    ping();
    ifdef jreplace_dyn_allowNull_debug
      printVars jreplace_dyn_allowNull_debug(+i, n := l(tok));
    endifdef
    i = findCodeTokens(tok, i, ignoreCase, toks, condition);
    if (i < 0) ret anyChange;
    int start = i, end = i+l(tokin)-2;
    i = end+2;
    S expansion = replacer.get(tok, start, end);
    if (expansion != null) {
      clearAllTokens(tok, start, end); // C to C
      ifdef jreplace_dyn_allowNull_debug
        printVars jreplace_dyn_allowNull_debug(+i, +start, +end, n := l(tok));
      endifdef
      tok.set(start, expansion);
      if (reTok) { // would this ever be false??
        int n = l(tok);
        reTok(tok, start, end);
        i += l(tok)-n; // adjust for replacement
      }
      set anyChange;
    }
  }
  throw fail("woot? 10000! " + quote(in) + " => " + replacer);
}

Author comment

Began life as a copy of #1009462

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032249
Snippet name: jreplace_dyn_allowNull - null stands for "don't replace"
Eternal ID of this version: #1032249/19
Text MD5: 2b4dfa0036f42701e22a077b5115ecb8
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-23 00:59:48
Source code size: 1614 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 121 / 185
Version history: 18 change(s)
Referenced in: [show references]