Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions src/main/java/at/favre/lib/bytes/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,20 @@ public int indexOf(byte target, int fromIndex) {
return indexOf(new byte[]{target}, fromIndex);
}

/**
* Returns the index of the first appearance of the value {@code target} in
* {@code array} from given start index 'fromIndex' to given end index 'toIndex'.
*
* @param target a primitive {@code byte} value
* @param fromIndex search from this index
* @param toIndex search to this index
* @return the least index {@code i} for which {@code array[i] == target}, or
* {@code -1} if no such index exists or fromIndex is gt target length.
*/
public int indexOf(byte target, int fromIndex, int toIndex) {
return indexOf(new byte[]{target}, fromIndex, toIndex);
}

/**
* Returns the start position of the first occurrence of the specified {@code
* target} within {@code array}, or {@code -1} if there is no such occurrence.
Expand Down Expand Up @@ -1311,6 +1325,25 @@ public int indexOf(byte[] subArray, int fromIndex) {
return Util.Byte.indexOf(internalArray(), subArray, fromIndex, length());
}

/**
* Returns the start position of the first occurrence of the specified {@code
* target} within {@code array} from given start index 'fromIndex' to given end
* index 'toIndex', or {@code -1} if there is no such occurrence.
* <p>
* More formally, returns the lowest index {@code i} such that {@code
* java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
* the same elements as {@code target}.
*
* @param subArray the array to search for as a sub-sequence of {@code array}
* @param fromIndex search from this index
* @param toIndex search to this index
* @return the least index {@code i} for which {@code array[i] == target}, or
* {@code -1} if no such index exists.
*/
public int indexOf(byte[] subArray, int fromIndex, int toIndex) {
return Util.Byte.indexOf(internalArray(), subArray, fromIndex, toIndex);
}

/**
* Checks if the given sub array is equal to the start of given array. That is, sub array must be gt or eq
* to the length of the internal array and <code>internal[i] == subArray[i]</code> for i=0..subArray.length-1
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/at/favre/lib/bytes/BytesMiscTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ public void indexOfByteFromIndex() {
assertEquals(10, Bytes.from(example_bytes_sixteen).indexOf((byte) 0xFD, 5));
}

@Test
public void indexOfByteFromIndexToIndex() {
assertEquals(4, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 0, 7));
assertEquals(4, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 3, 5));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 0, 3));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 6, 7));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0xCA, 0, 7));
}

@Test
public void indexOfArray() {
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}));
Expand All @@ -204,6 +213,16 @@ public void indexOfArrayFromIndex() {
assertEquals(2, Bytes.from(new byte[]{(byte) 0x8E, (byte) 0xD1, (byte) 0x8E, (byte) 0xD1, 0x12, (byte) 0xAF, (byte) 0x78, 0x09, 0x1E, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12}).indexOf(new byte[]{(byte) 0x8E, (byte) 0xD1}, 1));
}

@Test
public void indexOfArrayFromIndexToIndex() {
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 0, 7));
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 3, 5));
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF, (byte) 0xED }, 4, 5));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 0, 3));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 6, 7));
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0xCA, (byte) 0xFE }, 0, 7));
}

@Test
public void startsWidth() {
assertFalse(Bytes.allocate(0).startsWith(new byte[1]));
Expand Down