Skip to content

Commit 60b899b

Browse files
committed
Add startsWith() and endsWidth() methods
refs #12
1 parent 1c9a9e5 commit 60b899b

File tree

6 files changed

+75
-22
lines changed

6 files changed

+75
-22
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* add indexOf() with `fromIndex` parameter #14
99
* add support for base64 url safe encoding #15
1010
* use EMPTY constant instance for empty byte array to safe memory #16
11+
* add `startsWith()` and `endsWidth()` methods #12
1112

1213
## v0.6.0
1314

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,11 @@ Finding occurrence of specific bytes:
352352
```java
353353
Bytes.wrap(array).contains((byte) 0xE1);
354354
Bytes.wrap(array).indexOf((byte) 0xFD);
355+
Bytes.wrap(array).indexOf(new byte[] {(byte) 0xFD, 0x23});
355356
Bytes.wrap(array).indexOf((byte) 0xFD, 5); //search fromIndex 5
356357
Bytes.wrap(array).lastIndexOf((byte) 0xAE);
358+
Bytes.wrap(array).startsWith(new byte[] {(byte) 0xAE, 0x32});
359+
Bytes.wrap(array).endsWidth(new byte[] {(byte) 0xAE, 0x23});
357360
```
358361

359362
Length checks:

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ public int indexOf(byte target) {
11201120
* {@code -1} if no such index exists or fromIndex is gt target length.
11211121
*/
11221122
public int indexOf(byte target, int fromIndex) {
1123-
return Util.indexOf(internalArray(), new byte[]{target}, fromIndex);
1123+
return indexOf(new byte[]{target}, fromIndex);
11241124
}
11251125

11261126
/**
@@ -1136,7 +1136,7 @@ public int indexOf(byte target, int fromIndex) {
11361136
* {@code -1} if no such index exists.
11371137
*/
11381138
public int indexOf(byte[] subArray) {
1139-
return Util.indexOf(internalArray(), subArray, 0);
1139+
return indexOf(subArray, 0);
11401140
}
11411141

11421142
/**
@@ -1154,7 +1154,18 @@ public int indexOf(byte[] subArray) {
11541154
* {@code -1} if no such index exists.
11551155
*/
11561156
public int indexOf(byte[] subArray, int fromIndex) {
1157-
return Util.indexOf(internalArray(), subArray, fromIndex);
1157+
return Util.indexOf(internalArray(), subArray, fromIndex, length());
1158+
}
1159+
1160+
/**
1161+
* Checks if the given sub array is equal to the start of given array. That is, sub array must be gt or eq
1162+
* to the length of the internal array and <code>internal[i] == subArray[i]</code> for i=0..subArray.length-1
1163+
*
1164+
* @param subArray to check against the start of the internal array
1165+
* @return true if the start of the internal array is eq to given sub array
1166+
*/
1167+
public boolean startsWith(byte[] subArray) {
1168+
return Util.indexOf(internalArray(), subArray, 0, 1) == 0;
11581169
}
11591170

11601171
/**
@@ -1169,6 +1180,18 @@ public int lastIndexOf(byte target) {
11691180
return Util.lastIndexOf(internalArray(), target, 0, length());
11701181
}
11711182

1183+
/**
1184+
* Checks if the given sub array is equal to the end of given array. That is, sub array must be gt or eq
1185+
* to the length of the internal array and <code>internal[i] == subArray[i]</code> for i=subArray.length...internal.length
1186+
*
1187+
* @param subArray to check against the end of the internal array
1188+
* @return true if the end of the internal array is eq to given sub array
1189+
*/
1190+
public boolean endsWith(byte[] subArray) {
1191+
int startIndex = length() - subArray.length;
1192+
return startIndex >= 0 && Util.indexOf(internalArray(), subArray, startIndex, startIndex + 1) == startIndex;
1193+
}
1194+
11721195
/**
11731196
* Returns the {@code bit} value as boolean at the specified index. Bit index 0 is the LSB, so for example byte word
11741197
* <code>1000 0000</code> has <code>bitAt(0) == false</code> and <code>bitAt(7) == true</code>.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ static byte[] concat(byte[]... arrays) {
6767
* @param array the array to search for the sequence {@code target}
6868
* @param target the array to search for as a sub-sequence of {@code array}
6969
*/
70-
static int indexOf(byte[] array, byte[] target, int start) {
70+
static int indexOf(byte[] array, byte[] target, int start, int end) {
7171
Objects.requireNonNull(array, "array must not be null");
7272
Objects.requireNonNull(target, "target must not be null");
73-
if (target.length == 0) {
73+
if (target.length == 0 || start < 0) {
7474
return -1;
7575
}
7676

7777
outer:
78-
for (int i = start; i < array.length - target.length + 1; i++) {
78+
for (int i = start; i < Math.min(end, array.length - target.length + 1); i++) {
7979
for (int j = 0; j < target.length; j++) {
8080
if (array[i + j] != target[j]) {
8181
continue outer;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ public void indexOfArrayFromIndex() {
190190
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));
191191
}
192192

193+
@Test
194+
public void startsWidth() {
195+
assertFalse(Bytes.allocate(0).startsWith(new byte[1]));
196+
assertTrue(Bytes.allocate(1).startsWith(new byte[1]));
197+
assertTrue(Bytes.allocate(128).startsWith(new byte[1]));
198+
assertTrue(Bytes.allocate(128).startsWith(new byte[128]));
199+
assertTrue(Bytes.from(example_bytes_seven).startsWith(new byte[]{0x4A}));
200+
assertTrue(Bytes.from(example_bytes_seven).startsWith(new byte[]{0x4A, (byte) 0x94}));
201+
assertTrue(Bytes.from(example_bytes_seven).startsWith(new byte[]{0x4A, (byte) 0x94, (byte) 0xFD}));
202+
assertFalse(Bytes.from(example_bytes_seven).startsWith(new byte[]{0x4A, (byte) 0x94, (byte) 0x1D}));
203+
assertTrue(Bytes.from(example_bytes_seven).startsWith(Bytes.from(example_bytes_seven).array()));
204+
assertFalse(Bytes.from(example_bytes_seven).startsWith(Bytes.from(example_bytes_seven).append(0x30).array()));
205+
}
206+
207+
@Test
208+
public void endsWith() {
209+
assertTrue(Bytes.from(example_bytes_seven).endsWith(new byte[]{(byte) 0xFF, 0x1E, (byte) 0xAF, (byte) 0xED}));
210+
assertTrue(Bytes.from(example_bytes_seven).endsWith(new byte[]{0x1E, (byte) 0xAF, (byte) 0xED}));
211+
assertTrue(Bytes.from(example_bytes_seven).endsWith(new byte[]{(byte) 0xAF, (byte) 0xED}));
212+
assertTrue(Bytes.from(example_bytes_seven).endsWith(new byte[]{(byte) 0xED}));
213+
assertFalse(Bytes.allocate(0).endsWith(new byte[1]));
214+
assertTrue(Bytes.allocate(1).endsWith(new byte[1]));
215+
assertTrue(Bytes.allocate(128).endsWith(new byte[1]));
216+
assertTrue(Bytes.allocate(128).endsWith(new byte[128]));
217+
}
218+
193219
@Test
194220
public void lastIndexOf() {
195221
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, new byte[]{b}, 0);
56+
return Util.indexOf(empty, new byte[]{b}, 0, empty.length);
5757
}
5858

5959
@Test
6060
public void testIndexOf_arrayTarget() {
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));
61+
assertEquals(-1, Util.indexOf(EMPTY, EMPTY, 0, EMPTY.length));
62+
assertEquals(-1, Util.indexOf(ARRAY234, EMPTY, 0, ARRAY234.length));
63+
assertEquals(-1, Util.indexOf(EMPTY, ARRAY234, 0, EMPTY.length));
64+
assertEquals(-1, Util.indexOf(ARRAY234, ARRAY1, 0, ARRAY234.length));
65+
assertEquals(-1, Util.indexOf(ARRAY1, ARRAY234, 0, ARRAY1.length));
66+
assertEquals(0, Util.indexOf(ARRAY1, ARRAY1, 0, ARRAY1.length));
67+
assertEquals(0, Util.indexOf(ARRAY234, ARRAY234, 0, ARRAY234.length));
6868
assertEquals(0, Util.indexOf(
69-
ARRAY234, new byte[]{(byte) 2, (byte) 3}, 0));
69+
ARRAY234, new byte[]{(byte) 2, (byte) 3}, 0, 2));
7070
assertEquals(1, Util.indexOf(
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));
71+
ARRAY234, new byte[]{(byte) 3, (byte) 4}, 0, 2));
72+
assertEquals(1, Util.indexOf(ARRAY234, new byte[]{(byte) 3}, 0, ARRAY234.length));
73+
assertEquals(2, Util.indexOf(ARRAY234, new byte[]{(byte) 4}, 0, ARRAY234.length));
74+
assertEquals(1, Util.indexOf(new byte[]{(byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3}, new byte[]{(byte) 3}, 0, 5));
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-
, 0));
79+
, 0, 7));
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-
, 0));
84+
, 0, 7));
8585
assertEquals(-1, Util.indexOf(
8686
new byte[]{(byte) 4, (byte) 3, (byte) 2},
8787
new byte[]{(byte) 2, (byte) 3, (byte) 4}
88-
, 0));
88+
, 0, 2));
8989
}
9090

9191
@Test

0 commit comments

Comments
 (0)