LUCENE-1163: get CharArraySet.contains(char[], int offset, int length) to actually pay attention to offset

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@618652 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-02-05 14:02:32 +00:00
parent d941d1276f
commit 53d3a0cfab
3 changed files with 27 additions and 15 deletions

View File

@ -40,6 +40,10 @@ Bug fixes
1. LUCENE-1134: Fixed BooleanQuery.rewrite to only optimze a single
clause query if minNumShouldMatch<=0. (Shai Erera via Michael Busch)
2. LUCENE-1163: Fixed bug in CharArraySet.contains(char[] buffer, int
offset, int len) that was ignoring offset and thus giving the
wrong answer. (Thomas Peuss via Mike McCandless)
New features

View File

@ -66,7 +66,7 @@ public class CharArraySet extends AbstractSet {
}
private int getSlot(char[] text, int off, int len) {
int code = getHashCode(text, len);
int code = getHashCode(text, off, len);
int pos = code & (entries.length-1);
char[] text2 = entries[pos];
if (text2 != null && !equals(text, off, len, text2)) {
@ -175,14 +175,15 @@ public class CharArraySet extends AbstractSet {
}
}
private int getHashCode(char[] text, int len) {
private int getHashCode(char[] text, int offset, int len) {
int code = 0;
final int stop = offset + len;
if (ignoreCase) {
for (int i=0; i<len; i++) {
for (int i=offset; i<stop; i++) {
code = code*31 + Character.toLowerCase(text[i]);
}
} else {
for (int i=0; i<len; i++) {
for (int i=offset; i<stop; i++) {
code = code*31 + text[i];
}
}

View File

@ -17,20 +17,27 @@ package org.apache.lucene.analysis;
* limitations under the License.
*/
import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.CharArraySet;
public class TestCharArraySet extends LuceneTestCase
{
public void testRehash() throws Exception {
CharArraySet cas = new CharArraySet(0, true);
for(int i=0;i<StopAnalyzer.ENGLISH_STOP_WORDS.length;i++)
cas.add(StopAnalyzer.ENGLISH_STOP_WORDS[i]);
assertEquals(StopAnalyzer.ENGLISH_STOP_WORDS.length, cas.size());
for(int i=0;i<StopAnalyzer.ENGLISH_STOP_WORDS.length;i++)
assertTrue(cas.contains(StopAnalyzer.ENGLISH_STOP_WORDS[i]));
}
public void testRehash() throws Exception {
CharArraySet cas = new CharArraySet(0, true);
for(int i=0;i<StopAnalyzer.ENGLISH_STOP_WORDS.length;i++)
cas.add(StopAnalyzer.ENGLISH_STOP_WORDS[i]);
assertEquals(StopAnalyzer.ENGLISH_STOP_WORDS.length, cas.size());
for(int i=0;i<StopAnalyzer.ENGLISH_STOP_WORDS.length;i++)
assertTrue(cas.contains(StopAnalyzer.ENGLISH_STOP_WORDS[i]));
}
public void testNonZeroOffset() {
String[] words={"Hello","World","this","is","a","test"};
char[] findme="xthisy".toCharArray();
CharArraySet set=new CharArraySet(10,true);
set.addAll(Arrays.asList(words));
assertTrue(set.contains(findme, 1, 4));
assertTrue(set.contains(new String(findme,1,4)));
}
}