Skip to content

Commit f901085

Browse files
committed
Add support from indexOf() with fromIndex for array search
fix #14
1 parent 344cc4a commit f901085

4 files changed

Lines changed: 50 additions & 56 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ public int indexOf(byte target) {
11181118
* {@code -1} if no such index exists or fromIndex is gt target length.
11191119
*/
11201120
public int indexOf(byte target, int fromIndex) {
1121-
return Util.indexOf(internalArray(), target, fromIndex, length());
1121+
return Util.indexOf(internalArray(), new byte[]{target}, fromIndex);
11221122
}
11231123

11241124
/**
@@ -1134,7 +1134,25 @@ public int indexOf(byte target, int fromIndex) {
11341134
* {@code -1} if no such index exists.
11351135
*/
11361136
public int indexOf(byte[] subArray) {
1137-
return Util.indexOf(internalArray(), subArray);
1137+
return Util.indexOf(internalArray(), subArray, 0);
1138+
}
1139+
1140+
/**
1141+
* Returns the start position of the first occurrence of the specified {@code
1142+
* target} within {@code array} from given start index 'fromIndex', or {@code -1}
1143+
* if there is no such occurrence.
1144+
* <p>
1145+
* More formally, returns the lowest index {@code i} such that {@code
1146+
* java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
1147+
* the same elements as {@code target}.
1148+
*
1149+
* @param subArray the array to search for as a sub-sequence of {@code array}
1150+
* @param fromIndex search from this index
1151+
* @return the least index {@code i} for which {@code array[i] == target}, or
1152+
* {@code -1} if no such index exists.
1153+
*/
1154+
public int indexOf(byte[] subArray, int fromIndex) {
1155+
return Util.indexOf(internalArray(), subArray, fromIndex);
11381156
}
11391157

11401158
/**

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

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,10 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayOutputStream;
25-
import java.io.DataInput;
26-
import java.io.File;
27-
import java.io.IOException;
28-
import java.io.InputStream;
24+
import java.io.*;
2925
import java.nio.ByteBuffer;
3026
import java.nio.file.Files;
31-
import java.util.ArrayList;
32-
import java.util.Collection;
33-
import java.util.HashMap;
34-
import java.util.Iterator;
35-
import java.util.List;
36-
import java.util.Map;
37-
import java.util.NoSuchElementException;
38-
import java.util.Objects;
39-
import java.util.Random;
40-
import java.util.UUID;
27+
import java.util.*;
4128

4229
/**
4330
* Common Util methods to convert or modify byte arrays
@@ -69,24 +56,6 @@ static byte[] concat(byte[]... arrays) {
6956
return result;
7057
}
7158

72-
/**
73-
* Returns the index of the first appearance of the value {@code target} in
74-
* {@code array}.
75-
*
76-
* @param array an array of {@code byte} values, possibly empty
77-
* @param target a primitive {@code byte} value
78-
* @return the least index {@code i} for which {@code array[i] == target}, or
79-
* {@code -1} if no such index exists.
80-
*/
81-
static int indexOf(byte[] array, byte target, int start, int end) {
82-
for (int i = start; i < end; i++) {
83-
if (array[i] == target) {
84-
return i;
85-
}
86-
}
87-
return -1;
88-
}
89-
9059
/**
9160
* Returns the start position of the first occurrence of the specified {@code
9261
* target} within {@code array}, or {@code -1} if there is no such occurrence.
@@ -98,15 +67,15 @@ static int indexOf(byte[] array, byte target, int start, int end) {
9867
* @param array the array to search for the sequence {@code target}
9968
* @param target the array to search for as a sub-sequence of {@code array}
10069
*/
101-
public static int indexOf(byte[] array, byte[] target) {
70+
static int indexOf(byte[] array, byte[] target, int start) {
10271
Objects.requireNonNull(array, "array must not be null");
10372
Objects.requireNonNull(target, "target must not be null");
10473
if (target.length == 0) {
105-
return 0;
74+
return -1;
10675
}
10776

10877
outer:
109-
for (int i = 0; i < array.length - target.length + 1; i++) {
78+
for (int i = start; i < array.length - target.length + 1; i++) {
11079
for (int j = 0; j < target.length; j++) {
11180
if (array[i + j] != target[j]) {
11281
continue outer;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,22 @@ public void indexOfByteFromIndex() {
174174
assertEquals(10, Bytes.from(example_bytes_sixteen).indexOf((byte) 0xFD, 5));
175175
}
176176

177-
178177
@Test
179178
public void indexOfArray() {
180179
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}));
181-
assertEquals(0, Bytes.allocate(1).indexOf(new byte[0]));
180+
assertEquals(-1, Bytes.allocate(1).indexOf(new byte[0]));
182181
assertEquals(2, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0xFF}));
183182
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0x00}));
184183
}
185184

185+
@Test
186+
public void indexOfArrayFromIndex() {
187+
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}, 0));
188+
assertEquals(-1, Bytes.allocate(1).indexOf(new byte[0], 0));
189+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0xFF}, 8));
190+
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));
191+
}
192+
186193
@Test
187194
public void lastIndexOf() {
188195
assertEquals(-1, Bytes.allocate(0).lastIndexOf((byte) 0xFD));

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,39 @@ public void testIndexOf() {
5353
}
5454

5555
private int indexOf(byte[] empty, byte b) {
56-
return Util.indexOf(empty, b, 0, empty.length);
56+
return Util.indexOf(empty, new byte[]{b}, 0);
5757
}
5858

5959
@Test
6060
public void testIndexOf_arrayTarget() {
61-
assertEquals(0, Util.indexOf(EMPTY, EMPTY));
62-
assertEquals(0, Util.indexOf(ARRAY234, EMPTY));
63-
assertEquals(-1, Util.indexOf(EMPTY, ARRAY234));
64-
assertEquals(-1, Util.indexOf(ARRAY234, ARRAY1));
65-
assertEquals(-1, Util.indexOf(ARRAY1, ARRAY234));
66-
assertEquals(0, Util.indexOf(ARRAY1, ARRAY1));
67-
assertEquals(0, Util.indexOf(ARRAY234, ARRAY234));
61+
assertEquals(-1, Util.indexOf(EMPTY, EMPTY, 0));
62+
assertEquals(-1, Util.indexOf(ARRAY234, EMPTY, 0));
63+
assertEquals(-1, Util.indexOf(EMPTY, ARRAY234, 0));
64+
assertEquals(-1, Util.indexOf(ARRAY234, ARRAY1, 0));
65+
assertEquals(-1, Util.indexOf(ARRAY1, ARRAY234, 0));
66+
assertEquals(0, Util.indexOf(ARRAY1, ARRAY1, 0));
67+
assertEquals(0, Util.indexOf(ARRAY234, ARRAY234, 0));
6868
assertEquals(0, Util.indexOf(
69-
ARRAY234, new byte[]{(byte) 2, (byte) 3}));
69+
ARRAY234, new byte[]{(byte) 2, (byte) 3}, 0));
7070
assertEquals(1, Util.indexOf(
71-
ARRAY234, new byte[]{(byte) 3, (byte) 4}));
72-
assertEquals(1, Util.indexOf(ARRAY234, new byte[]{(byte) 3}));
73-
assertEquals(2, Util.indexOf(ARRAY234, new byte[]{(byte) 4}));
74-
assertEquals(1, Util.indexOf(new byte[]{(byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3}, new byte[]{(byte) 3}));
71+
ARRAY234, new byte[]{(byte) 3, (byte) 4}, 0));
72+
assertEquals(1, Util.indexOf(ARRAY234, new byte[]{(byte) 3}, 0));
73+
assertEquals(2, Util.indexOf(ARRAY234, new byte[]{(byte) 4}, 0));
74+
assertEquals(1, Util.indexOf(new byte[]{(byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3}, new byte[]{(byte) 3}, 0));
7575
assertEquals(2, Util.indexOf(
7676
new byte[]{(byte) 2, (byte) 3, (byte) 2,
7777
(byte) 3, (byte) 4, (byte) 2, (byte) 3},
7878
new byte[]{(byte) 2, (byte) 3, (byte) 4}
79-
));
79+
, 0));
8080
assertEquals(1, Util.indexOf(
8181
new byte[]{(byte) 2, (byte) 2, (byte) 3,
8282
(byte) 4, (byte) 2, (byte) 3, (byte) 4},
8383
new byte[]{(byte) 2, (byte) 3, (byte) 4}
84-
));
84+
, 0));
8585
assertEquals(-1, Util.indexOf(
8686
new byte[]{(byte) 4, (byte) 3, (byte) 2},
8787
new byte[]{(byte) 2, (byte) 3, (byte) 4}
88-
));
88+
, 0));
8989
}
9090

9191
@Test

0 commit comments

Comments
 (0)