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

77
LINES

< > BotCompany Repo | #1015252 // unquoteUsingCharArray

JavaX fragment (include)

1  
static S unquoteUsingCharArray(S s, char[] buf) {
2  
  if (s == null) null;
3  
  if (startsWith(s, '[')) {
4  
    int i = 1;
5  
    while (i < s.length() && s.charAt(i) == '=') ++i;
6  
    if (i < s.length() && s.charAt(i) == '[') {
7  
      S m = s.substring(1, i);
8  
      if (s.endsWith("]" + m + "]"))
9  
        ret s.substring(i+1, s.length()-i-1);
10  
    }
11  
  }
12  
  
13  
  if (s.length() > 1) {
14  
    char c = s.charAt(0);
15  
    if (c == '\"' || c == '\'') {
16  
      int l = endsWith(s, c) ? s.length()-1 : s.length();
17  
      if (l > buf.length) ret unquote(s); // fallback
18  
      int n = 0;
19  
  
20  
      for (int i = 1; i < l; i++) {
21  
        char ch = s.charAt(i);
22  
        if (ch == '\\') {
23  
          char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
24  
          // Octal escape?
25  
          if (nextChar >= '0' && nextChar <= '7') {
26  
              String code = "" + nextChar;
27  
              i++;
28  
              if ((i < l - 1) && s.charAt(i + 1) >= '0'
29  
                      && s.charAt(i + 1) <= '7') {
30  
                  code += s.charAt(i + 1);
31  
                  i++;
32  
                  if ((i < l - 1) && s.charAt(i + 1) >= '0'
33  
                          && s.charAt(i + 1) <= '7') {
34  
                      code += s.charAt(i + 1);
35  
                      i++;
36  
                  }
37  
              }
38  
              buf[n++] = (char) Integer.parseInt(code, 8);
39  
              continue;
40  
          }
41  
          switch (nextChar) {
42  
          case '\"': ch = '\"'; break;
43  
          case '\\': ch = '\\'; break;
44  
          case 'b': ch = '\b'; break;
45  
          case 'f': ch = '\f'; break;
46  
          case 'n': ch = '\n'; break;
47  
          case 'r': ch = '\r'; break;
48  
          case 't': ch = '\t'; break;
49  
          case '\'': ch = '\''; break;
50  
          // Hex Unicode: u????
51  
          case 'u':
52  
              if (i >= l - 5) {
53  
                  ch = 'u';
54  
                  break;
55  
              }
56  
              int code = Integer.parseInt(
57  
                      "" + s.charAt(i + 2) + s.charAt(i + 3)
58  
                         + s.charAt(i + 4) + s.charAt(i + 5), 16);
59  
              char[] x = Character.toChars(code);
60  
              int lx = x.length;
61  
              for j to lx:
62  
                buf[n++] = x[j];
63  
              i += 5;
64  
              continue;
65  
          default:
66  
            ch = nextChar; // added by Stefan
67  
          }
68  
          i++;
69  
        }
70  
        buf[n++] = ch;
71  
      }
72  
      ret new String(buf, 0, n);
73  
    }
74  
  }
75  
    
76  
  ret s; // not quoted - return original
77  
}

Author comment

Began life as a copy of #1001735

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: #1015252
Snippet name: unquoteUsingCharArray
Eternal ID of this version: #1015252/4
Text MD5: 3855d5744915ff430acfaf60000f6289
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-11 02:34:01
Source code size: 2502 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 363 / 394
Version history: 3 change(s)
Referenced in: [show references]