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

59
LINES

< > BotCompany Repo | #1000647 // mysqlTok function - MySQL tokenizer

JavaX fragment (include)

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

Author comment

Began life as a copy of #728

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: #1000647
Snippet name: mysqlTok function - MySQL tokenizer
Eternal ID of this version: #1000647/1
Text MD5: 4e6c9de3b807da29e358aae4313e7640
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-22 16:26:14
Source code size: 1553 bytes / 59 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 696 / 946
Referenced in: [show references]