Skip to content

Commit 966feae

Browse files
committed
Add toIndex versions for indexOf()
1 parent 6fbe826 commit 966feae

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,20 @@ public int indexOf(byte target, int fromIndex) {
12771277
return indexOf(new byte[]{target}, fromIndex);
12781278
}
12791279

1280+
/**
1281+
* Returns the index of the first appearance of the value {@code target} in
1282+
* {@code array} from given start index 'fromIndex' to given end index 'toIndex'.
1283+
*
1284+
* @param target a primitive {@code byte} value
1285+
* @param fromIndex search from this index
1286+
* @param toIndex search to this index
1287+
* @return the least index {@code i} for which {@code array[i] == target}, or
1288+
* {@code -1} if no such index exists or fromIndex is gt target length.
1289+
*/
1290+
public int indexOf(byte target, int fromIndex, int toIndex) {
1291+
return indexOf(new byte[]{target}, fromIndex, toIndex);
1292+
}
1293+
12801294
/**
12811295
* Returns the start position of the first occurrence of the specified {@code
12821296
* target} within {@code array}, or {@code -1} if there is no such occurrence.
@@ -1311,6 +1325,25 @@ public int indexOf(byte[] subArray, int fromIndex) {
13111325
return Util.Byte.indexOf(internalArray(), subArray, fromIndex, length());
13121326
}
13131327

1328+
/**
1329+
* Returns the start position of the first occurrence of the specified {@code
1330+
* target} within {@code array} from given start index 'fromIndex' to given end
1331+
* index 'toIndex', or {@code -1} if there is no such occurrence.
1332+
* <p>
1333+
* More formally, returns the lowest index {@code i} such that {@code
1334+
* java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
1335+
* the same elements as {@code target}.
1336+
*
1337+
* @param subArray the array to search for as a sub-sequence of {@code array}
1338+
* @param fromIndex search from this index
1339+
* @param toIndex search to this index
1340+
* @return the least index {@code i} for which {@code array[i] == target}, or
1341+
* {@code -1} if no such index exists.
1342+
*/
1343+
public int indexOf(byte[] subArray, int fromIndex, int toIndex) {
1344+
return Util.Byte.indexOf(internalArray(), subArray, fromIndex, toIndex);
1345+
}
1346+
13141347
/**
13151348
* Checks if the given sub array is equal to the start of given array. That is, sub array must be gt or eq
13161349
* to the length of the internal array and <code>internal[i] == subArray[i]</code> for i=0..subArray.length-1

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ public void indexOfByteFromIndex() {
188188
assertEquals(10, Bytes.from(example_bytes_sixteen).indexOf((byte) 0xFD, 5));
189189
}
190190

191+
@Test
192+
public void indexOfByteFromIndexToIndex() {
193+
assertEquals(4, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 0, 7));
194+
assertEquals(4, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 3, 5));
195+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 0, 3));
196+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0x1E, 6, 7));
197+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0xCA, 0, 7));
198+
}
199+
191200
@Test
192201
public void indexOfArray() {
193202
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}));
@@ -204,6 +213,16 @@ public void indexOfArrayFromIndex() {
204213
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));
205214
}
206215

216+
@Test
217+
public void indexOfArrayFromIndexToIndex() {
218+
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 0, 7));
219+
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 3, 5));
220+
assertEquals(4, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF, (byte) 0xED }, 4, 5));
221+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 0, 3));
222+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0x1E, (byte) 0xAF }, 6, 7));
223+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[] { (byte) 0xCA, (byte) 0xFE }, 0, 7));
224+
}
225+
207226
@Test
208227
public void startsWidth() {
209228
assertFalse(Bytes.allocate(0).startsWith(new byte[1]));

0 commit comments

Comments
 (0)