Use the name "buffer", not "buf".

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@227240 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2005-08-03 18:02:35 +00:00
parent dcfba435bb
commit e77e2124c4
2 changed files with 94 additions and 94 deletions

View File

@ -65,7 +65,7 @@ public class StrBuilder implements Cloneable {
private static final long serialVersionUID = 7628716375283629643L; private static final long serialVersionUID = 7628716375283629643L;
/** Internal data storage. */ /** Internal data storage. */
protected char[] buf; protected char[] buffer;
/** Current size of the buffer. */ /** Current size of the buffer. */
protected int size; protected int size;
/** The null text. */ /** The null text. */
@ -89,7 +89,7 @@ public StrBuilder(int initialCapacity) {
if (initialCapacity <= 0) { if (initialCapacity <= 0) {
initialCapacity = CAPACITY; initialCapacity = CAPACITY;
} }
buf = new char[initialCapacity]; buffer = new char[initialCapacity];
} }
/** /**
@ -101,9 +101,9 @@ public StrBuilder(int initialCapacity) {
public StrBuilder(String str) { public StrBuilder(String str) {
super(); super();
if (str == null) { if (str == null) {
buf = new char[CAPACITY]; buffer = new char[CAPACITY];
} else { } else {
buf = new char[str.length() + CAPACITY]; buffer = new char[str.length() + CAPACITY];
append(str); append(str);
} }
} }
@ -161,7 +161,7 @@ public void setLength(int length) {
int newEnd = length; int newEnd = length;
size = length; size = length;
for (int i = oldEnd; i < newEnd; i++) { for (int i = oldEnd; i < newEnd; i++) {
buf[i] = '\0'; buffer[i] = '\0';
} }
} }
} }
@ -173,7 +173,7 @@ public void setLength(int length) {
* @return the capacity * @return the capacity
*/ */
public int capacity() { public int capacity() {
return buf.length; return buffer.length;
} }
/** /**
@ -182,10 +182,10 @@ public int capacity() {
* @param capacity the capacity to ensure * @param capacity the capacity to ensure
*/ */
public void ensureCapacity(int capacity) { public void ensureCapacity(int capacity) {
if (capacity > buf.length) { if (capacity > buffer.length) {
char[] old = buf; char[] old = buffer;
buf = new char[capacity]; buffer = new char[capacity];
System.arraycopy(old, 0, buf, 0, size); System.arraycopy(old, 0, buffer, 0, size);
} }
} }
@ -193,10 +193,10 @@ public void ensureCapacity(int capacity) {
* Minimizes the capacity to the actual length of the string. * Minimizes the capacity to the actual length of the string.
*/ */
public void minimizeCapacity() { public void minimizeCapacity() {
if (buf.length > length()) { if (buffer.length > length()) {
char[] old = buf; char[] old = buffer;
buf = new char[length()]; buffer = new char[length()];
System.arraycopy(old, 0, buf, 0, size); System.arraycopy(old, 0, buffer, 0, size);
} }
} }
@ -249,7 +249,7 @@ public char charAt(int index) {
if (index < 0 || index >= length()) { if (index < 0 || index >= length()) {
throw new StringIndexOutOfBoundsException(index); throw new StringIndexOutOfBoundsException(index);
} }
return buf[index]; return buffer[index];
} }
/** /**
@ -263,7 +263,7 @@ public void setCharAt(int index, char ch) {
if (index < 0 || index >= length()) { if (index < 0 || index >= length()) {
throw new StringIndexOutOfBoundsException(index); throw new StringIndexOutOfBoundsException(index);
} }
buf[index] = ch; buffer[index] = ch;
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -277,7 +277,7 @@ public char[] toCharArray() {
return ArrayUtils.EMPTY_CHAR_ARRAY; return ArrayUtils.EMPTY_CHAR_ARRAY;
} }
char chars[] = new char[size]; char chars[] = new char[size];
System.arraycopy(buf, 0, chars, 0, size); System.arraycopy(buffer, 0, chars, 0, size);
return chars; return chars;
} }
@ -300,7 +300,7 @@ public char[] toCharArray(int startIndex, int endIndex) {
return ArrayUtils.EMPTY_CHAR_ARRAY; return ArrayUtils.EMPTY_CHAR_ARRAY;
} }
char chars[] = new char[len]; char chars[] = new char[len];
System.arraycopy(buf, startIndex, chars, 0, len); System.arraycopy(buffer, startIndex, chars, 0, len);
return chars; return chars;
} }
@ -315,7 +315,7 @@ public char[] getChars(char[] destination) {
if (destination == null || destination.length < len) { if (destination == null || destination.length < len) {
destination = new char[len]; destination = new char[len];
} }
System.arraycopy(buf, 0, destination, 0, len); System.arraycopy(buffer, 0, destination, 0, len);
return destination; return destination;
} }
@ -339,7 +339,7 @@ public void getChars(int startIndex, int endIndex, char destination[], int desti
if (startIndex > endIndex) { if (startIndex > endIndex) {
throw new StringIndexOutOfBoundsException("end < start"); throw new StringIndexOutOfBoundsException("end < start");
} }
System.arraycopy(buf, startIndex, destination, destinationIndex, endIndex - startIndex); System.arraycopy(buffer, startIndex, destination, destinationIndex, endIndex - startIndex);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -384,7 +384,7 @@ public StrBuilder append(String str) {
if (strLen > 0) { if (strLen > 0) {
int len = length(); int len = length();
ensureCapacity(len + strLen); ensureCapacity(len + strLen);
str.getChars(0, strLen, buf, len); str.getChars(0, strLen, buffer, len);
size += strLen; size += strLen;
} }
return this; return this;
@ -405,7 +405,7 @@ public StrBuilder append(StringBuffer str) {
if (strLen > 0) { if (strLen > 0) {
int len = length(); int len = length();
ensureCapacity(len + strLen); ensureCapacity(len + strLen);
str.getChars(0, strLen, buf, len); str.getChars(0, strLen, buffer, len);
size += strLen; size += strLen;
} }
return this; return this;
@ -426,7 +426,7 @@ public StrBuilder append(StrBuilder str) {
if (strLen > 0) { if (strLen > 0) {
int len = length(); int len = length();
ensureCapacity(len + strLen); ensureCapacity(len + strLen);
System.arraycopy(str.buf, 0, buf, len, strLen); System.arraycopy(str.buffer, 0, buffer, len, strLen);
size += strLen; size += strLen;
} }
return this; return this;
@ -450,7 +450,7 @@ public StrBuilder append(char[] chars) {
if (strLen > 0) { if (strLen > 0) {
int len = length(); int len = length();
ensureCapacity(len + strLen); ensureCapacity(len + strLen);
System.arraycopy(chars, 0, buf, len, strLen); System.arraycopy(chars, 0, buffer, len, strLen);
size += strLen; size += strLen;
} }
return this; return this;
@ -481,7 +481,7 @@ public StrBuilder append(char[] chars, int startIndex, int length) {
if (length > 0) { if (length > 0) {
int len = length(); int len = length();
ensureCapacity(len + length); ensureCapacity(len + length);
System.arraycopy(chars, startIndex, buf, len, length); System.arraycopy(chars, startIndex, buffer, len, length);
size += length; size += length;
} }
return this; return this;
@ -496,17 +496,17 @@ public StrBuilder append(char[] chars, int startIndex, int length) {
public StrBuilder append(boolean value) { public StrBuilder append(boolean value) {
if (value) { if (value) {
ensureCapacity(size + 4); ensureCapacity(size + 4);
buf[size++] = 't'; buffer[size++] = 't';
buf[size++] = 'r'; buffer[size++] = 'r';
buf[size++] = 'u'; buffer[size++] = 'u';
buf[size++] = 'e'; buffer[size++] = 'e';
} else { } else {
ensureCapacity(size + 5); ensureCapacity(size + 5);
buf[size++] = 'f'; buffer[size++] = 'f';
buf[size++] = 'a'; buffer[size++] = 'a';
buf[size++] = 'l'; buffer[size++] = 'l';
buf[size++] = 's'; buffer[size++] = 's';
buf[size++] = 'e'; buffer[size++] = 'e';
} }
return this; return this;
} }
@ -520,7 +520,7 @@ public StrBuilder append(boolean value) {
public StrBuilder append(char ch) { public StrBuilder append(char ch) {
int len = length(); int len = length();
ensureCapacity(len + 1); ensureCapacity(len + 1);
buf[size++] = ch; buffer[size++] = ch;
return this; return this;
} }
@ -646,7 +646,7 @@ public StrBuilder appendPadding(int length, char padChar) {
if (length >= 0) { if (length >= 0) {
ensureCapacity(size + length); ensureCapacity(size + length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buf[size++] = padChar; buffer[size++] = padChar;
} }
} }
return this; return this;
@ -670,13 +670,13 @@ public StrBuilder appendFixedWidthPadLeft(Object obj, int width, char padChar) {
String str = (obj == null ? getNullText() : obj.toString()); String str = (obj == null ? getNullText() : obj.toString());
int strLen = str.length(); int strLen = str.length();
if (strLen >= width) { if (strLen >= width) {
str.getChars(strLen - width, strLen, buf, size); str.getChars(strLen - width, strLen, buffer, size);
} else { } else {
int padLen = width - strLen; int padLen = width - strLen;
for (int i = 0; i < padLen; i++) { for (int i = 0; i < padLen; i++) {
buf[size + i] = padChar; buffer[size + i] = padChar;
} }
str.getChars(0, strLen, buf, size + padLen); str.getChars(0, strLen, buffer, size + padLen);
} }
size += width; size += width;
} }
@ -714,12 +714,12 @@ public StrBuilder appendFixedWidthPadRight(Object obj, int width, char padChar)
String str = (obj == null ? getNullText() : obj.toString()); String str = (obj == null ? getNullText() : obj.toString());
int strLen = str.length(); int strLen = str.length();
if (strLen >= width) { if (strLen >= width) {
str.getChars(0, strLen, buf, size); str.getChars(0, strLen, buffer, size);
} else { } else {
int padLen = width - strLen; int padLen = width - strLen;
str.getChars(0, strLen, buf, size); str.getChars(0, strLen, buffer, size);
for (int i = 0; i < padLen; i++) { for (int i = 0; i < padLen; i++) {
buf[size + strLen + i] = padChar; buffer[size + strLen + i] = padChar;
} }
} }
size += width; size += width;
@ -776,9 +776,9 @@ public StrBuilder insert(int index, String str) {
if (strLen > 0) { if (strLen > 0) {
int newSize = size + strLen; int newSize = size + strLen;
ensureCapacity(newSize); ensureCapacity(newSize);
System.arraycopy(buf, index, buf, index + strLen, size - index); System.arraycopy(buffer, index, buffer, index + strLen, size - index);
size = newSize; size = newSize;
str.getChars(0, strLen, buf, index); str.getChars(0, strLen, buffer, index);
} }
return this; return this;
} }
@ -803,8 +803,8 @@ public StrBuilder insert(int index, char chars[]) {
int len = chars.length; int len = chars.length;
if (len > 0) { if (len > 0) {
ensureCapacity(size + len); ensureCapacity(size + len);
System.arraycopy(buf, index, buf, index + len, size - index); System.arraycopy(buffer, index, buffer, index + len, size - index);
System.arraycopy(chars, 0, buf, index, len); System.arraycopy(chars, 0, buffer, index, len);
size += len; size += len;
} }
return this; return this;
@ -837,8 +837,8 @@ public StrBuilder insert(int index, char chars[], int offset, int length) {
} }
if (length > 0) { if (length > 0) {
ensureCapacity(size + length); ensureCapacity(size + length);
System.arraycopy(buf, index, buf, index + length, size - index); System.arraycopy(buffer, index, buffer, index + length, size - index);
System.arraycopy(chars, offset, buf, index, length); System.arraycopy(chars, offset, buffer, index, length);
size += length; size += length;
} }
return this; return this;
@ -856,20 +856,20 @@ public StrBuilder insert(int index, boolean value) {
validateIndex(index); validateIndex(index);
if (value) { if (value) {
ensureCapacity(size + 4); ensureCapacity(size + 4);
System.arraycopy(buf, index, buf, index + 4, size - index); System.arraycopy(buffer, index, buffer, index + 4, size - index);
buf[index++] = 't'; buffer[index++] = 't';
buf[index++] = 'r'; buffer[index++] = 'r';
buf[index++] = 'u'; buffer[index++] = 'u';
buf[index] = 'e'; buffer[index] = 'e';
size += 4; size += 4;
} else { } else {
ensureCapacity(size + 5); ensureCapacity(size + 5);
System.arraycopy(buf, index, buf, index + 5, size - index); System.arraycopy(buffer, index, buffer, index + 5, size - index);
buf[index++] = 'f'; buffer[index++] = 'f';
buf[index++] = 'a'; buffer[index++] = 'a';
buf[index++] = 'l'; buffer[index++] = 'l';
buf[index++] = 's'; buffer[index++] = 's';
buf[index] = 'e'; buffer[index] = 'e';
size += 5; size += 5;
} }
return this; return this;
@ -886,8 +886,8 @@ public StrBuilder insert(int index, boolean value) {
public StrBuilder insert(int index, char value) { public StrBuilder insert(int index, char value) {
validateIndex(index); validateIndex(index);
ensureCapacity(size + 1); ensureCapacity(size + 1);
System.arraycopy(buf, index, buf, index + 1, size - index); System.arraycopy(buffer, index, buffer, index + 1, size - index);
buf[index] = value; buffer[index] = value;
size++; size++;
return this; return this;
} }
@ -954,7 +954,7 @@ public StrBuilder delete(int startIndex, int endIndex) {
endIndex = validateRange(startIndex, endIndex); endIndex = validateRange(startIndex, endIndex);
int len = endIndex - startIndex; int len = endIndex - startIndex;
if (len > 0) { if (len > 0) {
System.arraycopy(buf, endIndex, buf, startIndex, size - endIndex); System.arraycopy(buffer, endIndex, buffer, startIndex, size - endIndex);
size -= len; size -= len;
} }
return this; return this;
@ -971,7 +971,7 @@ public StrBuilder deleteCharAt(int index) {
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
throw new StringIndexOutOfBoundsException(index); throw new StringIndexOutOfBoundsException(index);
} }
System.arraycopy(buf, index + 1, buf, index, size - index - 1); System.arraycopy(buffer, index + 1, buffer, index, size - index - 1);
size--; size--;
return this; return this;
} }
@ -984,14 +984,14 @@ public StrBuilder deleteCharAt(int index) {
*/ */
public StrBuilder delete(char ch) { public StrBuilder delete(char ch) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (buf[i] == ch) { if (buffer[i] == ch) {
int start = i; int start = i;
while (++i < size) { while (++i < size) {
if (buf[i] != ch) { if (buffer[i] != ch) {
break; break;
} }
} }
System.arraycopy(buf, i, buf, start, size - i); System.arraycopy(buffer, i, buffer, start, size - i);
size -= (i - start); size -= (i - start);
} }
} }
@ -1037,10 +1037,10 @@ public StrBuilder replace(int startIndex, int endIndex, String str) {
ensureCapacity(newSize); ensureCapacity(newSize);
} }
if (insertLen != removeLen) { if (insertLen != removeLen) {
System.arraycopy(buf, endIndex, buf, startIndex + insertLen, size - endIndex); System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex);
size = newSize; size = newSize;
} }
str.getChars(0, insertLen, buf, startIndex); str.getChars(0, insertLen, buffer, startIndex);
return this; return this;
} }
@ -1064,11 +1064,11 @@ public StrBuilder replace(int startIndex, int endIndex, StrBuilder builder) {
} }
if (insertLen != removeLen) { if (insertLen != removeLen) {
//shift the current characters to the right //shift the current characters to the right
System.arraycopy(buf, endIndex, buf, startIndex + insertLen, size - endIndex); System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex);
//adjust the size accordingly //adjust the size accordingly
size += (insertLen - removeLen); size += (insertLen - removeLen);
} }
builder.getChars(0, insertLen, buf, startIndex); builder.getChars(0, insertLen, buffer, startIndex);
return this; return this;
} }
@ -1082,8 +1082,8 @@ public StrBuilder replace(int startIndex, int endIndex, StrBuilder builder) {
public StrBuilder replace(char search, char replace) { public StrBuilder replace(char search, char replace) {
if (search != replace) { if (search != replace) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (buf[i] == search) { if (buffer[i] == search) {
buf[i] = replace; buffer[i] = replace;
} }
} }
} }
@ -1131,7 +1131,7 @@ public boolean startsWith(String str) {
return false; return false;
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (buf[i] != str.charAt(i)) { if (buffer[i] != str.charAt(i)) {
return false; return false;
} }
} }
@ -1159,7 +1159,7 @@ public boolean endsWith(String str) {
} }
int pos = size - len; int pos = size - len;
for (int i = 0; i < len; i++,pos++) { for (int i = 0; i < len; i++,pos++) {
if (buf[pos] != str.charAt(i)) { if (buffer[pos] != str.charAt(i)) {
return false; return false;
} }
} }
@ -1193,7 +1193,7 @@ public String substring(int start) {
*/ */
public String substring(int startIndex, int endIndex) { public String substring(int startIndex, int endIndex) {
endIndex = validateRange(startIndex, endIndex); endIndex = validateRange(startIndex, endIndex);
return new String(buf, startIndex, endIndex - startIndex); return new String(buffer, startIndex, endIndex - startIndex);
} }
/** /**
@ -1212,9 +1212,9 @@ public String leftString(int length) {
if (length <= 0) { if (length <= 0) {
return ""; return "";
} else if (length >= size) { } else if (length >= size) {
return new String(buf, 0, size); return new String(buffer, 0, size);
} else { } else {
return new String(buf, 0, length); return new String(buffer, 0, length);
} }
} }
@ -1234,9 +1234,9 @@ public String rightString(int length) {
if (length <= 0) { if (length <= 0) {
return ""; return "";
} else if (length >= size) { } else if (length >= size) {
return new String(buf, 0, size); return new String(buffer, 0, size);
} else { } else {
return new String(buf, size - length, length); return new String(buffer, size - length, length);
} }
} }
@ -1264,9 +1264,9 @@ public String midString(int index, int length) {
return ""; return "";
} }
if (size <= index + length) { if (size <= index + length) {
return new String(buf, index, size - index); return new String(buffer, index, size - index);
} else { } else {
return new String(buf, index, length); return new String(buffer, index, length);
} }
} }
@ -1278,7 +1278,7 @@ public String midString(int index, int length) {
* @return true if the builder contains the character * @return true if the builder contains the character
*/ */
public boolean contains(char ch) { public boolean contains(char ch) {
char[] thisBuf = buf; char[] thisBuf = buffer;
for (int i = 0; i < thisBuf.length; i++) { for (int i = 0; i < thisBuf.length; i++) {
if (thisBuf[i] == ch) { if (thisBuf[i] == ch) {
return true; return true;
@ -1320,7 +1320,7 @@ public int indexOf(char ch, int startIndex) {
if (startIndex >= size) { if (startIndex >= size) {
return -1; return -1;
} }
char[] thisBuf = buf; char[] thisBuf = buffer;
for (int i = startIndex; i < thisBuf.length; i++) { for (int i = startIndex; i < thisBuf.length; i++) {
if (thisBuf[i] == ch) { if (thisBuf[i] == ch) {
return i; return i;
@ -1361,7 +1361,7 @@ public int indexOf(String str, int startIndex) {
if (strLen == 1) { if (strLen == 1) {
return indexOf(str.charAt(0), startIndex); return indexOf(str.charAt(0), startIndex);
} }
char[] thisBuf = buf; char[] thisBuf = buffer;
outer: outer:
for (int i = startIndex; i < thisBuf.length - strLen; i++) { for (int i = startIndex; i < thisBuf.length - strLen; i++) {
for (int j = 0; j < strLen; j++) { for (int j = 0; j < strLen; j++) {
@ -1402,7 +1402,7 @@ public int lastIndexOf(char ch, int startIndex) {
return -1; return -1;
} }
for (int i = startIndex; i >= 0; i--) { for (int i = startIndex; i >= 0; i--) {
if (buf[i] == ch) { if (buffer[i] == ch) {
return i; return i;
} }
} }
@ -1445,7 +1445,7 @@ public int lastIndexOf(String str, int startIndex) {
outer: outer:
for (int i = startIndex - strLen + 1; i >= 0; i--) { for (int i = startIndex - strLen + 1; i >= 0; i--) {
for (int j = 0; j < strLen; j++) { for (int j = 0; j < strLen; j++) {
if (str.charAt(j) != buf[i + j]) { if (str.charAt(j) != buffer[i + j]) {
continue outer; continue outer;
} }
} }
@ -1470,9 +1470,9 @@ public StrBuilder reverse() {
int half = size / 2; int half = size / 2;
for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) { for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) {
char swap = buf[leftIdx]; char swap = buffer[leftIdx];
buf[leftIdx] = buf[rightIdx]; buffer[leftIdx] = buffer[rightIdx];
buf[rightIdx] = swap; buffer[rightIdx] = swap;
} }
return this; return this;
} }
@ -1516,7 +1516,7 @@ public StrBuilder reverse() {
* @return the builder as a String * @return the builder as a String
*/ */
public String toString() { public String toString() {
return new String(buf, 0, size); return new String(buffer, 0, size);
} }
/** /**
@ -1526,7 +1526,7 @@ public String toString() {
* @return the builder as a StringBuffer * @return the builder as a StringBuffer
*/ */
public StringBuffer toStringBuffer() { public StringBuffer toStringBuffer() {
return new StringBuffer(size).append(buf, 0, size); return new StringBuffer(size).append(buffer, 0, size);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -282,10 +282,10 @@ public void testSetLength() {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testCapacity() { public void testCapacity() {
StrBuilder sb = new StrBuilder(); StrBuilder sb = new StrBuilder();
assertEquals(sb.buf.length, sb.capacity()); assertEquals(sb.buffer.length, sb.capacity());
sb.append("HelloWorldHelloWorldHelloWorldHelloWorld"); sb.append("HelloWorldHelloWorldHelloWorldHelloWorld");
assertEquals(sb.buf.length, sb.capacity()); assertEquals(sb.buffer.length, sb.capacity());
} }
public void testEnsureCapacity() { public void testEnsureCapacity() {
@ -336,7 +336,7 @@ public void testClear() {
sb.append("Hello"); sb.append("Hello");
sb.clear(); sb.clear();
assertEquals(0, sb.length()); assertEquals(0, sb.length());
assertEquals(true, sb.buf.length >= 5); assertEquals(true, sb.buffer.length >= 5);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------