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

feat: allow extracting to the end by omitting length parameter for extract op code #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 8 additions & 9 deletions src/impl/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,29 @@ export const expw = (a: internal.primitives.StubUint64Compat, b: internal.primit
return toUint128(base ** exponent)
}

export const extract = (
type ExtractType = ((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat) => bytes) &
((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubUint64Compat) => bytes)
export const extract = ((
a: internal.primitives.StubBytesCompat,
b: internal.primitives.StubUint64Compat,
c: internal.primitives.StubUint64Compat,
c?: internal.primitives.StubUint64Compat,
): bytes => {
const bytesValue = internal.primitives.BytesCls.fromCompat(a)
const bytesLength = bytesValue.length.asBigInt()

const start = internal.primitives.Uint64Cls.fromCompat(b).asBigInt()
const length = internal.primitives.Uint64Cls.fromCompat(c).asBigInt()
let end = start + length
if ((typeof b === 'number' || typeof b === 'bigint') && (typeof c === 'number' || typeof c === 'bigint') && length === 0n) {
end = bytesLength
}
const length = c !== undefined ? internal.primitives.Uint64Cls.fromCompat(c).asBigInt() : undefined
const end = length !== undefined ? start + length : undefined

if (start > bytesLength) {
internal.errors.codeError(`extraction start ${start} is beyond length`)
}
if (end > bytesLength) {
if (end !== undefined && end > bytesLength) {
internal.errors.codeError(`extraction end ${end} is beyond length`)
}

return bytesValue.slice(start, end).asAlgoTs()
}
}) as ExtractType

export const extractUint16 = (a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): uint64 => {
const result = extract(a, b, 2)
Expand Down
3 changes: 1 addition & 2 deletions tests/artifacts/miscellaneous-ops/contract.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@
return result
}

// TODO: recompile to check if this results in correct TEAL code
@arc4.abimethod()
public verify_extract_from_2(a: bytes): bytes {
const result = op.extract(a, 2, 0)
const result = op.extract(a, 2)

Check failure on line 100 in tests/artifacts/miscellaneous-ops/contract.algo.ts

View workflow job for this annotation

GitHub Actions / Build @algorandfoundation/algorand-typescript-testing / node-ci

Expected 3 arguments, but got 2.
return result
}

Expand Down
Loading
Loading