Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-4842: Unbreak SynonymMap.Builder.join(String[],CharsRef)

Steven Rowe9fffda8c34bf
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java
index 9a7555946f4..0a72ca4a65e 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java
@@ -90,26 +90,21 @@ public class SynonymMap {
public static CharsRef join(String[] words, CharsRef reuse) {
int upto = 0;
char[] buffer = reuse.chars;
- for(String word : words) {
- if (upto > 0) {
- if (upto >= buffer.length) {
- reuse.grow(upto);
- buffer = reuse.chars;
- }
- buffer[upto++] = SynonymMap.WORD_SEPARATOR;
- }
-
- final int wordLen = word.length();
- final int needed = upto + wordLen;
+ for (String word : words) {
+ final int wordLen = word.length();
+ final int needed = (0 == upto ? wordLen : 1 + upto + wordLen); // Add 1 for WORD_SEPARATOR
if (needed > buffer.length) {
reuse.grow(needed);
buffer = reuse.chars;
}
+ if (upto > 0) {
+ buffer[upto++] = SynonymMap.WORD_SEPARATOR;
+ }
word.getChars(0, wordLen, buffer, upto);
upto += wordLen;
}
-
+ reuse.length = upto;
return reuse;
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymMapFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymMapFilter.java
index 7addbe981ae..89146d88640 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymMapFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/TestSynonymMapFilter.java
@@ -55,9 +55,13 @@ public class TestSynonymMapFilter extends BaseTokenStreamTestCase {
if (VERBOSE) {
System.out.println(" add input=" + input + " output=" + output + " keepOrig=" + keepOrig);
}
- b.add(new CharsRef(input.replaceAll(" +", "\u0000")),
- new CharsRef(output.replaceAll(" +", "\u0000")),
- keepOrig);
+ CharsRef inputCharsRef = new CharsRef();
+ SynonymMap.Builder.join(input.split(" +"), inputCharsRef);
+
+ CharsRef outputCharsRef = new CharsRef();
+ SynonymMap.Builder.join(output.split(" +"), outputCharsRef);
+
+ b.add(inputCharsRef, outputCharsRef, keepOrig);
}
private void assertEquals(CharTermAttribute term, String expected) {