Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added case sensitive string matcher to BasicAutomata. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/dk/brics/automaton/BasicAutomata.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,33 @@ public static Automaton makeInterval(int min, int max, int digits) throws Illega
* Returns a new (deterministic) automaton that accepts the single given string.
*/
public static Automaton makeString(String s) {
return makeString(s, true);
}

/**
* Returns a new (deterministic) automaton that accepts the single given string.
*/
public static Automaton makeString(String s, boolean caseSensitive) {
Automaton a = new Automaton();
a.singleton = s;
a.deterministic = true;
if (caseSensitive) {
a.singleton = s;
a.deterministic = true;
} else {
State currentStat = a.initial;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
State nextState = new State();
currentStat.transitions.add(new Transition(c, nextState));
if (Character.isLowerCase(c)) {
currentStat.transitions.add(new Transition(Character.toUpperCase(c), nextState));
} else if (Character.isUpperCase(c)) {
currentStat.transitions.add(new Transition(Character.toLowerCase(c), nextState));
}
currentStat = nextState;
}
currentStat.setAccept(true);
a.deterministic = true;
}
return a;
}

Expand Down Expand Up @@ -439,6 +463,13 @@ public static Automaton makeDecimalValue(String value) {
* Constructs deterministic automaton that matches strings that contain the given substring.
*/
public static Automaton makeStringMatcher(String s) {
return makeStringMatcher(s, true);
}

/**
* Constructs deterministic automaton that matches strings that contain the given substring.
*/
public static Automaton makeStringMatcher(String s, boolean caseSensitive) {
Automaton a = new Automaton();
State[] states = new State[s.length() + 1];
states[0] = a.initial;
Expand All @@ -452,11 +483,29 @@ public static Automaton makeStringMatcher(String s) {
char c = s.charAt(i);
states[i].transitions.add(new Transition(c, states[i+1]));
done.add(c);
if (!caseSensitive) {
if (Character.isLowerCase(c)) {
states[i].transitions.add(new Transition(Character.toUpperCase(c), states[i + 1]));
done.add(Character.toUpperCase(c));
} else if (Character.isUpperCase(c)) {
states[i].transitions.add(new Transition(Character.toLowerCase(c), states[i + 1]));
done.add(Character.toLowerCase(c));
}
}
for (int j = i; j >= 1; j--) {
char d = s.charAt(j-1);
if (!done.contains(d) && s.substring(0, j-1).equals(s.substring(i-j+1, i))) {
states[i].transitions.add(new Transition(d, states[j]));
done.add(d);
if (!caseSensitive) {
if (Character.isLowerCase(d)) {
states[i].transitions.add(new Transition(Character.toUpperCase(d), states[j]));
done.add(Character.toUpperCase(d));
} else if (Character.isUpperCase(d)) {
states[i].transitions.add(new Transition(Character.toLowerCase(d), states[j]));
done.add(Character.toLowerCase(d));
}
}
}
}
char[] da = new char[done.size()];
Expand Down