Skip to content

Commit

Permalink
refactor format methods
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Jan 13, 2024
1 parent 7e3c836 commit 1cf3ea4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;

Expand Down Expand Up @@ -278,7 +279,8 @@ static void insertFFPadding(int n, ByteBuffer dest) {
}

private static final int LABEL_LEN = 6;
private static final int LABEL_PADDED_LEN = LABEL_LEN + 3;
static final int LABEL_PADDED_LEN = LABEL_LEN + 3;
static final int CHARS_PER_LINE = "\n".length() + LABEL_PADDED_LEN + (FastHex.CHARS_PER_BYTE * UNIT_LENGTH_BYTES);

public static String format(byte[] abi) {
return format(abi, (int row) -> {
Expand All @@ -289,7 +291,7 @@ public static String format(byte[] abi) {

public static String format(byte[] abi, IntFunction<String> labeler) {
Integers.checkIsMultiple(abi.length, UNIT_LENGTH_BYTES);
return finishFormat(abi, 0, abi.length, labeler, new StringBuilder());
return finishFormat(abi, 0, abi.length, labeler, new StringBuilder(abi.length / UNIT_LENGTH_BYTES * CHARS_PER_LINE));
}

static String finishFormat(byte[] buffer, int offset, int end, IntFunction<String> labeler, StringBuilder sb) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/esaulpaugh/headlong/abi/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,18 @@ public static String formatCall(byte[] call) {
* @throws IllegalArgumentException if {@code length} mod 32 is not equal to four
*/
public static String formatCall(byte[] buffer, final int offset, final int length, IntFunction<String> labeler) {
Integers.checkIsMultiple(length - SELECTOR_LEN, UNIT_LENGTH_BYTES);
final int bodyLen = length - SELECTOR_LEN;
Integers.checkIsMultiple(bodyLen, UNIT_LENGTH_BYTES);
final String label = ABIType.pad(0, "ID");
final String selectorHex = Strings.encode(buffer, offset, SELECTOR_LEN, Strings.HEX);
return ABIType.finishFormat(
buffer,
offset + SELECTOR_LEN,
offset + length,
labeler,
new StringBuilder(ABIType.pad(0, "ID"))
.append(Strings.encode(buffer, offset, SELECTOR_LEN, Strings.HEX))
new StringBuilder(label.length() + selectorHex.length() + (bodyLen / UNIT_LENGTH_BYTES) * ABIType.CHARS_PER_LINE)
.append(label)
.append(selectorHex)
);
}
}

0 comments on commit 1cf3ea4

Please sign in to comment.