Skip to content

Commit

Permalink
change return type and groupings
Browse files Browse the repository at this point in the history
  • Loading branch information
nixon124 committed Oct 21, 2024
1 parent 819643f commit 21b999e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 160 deletions.
217 changes: 109 additions & 108 deletions src/main/java/emissary/util/search/ByteMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

/**
Expand All @@ -17,7 +18,6 @@ public class ByteMatcher {
private KeywordScanner scanner = null;

public static final int NOTFOUND = -1;
public static final ArrayList<Integer> EMPTYLIST = new ArrayList<>();

public ByteMatcher() {
this(new byte[0]);
Expand Down Expand Up @@ -87,21 +87,6 @@ public int indexOf(byte[] pattern, int startOfs) {

}

/**
* This method finds a pattern in the text from {@code startOfs} and returns a list of offsets
*
* @param pattern bytes to find
* @param startOfs start index
*/
public ArrayList<Integer> listIndexOf(byte[] pattern, int startOfs) {

if (mydata == null) {
return EMPTYLIST;
}

return listIndexOf(pattern, startOfs, mydata.length);
}

/**
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset
*
Expand All @@ -123,6 +108,15 @@ public int indexOf(byte[] pattern, int beginIndex, int endIndex) {

}

/**
* Match pattern in the text
*/
public int indexOf(byte[] pattern) {

return indexOf(pattern, 0);

}

/**
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset list
*
Expand All @@ -132,23 +126,30 @@ public int indexOf(byte[] pattern, int beginIndex, int endIndex) {
*
* @return list of positions
*/
public ArrayList<Integer> listIndexOf(byte[] pattern, int beginIndex, int endIndex) {
public List<Integer> listIndexOf(byte[] pattern, int beginIndex, int endIndex) {

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

return scanner.listIndexOf(pattern, beginIndex, endIndex);
}


/**
* Match pattern in the text
* This method finds a pattern in the text from {@code startOfs} and returns a list of offsets
*
* @param pattern bytes to find
* @param startOfs start index
*/
public int indexOf(byte[] pattern) {
public List<Integer> listIndexOf(byte[] pattern, int startOfs) {

return indexOf(pattern, 0);
if (mydata == null) {
return Collections.emptyList();
}

return listIndexOf(pattern, startOfs, mydata.length);
}

/**
Expand All @@ -157,7 +158,7 @@ public int indexOf(byte[] pattern) {
* @param pattern bytes to find
* @return list of positions
*/
public ArrayList<Integer> listIndexOf(byte[] pattern) {
public List<Integer> listIndexOf(byte[] pattern) {
return listIndexOf(pattern, 0);
}

Expand All @@ -177,47 +178,47 @@ public int indexOf(String pattern, int beginIndex, int endIndex) {
}

/**
* Match pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset list
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return list of positions
* Match pattern in the text beginning at startOfs
*/
public ArrayList<Integer> listIndexOf(String pattern, int beginIndex, int endIndex) {
public int indexOf(String pattern, int startOfs) {

return indexOf(pattern.getBytes(), startOfs);

return listIndexOf(pattern.getBytes(), beginIndex, endIndex);
}

/**
* Match pattern in the text beginning at startOfs
* Match pattern in the text
*/
public int indexOf(String pattern, int startOfs) {
public int indexOf(String pattern) {

return indexOf(pattern.getBytes(), startOfs);
return indexOf(pattern.getBytes(), 0);

}

/**
* Match pattern in the text beginning at {@code startOfs}
* Match pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset list
*
* @param pattern bytes to find
* @param startOfs start index
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return list of positions
*/
public ArrayList<Integer> listIndexOf(String pattern, int startOfs) {
public List<Integer> listIndexOf(String pattern, int beginIndex, int endIndex) {

return listIndexOf(pattern.getBytes(), startOfs);
return listIndexOf(pattern.getBytes(), beginIndex, endIndex);
}

/**
* Match pattern in the text
* Match pattern in the text beginning at {@code startOfs}
*
* @param pattern bytes to find
* @param startOfs start index
* @return list of positions
*/
public int indexOf(String pattern) {

return indexOf(pattern.getBytes(), 0);
public List<Integer> listIndexOf(String pattern, int startOfs) {

return listIndexOf(pattern.getBytes(), startOfs);
}

/**
Expand All @@ -226,7 +227,7 @@ public int indexOf(String pattern) {
* @param pattern bytes to find
* @return list of positions
*/
public ArrayList<Integer> listIndexOf(String pattern) {
public List<Integer> listIndexOf(String pattern) {

return listIndexOf(pattern.getBytes(), 0);
}
Expand Down Expand Up @@ -335,32 +336,6 @@ public int indexIgnoreCase(byte[] pattern, int beginIndex, int endIndex) {

}

/**
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset list
* ignoring upper/lower case
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return list of positions
*/
public ArrayList<Integer> indexListIgnoreCase(byte[] pattern, int beginIndex, int endIndex) {

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

scanner.setCaseSensitive(false);
ArrayList<Integer> matchPosList = scanner.listIndexOf(pattern, beginIndex, endIndex);

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

return matchPosList;
}

/**
* This method finds a pattern in the text and returns the offset ignoring upper/lower case
*/
Expand All @@ -374,20 +349,10 @@ public int indexIgnoreCase(byte[] pattern, int startOfs) {
return indexIgnoreCase(pattern, startOfs, mydata.length);
}

/**
* This method finds a pattern in the text and returns the offset list ignoring upper/lower case
*
* @param pattern bytes to find
* @param startOfs start index
* @return list of positions
*/
public ArrayList<Integer> indexListIgnoreCase(byte[] pattern, int startOfs) {
public int indexIgnoreCase(byte[] pattern) {

if (mydata == null) {
return EMPTYLIST;
}
return indexIgnoreCase(pattern, 0);

return indexListIgnoreCase(pattern, startOfs, mydata.length);
}

public int indexIgnoreCase(String pattern) {
Expand All @@ -396,49 +361,79 @@ public int indexIgnoreCase(String pattern) {

}

public int indexIgnoreCase(String pattern, int startOfs) {

return indexIgnoreCase(pattern.getBytes(), startOfs);

}

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

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

return indexListIgnoreCase(pattern.getBytes(), 0);
}

public int indexIgnoreCase(String pattern, int startOfs) {
/**
* This method finds a pattern in the text from {@code beginIndex} to {@code endIndex} and returns the offset list
* ignoring upper/lower case
*
* @param pattern bytes to find
* @param beginIndex start index
* @param endIndex the index to stop searching at, exclusive
*
* @return list of positions
*/
public List<Integer> indexListIgnoreCase(byte[] pattern, int beginIndex, int endIndex) {

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

scanner.setCaseSensitive(false);
List<Integer> matchPosList = scanner.listIndexOf(pattern, beginIndex, endIndex);

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

return matchPosList;
}

/**
* Match pattern in the text from {@code startOfs} and returns the offset list ignoring upper/lower case
* This method finds a pattern in the text and returns the offset list ignoring upper/lower case
*
* @param pattern bytes to find
* @param startOfs start index
* @return list of positions
*/
public ArrayList<Integer> indexListIgnoreCase(String pattern, int startOfs) {
public List<Integer> indexListIgnoreCase(byte[] pattern, int startOfs) {

return indexListIgnoreCase(pattern.getBytes(), startOfs);
if (mydata == null) {
return Collections.emptyList();
}

return indexListIgnoreCase(pattern, startOfs, mydata.length);
}

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

return indexIgnoreCase(pattern.getBytes(), beginIndex, endIndex);
public List<Integer> indexListIgnoreCase(byte[] pattern) {

return indexListIgnoreCase(pattern, 0);
}

/**
Expand All @@ -451,26 +446,32 @@ public int indexIgnoreCase(String pattern, int beginIndex, int endIndex) {
*
* @return list of positions
*/
public ArrayList<Integer> indexListIgnoreCase(String pattern, int beginIndex, int endIndex) {
public List<Integer> indexListIgnoreCase(String pattern, int beginIndex, int endIndex) {

return indexListIgnoreCase(pattern.getBytes(), beginIndex, endIndex);
}

public int indexIgnoreCase(byte[] pattern) {

return indexIgnoreCase(pattern, 0);
/**
* Match pattern in the text from {@code startOfs} and returns the offset list ignoring upper/lower case
*
* @param pattern bytes to find
* @param startOfs start index
* @return list of positions
*/
public List<Integer> indexListIgnoreCase(String pattern, int startOfs) {

return indexListIgnoreCase(pattern.getBytes(), startOfs);
}

/**
* Match pattern in the test and returns the offset list ignoring upper/lower case
*
* Match pattern in the text
*
* @param pattern bytes to find
* @return list of positions
*/
public ArrayList<Integer> indexListIgnoreCase(byte[] pattern) {
public List<Integer> indexListIgnoreCase(String pattern) {

return indexListIgnoreCase(pattern, 0);
return indexListIgnoreCase(pattern.getBytes(), 0);
}

/**
Expand Down
Loading

0 comments on commit 21b999e

Please sign in to comment.