Skip to content

Commit

Permalink
Allow ByteMatcher to stop at a specified index (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbruton committed Apr 25, 2023
1 parent 44265a6 commit d981a76
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
83 changes: 71 additions & 12 deletions src/main/java/emissary/util/search/ByteMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void resetData(String data, Charset charset) {

/**
* Reset the byte array. Use of this method avoids having to instantiate a new ByteMatcher.
*
*
* @param data - bytes to match against
*/
public void resetData(byte[] data) {
Expand Down Expand Up @@ -68,19 +68,33 @@ public int length() {

/**
* This method finds a pattern in the text and returns the offset
*
*
* @param pattern bytes to find
* @param startOfs start index
*/

public int indexOf(byte[] pattern, int startOfs) {
return indexOf(pattern, startOfs, mydata.length);

}

/**
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return position
*/
public int indexOf(byte[] pattern, int beginIndex, int endIndex) {

// Impossible to find under these conditions
if (mydata == null || startOfs > (mydata.length - pattern.length))
if (mydata == null || beginIndex > (mydata.length - pattern.length) || endIndex > mydata.length)
return NOTFOUND;

// Use the Boyer-Moore scanning algorithm.
return scanner.indexOf(pattern, startOfs);
return scanner.indexOf(pattern, beginIndex, endIndex);

}

Expand Down Expand Up @@ -125,7 +139,7 @@ public byte byteAt(int i) {

/**
* Return a slice
*
*
* @param start index to start
* @param end index one past the end of desired range
* @return array slice
Expand Down Expand Up @@ -156,31 +170,45 @@ public boolean startsWith(String s) {
return true;
}


/**
* This method finds a pattern in the text and returns the offset ignoring upper/lower case
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset ignoring
* upper/lower case
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return position
*/

public int indexIgnoreCase(byte[] pattern, int startOfs) {
public int indexIgnoreCase(byte[] pattern, int beginIndex, int endIndex) {

// Impossible to find under these conditions
if (mydata == null || startOfs > (mydata.length - pattern.length)) {
if (mydata == null || beginIndex > (mydata.length - pattern.length) || endIndex > mydata.length) {
return NOTFOUND;
}

int matchPos = NOTFOUND;
int matchPos;

// Use the Boyer-Moore scanner. Set it to
// ignore case.

scanner.setCaseSensitive(false);
matchPos = scanner.indexOf(pattern, startOfs);
matchPos = scanner.indexOf(pattern, beginIndex, endIndex);

// Reset scanner to default state.
scanner.setCaseSensitive(true);

return matchPos;


}

/**
* This method finds a pattern in the text and returns the offset ignoring upper/lower case
*/

public int indexIgnoreCase(byte[] pattern, int startOfs) {
return indexIgnoreCase(pattern, startOfs, mydata.length);
}


Expand All @@ -193,6 +221,21 @@ public int indexOf(byte[] pattern) {

}

/**
* Match pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return position
*/
public int indexOf(String pattern, int beginIndex, int endIndex) {

return indexOf(pattern.getBytes(), beginIndex, endIndex);

}

/**
* Match pattern in the text beginning at startOfs
*/
Expand Down Expand Up @@ -223,6 +266,22 @@ public int indexIgnoreCase(String pattern, int startOfs) {

}

/**
* Match pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset ignoring upper/lower
* case
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return position
*/
public int indexIgnoreCase(String pattern, int beginIndex, int endIndex) {

return indexIgnoreCase(pattern.getBytes(), beginIndex, endIndex);

}

public int indexIgnoreCase(byte[] pattern) {

return indexIgnoreCase(pattern, 0);
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/emissary/util/search/ByteMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ void testNotFound() {
assertEquals(this.data.indexOf("llama"), this.b.indexOf("llama"), "Match pos same as string when not found");
}

@Test
void testIndexOfBytesExcludedByEndIndex() {
assertEquals(ByteMatcher.NOTFOUND, this.b.indexOf("dog".getBytes(), 0, b.length() - 1), "Match pos not found");
}

@Test
void testIndexOfBytesIncludedWithEndIndex() {
assertEquals(this.data.indexOf("dog"), this.b.indexOf("dog".getBytes(), 0, b.length()), "Match pos same as string");
}

@Test
void testIndexOfExcludedByEndIndex() {
assertEquals(ByteMatcher.NOTFOUND, this.b.indexOf("dog", 0, b.length() - 1), "Match pos not found");
}

@Test
void testIndexOfIncludedWithEndIndex() {
assertEquals(this.data.indexOf("dog"), this.b.indexOf("dog", 0, b.length()), "Match pos same as string");
}

@Test
void testLength() {
assertEquals(this.data.length(), this.b.length(), "Length same as string");
Expand Down Expand Up @@ -210,6 +230,16 @@ void testIgnoreCaseScanWithOffset() {
assertEquals(this.data.indexOf("fox"), this.b.indexIgnoreCase("foX", 0), "Pos in case insensitive search");
}

@Test
void testIndexIgnoreCaseScanExcludedByEndIndex() {
assertEquals(ByteMatcher.NOTFOUND, this.b.indexIgnoreCase("Dog", 0, b.length() - 1), "Match pos not found");
}

@Test
void testIndexIgnoreCaseScanIncludedWithEndIndex() {
assertEquals(this.data.indexOf("dog"), this.b.indexIgnoreCase("Dog", 0, b.length()), "Match pos same as string");
}

@Test
void testBadConditionsOnIndexOf() {
assertEquals(-1, this.b.indexOf("lazy", this.data.length() + 5), "Bad startOfs gives not found");
Expand Down

0 comments on commit d981a76

Please sign in to comment.