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

73
LINES

< > BotCompany Repo | #1001735 - unquote - opposite of quote. also takes single quoted strings

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

Transpiled version (1006L) is out of date.

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

Author comment

Began life as a copy of #2000521

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1001735
Snippet name: unquote - opposite of quote. also takes single quoted strings
Eternal ID of this version: #1001735/12
Text MD5: 637020ec22cf2392f621dbfebb579b12
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-06 02:48:01
Source code size: 2357 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 656 / 8862
Version history: 11 change(s)
Referenced in: [show references]

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