Skip to content

Commit

Permalink
minor refactoring;
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
esaulpaugh committed Nov 24, 2019
1 parent b321fb0 commit ff1b877
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public Tuple decodeCall(byte[] array) {
}

public Tuple decodeCall(ByteBuffer abiBuffer) {
byte[] unitBuffer = ABIType.newUnitBuffer();
final byte[] unitBuffer = ABIType.newUnitBuffer();
abiBuffer.get(unitBuffer, 0, SELECTOR_LEN);
final byte[] selector = this.selector;
for(int i = 0; i < SELECTOR_LEN; i++) {
Expand Down
49 changes: 17 additions & 32 deletions src/main/java/com/esaulpaugh/headlong/abi/util/BizarroIntegers.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ public static int putByte(byte val, byte[] o, int i) {
}

public static int putShort(short val, byte[] o, int i) {
byte b = 0;
byte b;
int v = val;
final int n;
if (v != -1) {
b = (byte) v;
if ((v >>= Byte.SIZE) != -1) {
n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return Integers.insertBytes(n, o, i, (byte) 0, (byte) 0, (byte) v, b);
}

public static int putInt(int val, byte[] o, int i) {
byte b = 0, c = 0, d = 0;
byte b = 0, c = 0, d;
final int n;
if (val != -1) {
d = (byte) val;
Expand All @@ -93,12 +93,12 @@ public static int putInt(int val, byte[] o, int i) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return Integers.insertBytes(n, o, i, (byte) val, b, c, d);
}

public static int putLong(long val, byte[] o, int i) {
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h;
final int n;
if (val != -1) {
h = (byte) val;
Expand All @@ -123,12 +123,12 @@ public static int putLong(long val, byte[] o, int i) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return Integers.insertBytes(n, o, i, (byte) val, b, c, d, e, f, g, h);
}

public static int putLong(long val, ByteBuffer o) {
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h;
final int n;
if (val != -1) {
h = (byte) val;
Expand All @@ -153,27 +153,12 @@ public static int putLong(long val, ByteBuffer o) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return Integers.insertBytes(n, o, (byte) val, b, c, d, e, f, g, h);
}
// *******************
private static byte _getByte(byte[] buffer, int i, int len) {
switch (len) {
case 0: return 0;
case 1: return buffer[i];
default: throw new IllegalArgumentException("len is out of range: " + len);
}
}

private static short _getShort(byte[] buffer, int i, int len) {
int shiftAmount = 0;
int val = 0;
switch (len) { /* cases 2 through 1 fall through */
case 2: val = buffer[i+1] & 0xFF; shiftAmount = Byte.SIZE; // & 0xFF to promote to int before left shift
case 1: val |= (buffer[i] & 0xFF) << shiftAmount;
case 0: return (short) val;
default: throw new IllegalArgumentException("len is out of range: " + len);
}
private static int _getShortInt(byte[] buffer, int i) {
return (buffer[i+1] & 0xFF) | ((buffer[i] & 0xFF) << Byte.SIZE);
}

private static int _getInt(byte[] buffer, int i, int len) {
Expand Down Expand Up @@ -209,7 +194,7 @@ private static long _getLong(final byte[] buffer, final int i, final int len) {
public static byte getByte(byte[] buffer, int index, int len) {
switch (len) {
case 0: return (byte) 0xFF;
case 1: return _getByte(buffer, index, 1);
case 1: return buffer[index];
default: throw new IllegalArgumentException("len is out of range: " + len);
}
}
Expand All @@ -218,8 +203,8 @@ public static short getShort(byte[] buffer, int index, int len) {
// do sign extension for negative shorts, i.e. len < 2
switch (len) {
case 0: return (short) 0xFFFF;
case 1: return (short) (0xFFFFFF00 | _getByte(buffer, index, 1));
case 2: return _getShort(buffer, index, 2);
case 1: return (short) (0xFFFFFF00 | buffer[index]);
case 2: return (short) _getShortInt(buffer, index);
default: throw new IllegalArgumentException("len is out of range: " + len);
}
}
Expand All @@ -228,8 +213,8 @@ public static int getInt(byte[] buffer, int index, int len) {
// do sign extension for negative ints, i.e. len < 4
switch (len) {
case 0: return 0xFFFFFFFF;
case 1: return 0xFFFFFF00 | _getByte(buffer, index, 1);
case 2: return 0xFFFF0000 | _getShort(buffer, index, 2);
case 1: return 0xFFFFFF00 | buffer[index];
case 2: return 0xFFFF0000 | _getShortInt(buffer, index);
case 3: return 0xFF000000 | _getInt(buffer, index, 3);
case 4: return _getInt(buffer, index, 4);
default: throw new IllegalArgumentException("len is out of range: " + len);
Expand All @@ -240,8 +225,8 @@ public static long getLong(final byte[] buffer, final int index, final int len)
// do sign extension for negative longs, i.e. len < 8
switch (len) {
case 0: return 0xFFFFFFFF_FFFFFFFFL;
case 1: return 0xFFFFFFFF_FFFFFF00L | _getByte(buffer, index, 1);
case 2: return 0xFFFFFFFF_FFFF0000L | _getShort(buffer, index, 2);
case 1: return 0xFFFFFFFF_FFFFFF00L | buffer[index];
case 2: return 0xFFFFFFFF_FFFF0000L | _getShortInt(buffer, index);
case 3: return 0xFFFFFFFF_FF000000L | _getInt(buffer, index, 3);
case 4: return 0xFFFFFFFF_00000000L | _getInt(buffer, index, 4);
case 5: return 0xFFFFFF00_00000000L | _getLong(buffer, index, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.Arrays;

import static com.esaulpaugh.headlong.util.Strings.UTF_8;
import static com.esaulpaugh.headlong.util.Strings.decode;
import static com.esaulpaugh.headlong.util.Strings.encode;

public final class KeyValuePair implements Comparable<KeyValuePair> {

Expand All @@ -40,8 +38,8 @@ public final class KeyValuePair implements Comparable<KeyValuePair> {
private final byte[] value;

public KeyValuePair(String key, String value, int valueEncoding) {
this.key = decode(key, UTF_8);
this.value = decode(value, valueEncoding);
this.key = Strings.decode(key, UTF_8);
this.value = Strings.decode(value, valueEncoding);
}

public KeyValuePair(byte[] key, byte[] value) {
Expand Down Expand Up @@ -71,16 +69,16 @@ public boolean equals(Object o) {

@Override
public String toString() {
return encode(key, UTF_8) + " --> " + encode(value, Strings.HEX);
return Strings.encode(key, UTF_8) + " --> " + Strings.encode(value, Strings.HEX);
}

@Override
public int compareTo(KeyValuePair o) {
int result = compare(key, o.key);
if (result == 0) {
throw new IllegalArgumentException("duplicate key: " + encode(o.key, UTF_8));
if (result != 0) {
return result;
}
return result;
throw new IllegalArgumentException("duplicate key: " + Strings.encode(o.key, UTF_8));
}

private static int compare(byte[] a, byte[] b) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/esaulpaugh/headlong/rlp/util/Integers.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static int putByte(byte val, byte[] o, int i) {
* @return the number of bytes inserted
*/
public static int putShort(short val, byte[] o, int i) {
byte b = 0;
byte b;
final int n;
if(val != 0) {
b = (byte) val;
Expand All @@ -122,7 +122,7 @@ public static int putShort(short val, byte[] o, int i) {
if (val != 0) {
n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return insertBytes(n, o, i, (byte) 0, (byte) 0, (byte) val, b);
}

Expand All @@ -138,7 +138,7 @@ public static int putShort(short val, byte[] o, int i) {
* @return the number of bytes inserted
*/
public static int putInt(int val, byte[] o, int i) {
byte b = 0, c = 0, d = 0;
byte b = 0, c = 0, d;
final int n;
if(val != 0) {
d = (byte) val;
Expand All @@ -151,7 +151,7 @@ public static int putInt(int val, byte[] o, int i) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return insertBytes(n, o, i, (byte) val, b, c, d);
}

Expand All @@ -167,7 +167,7 @@ public static int putInt(int val, byte[] o, int i) {
* @return the number of bytes inserted
*/
public static int putLong(long val, byte[] o, int i) {
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h;
final int n;
if(val != 0) {
h = (byte) val;
Expand All @@ -192,12 +192,12 @@ public static int putLong(long val, byte[] o, int i) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return insertBytes(n, o, i, (byte) val, b, c, d, e, f, g, h);
}

public static int putLong(long val, ByteBuffer o) {
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h;
final int n;
if(val != 0) {
h = (byte) val;
Expand All @@ -222,7 +222,7 @@ public static int putLong(long val, ByteBuffer o) {
} else n = 3;
} else n = 2;
} else n = 1;
} else n = 0;
} else return 0;
return insertBytes(n, o, (byte) val, b, c, d, e, f, g, h);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void testLongs(int iterations, Supplier<Long> supplier, byte[] ei
int n = BizarroIntegers.putLong(lo, eight, 0);
long r = BizarroIntegers.getLong(eight, 0, n);
if(lo != r) {
throw new AssertionError(lo + "!= " + r);
throw new AssertionError(lo + " != " + r);
}
}
}
Expand Down

0 comments on commit ff1b877

Please sign in to comment.