| 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.util.Arrays; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public final class LargeStringBuffer implements AppendableCharSequence { |
| 27 |
|
| 28 |
private final static int BITS = 16; |
| 29 |
private final static int SEGLEN = 1 << BITS; |
| 30 |
private final static int MASK = SEGLEN - 1; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private char[][] data; |
| 35 |
private int length; |
| 36 |
private int segmentsUsed; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public LargeStringBuffer() { |
| 43 |
data = new char[1][]; |
| 44 |
segmentsUsed = 0; |
| 45 |
length = 0; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private void addSegment(char[] seg) { |
| 56 |
int segs = data.length; |
| 57 |
if (segmentsUsed + 1 > segs) { |
| 58 |
if (segmentsUsed == 32768) { |
| 59 |
throw new IllegalStateException("Source document too large: more than 1G characters in text nodes"); |
| 60 |
} |
| 61 |
data = Arrays.copyOf(data, segs * 2); |
| 62 |
} |
| 63 |
data[segmentsUsed++] = seg; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void append(CharSequence s) { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if (s instanceof CompressedWhitespace) { |
| 79 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 80 |
((CompressedWhitespace)s).uncompress(fsb); |
| 81 |
append(fsb); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
final int len = s.length(); |
| 86 |
char[] firstSeg; |
| 87 |
int firstSegOffset = length & MASK; |
| 88 |
if (firstSegOffset == 0) { |
| 89 |
firstSeg = new char[SEGLEN]; |
| 90 |
addSegment(firstSeg); |
| 91 |
} else { |
| 92 |
firstSeg = data[length>>BITS]; |
| 93 |
} |
| 94 |
int firstSegLen; |
| 95 |
int fullSegments; |
| 96 |
int lastSegLen; |
| 97 |
if (len <= SEGLEN - firstSegOffset) { |
| 98 |
|
| 99 |
firstSegLen = len; |
| 100 |
fullSegments = 0; |
| 101 |
lastSegLen = 0; |
| 102 |
} else { |
| 103 |
firstSegLen = SEGLEN - firstSegOffset; |
| 104 |
fullSegments = (len - firstSegLen) >> BITS; |
| 105 |
lastSegLen = (len - firstSegLen) & MASK; |
| 106 |
} |
| 107 |
|
| 108 |
if (s instanceof CharSlice) { |
| 109 |
((CharSlice)s).getChars(0, firstSegLen, firstSeg, firstSegOffset); |
| 110 |
int start = firstSegLen; |
| 111 |
for (int i=0; i<fullSegments; i++) { |
| 112 |
char[] seg = new char[SEGLEN]; |
| 113 |
addSegment(seg); |
| 114 |
((CharSlice)s).getChars(start, start+SEGLEN, seg, 0); |
| 115 |
start += SEGLEN; |
| 116 |
} |
| 117 |
if (lastSegLen > 0) { |
| 118 |
char[] seg = new char[SEGLEN]; |
| 119 |
addSegment(seg); |
| 120 |
((CharSlice)s).getChars(start, len, seg, 0); |
| 121 |
} |
| 122 |
length += len; |
| 123 |
|
| 124 |
} else if (s instanceof FastStringBuffer) { |
| 125 |
((FastStringBuffer)s).getChars(0, firstSegLen, firstSeg, firstSegOffset); |
| 126 |
int start = firstSegLen; |
| 127 |
for (int i=0; i<fullSegments; i++) { |
| 128 |
char[] seg = new char[SEGLEN]; |
| 129 |
addSegment(seg); |
| 130 |
((FastStringBuffer)s).getChars(start, start+SEGLEN, seg, 0); |
| 131 |
start += SEGLEN; |
| 132 |
} |
| 133 |
if (lastSegLen > 0) { |
| 134 |
char[] seg = new char[SEGLEN]; |
| 135 |
addSegment(seg); |
| 136 |
((FastStringBuffer)s).getChars(start, len, seg, 0); |
| 137 |
} |
| 138 |
length += len; |
| 139 |
|
| 140 |
} else { |
| 141 |
if (!(s instanceof String)) { |
| 142 |
s = s.toString(); |
| 143 |
} |
| 144 |
|
| 145 |
((String)s).getChars(0, firstSegLen, firstSeg, firstSegOffset); |
| 146 |
int start = firstSegLen; |
| 147 |
for (int i=0; i<fullSegments; i++) { |
| 148 |
char[] seg = new char[SEGLEN]; |
| 149 |
addSegment(seg); |
| 150 |
((String)s).getChars(start, start+SEGLEN, seg, 0); |
| 151 |
start += SEGLEN; |
| 152 |
} |
| 153 |
if (lastSegLen > 0) { |
| 154 |
char[] seg = new char[SEGLEN]; |
| 155 |
addSegment(seg); |
| 156 |
((String)s).getChars(start, len, seg, 0); |
| 157 |
} |
| 158 |
length += len; |
| 159 |
|
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public int length() { |
| 170 |
return length; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void setLength(int length) { |
| 182 |
if (length < this.length) { |
| 183 |
int usedInLastSegment = length & MASK; |
| 184 |
this.length = length; |
| 185 |
this.segmentsUsed = length / SEGLEN + (usedInLastSegment == 0 ? 0 : 1); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public char charAt(int index) { |
| 201 |
if (index < 0 || index >= length) { |
| 202 |
throw new IndexOutOfBoundsException(index + ""); |
| 203 |
} |
| 204 |
return data[index >> BITS][index & MASK]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public CharSequence subSequence(int start, int end) { |
| 223 |
int firstSeg = start >> BITS; |
| 224 |
int lastSeg = (end - 1) >> BITS; |
| 225 |
if (firstSeg == lastSeg) { |
| 226 |
try { |
| 227 |
return new CharSlice(data[firstSeg], start & MASK, end - start); |
| 228 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 229 |
e.printStackTrace(); |
| 230 |
throw e; |
| 231 |
} |
| 232 |
} else { |
| 233 |
FastStringBuffer fsb = new FastStringBuffer(end - start); |
| 234 |
int firstSegLen = SEGLEN - (start & MASK); |
| 235 |
fsb.append(data[firstSeg], start & MASK, firstSegLen); |
| 236 |
int doneTo = start + firstSegLen; |
| 237 |
while (true) { |
| 238 |
firstSeg++; |
| 239 |
if (doneTo + SEGLEN < end) { |
| 240 |
fsb.append(data[firstSeg]); |
| 241 |
doneTo += SEGLEN; |
| 242 |
} else { |
| 243 |
fsb.append(data[firstSeg], 0, end - doneTo); |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
return fsb; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public String toString() { |
| 256 |
return subSequence(0, length).toString(); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public boolean equals(Object other) { |
| 264 |
return other instanceof CharSequence && toString().equals(other.toString()); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public int hashCode() { |
| 272 |
|
| 273 |
int h = 0; |
| 274 |
for (char[] chars : data) { |
| 275 |
for (int i = 0; i < SEGLEN; i++) { |
| 276 |
h = 31 * h + chars[i]; |
| 277 |
} |
| 278 |
} |
| 279 |
return h; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public String substring(int start, int end) { |
| 292 |
return subSequence(start, end).toString(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void write(java.io.Writer writer) throws java.io.IOException { |
| 303 |
writer.write(toString()); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
} |
| 333 |
|