9 package javolution.text;
11 import java.io.Serializable;
29 public class TextBuilder implements Appendable, CharSequence, Serializable {
34 private static final int B0 = 5;
35 private static final int C0 = 1 <<
B0;
36 private static final int B1 = 10;
37 private static final int C1 = 1 <<
B1;
38 private static final int M1 =
C1 - 1;
42 private char[][]
_high =
new char[1][];
105 throw new IndexOutOfBoundsException();
106 return index < C1 ? _low[index] : _high[index >>
B1][index &
M1];
121 public final void getChars(
int srcBegin,
int srcEnd,
char[] dst,
123 if ((srcBegin < 0) || (srcBegin > srcEnd) || (srcEnd > this._length))
124 throw new IndexOutOfBoundsException();
125 for (
int i = srcBegin, j = dstBegin; i < srcEnd;) {
126 char[] chars0 =
_high[i >>
B1];
129 System.arraycopy(chars0, i0, dst, j,
length);
144 if ((index < 0) || (index >=
_length))
145 throw new IndexOutOfBoundsException();
169 public final void setLength(
int newLength,
char fillChar) {
171 throw new IndexOutOfBoundsException();
175 for (
int i =
_length; i++ < newLength;) {
190 public final java.lang.CharSequence
subSequence(
int start,
int end) {
191 if ((start < 0) || (end < 0) || (start > end) || (end >
_length))
192 throw new IndexOutOfBoundsException();
216 if (obj ==
null)
return append(
"null");
218 if (textFormat ==
null)
return append(obj.toString());
219 return textFormat.
format(obj,
this);
231 return (csq ==
null) ?
append(
"null") :
append(csq, 0, csq.length());
250 if ((start < 0) || (end < 0) || (start > end) || (end > csq.length()))
251 throw new IndexOutOfBoundsException();
252 for (
int i = start; i < end;) {
267 return (str ==
null) ?
append(
"null") :
append(str, 0, str.length());
285 if ((start < 0) || (end < 0) || (start > end) || (end > str.length()))
286 throw new IndexOutOfBoundsException(
"start: " + start +
", end: "
287 + end +
", str.length(): " + str.length());
288 int newLength =
_length + end - start;
292 for (
int i = start, j =
_length; i < end;) {
294 int dstBegin = j &
M1;
296 str.getChars(i, (i += inc), chars, dstBegin);
330 if ((start < 0) || (end < 0) || (start > end) || (end > txt.
length()))
331 throw new IndexOutOfBoundsException();
332 int newLength =
_length + end - start;
336 for (
int i = start, j =
_length; i < end;) {
338 int dstBegin = j &
M1;
340 txt.
getChars(i, (i += inc), chars, dstBegin);
354 append(chars, 0, chars.length);
369 final int end = offset +
length;
370 if ((offset < 0) || (
length < 0) || (end > chars.length))
371 throw new IndexOutOfBoundsException();
376 for (
int i = offset, j =
_length; i < end;) {
377 char[] dstChars =
_high[j >>
B1];
378 int dstBegin = j &
M1;
380 System.arraycopy(chars, i, dstChars, dstBegin, inc);
411 if (i == Integer.MIN_VALUE)
412 return append(
"-2147483648");
420 for (
int index =
_length - 1;; index--) {
422 _high[index >>
B1][index &
M1] = (char) (
'0' + i - (j * 10));
440 if (radix < 2 || radix > 36)
441 throw new IllegalArgumentException(
"radix: " + radix);
444 if (i == Integer.MIN_VALUE) {
476 '6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
477 'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
478 'w',
'x',
'y',
'z' };
491 if (l == Long.MIN_VALUE)
492 return append(
"-9223372036854775808");
496 if (l <= Integer.MAX_VALUE)
499 int i = (int) (l % 1000000000);
501 append(
"000000000", 0, 9 - digits);
516 if (radix < 2 || radix > 36)
517 throw new IllegalArgumentException(
"radix: " + radix);
520 if (l == Long.MIN_VALUE) {
532 long l2 = l1 / radix;
535 long l3 = l2 / radix;
538 long l4 = l3 / radix;
595 throw new IllegalArgumentException(
"digits: " + digits);
598 if (d == Double.POSITIVE_INFINITY)
599 return append(
"Infinity");
600 if (d == Double.NEGATIVE_INFINITY)
601 return append(
"-Infinity");
608 for (
int j = 1; j < digits; j++) {
641 if (scientific || (e >= digits)) {
644 int k = (int) (m / pow10);
651 int exp = digits - e - 1;
668 for (
int i = 0; i < digits; i++) {
675 for (
int j =
length; j < digits; j++) {
679 while (l % 10 == 0) {
686 private static final long[]
POW10_LONG =
new long[] { 1L, 10L, 100L, 1000L,
687 10000L, 100000L, 1000000L, 10000000L, 100000000L, 1000000000L,
688 10000000000L, 100000000000L, 1000000000000L, 10000000000000L,
689 100000000000000L, 1000000000000000L, 10000000000000000L,
690 100000000000000000L, 1000000000000000000L };
702 if ((index < 0) || (index >
_length))
703 throw new IndexOutOfBoundsException(
"index: " + index);
704 final int shift = csq.length();
705 int newLength =
_length + shift;
710 for (
int i =
_length - shift; --i >= index;) {
713 for (
int i = csq.length(); --i >= 0;) {
714 this.
setCharAt(index + i, csq.charAt(i));
740 if ((start < 0) || (end < 0) || (start > end) || (end > this.
length()))
741 throw new IndexOutOfBoundsException();
742 for (
int i = end, j = start; i <
_length;) {
756 for (
int j = (n - 1) >> 1; j >= 0;) {
785 char[] data =
new char[
_length];
787 return new String(data, 0,
_length);
817 for (
int i = 0; i <
_length;) {
833 public final boolean equals(Object obj) {
839 if (this._length != that.
_length)
841 for (
int i = 0; i <
_length;) {
859 for (
int i = 0; i <
_length;) {
861 if (csq.charAt(i++) != c)
879 if (j >=
_high.length) {
880 char[][] tmp =
new char[
_high.length * 2][];
881 System.arraycopy(
_high, 0, tmp, 0,
_high.length);