-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDebugOutput.java
64 lines (62 loc) · 1.17 KB
/
DebugOutput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package edu.lsu.cct.piraha;
import java.io.PrintWriter;
public class DebugOutput {
public int indent;
PrintWriter pw;
boolean newline = true;
public DebugOutput() {
pw = new PrintWriter(System.out);
}
public DebugOutput(PrintWriter pw) {
this.pw = pw;
}
public static String outc(char c) {
if(c == '\n')
return "\\n";
else if(c == '\r')
return "\\r";
else if(c == '\t')
return "\\t";
else if(c == '\b')
return "\\b";
else if(c < 10 || c >= 128) {
String hex = Integer.toHexString(c);
while(hex.length() < 4)
hex = "0"+hex;
return "\\u"+hex;
} else {
return Character.toString(c);
}
}
public void print(Object o) {
if(o != null) {
if(newline) {
for(int i=0;i<indent;i++)
pw.print(' ');
newline=false;
}
String s = o.toString();
outs(s);
}
pw.flush();
}
public void outs(String s) {
for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
pw.print(outc(c));
}
}
public void println(Object o) {
print(o);
println();
}
public void println() {
pw.println();
pw.flush();
newline = true;
}
public final static DebugOutput out = new DebugOutput();
public void flush() {
pw.flush();
}
}