diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java index 7310cc73c00c..22dc69da34ef 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java @@ -16,6 +16,7 @@ */ package org.apache.lucene.queryparser.flexible.core.util; +import java.util.BitSet; import java.util.Locale; /** CharsSequence with escaped chars information. */ @@ -89,21 +90,18 @@ public String toStringEscaped() { * @return a escaped String */ public String toStringEscaped(char[] enabledChars) { - // TODO: non efficient implementation, refactor this code StringBuilder result = new StringBuilder(this.length()); + BitSet enabled = new BitSet(1 << 16); + for (char character : enabledChars) { + enabled.set(character); + } + for (int i = 0; i < this.length(); i++) { - if (this.chars[i] == '\\') { + char current = this.chars[i]; + if (current == '\\' || (this.wasEscaped[i] && enabled.get(current))) { result.append('\\'); - } else { - for (char character : enabledChars) { - if (this.chars[i] == character && this.wasEscaped[i]) { - result.append('\\'); - break; - } - } } - - result.append(this.chars[i]); + result.append(current); } return result.toString(); }