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

20
LINES

< > BotCompany Repo | #1025618 - tok_splitAtEmptyLines - token-aware split at empty lines

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

Libraryless. Click here for Pure Java version (2501L/16K).

// rtrims each item
static LS tok_splitAtEmptyLines(S s) {
  new L<Int> indices;
  LS tok = javaTokWithAllBrackets(s);
  new LS out;
  int start = 0;
  S spacing = "";
  for (int i = 2; i+1 < l(tok); i += 2) {
    int idx = indexOf_regexp(tok.get(i), "\r?\n\\s*\r?\n");
    if (idx < 0) continue;
    int idx2 = idx+1;
    if (tok.get(i).charAt(idx) == '\r') ++idx2;
    out.add(dropPrefix("\n", spacing + joinSubList(tok, start, i)));
    start = i+1;
    spacing = substring(tok.get(i), idx2); // TODO: not exact
  }
  if (start < l(tok))
    out.add(dropPrefix("\n", spacing + joinSubList(tok, start)));
  ret out;
}

Author comment

Began life as a copy of #1024054

download  show line numbers  debug dex   

Travelled to 2 computer(s): mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1025618
Snippet name: tok_splitAtEmptyLines - token-aware split at empty lines
Eternal ID of this version: #1025618/6
Text MD5: c18268385f86752604e36361880c9cbc
Transpilation MD5: aa82b59a49a6e7c2b146bcd22fd0be5c
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-10 01:24:43
Source code size: 638 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 16 / 56
Version history: 5 change(s)
Referenced in: [show references]