1 7 8 package java.lang; 9 10 import sun.misc.FloatingDecimal; 11 12 23 abstract class AbstractStringBuilder implements Appendable , CharSequence { 24 27 char value[]; 28 29 32 int count; 33 34 37 AbstractStringBuilder() { 38 } 39 40 43 AbstractStringBuilder(int capacity) { 44 value = new char[capacity]; 45 } 46 47 53 public int length() { 54 return count; 55 } 56 57 64 public int capacity() { 65 return value.length; 66 } 67 68 82 public void ensureCapacity(int minimumCapacity) { 83 if (minimumCapacity > value.length) { 84 expandCapacity(minimumCapacity); 85 } 86 } 87 88 92 void expandCapacity(int minimumCapacity) { 93 int newCapacity = (value.length + 1) * 2; 94 if (newCapacity < 0) { 95 newCapacity = Integer.MAX_VALUE; 96 } else if (minimumCapacity > newCapacity) { 97 newCapacity = minimumCapacity; 98 } 99 char newValue[] = new char[newCapacity]; 100 System.arraycopy(value, 0, newValue, 0, count); 101 value = newValue; 102 } 103 104 111 public void trimToSize() { 112 if (count < value.length) { 113 char[] newValue = new char[count]; 114 System.arraycopy(value, 0, newValue, 0, count); 115 this.value = newValue; 116 } 117 } 118 119 144 public void setLength(int newLength) { 145 if (newLength < 0) 146 throw new StringIndexOutOfBoundsException (newLength); 147 if (newLength > value.length) 148 expandCapacity(newLength); 149 150 if (count < newLength) { 151 for (; count < newLength; count++) 152 value[count] = '\0'; 153 } else { 154 count = newLength; 155 } 156 } 157 158 175 public char charAt(int index) { 176 if ((index < 0) || (index >= count)) 177 throw new StringIndexOutOfBoundsException (index); 178 return value[index]; 179 } 180 181 202 public int codePointAt(int index) { 203 if ((index < 0) || (index >= count)) { 204 throw new StringIndexOutOfBoundsException (index); 205 } 206 return Character.codePointAt(value, index); 207 } 208 209 230 public int codePointBefore(int index) { 231 int i = index - 1; 232 if ((i < 0) || (i >= count)) { 233 throw new StringIndexOutOfBoundsException (index); 234 } 235 return Character.codePointBefore(value, index); 236 } 237 238 258 public int codePointCount(int beginIndex, int endIndex) { 259 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { 260 throw new IndexOutOfBoundsException (); 261 } 262 return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex); 263 } 264 265 284 public int offsetByCodePoints(int index, int codePointOffset) { 285 if (index < 0 || index > count) { 286 throw new IndexOutOfBoundsException (); 287 } 288 return Character.offsetByCodePointsImpl(value, 0, count, 289 index, codePointOffset); 290 } 291 292 322 public void getChars(int srcBegin, int srcEnd, char dst[], 323 int dstBegin) 324 { 325 if (srcBegin < 0) 326 throw new StringIndexOutOfBoundsException (srcBegin); 327 if ((srcEnd < 0) || (srcEnd > count)) 328 throw new StringIndexOutOfBoundsException (srcEnd); 329 if (srcBegin > srcEnd) 330 throw new StringIndexOutOfBoundsException ("srcBegin > srcEnd"); 331 System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); 332 } 333 334 348 public void setCharAt(int index, char ch) { 349 if ((index < 0) || (index >= count)) 350 throw new StringIndexOutOfBoundsException (index); 351 value[index] = ch; 352 } 353 354 365 public AbstractStringBuilder append(Object obj) { 366 return append(String.valueOf(obj)); 367 } 368 369 387 public AbstractStringBuilder append(String str) { 388 if (str == null) str = "null"; 389 int len = str.length(); 390 if (len == 0) return this; 391 int newCount = count + len; 392 if (newCount > value.length) 393 expandCapacity(newCount); 394 str.getChars(0, len, value, count); 395 count = newCount; 396 return this; 397 } 398 399 public AbstractStringBuilder append(StringBuffer sb) { 401 if (sb == null) 402 return append("null"); 403 int len = sb.length(); 404 int newCount = count + len; 405 if (newCount > value.length) 406 expandCapacity(newCount); 407 sb.getChars(0, len, value, count); 408 count = newCount; 409 return this; 410 } 411 412 public AbstractStringBuilder append(CharSequence s) { 414 if (s == null) 415 s = "null"; 416 if (s instanceof String ) 417 return this.append((String )s); 418 if (s instanceof StringBuffer ) 419 return this.append((StringBuffer )s); 420 return this.append(s, 0, s.length()); 421 } 422 423 452 public AbstractStringBuilder append(CharSequence s, int start, int end) { 453 if (s == null) 454 s = "null"; 455 if ((start < 0) || (end < 0) || (start > end) || (end > s.length())) 456 throw new IndexOutOfBoundsException ( 457 "start " + start + ", end " + end + ", s.length() " 458 + s.length()); 459 int len = end - start; 460 if (len == 0) 461 return this; 462 int newCount = count + len; 463 if (newCount > value.length) 464 expandCapacity(newCount); 465 for (int i=start; i<end; i++) 466 value[count++] = s.charAt(i); 467 count = newCount; 468 return this; 469 } 470 471 487 public AbstractStringBuilder append(char str[]) { 488 int newCount = count + str.length; 489 if (newCount > value.length) 490 expandCapacity(newCount); 491 System.arraycopy(str, 0, value, count, str.length); 492 count = newCount; 493 return this; 494 } 495 496 515 public AbstractStringBuilder append(char str[], int offset, int len) { 516 int newCount = count + len; 517 if (newCount > value.length) 518 expandCapacity(newCount); 519 System.arraycopy(str, offset, value, count, len); 520 count = newCount; 521 return this; 522 } 523 524 535 public AbstractStringBuilder append(boolean b) { 536 if (b) { 537 int newCount = count + 4; 538 if (newCount > value.length) 539 expandCapacity(newCount); 540 value[count++] = 't'; 541 value[count++] = 'r'; 542 value[count++] = 'u'; 543 value[count++] = 'e'; 544 } else { 545 int newCount = count + 5; 546 if (newCount > value.length) 547 expandCapacity(newCount); 548 value[count++] = 'f'; 549 value[count++] = 'a'; 550 value[count++] = 'l'; 551 value[count++] = 's'; 552 value[count++] = 'e'; 553 } 554 return this; 555 } 556 557 572 public AbstractStringBuilder append(char c) { 573 int newCount = count + 1; 574 if (newCount > value.length) 575 expandCapacity(newCount); 576 value[count++] = c; 577 return this; 578 } 579 580 591 public AbstractStringBuilder append(int i) { 592 if (i == Integer.MIN_VALUE) { 593 append("-2147483648"); 594 return this; 595 } 596 int appendedLength = (i < 0) ? stringSizeOfInt(-i) + 1 597 : stringSizeOfInt(i); 598 int spaceNeeded = count + appendedLength; 599 if (spaceNeeded > value.length) 600 expandCapacity(spaceNeeded); 601 Integer.getChars(i, spaceNeeded, value); 602 count = spaceNeeded; 603 return this; 604 } 605 606 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 607 99999999, 999999999, Integer.MAX_VALUE }; 608 609 static int stringSizeOfInt(int x) { 611 for (int i=0; ; i++) 612 if (x <= sizeTable[i]) 613 return i+1; 614 } 615 616 627 public AbstractStringBuilder append(long l) { 628 if (l == Long.MIN_VALUE) { 629 append("-9223372036854775808"); 630 return this; 631 } 632 int appendedLength = (l < 0) ? stringSizeOfLong(-l) + 1 633 : stringSizeOfLong(l); 634 int spaceNeeded = count + appendedLength; 635 if (spaceNeeded > value.length) 636 expandCapacity(spaceNeeded); 637 Long.getChars(l, spaceNeeded, value); 638 count = spaceNeeded; 639 return this; 640 } 641 642 static int stringSizeOfLong(long x) { 644 long p = 10; 645 for (int i=1; i<19; i++) { 646 if (x < p) 647 return i; 648 p = 10*p; 649 } 650 return 19; 651 } 652 653 664 public AbstractStringBuilder append(float f) { 665 new FloatingDecimal(f).appendTo(this); 666 return this; 667 } 668 669 680 public AbstractStringBuilder append(double d) { 681 new FloatingDecimal(d).appendTo(this); 682 return this; 683 } 684 685 699 public AbstractStringBuilder delete(int start, int end) { 700 if (start < 0) 701 throw new StringIndexOutOfBoundsException (start); 702 if (end > count) 703 end = count; 704 if (start > end) 705 throw new StringIndexOutOfBoundsException (); 706 int len = end - start; 707 if (len > 0) { 708 System.arraycopy(value, start+len, value, start, count-end); 709 count -= len; 710 } 711 return this; 712 } 713 714 733 public AbstractStringBuilder appendCodePoint(int codePoint) { 734 if (!Character.isValidCodePoint(codePoint)) { 735 throw new IllegalArgumentException (); 736 } 737 int n = 1; 738 if (codePoint >= Character.MIN_SUPPLEMENTARY_CODE_POINT) { 739 n++; 740 } 741 int newCount = count + n; 742 if (newCount > value.length) { 743 expandCapacity(newCount); 744 } 745 if (n == 1) { 746 value[count++] = (char) codePoint; 747 } else { 748 Character.toSurrogates(codePoint, value, count); 749 count += n; 750 } 751 return this; 752 } 753 754 771 public AbstractStringBuilder deleteCharAt(int index) { 772 if ((index < 0) || (index >= count)) 773 throw new StringIndexOutOfBoundsException (index); 774 System.arraycopy(value, index+1, value, index, count-index-1); 775 count--; 776 return this; 777 } 778 779 798 public AbstractStringBuilder replace(int start, int end, String str) { 799 if (start < 0) 800 throw new StringIndexOutOfBoundsException (start); 801 if (start > count) 802 throw new StringIndexOutOfBoundsException ("start > length()"); 803 if (start > end) 804 throw new StringIndexOutOfBoundsException ("start > end"); 805 if (end > count) 806 end = count; 807 808 if (end > count) 809 end = count; 810 int len = str.length(); 811 int newCount = count + len - (end - start); 812 if (newCount > value.length) 813 expandCapacity(newCount); 814 815 System.arraycopy(value, end, value, start + len, count - end); 816 str.getChars(value, start); 817 count = newCount; 818 return this; 819 } 820 821 832 public String substring(int start) { 833 return substring(start, count); 834 } 835 836 862 public CharSequence subSequence(int start, int end) { 863 return substring(start, end); 864 } 865 866 880 public String substring(int start, int end) { 881 if (start < 0) 882 throw new StringIndexOutOfBoundsException (start); 883 if (end > count) 884 throw new StringIndexOutOfBoundsException (end); 885 if (start > end) 886 throw new StringIndexOutOfBoundsException (end - start); 887 return new String (value, start, end - start); 888 } 889 890 911 public AbstractStringBuilder insert(int index, char str[], int offset, 912 int len) 913 { 914 if ((index < 0) || (index > length())) 915 throw new StringIndexOutOfBoundsException (index); 916 if ((offset < 0) || (len < 0) || (offset > str.length - len)) 917 throw new StringIndexOutOfBoundsException ( 918 "offset " + offset + ", len " + len + ", str.length " 919 + str.length); 920 int newCount = count + len; 921 if (newCount > value.length) 922 expandCapacity(newCount); 923 System.arraycopy(value, index, value, index + len, count - index); 924 System.arraycopy(str, offset, value, index, len); 925 count = newCount; 926 return this; 927 } 928 929 947 public AbstractStringBuilder insert(int offset, Object obj) { 948 return insert(offset, String.valueOf(obj)); 949 } 950 951 982 public AbstractStringBuilder insert(int offset, String str) { 983 if ((offset < 0) || (offset > length())) 984 throw new StringIndexOutOfBoundsException (offset); 985 if (str == null) 986 str = "null"; 987 int len = str.length(); 988 int newCount = count + len; 989 if (newCount > value.length) 990 expandCapacity(newCount); 991 System.arraycopy(value, offset, value, offset + len, count - offset); 992 str.getChars(value, offset); 993 count = newCount; 994 return this; 995 } 996 997 1018 public AbstractStringBuilder insert(int offset, char str[]) { 1019 if ((offset < 0) || (offset > length())) 1020 throw new StringIndexOutOfBoundsException (offset); 1021 int len = str.length; 1022 int newCount = count + len; 1023 if (newCount > value.length) 1024 expandCapacity(newCount); 1025 System.arraycopy(value, offset, value, offset + len, count - offset); 1026 System.arraycopy(str, 0, value, offset, len); 1027 count = newCount; 1028 return this; 1029 } 1030 1031 1050 public AbstractStringBuilder insert(int dstOffset, CharSequence s) { 1051 if (s == null) 1052 s = "null"; 1053 if (s instanceof String ) 1054 return this.insert(dstOffset, (String )s); 1055 return this.insert(dstOffset, s, 0, s.length()); 1056 } 1057 1058 1102 public AbstractStringBuilder insert(int dstOffset, CharSequence s, 1103 int start, int end) { 1104 if (s == null) 1105 s = "null"; 1106 if ((dstOffset < 0) || (dstOffset > this.length())) 1107 throw new IndexOutOfBoundsException ("dstOffset "+dstOffset); 1108 if ((start < 0) || (end < 0) || (start > end) || (end > s.length())) 1109 throw new IndexOutOfBoundsException ( 1110 "start " + start + ", end " + end + ", s.length() " 1111 + s.length()); 1112 int len = end - start; 1113 if (len == 0) 1114 return this; 1115 int newCount = count + len; 1116 if (newCount > value.length) 1117 expandCapacity(newCount); 1118 System.arraycopy(value, dstOffset, value, dstOffset + len, 1119 count - dstOffset); 1120 for (int i=start; i<end; i++) 1121 value[dstOffset++] = s.charAt(i); 1122 count = newCount; 1123 return this; 1124 } 1125 1126 1144 public AbstractStringBuilder insert(int offset, boolean b) { 1145 return insert(offset, String.valueOf(b)); 1146 } 1147 1148 1171 public AbstractStringBuilder insert(int offset, char c) { 1172 int newCount = count + 1; 1173 if (newCount > value.length) 1174 expandCapacity(newCount); 1175 System.arraycopy(value, offset, value, offset + 1, count - offset); 1176 value[offset] = c; 1177 count = newCount; 1178 return this; 1179 } 1180 1181 1199 public AbstractStringBuilder insert(int offset, int i) { 1200 return insert(offset, String.valueOf(i)); 1201 } 1202 1203 1221 public AbstractStringBuilder insert(int offset, long l) { 1222 return insert(offset, String.valueOf(l)); 1223 } 1224 1225 1243 public AbstractStringBuilder insert(int offset, float f) { 1244 return insert(offset, String.valueOf(f)); 1245 } 1246 1247 1265 public AbstractStringBuilder insert(int offset, double d) { 1266 return insert(offset, String.valueOf(d)); 1267 } 1268 1269 1286 public int indexOf(String str) { 1287 return indexOf(str, 0); 1288 } 1289 1290 1307 public int indexOf(String str, int fromIndex) { 1308 return String.indexOf(value, 0, count, 1309 str.toCharArray(), 0, str.length(), fromIndex); 1310 } 1311 1312 1330 public int lastIndexOf(String str) { 1331 return lastIndexOf(str, count); 1332 } 1333 1334 1351 public int lastIndexOf(String str, int fromIndex) { 1352 return String.lastIndexOf(value, 0, count, 1353 str.toCharArray(), 0, str.length(), fromIndex); 1354 } 1355 1356 1378 public AbstractStringBuilder reverse() { 1379 boolean hasSurrogate = false; 1380 int n = count - 1; 1381 for (int j = (n-1) >> 1; j >= 0; --j) { 1382 char temp = value[j]; 1383 char temp2 = value[n - j]; 1384 if (!hasSurrogate) { 1385 hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE) 1386 || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE); 1387 } 1388 value[j] = temp2; 1389 value[n - j] = temp; 1390 } 1391 if (hasSurrogate) { 1392 for (int i = 0; i < count - 1; i++) { 1394 char c2 = value[i]; 1395 if (Character.isLowSurrogate(c2)) { 1396 char c1 = value[i + 1]; 1397 if (Character.isHighSurrogate(c1)) { 1398 value[i++] = c1; 1399 value[i] = c2; 1400 } 1401 } 1402 } 1403 } 1404 return this; 1405 } 1406 1407 1417 public abstract String toString(); 1418 1419 1422 final char[] getValue() { 1423 return value; 1424 } 1425 1426} 1427 | Popular Tags |