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.

1  
// goes over input only once (doesn't start again at 1 like jreplace_dyn)
2  
3  
static S jreplace_dyn_allowNull(S s, S in, TokReplacer replacer, ITokCondition condition default null) {
4  
  L<S> tok = javaTok(s);
5  
  jreplace_dyn_allowNull(tok, in, replacer, condition);
6  
  ret join(tok);
7  
}
8  
9  
sbool jreplace_dyn_allowNull(LS tok, S in, TokReplacer replacer, ITokCondition condition default null) {
10  
  ret jreplace_dyn_allowNull(tok, in, replacer, condition, false, true);
11  
}
12  
13  
sbool jreplace_dyn_allowNull(LS tok, S in, TokReplacer replacer, ITokCondition condition, bool ignoreCase, bool reTok) {
14  
  LS tokin = javaTok(in);
15  
  jfind_preprocess(tokin);
16  
  S[] toks = toStringArray(codeTokensOnly(tokin));  
17  
18  
  bool anyChange;
19  
  int i = 0;
20  
  for safety to 10000: {
21  
    ping();
22  
    ifdef jreplace_dyn_allowNull_debug
23  
      printVars jreplace_dyn_allowNull_debug(+i, n := l(tok));
24  
    endifdef
25  
    i = findCodeTokens(tok, i, ignoreCase, toks, condition);
26  
    if (i < 0) ret anyChange;
27  
    int start = i, end = i+l(tokin)-2;
28  
    i = end+2;
29  
    S expansion = replacer.get(tok, start, end);
30  
    if (expansion != null) {
31  
      clearAllTokens(tok, start, end); // C to C
32  
      ifdef jreplace_dyn_allowNull_debug
33  
        printVars jreplace_dyn_allowNull_debug(+i, +start, +end, n := l(tok));
34  
      endifdef
35  
      tok.set(start, expansion);
36  
      if (reTok) { // would this ever be false??
37  
        int n = l(tok);
38  
        reTok(tok, start, end);
39  
        i += l(tok)-n; // adjust for replacement
40  
      }
41  
      set anyChange;
42  
    }
43  
  }
44  
  throw fail("woot? 10000! " + quote(in) + " => " + replacer);
45  
}

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: 181 / 263
Version history: 18 change(s)
Referenced in: [show references]