| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class CompressedWhitespace implements CharSequence { |
| 23 |
|
| 24 |
private static char[] WHITE_CHARS = {0x09, 0x0A, 0x0D, 0x20}; |
| 25 |
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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static CharSequence compress( 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public FastStringBuffer uncompress( 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, 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public CharSequence subSequence(int start, int end) { |
| 182 |
return uncompress(null).subSequence(start, end); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public boolean equals( Object obj) { |
| 189 |
if (obj instanceof CompressedWhitespace) { |
| 190 |
return value == ((CompressedWhitespace) obj).value; |
| 191 |
} |
| 192 |
return uncompress(null).equals(obj); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public int hashCode() { |
| 199 |
return uncompress(null).hashCode(); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public String toString() { |
| 206 |
return uncompress(null).toString(); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public void write( 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 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 = "
"; |
| 252 |
} else if (c == '\r') { |
| 253 |
e = "
"; |
| 254 |
} else if (c == '\t') { |
| 255 |
e = "	"; |
| 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 |
|