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

43
LINES

< > BotCompany Repo | #1009462 - jreplace_dyn

JavaX fragment (include)

// out: func(L<S> tok, int cIndex) -> S
// condition: func(L<S> tok, int nIndex) -> S  [yeah it's inconsistent]
static S jreplace_dyn(S s, S in, O out) {
  ret jreplace_dyn(s, in, out, null);
}

static S jreplace_dyn(S s, S in, O out, O condition) {
  L<S> tok = javaTok(s);
  jreplace_dyn(tok, in, out, condition);
  ret join(tok);
}

// leaves tok properly tokenized
// returns true iff anything was replaced
static boolean jreplace_dyn(L<S> tok, S in, O out) {
  ret jreplace_dyn(tok, in, out, false, true, null);
}

static boolean jreplace_dyn(L<S> tok, S in, O out, O condition) {
  ret jreplace_dyn(tok, in, out, false, true, condition);
}

static boolean jreplace_dyn(L<S> tok, S in, O out, boolean ignoreCase, boolean reTok, O condition) {
  L<S> tokin = javaTok(in);
  jfind_preprocess(tokin);
  
  S[] toks = toStringArray(codeTokensOnly(tokin));

  boolean anyChange = false;
  for (int n = 0; n < 10000; n++) {
    int i = findCodeTokens(tok, 1, ignoreCase, toks, condition);
    if (i < 0)
      ret anyChange;
    S expansion = cast callF(out, tok, i);
    int end = i+l(tokin)-2;
    clearAllTokens(tok, i, end); // C to C
    tok.set(i, expansion);
    if (reTok) // would this ever be false??
      reTok(tok, i, end);
    anyChange = true;
  }
  throw fail("woot? 10000! " + quote(in) + " => " + quote(out));
}

Author comment

Began life as a copy of #1002133

download  show line numbers  debug dex   

Travelled to 9 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1009462
Snippet name: jreplace_dyn
Eternal ID of this version: #1009462/4
Text MD5: c3d36a57ab4bb5102ecef2e0c524f5ce
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-03 17:56:10
Source code size: 1370 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 292 / 312
Version history: 3 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1009462 & http://1009462.tinybrain.de