-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add Replace #413
Merged
Merged
Add Replace #413
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,7 @@ __all__ = [ | |
"Or", | ||
"Pop", | ||
"Reject", | ||
"Replace", | ||
"Return", | ||
"ScratchIndex", | ||
"ScratchLoad", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from typing import cast, TYPE_CHECKING | ||
|
||
from pyteal.types import TealType, require_type | ||
from pyteal.errors import verifyTealVersion | ||
from pyteal.ir import TealOp, Op, TealBlock | ||
from pyteal.ast.expr import Expr | ||
from pyteal.ast.int import Int | ||
from pyteal.ast.ternaryexpr import TernaryExpr | ||
|
||
if TYPE_CHECKING: | ||
from pyteal.compiler import CompileOptions | ||
|
||
|
||
class ReplaceExpr(Expr): | ||
"""An expression for replacing a section of a byte string at a given start index""" | ||
|
||
def __init__(self, original: Expr, start: Expr, replacement: Expr) -> None: | ||
super().__init__() | ||
|
||
require_type(original, TealType.bytes) | ||
require_type(start, TealType.uint64) | ||
require_type(replacement, TealType.bytes) | ||
|
||
self.original = original | ||
self.start = start | ||
self.replacement = replacement | ||
|
||
# helper method for correctly populating op | ||
def __get_op(self, options: "CompileOptions"): | ||
s = cast(Int, self.start).value | ||
if s < 2**8: | ||
return Op.replace2 | ||
else: | ||
return Op.replace3 | ||
|
||
def __teal__(self, options: "CompileOptions"): | ||
if not isinstance(self.start, Int): | ||
return TernaryExpr( | ||
Op.replace3, | ||
(TealType.bytes, TealType.uint64, TealType.bytes), | ||
TealType.bytes, | ||
self.original, | ||
self.start, | ||
self.replacement, | ||
).__teal__(options) | ||
|
||
op = self.__get_op(options) | ||
|
||
verifyTealVersion( | ||
op.min_version, | ||
options.version, | ||
"TEAL version too low to use op {}".format(op), | ||
) | ||
|
||
s = cast(Int, self.start).value | ||
if op == Op.replace2: | ||
return TealBlock.FromOp( | ||
options, TealOp(self, op, s), self.original, self.replacement | ||
) | ||
elif op == Op.replace3: | ||
return TealBlock.FromOp( | ||
options, TealOp(self, op), self.original, self.start, self.replacement | ||
) | ||
|
||
def __str__(self): | ||
return "(Replace {} {} {})".format(self.original, self.start, self.replacement) | ||
|
||
def type_of(self): | ||
return TealType.bytes | ||
|
||
def has_return(self): | ||
return False | ||
|
||
|
||
def Replace(original: Expr, start: Expr, replacement: Expr) -> Expr: | ||
""" | ||
Replace a portion of original bytes with new bytes at a given starting point. | ||
|
||
Requires TEAL version 7 or higher. | ||
|
||
Args: | ||
original: The value containing the original bytes. Must evaluate to bytes. | ||
start: The index of the byte where replacement starts. Must evaluate to an integer less than Len(original). | ||
replacement: The value containing the replacement bytes. Must evaluate to bytes with length at most Len(original) - start. | ||
""" | ||
return ReplaceExpr(original, start, replacement) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import pytest | ||
|
||
import pyteal as pt | ||
|
||
teal6Options = pt.CompileOptions(version=6) | ||
teal7Options = pt.CompileOptions(version=7) | ||
|
||
|
||
def test_replace_immediate(): | ||
args = [pt.Bytes("my string"), pt.Int(0), pt.Bytes("abcdefghi")] | ||
expr = pt.Replace(args[0], args[1], args[2]) | ||
assert expr.type_of() == pt.TealType.bytes | ||
|
||
expected = pt.TealSimpleBlock( | ||
[ | ||
pt.TealOp(args[0], pt.Op.byte, '"my string"'), | ||
pt.TealOp(args[2], pt.Op.byte, '"abcdefghi"'), | ||
pt.TealOp(expr, pt.Op.replace2, 0), | ||
] | ||
) | ||
|
||
actual, _ = expr.__teal__(teal7Options) | ||
actual.addIncoming() | ||
actual = pt.TealBlock.NormalizeBlocks(actual) | ||
|
||
assert actual == expected | ||
|
||
with pytest.raises(pt.TealInputError): | ||
expr.__teal__(teal6Options) | ||
|
||
|
||
def test_replace_stack_int(): | ||
my_string = "*" * 257 | ||
args = [pt.Bytes(my_string), pt.Int(256), pt.Bytes("ab")] | ||
expr = pt.Replace(args[0], args[1], args[2]) | ||
assert expr.type_of() == pt.TealType.bytes | ||
|
||
expected = pt.TealSimpleBlock( | ||
[ | ||
pt.TealOp(args[0], pt.Op.byte, '"{my_string}"'.format(my_string=my_string)), | ||
pt.TealOp(args[1], pt.Op.int, 256), | ||
pt.TealOp(args[2], pt.Op.byte, '"ab"'), | ||
pt.TealOp(expr, pt.Op.replace3), | ||
] | ||
) | ||
|
||
actual, _ = expr.__teal__(teal7Options) | ||
actual.addIncoming() | ||
actual = pt.TealBlock.NormalizeBlocks(actual) | ||
|
||
assert actual == expected | ||
|
||
with pytest.raises(pt.TealInputError): | ||
expr.__teal__(teal6Options) | ||
|
||
|
||
# Mirrors `test_replace_stack_int`, but attempts replacement with start != pt.Int. | ||
def test_replace_stack_not_int(): | ||
my_string = "*" * 257 | ||
add = pt.Add(pt.Int(254), pt.Int(2)) | ||
args = [pt.Bytes(my_string), add, pt.Bytes("ab")] | ||
expr = pt.Replace(args[0], args[1], args[2]) | ||
assert expr.type_of() == pt.TealType.bytes | ||
|
||
expected = pt.TealSimpleBlock( | ||
[ | ||
pt.TealOp(args[0], pt.Op.byte, '"{my_string}"'.format(my_string=my_string)), | ||
pt.TealOp(pt.Int(254), pt.Op.int, 254), | ||
pt.TealOp(pt.Int(2), pt.Op.int, 2), | ||
pt.TealOp(add, pt.Op.add), | ||
pt.TealOp(args[2], pt.Op.byte, '"ab"'), | ||
pt.TealOp(expr, pt.Op.replace3), | ||
] | ||
) | ||
|
||
actual, _ = expr.__teal__(teal7Options) | ||
actual.addIncoming() | ||
actual = pt.TealBlock.NormalizeBlocks(actual) | ||
|
||
with pt.TealComponent.Context.ignoreExprEquality(): | ||
assert actual == expected | ||
|
||
with pytest.raises(pt.TealInputError): | ||
expr.__teal__(teal6Options) | ||
|
||
|
||
def test_replace_invalid(): | ||
with pytest.raises(pt.TealTypeError): | ||
pt.Replace(pt.Bytes("my string"), pt.Int(0), pt.Int(1)) | ||
|
||
with pytest.raises(pt.TealTypeError): | ||
pt.Replace( | ||
pt.Bytes("my string"), pt.Bytes("should be int"), pt.Bytes("abcdefghi") | ||
) | ||
|
||
with pytest.raises(pt.TealTypeError): | ||
pt.Replace(pt.Bytes("my string"), pt.Txn.sender(), pt.Bytes("abcdefghi")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdtzmn Unrelated to line: While reviewing the PR, I added a couple of tests in jdtzmn#1.
Can you see if you feel it's worth pulling into the PR? Otherwise, we can close the child PR.