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

56
LINES

< > BotCompany Repo | #728 // JsonTok - JSON Tokenizer (old, bad)

JavaX fragment (include)

class JsonTok {
  public static List<String> split(String s) {
    List<String> tok = new ArrayList<String>();
    int l = s.length();
    
    int i = 0;
    while (i < l) {
      int j = i;
      char c; String cc;
      
      // scan for whitespace
      while (j < l) {
        c = s.charAt(j);
        cc = s.substring(j, Math.min(j+2, l));
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          ++j;
        else if (cc.equals("/*")) {
          do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
          j = Math.min(j+2, l);
        } else if (cc.equals("//")) {
          do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
        } else
          break;
      }
      
      tok.add(s.substring(i, j));
      i = j;
      if (i >= l) break;
      c = s.charAt(i); // cc is not needed in rest of loop body

      // scan for non-whitespace (json strings and the "null" identifier. everything else automatically becomes a one character token.)
      if (c == '\'' || c == '"') {
        char opener = c;
        ++j;
        while (j < l) {
          if (s.charAt(j) == opener) {
            ++j;
            break;
          } else if (s.charAt(j) == '\\' && j+1 < l)
            j += 2;
          else
            ++j;
        }
      } else if (Character.isLetter(c))
        do ++j; while (j < l && Character.isLetter(s.charAt(j)));
      else
        ++j;

      tok.add(s.substring(i, j));
      i = j;
    }
    
    if ((tok.size() % 2) == 0) tok.add("");
    return tok;
  }
}

Author comment

Began life as a copy of #727

download  show line numbers  debug dex  old transpilations   

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

Comments [hide]

ID Author/Program Comment Date
1072 stefan Don't use. Use function "jsonTok" 2015-08-30 17:56:36

add comment

Snippet ID: #728
Snippet name: JsonTok - JSON Tokenizer (old, bad)
Eternal ID of this version: #728/1
Text MD5: 02334b9da8660693066a16267dcca41e
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-30 17:56:55
Source code size: 1596 bytes / 56 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 834 / 1533
Referenced in: [show references]