Debian Codegraph @

net/sf/saxon/tree/tiny/CompressedWhitespace.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.tiny;
9
10 import net.sf.saxon.tree.util.FastStringBuffer;
11
12 import java.io.Writer;
13
14 /**
15 * This class provides a compressed representation of a sequence of whitespace characters. The representation
16 * is a sequence of bytes: in each byte the top two bits indicate which whitespace character is used
17 * (x9, xA, xD, or x20) and the bottom six bits indicate the number of such characters. A zero byte is a filler.
18 * We don't compress the sequence if it would occupy more than 8 bytes, because that's the space we've got available
19 * in the TinyTree arrays.
20 */
21
22 public class CompressedWhitespace implements CharSequence {
23
24 /*@NotNull*/ private static char[] WHITE_CHARS = {0x09, 0x0A, 0x0D, 0x20};
25 /*@NotNull*/ private static int[] CODES =
26 {-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, 2, -1, -1,
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 3};
29
30 private long value;
31
32 public CompressedWhitespace(long compressedValue) {
33 value = compressedValue;
34 }
35
36 /**
37 * Attempt to compress a CharSequence
38 *
39 * @param in the CharSequence to be compressed
40 * @return the compressed sequence if it can be compressed; or the original CharSequence otherwise
41 */
42
43 /*@NotNull*/
44 public static CharSequence compress(/*@NotNull*/ CharSequence in) {
45 final int inlen = in.length();
46 if (inlen == 0) {
47 return in;
48 }
49 int runlength = 1;
50 int outlength = 0;
51 for (int i = 0; i < inlen; i++) {
52 final char c = in.charAt(i);
53 if (c <= 32 && CODES[c] >= 0) {
54 if (i == inlen - 1 || c != in.charAt(i + 1) || runlength == 63) {
55 runlength = 1;
56 outlength++;
57 if (outlength > 8) {
58 return in;
59 }
60 } else {
61 runlength++;
62 }
63 } else {
64 return in;
65 }
66 }
67 int ix = 0;
68 runlength = 1;
69 int[] out = new int[outlength];
70 for (int i = 0; i < inlen; i++) {
71 final char c = in.charAt(i);
72 if (i == inlen - 1 || c != in.charAt(i + 1) || runlength == 63) {
73 out[ix++] = (CODES[c] << 6) | runlength;
74 runlength = 1;
75 } else {
76 runlength++;
77 }
78 }
79 long value = 0;
80 for (int i = 0; i < outlength; i++) {
81 value = (value << 8) | out[i];
82 }
83 value <<= 8 * (8 - outlength);
84 return new CompressedWhitespace(value);
85 }
86
87 /**
88 * Uncompress the whitespace to a FastStringBuffer
89 *
90 * @param buffer the buffer to which the whitespace is to be appended. The parameter may be
91 * null, in which case a new buffer is created.
92 * @return the FastStringBuffer to which the whitespace has been appended. If a buffer was
93 * supplied in the argument, this will be the same buffer.
94 */
95
96 public FastStringBuffer uncompress(/*@Nullable*/ FastStringBuffer buffer) {
97 if (buffer == null) {
98 buffer = new FastStringBuffer(length());
99 }
100 uncompress(value, buffer);
101 return buffer;
102 }
103
104 public static void uncompress(long value, /*@NotNull*/ FastStringBuffer buffer) {
105 for (int s = 56; s >= 0; s -= 8) {
106 byte b = (byte) ((value >>> s) & 0xff);
107 if (b == 0) {
108 break;
109 }
110 char c = WHITE_CHARS[b >>> 6 & 0x3];
111 int len = b & 0x3f;
112 buffer.ensureCapacity(len);
113 for (int j = 0; j < len; j++) {
114 buffer.append(c);
115 }
116 }
117 }
118
119 public long getCompressedValue() {
120 return value;
121 }
122
123 public int length() {
124 int count = 0;
125 long val = value;
126 for (int s = 56; s >= 0; s -= 8) {
127 int c = (int) ((val >>> s) & 0x3f);
128 if (c == 0) {
129 break;
130 }
131 count += c;
132 }
133 return count;
134 }
135
136 /**
137 * Returns the <code>char</code> value at the specified index. An index ranges from zero
138 * to <tt>length() - 1</tt>. The first <code>char</code> value of the sequence is at
139 * index zero, the next at index one, and so on, as for array
140 * indexing.
141 * <p>If the <code>char</code> value specified by the index is a
142 * <a href="Character.html#unicode">surrogate</a>, the surrogate
143 * value is returned.</p>
144 *
145 * @param index the index of the <code>char</code> value to be returned
146 * @return the specified <code>char</code> value
147 * @throws IndexOutOfBoundsException if the <tt>index</tt> argument is negative or not less than
148 * <tt>length()</tt>
149 */
150 public char charAt(int index) {
151 int count = 0;
152 final long val = value;
153 for (int s = 56; s >= 0; s -= 8) {
154 byte b = (byte) ((val >>> s) & 0xff);
155 if (b == 0) {
156 break;
157 }
158 count += b & 0x3f;
159 if (count > index) {
160 return WHITE_CHARS[b >>> 6 & 0x3];
161 }
162 }
163 throw new IndexOutOfBoundsException(index + "");
164 }
165
166 /**
167 * Returns a new <code>CharSequence</code> that is a subsequence of this sequence.
168 * The subsequence starts with the <code>char</code> value at the specified index and
169 * ends with the <code>char</code> value at index <tt>end - 1</tt>. The length
170 * (in <code>char</code>s) of the
171 * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>
172 * then an empty sequence is returned.
173 *
174 * @param start the start index, inclusive
175 * @param end the end index, exclusive
176 * @return the specified subsequence
177 * @throws IndexOutOfBoundsException if <tt>start</tt> or <tt>end</tt> are negative,
178 * if <tt>end</tt> is greater than <tt>length()</tt>,
179 * or if <tt>start</tt> is greater than <tt>end</tt>
180 */
181 public CharSequence subSequence(int start, int end) {
182 return uncompress(null).subSequence(start, end);
183 }
184
185 /**
186 * Indicates whether some other object is "equal to" this one.
187 */
188 public boolean equals(/*@NotNull*/ Object obj) {
189 if (obj instanceof CompressedWhitespace) {
190 return value == ((CompressedWhitespace) obj).value;
191 }
192 return uncompress(null).equals(obj);
193 }
194
195 /**
196 * Returns a hash code value for the object.
197 */
198 public int hashCode() {
199 return uncompress(null).hashCode();
200 }
201
202 /**
203 * Returns a string representation of the object.
204 */
205 public String toString() {
206 return uncompress(null).toString();
207 }
208
209 /**
210 * Write the value to a Writer
211 *
212 * @param writer the writer to write to
213 * @throws java.io.IOException if an error occurs downstream
214 */
215
216 public void write(/*@NotNull*/ Writer writer) throws java.io.IOException {
217 final long val = value;
218 for (int s = 56; s >= 0; s -= 8) {
219 final byte b = (byte) ((val >>> s) & 0xff);
220 if (b == 0) {
221 break;
222 }
223 final char c = WHITE_CHARS[b >>> 6 & 0x3];
224 final int len = b & 0x3f;
225 for (int j = 0; j < len; j++) {
226 writer.write(c);
227 }
228 }
229 }
230
231 /**
232 * Write the value to a Writer with escaping of special characters
233 *
234 * @param specialChars identifies which characters are considered special
235 * @param writer the writer to write to
236 * @throws java.io.IOException if an error occurs downstream
237 */
238
239 public void writeEscape(boolean[] specialChars, Writer writer) throws java.io.IOException {
240 final long val = value;
241 for (int s = 56; s >= 0; s -= 8) {
242 final byte b = (byte) ((val >>> s) & 0xff);
243 if (b == 0) {
244 break;
245 }
246 final char c = WHITE_CHARS[b >>> 6 & 0x3];
247 final int len = b & 0x3f;
248 if (specialChars[c]) {
249 String e = "";
250 if (c == '\n') {
251 e = "&#xA;";
252 } else if (c == '\r') {
253 e = "&#xD;";
254 } else if (c == '\t') {
255 e = "&#x9;";
256 }
257 for (int j = 0; j < len; j++) {
258 writer.write(e);
259 }
260 } else {
261 for (int j = 0; j < len; j++) {
262 writer.write(c);
263 }
264 }
265 }
266 }
267 }
268