1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package ch.qos.cal10n.util;
23
24
25
26
27 public class LexicalUtil {
28
29
30 public static StringBuilder convertSpecialCharacters(StringBuilder inBuf) {
31
32 int i = inBuf.indexOf("\\");
33
34 if (i == -1) {
35 return inBuf;
36 }
37
38 StringBuilder outBuf = new StringBuilder(inBuf.length());
39
40 int followIndex = -1;
41 while (i != -1) {
42 outBuf.append(inBuf.subSequence(followIndex + 1, i));
43 followIndex = i + 1;
44 char c = inBuf.charAt(followIndex);
45 switch (c) {
46 case 'u':
47 char unicodeChar = readUnicode(inBuf, followIndex + 1);
48 outBuf.append(unicodeChar);
49 followIndex += 4;
50 break;
51 case 'n':
52 outBuf.append('\n');
53 break;
54 case 'r':
55 outBuf.append('\r');
56 break;
57 case 't':
58 outBuf.append('\t');
59 break;
60 case 'f':
61 outBuf.append('\f');
62 break;
63 case ':':
64 case '#':
65 case '!':
66 case '=':
67 outBuf.append(c);
68 break;
69 default:
70 outBuf.append('\\');
71 outBuf.append(c);
72 }
73
74 i = inBuf.indexOf("\\", followIndex + 1);
75 }
76 outBuf.append(inBuf.subSequence(followIndex + 1, inBuf.length()));
77 return outBuf;
78 }
79
80
81
82 private static char readUnicode(StringBuilder inBuf, int i) {
83 int r = 0;
84 for (int j = i; j < i + 4; j++) {
85 char atJ = inBuf.charAt(j);
86
87 if (atJ >= '0' && atJ <= '9') {
88 r = (r << 4) + atJ - '0';
89 continue;
90 }
91 if (atJ >= 'A' && atJ <= 'F') {
92
93 r = (r << 4) + atJ - '7';
94 continue;
95 }
96 if (atJ >= 'a' && atJ <= 'f') {
97
98 r = (r << 4) + atJ - 'W';
99 continue;
100 }
101 }
102 return (char) r;
103 }
104 }