]> Repositorios git - scryer-prolog.git/commitdiff
introduce and use u8s_to_string
authorMarkus Triska <[email protected]>
Sat, 3 Sep 2022 18:40:15 +0000 (20:40 +0200)
committerMark Thom <[email protected]>
Thu, 27 Oct 2022 05:36:07 +0000 (23:36 -0600)
src/machine/system_calls.rs

index aaeb13dd66f16d506991b613ae5e477a284980ac..5b52e795d25054c086cf023f6e67a33b3c1cb790 100644 (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 {