Serialize zero as [0] rather than [] in bytecode (#592)

This commit is contained in:
Daniel Lubarov 2022-07-01 09:29:56 -07:00 committed by GitHub
parent bc9e618967
commit f6d48f1328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,7 +40,7 @@ pub(crate) enum Literal {
impl Literal {
pub(crate) fn to_trimmed_be_bytes(&self) -> Vec<u8> {
let u256 = self.to_u256();
let num_bytes = ceil_div_usize(u256.bits(), 8);
let num_bytes = ceil_div_usize(u256.bits(), 8).max(1);
// `byte` is little-endian, so we manually reverse it.
(0..num_bytes).rev().map(|i| u256.byte(i)).collect()
}
@ -70,6 +70,11 @@ mod tests {
#[test]
fn literal_to_be_bytes() {
assert_eq!(
Literal::Decimal("0".into()).to_trimmed_be_bytes(),
vec![0x00]
);
assert_eq!(
Literal::Decimal("768".into()).to_trimmed_be_bytes(),
vec![0x03, 0x00]