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

63
LINES

< > BotCompany Repo | #1009462 // jreplace_dyn

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

Libraryless. Click here for Pure Java version (11430L/63K).

1  
// out: func(L<S> tok, int cIndex) -> S
2  
// condition: func(L<S> tok, int nIndex) -> S  [yeah it's inconsistent]
3  
static S jreplace_dyn(S s, S in, O out) {
4  
  ret jreplace_dyn(s, in, out, null);
5  
}
6  
7  
sS jreplace_dyn(S s, S in, O out, O condition) {
8  
  L<S> tok = javaTok(s);
9  
  jreplace_dyn(tok, in, out, condition);
10  
  ret join(tok);
11  
}
12  
13  
// leaves tok properly tokenized
14  
// returns true iff anything was replaced
15  
sbool jreplace_dyn(LS tok, S in, O out) {
16  
  ret jreplace_dyn(tok, in, out, false, true, null);
17  
}
18  
19  
sbool jreplace_dyn(LS tok, S in, IF0<S> out) {
20  
  ret jreplace_dyn(tok, in, if0ToIF2(out));
21  
}
22  
23  
sbool jreplace_dyn(LS tok, S in, ITokCondition cond, IF0<S> out) {
24  
  ret jreplace_dyn(tok, in, cond, if0ToIF2(out));
25  
}
26  
27  
sbool jreplace_dyn(LS tok, S in, IF1<Int, S> out) {
28  
  ret jreplace_dyn(tok, in, (_tok, cIdx) -> out.get(cIdx));
29  
}
30  
31  
sbool jreplace_dyn(LS tok, S in, IF2<LS, Int, S> out) {
32  
  ret jreplace_dyn(tok, in, (O) out);
33  
}
34  
35  
sbool jreplace_dyn(LS tok, S in, IF2<LS, Int, S> out, IF2<LS, Int, Bool> condition) {
36  
  ret jreplace_dyn(tok, in, (O) out, (O) condition);
37  
}
38  
39  
static boolean jreplace_dyn(L<S> tok, S in, O out, O condition) {
40  
  ret jreplace_dyn(tok, in, out, false, true, condition);
41  
}
42  
43  
static boolean jreplace_dyn(L<S> tok, S in, O out, boolean ignoreCase, boolean reTok, O condition) {
44  
  L<S> tokin = javaTok(in);
45  
  jfind_preprocess(tokin);
46  
  
47  
  S[] toks = toStringArray(codeTokensOnly(tokin));
48  
49  
  boolean anyChange = false;
50  
  for (int n = 0; n < 10000; n++) {
51  
    int i = findCodeTokens(tok, 1, ignoreCase, toks, condition);
52  
    if (i < 0)
53  
      ret anyChange;
54  
    S expansion = cast callF(out, tok, i);
55  
    int end = i+l(tokin)-2;
56  
    clearAllTokens(tok, i, end); // C to C
57  
    tok.set(i, unnull(expansion));
58  
    if (reTok) // would this ever be false??
59  
      reTok(tok, i, end);
60  
    anyChange = true;
61  
  }
62  
  throw fail("woot? 10000! " + quote(in) + " => " + quote(out));
63  
}

Author comment

Began life as a copy of #1002133

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: #1009462
Snippet name: jreplace_dyn
Eternal ID of this version: #1009462/10
Text MD5: d6066e2c7f7dc5465509d7f85586f8ac
Transpilation MD5: 9471f964d22d8bdc0ab4427895600ca6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-08-20 16:38:16
Source code size: 1949 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 592 / 738
Version history: 9 change(s)
Referenced in: [show references]