Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allowing zero length in static array #432

Merged
merged 8 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .test-env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configs for testing repo download:
SDK_TESTING_URL="https://github.com/algorand/algorand-sdk-testing"
SDK_TESTING_BRANCH="master"
SDK_TESTING_BRANCH="zero-length"
ahangsu marked this conversation as resolved.
Show resolved Hide resolved
SDK_TESTING_HARNESS="test-harness"

INSTALL_ONLY=0
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/algorand/algosdk/abi/ABIType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public abstract class ABIType {
public static final int ABI_DYNAMIC_HEAD_BYTE_LEN = 2;
private static final Pattern staticArrayPattern = Pattern.compile("^(?<elemT>[a-z\\d\\[\\](),]+)\\[(?<len>[1-9][\\d]*)]$");
private static final Pattern staticArrayPattern = Pattern.compile("^(?<elemT>[a-z\\d\\[\\](),]+)\\[(?<len>0|[1-9][\\d]*)]$");
ahangsu marked this conversation as resolved.
Show resolved Hide resolved
private static final Pattern ufixedPattern = Pattern.compile("^ufixed(?<size>[1-9][\\d]*)x(?<precision>[1-9][\\d]*)$");

/**
Expand Down Expand Up @@ -118,8 +118,13 @@ else if (str.charAt(i) == ')') {
if (parenStack.isEmpty())
throw new IllegalArgumentException("parsing error: tuple parentheses are not balanced: " + str);
int leftParenIndex = parenStack.pop();
if (parenStack.isEmpty())
if (parenStack.isEmpty()) {
int forwardIndex = i + 1;
while (forwardIndex < str.length() && str.charAt(forwardIndex) != ',')
forwardIndex++;
i = forwardIndex - 1;
parenSegments.add(new Segment(leftParenIndex, i));
}
}
}
if (!parenStack.isEmpty())
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/algorand/algosdk/abi/TypeArrayStatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class TypeArrayStatic extends ABIType {
public final int length;

public TypeArrayStatic(ABIType elemType, int length) {
if (length < 1)
throw new IllegalArgumentException("static-array initialize failure: array length should be at least 1");
if (length < 0)
throw new IllegalArgumentException("static-array initialize failure: array length should be positive");
ahangsu marked this conversation as resolved.
Show resolved Hide resolved
this.elemType = elemType;
this.length = length;
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/com/algorand/algosdk/abi/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ public void TestTypeFromStringValid() {
)
)
);
assertThat(ABIType.valueOf("(uint32,(uint64,bool)[10],byte)")).isEqualTo(
new TypeTuple(
Arrays.asList(
new TypeUint(32),
new TypeArrayStatic(
new TypeTuple(
Arrays.asList(
new TypeUint(64),
new TypeBool()
)
),
10),
new TypeByte()
)
)
);
}

@Test
Expand Down Expand Up @@ -266,7 +282,6 @@ public void TestTypeFromStringInvalid() {
"[][][]",
"stuff[]",
// static array
"ufixed32x10[0]",
"byte[10 ]",
"uint64[0x21]",
// tuple
Expand Down