LUCENE-3548: fix broken CharsRef#append

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1196228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-11-01 19:23:43 +00:00
parent 1ce1b914d3
commit f94d21b022
3 changed files with 41 additions and 5 deletions

View File

@ -707,6 +707,9 @@ Bug fixes
across multiple threads, because all clones shared the same buffer.
(Robert Muir)
* LUCENE-3548: Fix CharsRef#append to extend length of the existing char[]
and presever existing chars. (Simon Willnauer)
New Features
* LUCENE-3448: Added FixedBitSet.and(other/DISI), andNot(other/DISI).

View File

@ -189,18 +189,22 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence {
* Copies the given array into this CharsRef starting at offset 0
*/
public void copy(char[] otherChars, int otherOffset, int otherLength) {
grow(otherLength);
System.arraycopy(otherChars, otherOffset, this.chars, 0,
otherLength);
this.offset = 0;
append(otherChars, otherOffset, otherLength);
this.length = otherLength;
}
/**
* Appends the given array to this CharsRef starting at the current offset
* Appends the given array to this CharsRef
*/
public void append(char[] otherChars, int otherOffset, int otherLength) {
grow(this.offset + otherLength);
System.arraycopy(otherChars, otherOffset, this.chars, this.offset,
final int newLength = length + otherLength;
grow(this.offset + newLength);
System.arraycopy(otherChars, otherOffset, this.chars, this.offset+length,
otherLength);
this.length = otherLength;
this.length += otherLength;
}
@Override

View File

@ -38,4 +38,33 @@ public class TestCharsRef extends LuceneTestCase {
assertEquals(utf8[i].utf8ToString(), utf16[i].toString());
}
}
public void testAppend() {
CharsRef ref = new CharsRef();
StringBuilder builder = new StringBuilder();
int numStrings = atLeast(10);
for (int i = 0; i < numStrings; i++) {
char[] charArray = _TestUtil.randomRealisticUnicodeString(random, 1, 100).toCharArray();
int offset = random.nextInt(charArray.length);
int length = charArray.length - offset;
builder.append(charArray, offset, length);
ref.append(charArray, offset, length);
}
assertEquals(builder.toString(), ref.toString());
}
public void testCopy() {
int numIters = atLeast(10);
for (int i = 0; i < numIters; i++) {
CharsRef ref = new CharsRef();
char[] charArray = _TestUtil.randomRealisticUnicodeString(random, 1, 100).toCharArray();
int offset = random.nextInt(charArray.length);
int length = charArray.length - offset;
String str = new String(charArray, offset, length);
ref.copy(charArray, offset, length);
assertEquals(str, ref.toString());
}
}
}