Extended ByteArarySearcher API
This commit is contained in:
@@ -5,24 +5,33 @@ public final class ByteArraySearcher {
|
|||||||
private ByteArraySearcher() {
|
private ByteArraySearcher() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int indexOf(byte[] haystack, byte[] needle) {
|
||||||
|
if (needle.length > haystack.length) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any needle to search?
|
||||||
|
if (needle.length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i <= haystack.length - needle.length; i++) {
|
||||||
|
boolean found = true;
|
||||||
|
for (int j = 0; j < needle.length; j++) {
|
||||||
|
if (haystack[i + j] != needle[j]) {
|
||||||
|
found = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (found) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean contains(byte[] haystack, byte[] needle) {
|
public static boolean contains(byte[] haystack, byte[] needle) {
|
||||||
if (needle.length > haystack.length) {
|
return indexOf (haystack, needle) >= 0;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i <= haystack.length - needle.length; i++) {
|
|
||||||
boolean found = true;
|
|
||||||
for (int j = 0; j < needle.length; j++) {
|
|
||||||
if (haystack[i + j] != needle[j]) {
|
|
||||||
found = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.mustangproject.validator;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ByteArraySearcherTest {
|
||||||
|
@Test
|
||||||
|
public void testIndexOf () {
|
||||||
|
byte[] haystack = "Hello World".getBytes (StandardCharsets.ISO_8859_1);
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H'}));
|
||||||
|
assertEquals (1, ByteArraySearcher.indexOf (haystack, new byte[] {'e'}));
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e'}));
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e'}));
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e', 'l', 'l'}));
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, haystack));
|
||||||
|
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[0]));
|
||||||
|
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'a'}));
|
||||||
|
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'h'}));
|
||||||
|
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'r', 'o'}));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user