Skip to content

Commit

Permalink
Closes #744
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Jun 9, 2020
1 parent 2331523 commit 475ca7b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ public boolean matches(Filter filter, Post.Builder post) {

@AnyThread
public boolean matches(Filter filter, boolean matchRegex, String text, boolean forceCompile) {
if (TextUtils.isEmpty(text)) {
return false;
}

if (matchRegex) {
Pattern pattern = null;
if (!forceCompile) {
Expand Down Expand Up @@ -248,14 +244,16 @@ public Pattern compile(String rawPattern) {
}

try {
pattern = Pattern.compile(isRegex.group(1), flags);
//Don't allow an empty regex string (would match everything)
pattern = isRegex.group(1).length() > 0 ? Pattern.compile(isRegex.group(1), flags) : null;
} catch (PatternSyntaxException e) {
return null;
}
} else if (rawPattern.length() >= 2 && rawPattern.charAt(0) == '"' && rawPattern.charAt(rawPattern.length() - 1) == '"') {
// "matches an exact sentence"
String text = escapeRegex(rawPattern.substring(1, rawPattern.length() - 1));
pattern = Pattern.compile(text, Pattern.CASE_INSENSITIVE);
//Don't allow only double quotes (would match everything)
pattern = rawPattern.length() != 2 ? Pattern.compile(text, Pattern.CASE_INSENSITIVE) : null;
} else {
String[] words = rawPattern.split(" ");
String text = "";
Expand All @@ -268,8 +266,8 @@ public Pattern compile(String rawPattern) {
text += "|";
}
}

pattern = Pattern.compile(text, Pattern.CASE_INSENSITIVE);
//Don't allow only spaces (would match everything after split)
pattern = !TextUtils.isEmpty(text) ? Pattern.compile(text.toString(), Pattern.CASE_INSENSITIVE) : null;
}

return pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
public class FilterLayout extends LinearLayout implements View.OnClickListener {
private TextView typeText;
private TextView boardsSelector;
private boolean patternContainerErrorShowing = false;
private TextView pattern;
private TextView patternPreview;
private TextView patternPreviewStatus;
Expand Down Expand Up @@ -341,11 +340,7 @@ public void onClick(DialogInterface dialog, int which) {

private void updateFilterValidity() {
boolean valid = !TextUtils.isEmpty(filter.pattern) && filterEngine.compile(filter.pattern) != null;

if (valid != patternContainerErrorShowing) {
patternContainerErrorShowing = valid;
pattern.setError(valid ? null : getString(R.string.filter_invalid_pattern));
}
pattern.setError(valid ? null : getString(R.string.filter_invalid_pattern));

if (callback != null) {
callback.setSaveButtonEnabled(valid);
Expand Down Expand Up @@ -385,7 +380,7 @@ private void updateFilterType() {

private void updatePatternPreview() {
String text = patternPreview.getText().toString();
boolean matches = text.length() > 0 && filterEngine.matches(filter, true, text, true);
boolean matches = filterEngine.matches(filter, true, text, true);
patternPreviewStatus.setText(matches ? R.string.filter_matches : R.string.filter_no_matches);
}

Expand Down

0 comments on commit 475ca7b

Please sign in to comment.