Package dk.brics.automaton
Class MatchOnlyRunAutomaton
- java.lang.Object
-
- dk.brics.automaton.MatchOnlyRunAutomaton
-
- All Implemented Interfaces:
java.io.Serializable
public class MatchOnlyRunAutomaton extends java.lang.Object implements java.io.SerializableSupports an optimised version of substring matching an automaton against an input string in linear time with no backtracking.In essence we simply take an automaton "mypattern" and match against ".*mypattern". See the docs here from Russ Cox: Link.
As such we do not currently support identifying the start, end position of a match, simply that a match exists.
Execution time is linear to the length of the string O(n) for the method
matches(CharSequence)below.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static intACCEPTED_STATEprivate RunAutomatonautomaton(package private) static longserialVersionUIDprivate static AutomatonWILDCARD_ZERO_OR_MORE
-
Constructor Summary
Constructors Constructor Description MatchOnlyRunAutomaton(Automaton a)MatchOnlyRunAutomaton(Automaton a, boolean tableize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static AutomatonaddWildcard(Automaton automaton)static MatchOnlyRunAutomatonload(java.io.InputStream stream)Retrieves a serializedMatchOnlyRunAutomatonfrom a stream.booleanmatches(java.lang.CharSequence input)Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.booleanmatches(java.lang.CharSequence input, int pos)Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.voidstore(java.io.OutputStream stream)Writes thisMatchOnlyRunAutomatonto the given stream.java.lang.StringtoString()
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
automaton
private final RunAutomaton automaton
-
WILDCARD_ZERO_OR_MORE
private static final Automaton WILDCARD_ZERO_OR_MORE
-
ACCEPTED_STATE
private static final int ACCEPTED_STATE
- See Also:
- Constant Field Values
-
-
Method Detail
-
matches
public boolean matches(java.lang.CharSequence input)
Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.Has the following constraints:
- In the worst case, the execution is equivalent to the length of the input O(n) as we scan the whole input.
- There is no backtracking, we simply scan the input char by char until we either match or reach the end.
- Method returns immediately if it finds enough chars to complete a match of the pattern.
- The method and class has no ability to locate the position of the match, simply that is either exists or not.
- Parameters:
input- the input to match a substring pattern against- Returns:
trueif the pattern is found in a substring in the input, orfalseif the input is not matched. We also return false for null inputs.
-
matches
public boolean matches(java.lang.CharSequence input, int pos)Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.See
matches(CharSequence)for more information.This version of the method searches from a specific starting position.
- Parameters:
input- the input to match a substring pattern againstpos- the position to start searching from- Returns:
trueif the pattern is found in a substring in the input, orfalseif the input is not matched. We also return false for null inputs- Throws:
java.lang.IllegalArgumentException- if the pos is negative or larger than the input length.
-
load
public static MatchOnlyRunAutomaton load(java.io.InputStream stream) throws java.io.IOException, java.lang.ClassCastException, java.lang.ClassNotFoundException
Retrieves a serializedMatchOnlyRunAutomatonfrom a stream.- Parameters:
stream- input stream with serialized automaton- Throws:
java.io.IOException- if input/output related exception occursjava.lang.ClassCastException- if the data is not a serializedMatchOnlyRunAutomatonjava.lang.ClassNotFoundException- if the class of the serialized object cannot be found
-
store
public void store(java.io.OutputStream stream) throws java.io.IOExceptionWrites thisMatchOnlyRunAutomatonto the given stream.- Parameters:
stream- output stream for serialized automaton- Throws:
java.io.IOException- if input/output related exception occurs
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-