fixed search term parser for empty terms

This commit is contained in:
Daniel Gultsch 2018-04-30 17:37:39 +02:00
parent 27f31446c0
commit 438ae34988
1 changed files with 20 additions and 2 deletions

View File

@ -34,6 +34,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
public class FtsUtils { public class FtsUtils {
private static List<String> KEYWORDS = Arrays.asList("OR", "AND"); private static List<String> KEYWORDS = Arrays.asList("OR", "AND");
@ -44,10 +45,10 @@ public class FtsUtils {
if (part.isEmpty()) { if (part.isEmpty()) {
continue; continue;
} }
final String cleaned = part.substring(getStartIndex(part), getEndIndex(part) +1); final String cleaned = clean(part);
if (isKeyword(cleaned)) { if (isKeyword(cleaned)) {
term.add(part); term.add(part);
} else { } else if (!cleaned.isEmpty()) {
term.add(cleaned); term.add(cleaned);
} }
} }
@ -76,9 +77,13 @@ public class FtsUtils {
} }
private static int getStartIndex(String term) { private static int getStartIndex(String term) {
int length = term.length();
int index = 0; int index = 0;
while (term.charAt(index) == '*') { while (term.charAt(index) == '*') {
++index; ++index;
if (index >= length) {
break;
}
} }
return index; return index;
} }
@ -87,8 +92,21 @@ public class FtsUtils {
int index = term.length() - 1; int index = term.length() - 1;
while (term.charAt(index) == '*') { while (term.charAt(index) == '*') {
--index; --index;
if (index < 0) {
break;
}
} }
return index; return index;
} }
private static String clean(String input) {
int begin = getStartIndex(input);
int end = getEndIndex(input);
if (begin > end) {
return "";
} else {
return input.substring(begin, end + 1);
}
}
} }