| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.util; |
| 9 |
|
| 10 |
import net.sf.saxon.regex.BMPString; |
| 11 |
import net.sf.saxon.regex.GeneralUnicodeString; |
| 12 |
import net.sf.saxon.regex.UnicodeString; |
| 13 |
import net.sf.saxon.serialize.charcode.UTF16CharacterSet; |
| 14 |
import net.sf.saxon.tree.tiny.AppendableCharSequence; |
| 15 |
import net.sf.saxon.tree.tiny.CharSlice; |
| 16 |
import net.sf.saxon.tree.tiny.CompressedWhitespace; |
| 17 |
|
| 18 |
import java.io.Writer; |
| 19 |
import java.util.Arrays; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class FastStringBuffer implements AppendableCharSequence { |
| 29 |
|
| 30 |
public static final int C16 = 16; |
| 31 |
public static final int C64 = 64; |
| 32 |
public static final int C256 = 256; |
| 33 |
public static final int C1024 = 1024; |
| 34 |
|
| 35 |
private char[] array; |
| 36 |
private int used = 0; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public FastStringBuffer(int initialSize) { |
| 45 |
|
| 46 |
array = new char[initialSize]; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public FastStringBuffer(CharSequence cs) { |
| 57 |
array = new char[cs.length()]; |
| 58 |
append(cs); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public void append(String s) { |
| 68 |
int len = s.length(); |
| 69 |
ensureCapacity(len); |
| 70 |
s.getChars(0, len, array, used); |
| 71 |
used += len; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void append(CharSlice s) { |
| 81 |
int len = s.length(); |
| 82 |
ensureCapacity(len); |
| 83 |
s.copyTo(array, used); |
| 84 |
used += len; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void append(FastStringBuffer s) { |
| 94 |
int len = s.length(); |
| 95 |
ensureCapacity(len); |
| 96 |
s.getChars(0, len, array, used); |
| 97 |
used += len; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void append(StringBuffer s) { |
| 107 |
int len = s.length(); |
| 108 |
ensureCapacity(len); |
| 109 |
s.getChars(0, len, array, used); |
| 110 |
used += len; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void append(CharSequence s) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
final int len = s.length(); |
| 124 |
ensureCapacity(len); |
| 125 |
if (s instanceof CharSlice) { |
| 126 |
((CharSlice) s).copyTo(array, used); |
| 127 |
} else if (s instanceof String) { |
| 128 |
((String) s).getChars(0, len, array, used); |
| 129 |
} else if (s instanceof FastStringBuffer) { |
| 130 |
((FastStringBuffer) s).getChars(0, len, array, used); |
| 131 |
} else if (s instanceof CompressedWhitespace) { |
| 132 |
((CompressedWhitespace) s).uncompress(this); |
| 133 |
return; |
| 134 |
} else if (s instanceof BMPString) { |
| 135 |
append(((BMPString) s).getCharSequence()); |
| 136 |
return; |
| 137 |
} else if (s instanceof GeneralUnicodeString) { |
| 138 |
for (int i = 0; i < ((GeneralUnicodeString)s).uLength(); i++) { |
| 139 |
appendWideChar(((GeneralUnicodeString)s).uCharAt(i)); |
| 140 |
} |
| 141 |
return; |
| 142 |
} else { |
| 143 |
s.toString().getChars(0, len, array, used); |
| 144 |
} |
| 145 |
used += len; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void append(char[] srcArray, int start, int length) { |
| 157 |
ensureCapacity(length); |
| 158 |
System.arraycopy(srcArray, start, array, used, length); |
| 159 |
used += length; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public void append(char[] srcArray) { |
| 169 |
final int length = srcArray.length; |
| 170 |
ensureCapacity(length); |
| 171 |
System.arraycopy(srcArray, 0, array, used, length); |
| 172 |
used += length; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void append(char ch) { |
| 182 |
ensureCapacity(1); |
| 183 |
array[used++] = ch; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void appendWideChar(int ch) { |
| 193 |
if (ch > 0xffff) { |
| 194 |
append(UTF16CharacterSet.highSurrogate(ch)); |
| 195 |
append(UTF16CharacterSet.lowSurrogate(ch)); |
| 196 |
} else { |
| 197 |
append((char) ch); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void append(UnicodeString str) { |
| 208 |
if (str instanceof BMPString) { |
| 209 |
append(((BMPString) str).getCharSequence()); |
| 210 |
} else { |
| 211 |
for (int i = 0; i < str.uLength(); i++) { |
| 212 |
appendWideChar(str.uCharAt(i)); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public void prependWideChar(int ch) { |
| 224 |
if (ch > 0xffff) { |
| 225 |
prepend(UTF16CharacterSet.lowSurrogate(ch)); |
| 226 |
prepend(UTF16CharacterSet.highSurrogate(ch)); |
| 227 |
} else { |
| 228 |
prepend((char) ch); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public int length() { |
| 239 |
return used; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public boolean isEmpty() { |
| 248 |
return used==0; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public char charAt(int index) { |
| 266 |
if (index >= used) { |
| 267 |
throw new IndexOutOfBoundsException("" + index); |
| 268 |
} |
| 269 |
return array[index]; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public CharSequence subSequence(int start, int end) { |
| 288 |
return new CharSlice(array, start, end - start); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { |
| 321 |
if (srcBegin < 0) { |
| 322 |
throw new StringIndexOutOfBoundsException(srcBegin); |
| 323 |
} |
| 324 |
if (srcEnd > used) { |
| 325 |
throw new StringIndexOutOfBoundsException(srcEnd); |
| 326 |
} |
| 327 |
if (srcBegin > srcEnd) { |
| 328 |
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin); |
| 329 |
} |
| 330 |
System.arraycopy(array, srcBegin, dst, dstBegin, srcEnd - srcBegin); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public int indexOf(char ch) { |
| 341 |
for (int i = 0; i < used; i++) { |
| 342 |
if (array[i] == ch) { |
| 343 |
return i; |
| 344 |
} |
| 345 |
} |
| 346 |
return -1; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public String toString() { |
| 354 |
condense(); |
| 355 |
return new String(array, 0, used); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public boolean equals(Object other) { |
| 363 |
return other instanceof CharSequence && toString().equals(other.toString()); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public int hashCode() { |
| 371 |
|
| 372 |
int h = 0; |
| 373 |
for (int i = 0; i < used; i++) { |
| 374 |
h = 31 * h + array[i]; |
| 375 |
} |
| 376 |
return h; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public char[] toCharArray() { |
| 388 |
if (used == array.length) { |
| 389 |
return array; |
| 390 |
} else { |
| 391 |
char[] chars = new char[used]; |
| 392 |
System.arraycopy(array, 0, chars, 0, used); |
| 393 |
return chars; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public void setCharAt(int index, char ch) { |
| 406 |
if (index < 0 || index > used) { |
| 407 |
throw new IndexOutOfBoundsException("" + index); |
| 408 |
} |
| 409 |
array[index] = ch; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public void insert(int index, char ch) { |
| 421 |
if (index < 0 || index > used) { |
| 422 |
throw new IndexOutOfBoundsException("" + index); |
| 423 |
} |
| 424 |
ensureCapacity(1); |
| 425 |
System.arraycopy(array, index, array, index + 1, used - index); |
| 426 |
used++; |
| 427 |
array[index] = ch; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public void insertWideChar(int index, int ch) { |
| 439 |
if (index < 0 || index > used) { |
| 440 |
throw new IndexOutOfBoundsException("" + index); |
| 441 |
} |
| 442 |
if (ch > 0xffff) { |
| 443 |
ensureCapacity(2); |
| 444 |
System.arraycopy(array, index, array, index + 2, used - index); |
| 445 |
used += 2; |
| 446 |
array[index] = UTF16CharacterSet.highSurrogate(ch); |
| 447 |
array[index + 1] = UTF16CharacterSet.lowSurrogate(ch); |
| 448 |
} else { |
| 449 |
ensureCapacity(1); |
| 450 |
System.arraycopy(array, index, array, index + 1, used - index); |
| 451 |
used++; |
| 452 |
array[index] = (char) ch; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public void removeCharAt(int index) { |
| 465 |
if (index < 0 || index > used) { |
| 466 |
throw new IndexOutOfBoundsException("" + index); |
| 467 |
} |
| 468 |
used--; |
| 469 |
System.arraycopy(array, index + 1, array, index, used - index); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public void prepend(char ch) { |
| 479 |
char[] a2 = new char[array.length + 1]; |
| 480 |
System.arraycopy(array, 0, a2, 1, used); |
| 481 |
a2[0] = ch; |
| 482 |
used += 1; |
| 483 |
array = a2; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public void prepend(CharSequence str) { |
| 493 |
int len = str.length(); |
| 494 |
char[] a2 = new char[array.length + len]; |
| 495 |
System.arraycopy(array, 0, a2, len, used); |
| 496 |
for (int i = 0; i < len; i++) { |
| 497 |
a2[i] = str.charAt(i); |
| 498 |
} |
| 499 |
used += len; |
| 500 |
array = a2; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public void prependRepeated(char ch, int repeat) { |
| 512 |
if (repeat > 0) { |
| 513 |
char[] a2 = new char[array.length + repeat]; |
| 514 |
System.arraycopy(array, 0, a2, repeat, used); |
| 515 |
Arrays.fill(a2, 0, repeat, ch); |
| 516 |
used += repeat; |
| 517 |
array = a2; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public void setLength(int length) { |
| 530 |
if (length < 0 || length > used) { |
| 531 |
return; |
| 532 |
} |
| 533 |
used = length; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public void ensureCapacity(int extra) { |
| 543 |
if (used + extra > array.length) { |
| 544 |
int newlen = array.length * 2; |
| 545 |
if (newlen < used + extra) { |
| 546 |
newlen = used + extra * 2; |
| 547 |
} |
| 548 |
array = Arrays.copyOf(array, newlen); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public FastStringBuffer condense() { |
| 562 |
if (array.length - used > 256 || (array.length > used * 2 && array.length - used > 20)) { |
| 563 |
array = Arrays.copyOf(array, used); |
| 564 |
} |
| 565 |
return this; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public void write(Writer writer) throws java.io.IOException { |
| 575 |
writer.write(array, 0, used); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public static String diagnosticPrint( CharSequence in) { |
| 587 |
FastStringBuffer buff = new FastStringBuffer(in.length() * 2); |
| 588 |
for (int i = 0; i < in.length(); i++) { |
| 589 |
char c = in.charAt(i); |
| 590 |
if (c > 32 && c < 127) { |
| 591 |
buff.append(c); |
| 592 |
} else { |
| 593 |
buff.append("\\u"); |
| 594 |
for (int d = 12; d >= 0; d -= 4) { |
| 595 |
buff.append("0123456789ABCDEF".charAt((c >> d) & 0xf)); |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
return buff.toString(); |
| 600 |
} |
| 601 |
|
| 602 |
} |
| 603 |
|