introduce and use u8s_to_string

This commit is contained in:
Markus Triska
2022-09-03 20:40:15 +02:00
committed by Mark Thom
parent a891cc4edf
commit 3d04689660

View File

@@ -6278,15 +6278,7 @@ impl Machine {
)
);
let complete_string = {
let buffer = String::from_iter(in_out.iter().map(|b| *b as char));
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
}
};
let complete_string = self.u8s_to_string(&in_out);
unify!(self.machine_st, self.machine_st.registers[6], tag_list);
unify!(self.machine_st, self.machine_st.registers[7], complete_string);
@@ -6355,15 +6347,7 @@ impl Machine {
let scalar = secp256k1::Scalar::decode_reduce(&scalar_bytes);
point *= scalar;
let uncompressed = {
let buffer = String::from_iter(point.encode_uncompressed().iter().map(|b| *b as char));
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
}
};
let uncompressed = self.u8s_to_string(&point.encode_uncompressed());
unify!(self.machine_st, self.machine_st.registers[4], uncompressed);
}
@@ -6371,15 +6355,7 @@ impl Machine {
#[inline(always)]
pub(crate) fn ed25519_new_key_pair(&mut self) {
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(rng()).unwrap();
let complete_string = {
let buffer = String::from_iter(pkcs8_bytes.as_ref().iter().map(|b| *b as char));
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
}
};
let complete_string = self.u8s_to_string(pkcs8_bytes.as_ref());
unify!(self.machine_st, self.machine_st.registers[1], complete_string)
}
@@ -6396,17 +6372,7 @@ impl Machine {
}
};
let complete_string = {
let buffer = String::from_iter(
key_pair.public_key().as_ref().iter().map(|b| *b as char),
);
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
}
};
let complete_string = self.u8s_to_string(key_pair.public_key().as_ref());
unify!(self.machine_st, self.machine_st.registers[2], complete_string);
}
@@ -6469,14 +6435,9 @@ impl Machine {
let result = scalarmult(&scalar, &point).unwrap();
let string = String::from_iter(result[..].iter().map(|b| *b as char));
let cstr = if string.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&string))
};
let string = self.u8s_to_string(&result[..]);
unify!(self.machine_st, self.machine_st.registers[3], cstr);
unify!(self.machine_st, self.machine_st.registers[3], string);
}
#[inline(always)]
@@ -6658,14 +6619,9 @@ impl Machine {
match bytes {
Ok(bs) => {
let string = String::from_iter(bs.iter().map(|b| *b as char));
let cstr = if string.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&string))
};
let string = self.u8s_to_string(&bs);
unify!(self.machine_st, self.machine_st.registers[1], cstr);
unify!(self.machine_st, self.machine_st.registers[1], string);
}
_ => {
self.machine_st.fail = true;
@@ -6972,6 +6928,16 @@ impl Machine {
}
}
}
pub(super) fn u8s_to_string(&mut self, data: &[u8]) -> HeapCellValue {
let buffer = String::from_iter(data.iter().map(|b| *b as char));
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self.machine_st.atom_tbl.build_with(&buffer))
}
}
}
fn rng() -> &'static dyn SecureRandom {