Use regular expressions to match filenames.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jan 2014 13:04:52 +0000 (14:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jan 2014 13:04:52 +0000 (14:04 +0100)
src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java

index ad1d9d7..170365a 100644 (file)
@@ -19,12 +19,16 @@ package net.pterodactylus.xdcc.ui.stdin;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Arrays.asList;
+import static java.util.regex.Pattern.CASE_INSENSITIVE;
+import static java.util.regex.Pattern.UNICODE_CASE;
+import static java.util.regex.Pattern.compile;
 
 import java.io.IOException;
 import java.io.Writer;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.regex.Pattern;
 
 import net.pterodactylus.xdcc.core.Core;
 import net.pterodactylus.xdcc.data.Bot;
@@ -88,21 +92,20 @@ public class SearchCommand implements Command {
        private static class SearchParameter {
 
                private final boolean exclude;
-               private final String pattern;
+               private final Pattern pattern;
 
-               private SearchParameter(boolean exclude, String pattern) {
+               private SearchParameter(boolean exclude, Pattern pattern) {
                        this.exclude = exclude;
                        this.pattern = pattern;
                }
 
                public boolean matches(String filename) {
-                       boolean matches = filename.toLowerCase().contains(pattern.toLowerCase());
-                       return matches && !exclude;
+                       return pattern.matcher(filename).find() && !exclude;
                }
 
                public static SearchParameter from(String parameter) {
                        boolean exclude = parameter.startsWith("-");
-                       String pattern = exclude ? parameter.substring(1) : parameter;
+                       Pattern pattern = compile(exclude ? parameter.substring(1) : parameter, CASE_INSENSITIVE | UNICODE_CASE);
                        return new SearchParameter(exclude, pattern);
                }