Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/idswitch/CodePrinter.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.javascript.tools.idswitch;
7
8 class CodePrinter {
9
10 // length of u-type escape like \u12AB
11 private static final int LITERAL_CHAR_MAX_SIZE = 6;
12
13 private String lineTerminator = "\n";
14
15 private int indentStep = 4;
16 private int indentTabSize = 8;
17
18 private char[] buffer = new char[1 << 12]; // 4K
19 private int offset;
20
21 public String getLineTerminator() { return lineTerminator; }
22 public void setLineTerminator(String value) { lineTerminator = value; }
23
24 public int getIndentStep() { return indentStep; }
25 public void setIndentStep(int char_count) { indentStep = char_count; }
26
27 public int getIndentTabSize() { return indentTabSize; }
28 public void setIndentTabSize(int tab_size) { indentTabSize = tab_size; }
29
30 public void clear() {
31 offset = 0;
32 }
33
34 private int ensure_area(int area_size) {
35 int begin = offset;
36 int end = begin + area_size;
37 if (end > buffer.length) {
38 int new_capacity = buffer.length * 2;
39 if (end > new_capacity) { new_capacity = end; }
40 char[] tmp = new char[new_capacity];
41 System.arraycopy(buffer, 0, tmp, 0, begin);
42 buffer = tmp;
43 }
44 return begin;
45 }
46
47 private int add_area(int area_size) {
48 int pos = ensure_area(area_size);
49 offset = pos + area_size;
50 return pos;
51 }
52
53 public int getOffset() {
54 return offset;
55 }
56
57 public int getLastChar() {
58 return offset == 0 ? -1 : buffer[offset - 1];
59 }
60
61 public void p(char c) {
62 int pos = add_area(1);
63 buffer[pos] = c;
64 }
65
66 public void p(String s) {
67 int l = s.length();
68 int pos = add_area(l);
69 s.getChars(0, l, buffer, pos);
70 }
71
72 public final void p(char[] array) {
73 p(array, 0, array.length);
74 }
75
76 public void p(char[] array, int begin, int end) {
77 int l = end - begin;
78 int pos = add_area(l);
79 System.arraycopy(array, begin, buffer, pos, l);
80 }
81
82 public void p(int i) {
83 p(Integer.toString(i));
84 }
85
86 public void qchar(int c) {
87 int pos = ensure_area(2 + LITERAL_CHAR_MAX_SIZE);
88 buffer[pos] = '\'';
89 pos = put_string_literal_char(pos + 1, c, false);
90 buffer[pos] = '\'';
91 offset = pos + 1;
92 }
93
94 public void qstring(String s) {
95 int l = s.length();
96 int pos = ensure_area(2 + LITERAL_CHAR_MAX_SIZE * l);
97 buffer[pos] = '"';
98 ++pos;
99 for (int i = 0; i != l; ++i) {
100 pos = put_string_literal_char(pos, s.charAt(i), true);
101 }
102 buffer[pos] = '"';
103 offset = pos + 1;
104 }
105
106 private int put_string_literal_char(int pos, int c, boolean in_string) {
107 boolean backslash_symbol = true;
108 switch (c) {
109 case '\b': c = 'b'; break;
110 case '\t': c = 't'; break;
111 case '\n': c = 'n'; break;
112 case '\f': c = 'f'; break;
113 case '\r': c = 'r'; break;
114 case '\'': backslash_symbol = !in_string; break;
115 case '"': backslash_symbol = in_string; break;
116 default: backslash_symbol = false;
117 }
118
119 if (backslash_symbol) {
120 buffer[pos] = '\\';
121 buffer[pos + 1] = (char)c;
122 pos += 2;
123 }
124 else if (' ' <= c && c <= 126) {
125 buffer[pos] = (char)c;
126 ++pos;
127 }
128 else {
129 buffer[pos] = '\\';
130 buffer[pos + 1] = 'u';
131 buffer[pos + 2] = digit_to_hex_letter(0xF & (c >> 12));
132 buffer[pos + 3] = digit_to_hex_letter(0xF & (c >> 8));
133 buffer[pos + 4] = digit_to_hex_letter(0xF & (c >> 4));
134 buffer[pos + 5] = digit_to_hex_letter(0xF & c);
135 pos += 6;
136 }
137 return pos;
138 }
139
140 private static char digit_to_hex_letter(int d) {
141 return (char)((d < 10) ? '0' + d : 'A' - 10 + d);
142 }
143
144 public void indent(int level) {
145 int visible_size = indentStep * level;
146 int indent_size, tab_count;
147 if (indentTabSize <= 0) {
148 tab_count = 0; indent_size = visible_size;
149 }
150 else {
151 tab_count = visible_size / indentTabSize;
152 indent_size = tab_count + visible_size % indentTabSize;
153 }
154 int pos = add_area(indent_size);
155 int tab_end = pos + tab_count;
156 int indent_end = pos + indent_size;
157 for (; pos != tab_end; ++pos) { buffer[pos] = '\t'; }
158 for (; pos != indent_end; ++pos) { buffer[pos] = ' '; }
159 }
160
161 public void nl() {
162 p('\n');
163 }
164
165 public void line(int indent_level, String s) {
166 indent(indent_level); p(s); nl();
167 }
168
169 public void erase(int begin, int end) {
170 System.arraycopy(buffer, end, buffer, begin, offset - end);
171 offset -= end - begin;
172 }
173
174 @Override
175 public String toString() {
176 return new String(buffer, 0, offset);
177 }
178
179
180
181 }