diff --git a/src/studio/ui/search/SearchEngine.java b/src/studio/ui/search/SearchEngine.java index f23ba00b..16a107a0 100644 --- a/src/studio/ui/search/SearchEngine.java +++ b/src/studio/ui/search/SearchEngine.java @@ -9,50 +9,33 @@ public class SearchEngine { - private String what; private final boolean wholeWord; - private final boolean matchCase; - private Pattern pattern = null; + private final Pattern pattern; public SearchEngine(SearchContext context) throws PatternSyntaxException { - what = context.getSearchFor(); - matchCase = context.getMatchCase(); wholeWord = context.getWholeWord(); - if (context.isRegularExpression()) { - pattern = Pattern.compile(what, matchCase ? 0 : Pattern.CASE_INSENSITIVE); - } else { - if (!matchCase) what = what.toLowerCase(); - } + int flags = context.getMatchCase() ? 0 : Pattern.CASE_INSENSITIVE; + flags |= context.isRegularExpression() ? 0 : Pattern.LITERAL; + pattern = Pattern.compile(context.getSearchFor(), flags); } - public boolean containsIn(String text) { + public SearchResult search(String text) { + return search(text, 0); + } - Matcher matcher = null; - if (pattern != null) { - matcher = pattern.matcher(text); - } else { - if (!matchCase) { - text = text.toLowerCase(); - } - } + public SearchResult search(String text, int from) { + Matcher matcher = pattern.matcher(text); - int pos = 0; + int pos = from; while (pos 0) { @@ -62,11 +45,15 @@ public boolean containsIn(String text) { if (end < text.length()) { endWord = !Character.isLetterOrDigit(text.charAt(end)); } - if (startWord && endWord) return true; + if (startWord && endWord) return new SearchResult(matcher); } pos = end; } - return false; + return SearchResult.NOT_FOUND; + } + + public boolean containsIn(String text) { + return search(text).found(); } } diff --git a/src/studio/ui/search/SearchResult.java b/src/studio/ui/search/SearchResult.java new file mode 100644 index 00000000..f1112a4c --- /dev/null +++ b/src/studio/ui/search/SearchResult.java @@ -0,0 +1,24 @@ +package studio.ui.search; + +import java.util.regex.Matcher; + +public class SearchResult { + + private final Matcher matcher; + + public final static SearchResult NOT_FOUND = new SearchResult(null); + + public SearchResult(Matcher matcher) { + this.matcher = matcher; + } + + public boolean found() { + return matcher != null; + } + + public Matcher matcher() { + if (! found()) throw new IllegalStateException("No matcher for the SearchResult"); + return matcher; + } + +} diff --git a/test/studio/ui/search/SearchEngineTest.java b/test/studio/ui/search/SearchEngineTest.java index b720714d..8b1095b1 100644 --- a/test/studio/ui/search/SearchEngineTest.java +++ b/test/studio/ui/search/SearchEngineTest.java @@ -72,4 +72,25 @@ public void testWholeWordRegexp() { assertTrue(engine.containsIn("baa aa")); } + @Test + public void testSimpleWithSpecSymbols() { + context.setSearchFor("(aa"); + SearchEngine engine = new SearchEngine(context); + assertTrue(engine.containsIn("(aa")); + assertTrue(engine.containsIn("(a a(aa")); + assertFalse(engine.containsIn("(a a(aA")); + assertFalse(engine.containsIn("x(a ax")); + } + + @Test + public void testSimpleWithSpecSymbolsCaseInsensitive() { + context.setSearchFor("(aa"); + context.setMatchCase(false); + SearchEngine engine = new SearchEngine(context); + assertTrue(engine.containsIn("(aa")); + assertTrue(engine.containsIn("(a a(aa")); + assertTrue(engine.containsIn("(a a(aA")); + assertFalse(engine.containsIn("x(a ax")); + } + }