Skip to content

Commit dd62e5c

Browse files
committed
feat: allow extracting to the end by omitting length parameter for extract op code
1 parent 777faa0 commit dd62e5c

File tree

7 files changed

+1270
-1276
lines changed

7 files changed

+1270
-1276
lines changed

src/impl/pure.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,29 @@ export const expw = (a: internal.primitives.StubUint64Compat, b: internal.primit
103103
return toUint128(base ** exponent)
104104
}
105105

106-
export const extract = (
106+
type ExtractType = ((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat) => bytes) &
107+
((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubUint64Compat) => bytes)
108+
export const extract = ((
107109
a: internal.primitives.StubBytesCompat,
108110
b: internal.primitives.StubUint64Compat,
109-
c: internal.primitives.StubUint64Compat,
111+
c?: internal.primitives.StubUint64Compat,
110112
): bytes => {
111113
const bytesValue = internal.primitives.BytesCls.fromCompat(a)
112114
const bytesLength = bytesValue.length.asBigInt()
113115

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

121120
if (start > bytesLength) {
122121
internal.errors.codeError(`extraction start ${start} is beyond length`)
123122
}
124-
if (end > bytesLength) {
123+
if (end !== undefined && end > bytesLength) {
125124
internal.errors.codeError(`extraction end ${end} is beyond length`)
126125
}
127126

128127
return bytesValue.slice(start, end).asAlgoTs()
129-
}
128+
}) as ExtractType
130129

131130
export const extractUint16 = (a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): uint64 => {
132131
const result = extract(a, b, 2)

tests/artifacts/miscellaneous-ops/contract.algo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ export class MiscellaneousOpsContract extends arc4.Contract {
9595
return result
9696
}
9797

98-
// TODO: recompile to check if this results in correct TEAL code
9998
@arc4.abimethod()
10099
public verify_extract_from_2(a: bytes): bytes {
101-
const result = op.extract(a, 2, 0)
100+
const result = op.extract(a, 2)
102101
return result
103102
}
104103

0 commit comments

Comments
 (0)