1 | static S unquote_accurate(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 | int l = s.length(); |
14 | if (l > 1) { |
15 | char opener = s.charAt(0); |
16 | if (opener == '\"' || opener == '\'') { |
17 | if (s.charAt(l-1) != opener) ret s; |
18 | --l; |
19 | StringBuilder sb = new StringBuilder(l); |
20 | |
21 | for (int i = 1; i < l; i++) { |
22 | char ch = s.charAt(i); |
23 | if (ch == opener) ret s; // improper string |
24 | if (ch == '\\') { |
25 | char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); |
26 | // Octal escape? |
27 | if (nextChar >= '0' && nextChar <= '7') { |
28 | String code = "" + nextChar; |
29 | i++; |
30 | if ((i < l - 1) && s.charAt(i + 1) >= '0' |
31 | && s.charAt(i + 1) <= '7') { |
32 | code += s.charAt(i + 1); |
33 | i++; |
34 | if ((i < l - 1) && s.charAt(i + 1) >= '0' |
35 | && s.charAt(i + 1) <= '7') { |
36 | code += s.charAt(i + 1); |
37 | i++; |
38 | } |
39 | } |
40 | sb.append((char) Integer.parseInt(code, 8)); |
41 | continue; |
42 | } |
43 | switch (nextChar) { |
44 | case '\"': ch = '\"'; break; |
45 | case '\\': ch = '\\'; break; |
46 | case 'b': ch = '\b'; break; |
47 | case 'f': ch = '\f'; break; |
48 | case 'n': ch = '\n'; break; |
49 | case 'r': ch = '\r'; break; |
50 | case 't': ch = '\t'; break; |
51 | case '\'': ch = '\''; break; |
52 | // Hex Unicode: u???? |
53 | case 'u': |
54 | if (i >= l - 5) { |
55 | ch = 'u'; |
56 | break; |
57 | } |
58 | int code = Integer.parseInt( |
59 | "" + s.charAt(i + 2) + s.charAt(i + 3) |
60 | + s.charAt(i + 4) + s.charAt(i + 5), 16); |
61 | sb.append(Character.toChars(code)); |
62 | i += 5; |
63 | continue; |
64 | default: |
65 | ch = nextChar; // added by Stefan |
66 | } |
67 | i++; |
68 | } |
69 | sb.append(ch); |
70 | } |
71 | ret sb.toString(); |
72 | } |
73 | } |
74 | |
75 | ret s; // not quoted - return original |
76 | } |
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: | #1012991 |
Snippet name: | unquote_accurate - not fooled by "\"a\" or \"b\"" (dev.) |
Eternal ID of this version: | #1012991/3 |
Text MD5: | a6b76f05aae2ac588cdf938e420def8d |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-12-19 17:27:41 |
Source code size: | 2442 bytes / 76 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 430 / 454 |
Version history: | 2 change(s) |
Referenced in: | [show references] |