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

56
LINES

< > BotCompany Repo | #1000593 - function jsonTok

JavaX fragment (include)

1  
static LS jsonTok(S s) {
2  
  new LS tok;
3  
  int l = l(s);
4  
  
5  
  int i = 0;
6  
  while (i < l) {
7  
    int j = i;
8  
    char c; String cc;
9  
    
10  
    // scan for whitespace
11  
    while (j < l) {
12  
      c = s.charAt(j);
13  
      cc = s.substring(j, Math.min(j+2, l));
14  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
15  
        ++j;
16  
      else if (cc.equals("/*")) {
17  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
18  
        j = Math.min(j+2, l);
19  
      } else if (cc.equals("//")) {
20  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
21  
      } else
22  
        break;
23  
    }
24  
    
25  
    tok.add(s.substring(i, j));
26  
    i = j;
27  
    if (i >= l) break;
28  
    c = s.charAt(i); // cc is not needed in rest of loop body
29  
30  
    // scan for non-whitespace (json strings, "null" identifier, numbers. everything else automatically becomes a one character token.)
31  
    if (c == '\'' || c == '"') {
32  
      char opener = c;
33  
      ++j;
34  
      while (j < l) {
35  
        if (s.charAt(j) == opener) {
36  
          ++j;
37  
          break;
38  
        } else if (s.charAt(j) == '\\' && j+1 < l)
39  
          j += 2;
40  
        else
41  
          ++j;
42  
      }
43  
    } else if (Character.isLetter(c))
44  
      do ++j; while (j < l && Character.isLetter(s.charAt(j)));
45  
    else if (Character.isDigit(c))
46  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
47  
    else
48  
      ++j;
49  
50  
    tok.add(s.substring(i, j));
51  
    i = j;
52  
  }
53  
  
54  
  if ((tok.size() % 2) == 0) tok.add("");
55  
  return tok;
56  
}

Author comment

Began life as a copy of #728

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1000593
Snippet name: function jsonTok
Eternal ID of this version: #1000593/2
Text MD5: e0c7480596b5486adb62e5125f3b2156
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-27 00:26:28
Source code size: 1518 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 477 / 1958
Version history: 1 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000593 & http://1000593.tinybrain.de