1 | public static String unquote(String s) {
|
2 | if (s.startsWith("[")) {
|
3 | int i = 1;
|
4 | while (i < s.length() && s.charAt(i) == '=') ++i;
|
5 | if (i < s.length() && s.charAt(i) == '[') {
|
6 | String m = s.substring(1, i);
|
7 | if (s.endsWith("]" + m + "]"))
|
8 | return s.substring(i+1, s.length()-i-1);
|
9 | }
|
10 | }
|
11 |
|
12 | if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) {
|
13 | String st = s.substring(1, s.length()-1);
|
14 | StringBuilder sb = new StringBuilder(st.length());
|
15 |
|
16 | for (int i = 0; i < st.length(); i++) {
|
17 | char ch = st.charAt(i);
|
18 | if (ch == '\\') {
|
19 | char nextChar = (i == st.length() - 1) ? '\\' : st
|
20 | .charAt(i + 1);
|
21 | // Octal escape?
|
22 | if (nextChar >= '0' && nextChar <= '7') {
|
23 | String code = "" + nextChar;
|
24 | i++;
|
25 | if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
|
26 | && st.charAt(i + 1) <= '7') {
|
27 | code += st.charAt(i + 1);
|
28 | i++;
|
29 | if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
|
30 | && st.charAt(i + 1) <= '7') {
|
31 | code += st.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 >= st.length() - 5) {
|
66 | ch = 'u';
|
67 | break;
|
68 | }
|
69 | int code = Integer.parseInt(
|
70 | "" + st.charAt(i + 2) + st.charAt(i + 3)
|
71 | + st.charAt(i + 4) + st.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 | } else
|
84 | return s; // return original
|
85 | } |