1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.pattern.util;
15
16
17
18
19
20
21 public class RegularEscapeUtil implements IEscapeUtil {
22
23 public void escape(String escapeChars, StringBuffer buf, char next, int pointer) {
24 if (escapeChars.indexOf(next) >= 0) {
25 buf.append(next);
26 } else
27 switch (next) {
28 case '_':
29
30 break;
31 case '\\':
32 buf.append(next);
33 break;
34 case 't':
35 buf.append('\t');
36 break;
37 case 'r':
38 buf.append('\r');
39 break;
40 case 'n':
41 buf.append('\n');
42 break;
43 default:
44 String commaSeperatedEscapeChars = formatEscapeCharsForListing(escapeChars);
45 throw new IllegalArgumentException("Illegal char '" + next + " at column " + pointer
46 + ". Only \\\\, \\_" + commaSeperatedEscapeChars
47 + ", \\t, \\n, \\r combinations are allowed as escape characters.");
48 }
49 }
50
51 String formatEscapeCharsForListing(String escapeChars) {
52 StringBuilder commaSeperatedEscapeChars = new StringBuilder();
53 for (int i = 0; i < escapeChars.length(); i++) {
54 commaSeperatedEscapeChars.append(", \\").append(escapeChars.charAt(i));
55 }
56 return commaSeperatedEscapeChars.toString();
57 }
58
59
60
61 public static String basicEscape(String s) {
62 char c;
63 int len = s.length();
64 StringBuilder sbuf = new StringBuilder(len);
65
66 int i = 0;
67 while (i < len) {
68 c = s.charAt(i++);
69 if (c == '\\' && i < len ) {
70 c = s.charAt(i++);
71 if (c == 'n') {
72 c = '\n';
73 } else if (c == 'r') {
74 c = '\r';
75 } else if (c == 't') {
76 c = '\t';
77 } else if (c == 'f') {
78 c = '\f';
79 } else if (c == '\b') {
80 c = '\b';
81 } else if (c == '\"') {
82 c = '\"';
83 } else if (c == '\'') {
84 c = '\'';
85 } else if (c == '\\') {
86 c = '\\';
87 }
88
89 }
90 sbuf.append(c);
91 }
92 return sbuf.toString();
93 }
94 }