diff --git a/.gitignore b/.gitignore
index c177e3a..dfba200 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@
/database/
/.idea/
nb*.xml
-*.conf
\ No newline at end of file
+*.conf
+/nbproject/
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index f86b2f3..5d64904 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,6 +47,18 @@
config
1.3.2
+
+ junit
+ junit
+ 4.12
+ test
+
+
+ org.hamcrest
+ hamcrest-core
+ 1.3
+ test
+
diff --git a/src/main/java/com/jagrosh/vortex/automod/Filter.java b/src/main/java/com/jagrosh/vortex/automod/Filter.java
index 3e5c605..cfb0710 100644
--- a/src/main/java/com/jagrosh/vortex/automod/Filter.java
+++ b/src/main/java/com/jagrosh/vortex/automod/Filter.java
@@ -30,7 +30,7 @@
public final static int MAX_CONTENT_LENGTH = 255;
public final static int MAX_STRIKES = 100;
- private final static String[] TEST_CASES = {"welcome", "i will follow the rules"};
+ private final static String[] TEST_CASES = {"welcome", "i will follow the rules", "this is a sentence"};
public final String name;
public final int strikes;
@@ -150,15 +150,21 @@
glob = glob.replaceAll("\\*+", "*"); // remove double wildcards
this.startWildcard = glob.startsWith("*");
this.endWildcard = glob.endsWith("*");
- this.glob = (startWildcard ? "" : " ")
- + glob.substring(startWildcard ? 1 : 0, endWildcard ? glob.length()-1 : glob.length())
- + (endWildcard ? "" : " ");
+ this.glob = glob.substring(startWildcard ? 1 : 0, endWildcard ? glob.length()-1 : glob.length());
}
@Override
public boolean test(String message)
{
- return (" " + message.replaceAll("\\s+", " ").toLowerCase() + " ").contains(glob);
+ String lower = message.toLowerCase();
+ int index = -1;
+ while((index = lower.indexOf(glob, index + 1)) > -1)
+ {
+ if((startWildcard || isWordBoundary(lower, index - 1))
+ && (endWildcard || isWordBoundary(lower, index + glob.length())))
+ return true;
+ }
+ return false;
}
@Override
@@ -166,6 +172,14 @@
{
return (startWildcard ? "*" : "") + glob.trim() + (endWildcard ? "*" : "");
}
+
+ private static boolean isWordBoundary(String str, int index)
+ {
+ if(index < 0 || index >= str.length())
+ return true;
+ char c = str.charAt(index);
+ return !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9');
+ }
}
public static class Quote extends Item
diff --git a/src/test/java/com/jagrosh/vortex/FilterTest.java b/src/test/java/com/jagrosh/vortex/FilterTest.java
new file mode 100644
index 0000000..24bc007
--- /dev/null
+++ b/src/test/java/com/jagrosh/vortex/FilterTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2019 John Grosh (john.a.grosh@gmail.com).
+ *
+ * Licensed 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.
+ */
+package com.jagrosh.vortex;
+
+import com.jagrosh.vortex.automod.Filter.Glob;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author John Grosh (john.a.grosh@gmail.com)
+ */
+public class FilterTest
+{
+ @Test
+ public void globTest()
+ {
+ Glob glob;
+
+ assertTrue('0' < '9');
+
+ glob = new Glob("get");
+ assertFalse(glob.test("go together now"));
+ assertFalse(glob.test("go to gether now"));
+ assertFalse(glob.test("go toget her now"));
+ assertTrue(glob.test("go to get her now"));
+
+ glob = new Glob("*get");
+ assertFalse(glob.test("go together now"));
+ assertFalse(glob.test("go to gether now"));
+ assertTrue(glob.test("go toget her now"));
+ assertTrue(glob.test("go to get her now"));
+
+ glob = new Glob("get*");
+ assertFalse(glob.test("go together now"));
+ assertTrue(glob.test("go to gether now"));
+ assertFalse(glob.test("go toget her now"));
+ assertTrue(glob.test("go to get her now"));
+
+ glob = new Glob("*get*");
+ assertTrue(glob.test("go together now"));
+ assertTrue(glob.test("go to gether now"));
+ assertTrue(glob.test("go toget her now"));
+ assertTrue(glob.test("go to get her now"));
+
+ glob = new Glob("get");
+ assertTrue(glob.test("I for get"));
+ assertFalse(glob.test("I forget"));
+
+ glob = new Glob("get");
+ assertTrue(glob.test("get the food"));
+ assertFalse(glob.test("getthe food"));
+ }
+}