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

56
LINES

< > BotCompany Repo | #1008206 // firstJavaToken

JavaX fragment (include)

static S firstJavaToken(S s) {
  if (s == null) null;
  int l = s.length();

  int j = 0;
  char c, d;
    
  // scan for whitespace
  while (j < l) {
    c = s.charAt(j);
    d = j+1 >= l ? '\0' : s.charAt(j+1);
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
      ++j;
    else if (c == '/' && d == '*') {
      do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
      j = Math.min(j+2, l);
    } else if (c == '/' && d == '/') {
      do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
    } else
      break;
  }
    
  if (j >= l) null;
  int i = j;
  c = s.charAt(i);
  d = i+1 >= l ? '\0' : s.charAt(i+1);

  // scan for non-whitespace
  if (c == '\'' || c == '"') {
    char opener = c;
    ++j;
    while (j < l) {
      if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
        ++j;
        break;
      } else if (s.charAt(j) == '\\' && j+1 < l)
        j += 2;
      else
        ++j;
    }
  } else if (Character.isJavaIdentifierStart(c))
    do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
  else if (Character.isDigit(c)) {
    do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
  } else if (c == '[' && d == '[') {
    do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
    j = Math.min(j+2, l);
  } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
    do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
    j = Math.min(j+3, l);
  } else
    ++j;
    
  ret quickSubstring(s, i, j);
}

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: 436 / 489
Version history: 2 change(s)
Referenced in: [show references]