[Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core/structure FastStringBuffer.java, 1.2,
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-06-15 13:14:57
|
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/structure In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23038/src/org/python/pydev/core/structure Modified Files: FastStringBuffer.java Log Message: Comments / minors. Index: FastStringBuffer.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/structure/FastStringBuffer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FastStringBuffer.java 15 Jun 2008 12:44:22 -0000 1.2 --- FastStringBuffer.java 15 Jun 2008 13:15:04 -0000 1.3 *************** *** 2,8 **** /** ! * This is a custom string that works around char[] objects to provide minimum allocation/garbage collection overhead. ! * To be used mostly when several small concatenations of strings are used and in local contexts while reusing the ! * same object to create multiple strings. * * @author Fabio --- 2,14 ---- /** ! * This is a custom string buffer optimized for append(), clear() and deleteLast(). ! * ! * Basically it aims at being created once, being used for something, having clear() called and then reused ! * (ultimately providing minimum allocation/garbage collection overhead for that use-case). ! * ! * append() is optimizing by doing less checks (so, exceptions thrown may be uglier on invalid operations ! * and null is not checked for in the common case -- use appendObject if it may be null). ! * ! * clear() and deleteLast() only change the internal count and have almost zero overhead. * * @author Fabio *************** *** 48,52 **** /** ! * Appends a string to the buffer */ public FastStringBuffer append(String string) { --- 54,58 ---- /** ! * Appends a string to the buffer. Passing a null string will throw an exception. */ public FastStringBuffer append(String string) { *************** *** 63,71 **** } private void resizeForMinimum(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; ! if (newCapacity < 0) { ! newCapacity = Integer.MAX_VALUE; ! } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } --- 69,78 ---- } + /** + * Resizes the internal buffer to have at least the minimum capacity passed (but may be more) + */ private void resizeForMinimum(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; ! if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } *************** *** 75,78 **** --- 82,88 ---- } + /** + * Appends an int to the buffer. + */ public final FastStringBuffer append(int n) { append(String.valueOf(n)); *************** *** 80,83 **** --- 90,96 ---- } + /** + * Appends a char to the buffer. + */ public final FastStringBuffer append(char n) { if (count + 1 > value.length) { *************** *** 89,92 **** --- 102,108 ---- } + /** + * Appends a long to the buffer. + */ public final FastStringBuffer append(long n) { append(String.valueOf(n)); *************** *** 94,102 **** } public final FastStringBuffer append(boolean b) { append(String.valueOf(b)); return this; } ! public FastStringBuffer append(char[] chars) { int newCount = count + chars.length; --- 110,124 ---- } + /** + * Appends a boolean to the buffer. + */ public final FastStringBuffer append(boolean b) { append(String.valueOf(b)); return this; } ! ! /** ! * Appends an array of chars to the buffer. ! */ public FastStringBuffer append(char[] chars) { int newCount = count + chars.length; *************** *** 109,112 **** --- 131,137 ---- } + /** + * Appends another buffer to this buffer. + */ public FastStringBuffer append(FastStringBuffer other) { append(other.value, 0, other.count); *************** *** 114,126 **** } public FastStringBuffer append(char[] chars, int offset, int len) { ! if (count + len > value.length) { ! resizeForMinimum(count + len); } System.arraycopy(chars, offset, value, count, len); ! count += len; return this; } public FastStringBuffer reverse() { final int limit = count / 2; --- 139,159 ---- } + /** + * Appends an array of chars to this buffer, starting at the offset passed with the length determined. + */ public FastStringBuffer append(char[] chars, int offset, int len) { ! int newCount = count + len; ! if (newCount > value.length) { ! resizeForMinimum(newCount); } System.arraycopy(chars, offset, value, count, len); ! count = newCount; return this; } + + /** + * Reverses the contents on this buffer + */ public FastStringBuffer reverse() { final int limit = count / 2; *************** *** 133,144 **** --- 166,187 ---- } + + /** + * Clears this buffer. + */ public void clear() { this.count = 0; } + /** + * @return the length of this buffer + */ public int length() { return this.count; } + /** + * @return a new stringt with the contents of this buffer. + */ @Override public String toString() { *************** *** 146,149 **** --- 189,195 ---- } + /** + * Erases the last char in this buffer + */ public void deleteLast() { if (this.count > 0) { *************** *** 152,159 **** --- 198,211 ---- } + /** + * @return the char given at a specific position of the buffer (no bounds check) + */ public char charAt(int i) { return this.value[i]; } + /** + * Inserts a string at a given position in the buffer. + */ public FastStringBuffer insert(int offset, String str) { int len = str.length(); *************** *** 167,175 **** return this; } ! public FastStringBuffer appendObject(Object attribute) { ! return append(attribute != null?attribute.toString():"null"); } public void setCount(int newLen) { this.count = newLen; --- 219,247 ---- return this; } + + /** + * Inserts a char at a given position in the buffer. + */ + public FastStringBuffer insert(int offset, char c) { + int newCount = count + 1; + if (newCount > value.length){ + resizeForMinimum(newCount); + } + System.arraycopy(value, offset, value, offset + 1, count - offset); + value[offset] = c; + count = newCount; + return this; + } ! /** ! * Appends object.toString(). If null, "null" is appended. ! */ ! public FastStringBuffer appendObject(Object object) { ! return append(object != null?object.toString():"null"); } + /** + * Sets the new size of this buffer (warning: use with care: no validation is done of the len passed) + */ public void setCount(int newLen) { this.count = newLen; |