1 | static String unquote(String s) {
|
2 | if (s == null) return null; |
3 | if (s.startsWith("[")) {
|
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.startsWith("\"") || s.startsWith("\'")) && s.length() > 1) {
|
14 | int l = s.endsWith(substring(s, 0, 1)) ? s.length()-1 : s.length(); |
15 | StringBuilder sb = new StringBuilder(l-1); |
16 | |
17 | for (int i = 1; i < l; i++) {
|
18 | char ch = s.charAt(i); |
19 | if (ch == '\\') {
|
20 | char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); |
21 | // Octal escape? |
22 | if (nextChar >= '0' && nextChar <= '7') {
|
23 | String code = "" + nextChar; |
24 | i++; |
25 | if ((i < l - 1) && s.charAt(i + 1) >= '0' |
26 | && s.charAt(i + 1) <= '7') {
|
27 | code += s.charAt(i + 1); |
28 | i++; |
29 | if ((i < l - 1) && s.charAt(i + 1) >= '0' |
30 | && s.charAt(i + 1) <= '7') {
|
31 | code += s.charAt(i + 1); |
32 | i++; |
33 | } |
34 | } |
35 | sb.append((char) Integer.parseInt(code, 8)); |
36 | continue; |
37 | } |
38 | switch (nextChar) {
|
39 | case '\\': |
40 | ch = '\\'; |
41 | break; |
42 | case 'b': |
43 | ch = '\b'; |
44 | break; |
45 | case 'f': |
46 | ch = '\f'; |
47 | break; |
48 | case 'n': |
49 | ch = '\n'; |
50 | break; |
51 | case 'r': |
52 | ch = '\r'; |
53 | break; |
54 | case 't': |
55 | ch = '\t'; |
56 | break; |
57 | case '\"': |
58 | ch = '\"'; |
59 | break; |
60 | case '\'': |
61 | ch = '\''; |
62 | break; |
63 | // Hex Unicode: u???? |
64 | case 'u': |
65 | if (i >= l - 5) {
|
66 | ch = 'u'; |
67 | break; |
68 | } |
69 | int code = Integer.parseInt( |
70 | "" + s.charAt(i + 2) + s.charAt(i + 3) |
71 | + s.charAt(i + 4) + s.charAt(i + 5), 16); |
72 | sb.append(Character.toChars(code)); |
73 | i += 5; |
74 | continue; |
75 | default: |
76 | ch = nextChar; // added by Stefan |
77 | } |
78 | i++; |
79 | } |
80 | sb.append(ch); |
81 | } |
82 | return sb.toString(); |
83 | } |
84 | |
85 | return s; // not quoted - return original |
86 | } |
Began life as a copy of #1001735
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
No comments. add comment
| Snippet ID: | #1010581 |
| Snippet name: | unquote basic |
| Eternal ID of this version: | #1010581/1 |
| Text MD5: | bba8699ef61d9005710310d3c244c052 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-09-22 01:50:08 |
| Source code size: | 2475 bytes / 86 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 717 / 2901 |
| Referenced in: | [show references] |