closes #481, moved bytearraysearcher to library
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package org.mustangproject.validator;
|
||||
|
||||
public final class 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) {
|
||||
return indexOf (haystack, needle) >= 0;
|
||||
}
|
||||
|
||||
public static boolean startsWith(byte[] haystack, byte[] needle) {
|
||||
if (needle.length > haystack.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Any needle to search?
|
||||
if (needle.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int j = 0; j < needle.length; j++) {
|
||||
if (haystack[j] != needle[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import javax.xml.xpath.XPathExpression;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.mustangproject.util.ByteArraySearcher;
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDImporter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.mustangproject.util.ByteArraySearcher;
|
||||
import org.mustangproject.XMLTools;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package org.mustangproject.validator;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
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 (-1, 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' }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartsWith () {
|
||||
byte [] haystack = "Hello World".getBytes (StandardCharsets.ISO_8859_1);
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e', 'l', 'l' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, haystack));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [0]));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'a' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'h' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'r', 'o' }));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user