Skip to content

Commit

Permalink
Refactoring SearchEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Jan 19, 2024
1 parent 01a1bd8 commit 312e566
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
53 changes: 20 additions & 33 deletions src/studio/ui/search/SearchEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<text.length()) {
int start, end;
if (matcher != null) {
if (!matcher.find(pos)) return false;
start = matcher.start();
end = matcher.end();
} else {
start = text.indexOf(what, pos);
if (start == -1) return false;
end = start + what.length();
}
if (!matcher.find(pos)) return SearchResult.NOT_FOUND;
int start = matcher.start();
int end = matcher.end();

if (!wholeWord) {
return true;
return new SearchResult(matcher);
} else {
boolean startWord = true;
if (start > 0) {
Expand All @@ -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();
}
}
24 changes: 24 additions & 0 deletions src/studio/ui/search/SearchResult.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
21 changes: 21 additions & 0 deletions test/studio/ui/search/SearchEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

}

0 comments on commit 312e566

Please sign in to comment.