|
@@ -47,8 +47,8 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
this(CAPACITY);
|
|
this(CAPACITY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public StrBuilder(int initialCapacity) {
|
|
|
|
|
- buffer = new char[initialCapacity];
|
|
|
|
|
|
|
+ public StrBuilder(int capacity) {
|
|
|
|
|
+ buffer = new char[capacity];
|
|
|
options.push(new BuilderState(OPTIONS));
|
|
options.push(new BuilderState(OPTIONS));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -61,30 +61,24 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return size;
|
|
return size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
public int capacity() {
|
|
public int capacity() {
|
|
|
return buffer.length;
|
|
return buffer.length;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public StrBuilder resize(int length, char c) {
|
|
public StrBuilder resize(int length, char c) {
|
|
|
if (length < 0) {
|
|
if (length < 0) {
|
|
|
- throw new StringIndexOutOfBoundsException(length);
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("negative size: " + length);
|
|
|
}
|
|
}
|
|
|
if (length > size) {
|
|
if (length > size) {
|
|
|
reserve(length);
|
|
reserve(length);
|
|
|
ArrayUtils.fill(buffer, size, length, c);
|
|
ArrayUtils.fill(buffer, size, length, c);
|
|
|
- size = length;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ size = length;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public StrBuilder reserve(int capacity) {
|
|
public StrBuilder reserve(int capacity) {
|
|
|
- if (capacity > buffer.length) {
|
|
|
|
|
- char[] old = buffer;
|
|
|
|
|
- buffer = new char[capacity * 2];
|
|
|
|
|
- System.arraycopy(old, 0, buffer, 0, size);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ buffer = ArrayAllocator.grow(buffer, capacity, size);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -99,7 +93,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
|
|
|
|
|
public StrBuilder trim(int n) {
|
|
public StrBuilder trim(int n) {
|
|
|
if (buffer.length > n) {
|
|
if (buffer.length > n) {
|
|
|
- buffer = ArrayAllocator.trim(buffer, size);
|
|
|
|
|
|
|
+ buffer = ArrayAllocator.trim(buffer, Math.max(n,size));
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -114,10 +108,12 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public char charAt(int index) {
|
|
public char charAt(int index) {
|
|
|
|
|
+ StringUtils.checkIndex(size, index);
|
|
|
return buffer[index];
|
|
return buffer[index];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public StrBuilder setCharAt(int index, char value) {
|
|
public StrBuilder setCharAt(int index, char value) {
|
|
|
|
|
+ StringUtils.checkIndex(size, index);
|
|
|
buffer[index] = value;
|
|
buffer[index] = value;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -133,6 +129,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public char[] getChars(int begin, int end) {
|
|
public char[] getChars(int begin, int end) {
|
|
|
|
|
+ StringUtils.checkRange(size, begin, end);
|
|
|
if (end == begin) {
|
|
if (end == begin) {
|
|
|
return EMPTY; //NOPMD
|
|
return EMPTY; //NOPMD
|
|
|
}
|
|
}
|
|
@@ -144,7 +141,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public char[] getChars(int begin, int end, char target[], int offset) {
|
|
public char[] getChars(int begin, int end, char target[], int offset) {
|
|
|
- NativeArrayAllocator.ensureFromTo(size, begin, end);
|
|
|
|
|
|
|
+ StringUtils.checkRange(size, begin, end);
|
|
|
System.arraycopy(buffer, begin, target, offset, end - begin);
|
|
System.arraycopy(buffer, begin, target, offset, end - begin);
|
|
|
return target;
|
|
return target;
|
|
|
}
|
|
}
|
|
@@ -237,7 +234,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
if (n > 0) {
|
|
if (n > 0) {
|
|
|
reserve(size + n);
|
|
reserve(size + n);
|
|
|
value.getChars(begin, end, buffer, size);
|
|
value.getChars(begin, end, buffer, size);
|
|
|
- size += end;
|
|
|
|
|
|
|
+ size += n;
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -255,7 +252,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
if (n > 0) {
|
|
if (n > 0) {
|
|
|
reserve(size + n);
|
|
reserve(size + n);
|
|
|
value.getChars(begin, end, buffer, size);
|
|
value.getChars(begin, end, buffer, size);
|
|
|
- size += end;
|
|
|
|
|
|
|
+ size += n;
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -273,7 +270,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
if (n > 0) {
|
|
if (n > 0) {
|
|
|
reserve(size + n);
|
|
reserve(size + n);
|
|
|
value.getChars(begin, end, buffer, size);
|
|
value.getChars(begin, end, buffer, size);
|
|
|
- size += end;
|
|
|
|
|
|
|
+ size += n;
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -291,7 +288,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
if (n > 0) {
|
|
if (n > 0) {
|
|
|
reserve(size + end);
|
|
reserve(size + end);
|
|
|
System.arraycopy(value, begin, buffer, size, n);
|
|
System.arraycopy(value, begin, buffer, size, n);
|
|
|
- size += end;
|
|
|
|
|
|
|
+ size += n;
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
@@ -371,17 +368,16 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public <T> StrBuilder append(NativeArray values) {
|
|
public <T> StrBuilder append(NativeArray values) {
|
|
|
- return append(values, i -> String.valueOf(values.get(i)));
|
|
|
|
|
|
|
+ return append(values.size(), i -> String.valueOf(values.get(i)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public <T> StrBuilder append(NativeArray array, IntFunction<String> function) {
|
|
|
|
|
- int n = array.size();
|
|
|
|
|
- if ( 0==n ) {
|
|
|
|
|
|
|
+ public <T> StrBuilder append(int size, IntFunction<String> function) {
|
|
|
|
|
+ if ( 0==size ) {
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
String separator = options.peek().separator();
|
|
String separator = options.peek().separator();
|
|
|
- append(function.apply(0));
|
|
|
|
|
- for (int i=1; i<n; i++) {
|
|
|
|
|
|
|
+ append(function.apply(0));
|
|
|
|
|
+ for (int i=1; i<size; i++) {
|
|
|
append(separator).append(function.apply(i));
|
|
append(separator).append(function.apply(i));
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
@@ -392,10 +388,16 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
if (this == object) {
|
|
if (this == object) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
- if (!(object instanceof StrBuilder)) {
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ if (object instanceof StrBuilder) {
|
|
|
|
|
+ return equalsSB((StrBuilder)object);
|
|
|
}
|
|
}
|
|
|
- StrBuilder other = (StrBuilder)object;
|
|
|
|
|
|
|
+ if (object instanceof CharSequence) {
|
|
|
|
|
+ return equalsCS((CharSequence)object);
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean equalsSB(StrBuilder other) {
|
|
|
if (this.size != other.size) {
|
|
if (this.size != other.size) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
@@ -407,6 +409,18 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private boolean equalsCS(CharSequence other) {
|
|
|
|
|
+ if (this.size != other.length()) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ for(int i=0; i<size; i++) {
|
|
|
|
|
+ if(this.buffer[i] != other.charAt(i)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public int hashCode() {
|
|
public int hashCode() {
|
|
|
return NativeArray.wrap(buffer).hashCode(0, size);
|
|
return NativeArray.wrap(buffer).hashCode(0, size);
|
|
@@ -563,7 +577,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
BuilderState state = new BuilderState(OPTIONS);
|
|
BuilderState state = new BuilderState(OPTIONS);
|
|
|
|
|
|
|
|
state.blank(this.blank());
|
|
state.blank(this.blank());
|
|
|
- state.endl(this.blank());
|
|
|
|
|
|
|
+ state.endl(this.endl());
|
|
|
state.separator(this.separator());
|
|
state.separator(this.separator());
|
|
|
state.open(this.open());
|
|
state.open(this.open());
|
|
|
state.close(this.close());
|
|
state.close(this.close());
|