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

56
LINES

< > BotCompany Repo | #1008206 // firstJavaToken

JavaX fragment (include)

1  
static S firstJavaToken(S s) {
2  
  if (s == null) null;
3  
  int l = s.length();
4  
5  
  int j = 0;
6  
  char c, d;
7  
    
8  
  // scan for whitespace
9  
  while (j < l) {
10  
    c = s.charAt(j);
11  
    d = j+1 >= l ? '\0' : s.charAt(j+1);
12  
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
13  
      ++j;
14  
    else if (c == '/' && d == '*') {
15  
      do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
16  
      j = Math.min(j+2, l);
17  
    } else if (c == '/' && d == '/') {
18  
      do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
19  
    } else
20  
      break;
21  
  }
22  
    
23  
  if (j >= l) null;
24  
  int i = j;
25  
  c = s.charAt(i);
26  
  d = i+1 >= l ? '\0' : s.charAt(i+1);
27  
28  
  // scan for non-whitespace
29  
  if (c == '\'' || c == '"') {
30  
    char opener = c;
31  
    ++j;
32  
    while (j < l) {
33  
      if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
34  
        ++j;
35  
        break;
36  
      } else if (s.charAt(j) == '\\' && j+1 < l)
37  
        j += 2;
38  
      else
39  
        ++j;
40  
    }
41  
  } else if (Character.isJavaIdentifierStart(c))
42  
    do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
43  
  else if (Character.isDigit(c)) {
44  
    do ++j; while (j < l && Character.isDigit(s.charAt(j)));
45  
    if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
46  
  } else if (c == '[' && d == '[') {
47  
    do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
48  
    j = Math.min(j+2, l);
49  
  } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
50  
    do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
51  
    j = Math.min(j+3, l);
52  
  } else
53  
    ++j;
54  
    
55  
  ret quickSubstring(s, i, j);
56  
}

Author comment

Began life as a copy of #1007538

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, whxojlpjdney

No comments. add comment

Snippet ID: #1008206
Snippet name: firstJavaToken
Eternal ID of this version: #1008206/3
Text MD5: f2bd11846170b94cb31f830fe837f450
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-17 00:24:04
Source code size: 1757 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 502 / 563
Version history: 2 change(s)
Referenced in: [show references]