Skip to content

Commit cfda802

Browse files
committedFeb 20, 2025·
add branch for literal bytestrings
1 parent bb45f50 commit cfda802

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed
 

‎vyper/builtins/_convert.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,28 @@ def to_decimal(expr, arg, out_typ):
363363
raise CompilerPanic("unreachable")
364364

365365

366-
@_input_types(IntegerT, DecimalT, BytesM_T, AddressT, BytesT, BoolT)
366+
@_input_types(IntegerT, DecimalT, BytesM_T, AddressT, BytesT, StringT, BoolT)
367367
def to_bytes_m(expr, arg, out_typ):
368368
_check_bytes(expr, arg, out_typ, max_bytes_allowed=out_typ.m)
369369

370-
if isinstance(arg.typ, BytesT):
371-
bytes_val = LOAD(bytes_data_ptr(arg))
372-
373-
# zero out any dirty bytes (which can happen in the last
374-
# word of a bytearray)
375-
len_ = get_bytearray_length(arg)
376-
num_zero_bits = IRnode.from_list(["mul", ["sub", 32, len_], 8])
377-
with num_zero_bits.cache_when_complex("bits") as (b, num_zero_bits):
378-
arg = shl(num_zero_bits, shr(num_zero_bits, bytes_val))
379-
arg = b.resolve(arg)
370+
if isinstance(arg.typ, _BytestringT):
371+
# handle literal bytestrings first
372+
if isinstance(expr, vy_ast.Constant) and arg.typ.length <= out_typ.m:
373+
arg = int(expr.value.encode("utf-8").hex(), 16)
374+
375+
elif isinstance(arg.typ, BytesT):
376+
bytes_val = LOAD(bytes_data_ptr(arg))
377+
378+
# zero out any dirty bytes (which can happen in the last
379+
# word of a bytearray)
380+
len_ = get_bytearray_length(arg)
381+
num_zero_bits = IRnode.from_list(["mul", ["sub", 32, len_], 8])
382+
with num_zero_bits.cache_when_complex("bits") as (b, num_zero_bits):
383+
arg = shl(num_zero_bits, shr(num_zero_bits, bytes_val))
384+
arg = b.resolve(arg)
385+
386+
else:
387+
_FAIL(arg.typ, out_typ, expr)
380388

381389
elif is_bytes_m_type(arg.typ):
382390
# clamp if it's a downcast

0 commit comments

Comments
 (0)
Please sign in to comment.