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

44
LINES

< > BotCompany Repo | #1023688 // javaTok_noCommentsNoStrings

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

Libraryless. Click here for Pure Java version (85L/1K).

static List<String> javaTok_noCommentsNoStrings(String s) {
  new ArrayList<S> tok;
  int l = s == null ? 0 : s.length();
  
  int i = 0, n = 0;
  while (i < l) {
    int j = i;
    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
        break;
    }
    
    tok.add(javaTok_substringN(s, i, j));
    ++n;
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    
    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
      ++j;
      
    tok.add(javaTok_substringC(s, i, j));
    ++n;
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  ret tok;
}

Author comment

Began life as a copy of #1000688

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023688
Snippet name: javaTok_noCommentsNoStrings
Eternal ID of this version: #1023688/1
Text MD5: e181e869e55d0fdc1e9e441c1a4d03a5
Transpilation MD5: 6b2cbb2688107c2ad528c52d51a6b65e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-02 20:28:07
Source code size: 1150 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 312 / 380
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1023689 - wordTok2_uncached - without strings and comments
#1034375 - letterSeqOnlyTok - only sequences of letters are tokens