Package org.eclipse.jetty.util
Class SearchPattern
java.lang.Object
org.eclipse.jetty.util.SearchPattern
SearchPattern
Fast search for patterns within strings and arrays of bytes.
Uses an implementation of the Boyer–Moore–Horspool algorithm
with a 256 character alphabet.
The algorithm has an average-case complexity of O(n)
on random text and O(nm) in the worst case.
where:
m = pattern length
n = length of data to search
-
Method Summary
Modifier and TypeMethodDescriptionstatic SearchPattern
compile
(byte[] pattern) Produces a SearchPattern instance which can be used to find matches of the pattern in datastatic SearchPattern
Produces a SearchPattern instance which can be used to find matches of the pattern in dataint
endsWith
(byte[] data, int offset, int length) Search for a partial match of the pattern at the end of the data.int
int
match
(byte[] data, int offset, int length) Search for a complete match of the pattern within the dataint
startsWith
(byte[] data, int offset, int length, int matched) Search for a possibly partial match of the pattern at the start of the data.
-
Method Details
-
compile
Produces a SearchPattern instance which can be used to find matches of the pattern in data- Parameters:
pattern
- byte array containing the pattern- Returns:
- a new SearchPattern instance using the given pattern
-
compile
Produces a SearchPattern instance which can be used to find matches of the pattern in data- Parameters:
pattern
- string containing the pattern- Returns:
- a new SearchPattern instance using the given pattern
-
match
public int match(byte[] data, int offset, int length) Search for a complete match of the pattern within the data- Parameters:
data
- The data in which to search for. The data may be arbitrary binary data, but the pattern will always beStandardCharsets.US_ASCII
encoded.offset
- The offset within the data to start the searchlength
- The length of the data to search- Returns:
- The index within the data array at which the first instance of the pattern or -1 if not found
-
endsWith
public int endsWith(byte[] data, int offset, int length) Search for a partial match of the pattern at the end of the data.- Parameters:
data
- The data in which to search for. The data may be arbitrary binary data, but the pattern will always beStandardCharsets.US_ASCII
encoded.offset
- The offset within the data to start the searchlength
- The length of the data to search- Returns:
- the length of the partial pattern matched and 0 for no match.
-
startsWith
public int startsWith(byte[] data, int offset, int length, int matched) Search for a possibly partial match of the pattern at the start of the data.- Parameters:
data
- The data in which to search for. The data may be arbitrary binary data, but the pattern will always beStandardCharsets.US_ASCII
encoded.offset
- The offset within the data to start the searchlength
- The length of the data to searchmatched
- The length of the partial pattern already matched- Returns:
- the length of the partial pattern matched and 0 for no match.
-
getLength
public int getLength()- Returns:
- The length of the pattern in bytes.
-