PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/kilim/analysis/Utils.java

http://github.com/kilim/kilim
Java | 78 lines | 54 code | 12 blank | 12 comment | 8 complexity | 63722cb0f9e369946863448fd2d5612e MD5 | raw file
 1/* Copyright (c) 2006, Sriram Srinivasan
 2 *
 3 * You may distribute this software under the terms of the license 
 4 * specified in the file "License"
 5 */
 6
 7package kilim.analysis;
 8
 9/**
10 * Simple string utils for pretty printing support
11 *
12 */
13public class Utils {
14    public static String indentStr = "";
15    public static String spaces    = "                                        ";
16
17    public static void indentWith(String s) {
18        indentStr = indentStr + s;
19    }
20
21    public static void indent(int numSpaces) {
22        indentWith(spaces.substring(0, numSpaces));
23    }
24
25    public static void dedent(int numSpaces) {
26        indentStr = indentStr.substring(0, indentStr.length() - numSpaces);
27    }
28
29    public static String format(String s) {
30        if (indentStr.length() == 0)
31            return s;
32        int i = s.indexOf('\n'); // i is always the index of newline
33        if (i >= 0) {
34            StringBuffer sb = new StringBuffer(100);
35            sb.append(indentStr); // leading indent
36            int prev = 0; // prev value of i in loop
37            do {
38                // copy from prev to i (including \n)
39                sb.append(s, prev, i + 1);
40                // add indentation wherever \n occurs
41                sb.append(indentStr);
42                prev = i + 1;
43                if (prev >= s.length())
44                    break;
45                i = s.indexOf('\n', prev);
46            } while (i != -1);
47            // copy left over chars from the last segment
48            sb.append(s, prev, s.length());
49            return sb.toString();
50        } else {
51            return indentStr + s;
52        }
53    }
54
55    public static void resetIndentation() {
56        indentStr = "";
57    }
58
59    public static void p(String s) {
60        System.out.print(format(s));
61    }
62
63    public static void pn(String s) {
64        System.out.println(format(s));
65    }
66
67    public static void pn(int i) {
68        System.out.println(format("" + i));
69    }
70
71    public static void pn() {
72        System.out.println();
73    }
74
75    public static void pn(Object o) {
76        pn((o == null) ? "null" : o.toString());
77    }
78}