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

62
LINES

< > BotCompany Repo | #1008761 // javaTokC_noMLS_iterator - javaTokC as Producer, no multi-line strings

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

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

static Producer<S> javaTokC_noMLS_iterator(final S s) {
  ret javaTokC_noMLS_iterator(s, 0);
}

static Producer<S> javaTokC_noMLS_iterator(final S s, final int startIndex) {
  ret new Producer<S>() {
    final int l = s.length();
    int i = startIndex;
    
    public S next() {
      if (i >= l) ret null;
      
      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 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;
      }
      
      i = j;
      if (i >= l) ret null;
      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)));
      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;
        
      S t = quickSubstring(s, i, j);
      i = j;
      ret t;
    }
  };
}

Author comment

Began life as a copy of #1005977

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: #1008761
Snippet name: javaTokC_noMLS_iterator - javaTokC as Producer, no multi-line strings
Eternal ID of this version: #1008761/5
Text MD5: 0d5ba4aa36426a1bedab409118031116
Transpilation MD5: 9c81f1e2a439e087f7f03b66a0f9ac76
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-13 20:03:21
Source code size: 1862 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 466 / 592
Version history: 4 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)