-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathLikeImplementationHelper.java
More file actions
57 lines (52 loc) · 1.78 KB
/
LikeImplementationHelper.java
File metadata and controls
57 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package sqlancer;
public final class LikeImplementationHelper {
private LikeImplementationHelper() {
}
public static boolean match(String str, String regex, int regexPosition, int strPosition, boolean caseSensitive) {
if (strPosition == str.length() && regexPosition == regex.length()) {
return true;
}
if (regexPosition >= regex.length()) {
return false;
}
char cur = regex.charAt(regexPosition);
if (strPosition >= str.length()) {
if (cur == '%') {
return match(str, regex, regexPosition + 1, strPosition, caseSensitive);
} else {
return false;
}
}
switch (cur) {
case '%':
// match
boolean foundMatch = match(str, regex, regexPosition, strPosition + 1, caseSensitive);
if (!foundMatch) {
return match(str, regex, regexPosition + 1, strPosition, caseSensitive);
} else {
return true;
}
case '_':
return match(str, regex, regexPosition + 1, strPosition + 1, caseSensitive);
default:
boolean charMatches;
if (!caseSensitive) {
charMatches = toUpper(cur) == toUpper(str.charAt(strPosition));
} else {
charMatches = cur == str.charAt(strPosition);
}
if (charMatches) {
return match(str, regex, regexPosition + 1, strPosition + 1, caseSensitive);
} else {
return false;
}
}
}
private static char toUpper(char cur) {
if (cur >= 'a' && cur <= 'z') {
return (char) (cur + 'A' - 'a');
} else {
return cur;
}
}
}