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

88
LINES

< > BotCompany Repo | #1013521 // javaTokWithExisting

JavaX fragment (include)

1  
please include function javaTok.
2  
3  
static List<String> javaTokWithExisting(String s, L<S> existing) {
4  
  ++javaTok_n;
5  
  int nExisting = javaTok_opt && existing != null ? existing.size() : 0;
6  
  ArrayList<String> tok = existing != null ? new ArrayList(nExisting) : new ArrayList();
7  
  int l = s.length();
8  
  
9  
  int i = 0, n = 0;
10  
  while (i < l) {
11  
    int j = i;
12  
    char c, d;
13  
    
14  
    // scan for whitespace
15  
    while (j < l) {
16  
      c = s.charAt(j);
17  
      d = j+1 >= l ? '\0' : s.charAt(j+1);
18  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
19  
        ++j;
20  
      else if (c == '/' && d == '*') {
21  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
22  
        j = Math.min(j+2, l);
23  
      } else if (c == '/' && d == '/') {
24  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
25  
      } else
26  
        break;
27  
    }
28  
    
29  
    if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j))
30  
      tok.add(existing.get(n));
31  
    else
32  
      tok.add(javaTok_substringN(s, i, j));
33  
    ++n;
34  
    i = j;
35  
    if (i >= l) break;
36  
    c = s.charAt(i);
37  
    d = i+1 >= l ? '\0' : s.charAt(i+1);
38  
39  
    // scan for non-whitespace
40  
    
41  
    // Special JavaX syntax: 'identifier
42  
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
43  
      j += 2;
44  
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
45  
        ++j;
46  
    } else if (c == '\'' || c == '"') {
47  
      char opener = c;
48  
      ++j;
49  
      while (j < l) {
50  
        if (s.charAt(j) == opener /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings
51  
          ++j;
52  
          break;
53  
        } else if (s.charAt(j) == '\\' && j+1 < l)
54  
          j += 2;
55  
        else
56  
          ++j;
57  
      }
58  
    } else if (Character.isJavaIdentifierStart(c))
59  
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
60  
    else if (Character.isDigit(c)) {
61  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
62  
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
63  
    } else if (c == '[' && d == '[') {
64  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
65  
      j = Math.min(j+2, l);
66  
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
67  
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
68  
      j = Math.min(j+3, l);
69  
    } else
70  
      ++j;
71  
      
72  
    if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j))
73  
      tok.add(existing.get(n));
74  
    else
75  
      tok.add(javaTok_substringC(s, i, j));
76  
    ++n;
77  
    i = j;
78  
  }
79  
  
80  
  if ((tok.size() % 2) == 0) tok.add("");
81  
  javaTok_elements += tok.size();
82  
  return tok;
83  
}
84  
85  
static bool javaTokWithExisting_isCopyable(S t, S s, int i, int j) {
86  
  ret t.length() == j-i
87  
    && s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave
88  
}

Author comment

Began life as a copy of #1000688

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1013521
Snippet name: javaTokWithExisting
Eternal ID of this version: #1013521/4
Text MD5: aac89d3011d1086fa365589204239426
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-08 03:16:17
Source code size: 2975 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 400 / 459
Version history: 3 change(s)
Referenced in: [show references]