01: package gnu.jemacs.lang;
02:
03: import gnu.kawa.functions.DisplayFormat;
04: import gnu.lists.Consumer;
05:
06: public class Print extends DisplayFormat {
07: public Print(boolean readable) {
08: super (readable, 'E');
09: }
10:
11: public static String escapeChar(int ch) {
12: StringBuffer sbuf = new StringBuffer();
13: sbuf.append('?');
14: if (ch < ' ') {
15: sbuf.append('\\');
16: switch (ch) {
17: case '\t':
18: sbuf.append('t');
19: break;
20: case '\n':
21: sbuf.append('n');
22: break;
23: case '\r':
24: sbuf.append('r');
25: break;
26: default:
27: sbuf.append('^');
28: sbuf.append((char) (ch + 64));
29: if ((ch + 64) == '\\')
30: sbuf.append('\\');
31: }
32: } else if (ch < 127) {
33: /* syntactically special characters should be escaped. */
34: switch (ch) {
35: case ' ':
36: case '"':
37: case '#':
38: case '\'':
39: case '(':
40: case ')':
41: case ',':
42: case '.':
43: case ';':
44: case '?':
45: case '[':
46: case '\\':
47: case ']':
48: case '`':
49: sbuf.append('\\');
50: }
51: sbuf.append((char) ch);
52: } else if (ch == 127)
53: sbuf.append("\\^?");
54: else {
55: if (ch < 160)
56: sbuf.append("\\^");
57: sbuf.append(ch);
58: }
59: return sbuf.toString();
60: }
61:
62: public void write(int v, Consumer out) {
63: if (getReadableOutput())
64: write(Print.escapeChar(v), out);
65: else
66: out.write(v);
67: }
68:
69: }
|