Skip to content

Apache Lucene open-source search software

LUCENE-5777: fix double escaping of dash in hunspell conditions

Robert Muir1e881bf79310
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index dcec52cb94b..1f54db01e39 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -343,7 +343,7 @@ Bug fixes
* LUCENE-5747: Project-specific settings for the eclipse development
environment will prevent automatic code reformatting. (Shawn Heisey)
-* LUCENE-5768: Hunspell condition checks containing character classes
+* LUCENE-5768, LUCENE-5777: Hunspell condition checks containing character classes
were buggy. (Clinton Gormley, Robert Muir)
Test Framework
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
index b67a978fab2..8a16602a54b 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
@@ -356,6 +356,25 @@ public class Dictionary {
}
return builder.finish();
}
+
+ static String escapeDash(String re) {
+ // we have to be careful, even though dash doesn't have a special meaning,
+ // some dictionaries already escape it (e.g. pt_PT), so we don't want to nullify it
+ StringBuilder escaped = new StringBuilder();
+ for (int i = 0; i < re.length(); i++) {
+ char c = re.charAt(i);
+ if (c == '-') {
+ escaped.append("\\-");
+ } else {
+ escaped.append(c);
+ if (c == '\\' && i + 1 < re.length()) {
+ escaped.append(re.charAt(i+1));
+ i++;
+ }
+ }
+ }
+ return escaped.toString();
+ }
/**
* Parses a specific affix rule putting the result into the provided affix map
@@ -425,7 +444,7 @@ public class Dictionary {
}
// "dash hasn't got special meaning" (we must escape it)
if (condition.indexOf('-') >= 0) {
- condition = condition.replace("-", "\\-");
+ condition = escapeDash(condition);
}
final String regex;
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDoubleEscape.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDoubleEscape.java
new file mode 100644
index 00000000000..4267ae455e5
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDoubleEscape.java
@@ -0,0 +1,31 @@
+package org.apache.lucene.analysis.hunspell;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.junit.BeforeClass;
+
+public class TestDoubleEscape extends StemmerTestBase {
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ init("double-escaped.aff", "double-escaped.dic");
+ }
+
+ public void testStemming() {
+ assertStemsTo("adubo", "adubar");
+ }
+}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.aff b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.aff
new file mode 100644
index 00000000000..ab74afa4034
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.aff
@@ -0,0 +1,5 @@
+SET UTF-8
+
+SFX X Y 1
+SFX X ar o [^\-]ar
+
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.dic b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.dic
new file mode 100644
index 00000000000..42ddb5ef9e9
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/double-escaped.dic
@@ -0,0 +1,2 @@
+1
+adubar/X