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

56
LINES

< > BotCompany Repo | #1002133 // jreplace

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

Libraryless. Click here for Pure Java version (9819L/55K).

1  
meta-postProcess { tok_jreplace_standardParameter }
2  
3  
static S jreplace(S s, S in, S out) {
4  
  ret jreplace(s, in, out, null);
5  
}
6  
7  
static S jreplace(S s, S in, S out, O condition) {
8  
  LS tok = javaTok(s);
9  
  ret jreplace(tok, in, out, condition) ? join(tok) : s;
10  
}
11  
12  
// leaves tok properly tokenized
13  
// returns true iff anything was replaced
14  
static boolean jreplace(LS tok, S in, S out) {
15  
  ret jreplace(tok, in, out, false, true, null);
16  
}
17  
18  
static boolean jreplace(LS tok, S in, S out, O condition) {
19  
  ret jreplace(tok, in, out, false, true, condition);
20  
}
21  
22  
sbool jreplace(LS tok, S in, ITokCondition condition, S out) {
23  
  ret jreplace(tok, in, out, (O) condition);
24  
}
25  
26  
sbool jreplace(LS tok, S in, S out, IF2<LS, Int, Bool> condition) {
27  
  ret jreplace(tok, in, out, (O) condition);
28  
}
29  
30  
sbool jreplace(LS tok, S in, S out, boolean ignoreCase, bool reTok, O condition) {
31  
  S[] toks = javaTokForJFind_array(in);
32  
  int lTokin = toks.length*2+1;
33  
34  
  boolean anyChange = false;
35  
  int i = -1;
36  
  for (int n = 0; n < 10000; n++) { // TODO: don't need this check anymore
37  
    i = findCodeTokens(tok, i+1, ignoreCase, toks, condition);
38  
    if (i < 0)
39  
      ret anyChange;
40  
    LS subList = tok.subList(i-1, i+lTokin-1); // N to N
41  
    S expansion = jreplaceExpandRefs(out, subList);
42  
    int end = i+lTokin-2;
43  
    ifdef jreplace_performing
44  
    jreplace_performing(tok, i, end, expansion);
45  
    endifdef
46  
    clearAllTokens(tok, i, end); // C to C
47  
    tok.set(i, expansion);
48  
    if (reTok) // would this ever be false??
49  
      reTok(tok, i, end);
50  
    i = end;
51  
    anyChange = true;
52  
  }
53  
  throw fail("woot? 10000! " + quote(in) + " => " + quote(out));
54  
}
55  
56  
static bool jreplace_debug;

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1002133
Snippet name: jreplace
Eternal ID of this version: #1002133/11
Text MD5: 2223ca74e842f420b2a38ee1f8945714
Transpilation MD5: a780dbe2391eb0c0cf7b50794b4ea92a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-16 17:12:11
Source code size: 1710 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 865 / 2299
Version history: 10 change(s)
Referenced in: [show references]