Permalink
Browse files

Re-add array_subset_of, which disappeared somehow

  • Loading branch information...
LadyCailin committed Nov 6, 2018
1 parent 8f7d354 commit 52dd0c479f8fbd9aca4800f60837cbc58d03859f
@@ -5,9 +5,11 @@
* @author Cailin
*/
public class ByteArrayUtils {
private final boolean useUpper;
private final String middleUpper = " X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF ";
private final String middleLower = " x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf ";
private static final String MIDDLE_UPPER = " X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF ";
private static final String MIDDLE_LOWER = " x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf ";
/**
* Creates a new ByteArrayUtils object with default options
*/
@@ -18,50 +20,52 @@ public ByteArrayUtils() {
public ByteArrayUtils(boolean useUpperCase) {
this.useUpper = useUpperCase;
}
public String baToHexTable(byte [] ba) {
StringBuilder b = new StringBuilder();
StringBuilder c1 = new StringBuilder();
StringBuilder c2 = new StringBuilder();
b.append("Address |").append(useUpper ? middleUpper : middleLower).append("| ASCII |\n");
for(int i = 0; i < ba.length + (16 - (ba.length % 16)); i++) {
if(i % 16 == 0) {
// First line, the address
b.append(String.format("0x%07" + (useUpper?"X":"x"), i / 16)).append(useUpper ? "X " : "x ");
}
if(i < ba.length) {
byte by = ba[i];
c1.append(toHex(by)).append(" ");
char w = '.';
if(by != 0) {
w = (char) by;
}
if(Character.isISOControl(by)) {
w = '.';
}
c2.append(w);
} else {
c1.append(".. ");
c2.append(".");
}
if(i % 16 == 15) {
// End of line, construct line
b.append("| ").append(c1.toString()).append("| ")
.append(c2.toString()).append(" |\n");
c1 = new StringBuilder();
c2 = new StringBuilder();
}
}
return b.toString();
}
private final static char[] upperHexArray = "0123456789ABCDEF".toCharArray();
private final static char[] lowerHexArray = "0123456789abcdef".toCharArray();
private String toHex(byte b) {
int v = b & 0xFF;
public String baToHexTable(byte[] ba) {
StringBuilder b = new StringBuilder();
StringBuilder c1 = new StringBuilder();
StringBuilder c2 = new StringBuilder();
b.append("Address |").append(useUpper ? MIDDLE_UPPER : MIDDLE_LOWER).append("| ASCII |\n");
for(int i = 0; i < ba.length + (16 - (ba.length % 16)); i++) {
if(i % 16 == 0) {
// First line, the address
b.append(String.format("0x%07" + (useUpper ? "X" : "x"), i / 16)).append(useUpper ? "X " : "x ");
}
if(i < ba.length) {
byte by = ba[i];
c1.append(toHex(by)).append(" ");
char w = '.';
if(by != 0) {
w = (char) by;
}
if(Character.isISOControl(by)) {
w = '.';
}
c2.append(w);
} else {
c1.append(".. ");
c2.append(".");
}
if(i % 16 == 15) {
// End of line, construct line
b.append("| ").append(c1.toString()).append("| ")
.append(c2.toString()).append(" |\n");
c1 = new StringBuilder();
c2 = new StringBuilder();
}
}
return b.toString();
}
private static final char[] UPPER_HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private static final char[] LOWER_HEX_ARRAY = "0123456789abcdef".toCharArray();
private String toHex(byte b) {
int v = b & 0xFF;
if(useUpper) {
return new String(new char[]{upperHexArray[v >>> 4], upperHexArray[v & 0x0F]});
return new String(new char[]{UPPER_HEX_ARRAY[v >>> 4], UPPER_HEX_ARRAY[v & 0x0F]});
} else {
return new String(new char[]{lowerHexArray[v >>> 4], lowerHexArray[v & 0x0F]});
return new String(new char[]{LOWER_HEX_ARRAY[v >>> 4], LOWER_HEX_ARRAY[v & 0x0F]});
}
}
}
}
@@ -3208,7 +3208,119 @@ public Version since() {
};
}
@api
public static class array_subset_of extends AbstractFunction {
@Override
public Version since() {
return CHVersion.V3_3_2;
}
@Override
public String getName() {
return "array_subset_of";
}
@Override
public Integer[] numArgs() {
return new Integer[]{2};
}
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREIllegalArgumentException.class};
}
@Override
public String docs() {
return "boolean {array, array} "
+ "Returns true if first array is a subset of second array.";
}
@Override
public boolean isRestricted() {
return false;
}
@Override
public Boolean runAsync() {
return null;
}
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
Construct constA = args[0];
Construct constB = args[1];
if(!(constA instanceof CArray)) {
throw new CREIllegalArgumentException("Expecting an array, but received " + constA, t);
}
if(!(constB instanceof CArray)) {
throw new CREIllegalArgumentException("Expecting an array, but received " + constB, t);
}
return CBoolean.get(subsetOf(constA, constB, t));
}
@Override
public ExampleScript[] examples() throws ConfigCompileException {
return new ExampleScript[]{
new ExampleScript("Basic usage",
"@arrayA = array(0, 1)\n"
+ "@arrayB = array(0, 1, 5, 9)\n"
+ "array_subset_of(@arrayA, @arrayB)"),
new ExampleScript("Basic usage",
"@arrayA = array(0, 1)\n"
+ "@arrayB = array(0, 2, 5, 9)\n"
+ "array_subset_of(@arrayA, @arrayB)"),
new ExampleScript("Mix array",
"@arrayA = array(a: 1, b: array(one, two))\n"
+ "@arrayB = array(a: 1, b: array(one, two, three), c: 3)\n"
+ "array_subset_of(@arrayA, @arrayB)"),
new ExampleScript("Mix array",
"@arrayA = array(a: 1, b: array(one, two))\n"
+ "@arrayB = array(a: 1, b: array(two, one, three), c: 3)\n"
+ "array_subset_of(@arrayA, @arrayB)")
};
}
public boolean subsetOf(Construct constA, Construct constB, Target t) {
if(constA.getCType() != constB.getCType()) {
return false;
}
if(constA instanceof CArray) {
CArray arrA = (CArray) constA;
CArray arrB = (CArray) constB;
if(arrA.isAssociative() != arrB.isAssociative()) {
return false;
}
if(arrA.isAssociative()) {
for(String key : arrA.stringKeySet()) {
if(!arrB.containsKey(key)) {
return false;
}
Construct eltA = arrA.get(key, t);
Construct eltB = arrB.get(key, t);
if(!subsetOf(eltA, eltB, t)) {
return false;
}
}
} else {
for(int i = 0; i < arrA.size(); i++) {
if(!arrB.containsKey(i)) {
return false;
}
Construct eltA = arrA.get(i, t);
Construct eltB = arrB.get(i, t);
if(!subsetOf(eltA, eltB, t)) {
return false;
}
}
}
} else if(!equals.doEquals(constA, constB)) {
return false;
}
return true;
}
}
}
}

0 comments on commit 52dd0c4

Please sign in to comment.