I am writting a regex parser for which, a loop-switch anti-pattern appears to be the only approach. Please ignore actual rules of parsing, as this parsing-logic is just custom made for an internal application, and deviant from common use.
public static boolean match(String regex, String str) {
int i = 0;
int j = 0;
while ((i < regex.length() && j < str.length())) {
switch(regex.charAt(i)) {
case '.' : i++; j++; break;
case '*' : // do something ; break
default : if (regex.charAt(i) != str.charAt(j)) { return false; } else {i++; j++};
}
}
}
How can the loop-switch be prevented in such a case ? Is there any design pattern meant for such a purpose ?