refactor: edit function signatures and rewrite a few code sections

This commit is contained in:
doryan 2024-08-10 01:13:00 +04:00
parent 5a8a4b00ce
commit 78d52388f7

View File

@ -40,8 +40,9 @@ pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String> {
} }
} }
pub fn hamming_encrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) { pub fn hamming_encrypt_data(data: &[u8], length_of_code: usize) -> String {
let mut i: usize = length_of_code; let mut i: usize = length_of_code;
let mut result = String::new();
while i <= data.len() { while i <= data.len() {
let data_bits: &[u8] = &data[i - length_of_code..i]; let data_bits: &[u8] = &data[i - length_of_code..i];
@ -50,17 +51,23 @@ pub fn hamming_encrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
data_bits[0] ^ data_bits[2] ^ data_bits[3], data_bits[0] ^ data_bits[2] ^ data_bits[3],
data_bits[1] ^ data_bits[2] ^ data_bits[3], data_bits[1] ^ data_bits[2] ^ data_bits[3],
); );
result_string.push_str(&*format!(
"{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ", result.push_str(
data_bits[0], data_bits[1], data_bits[2], data_bits[3] format!(
)); "{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ",
data_bits[0], data_bits[1], data_bits[2], data_bits[3]
)
.as_str(),
);
i += length_of_code; i += length_of_code;
} }
result
} }
pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) { pub fn hamming_decrypt_data(data: &[u8], length_of_code: usize) -> String {
let mut general_length: usize = length_of_code; let mut general_length: usize = length_of_code;
let mut result = String::new();
let mut errors: String = String::new(); let mut errors: String = String::new();
@ -77,7 +84,7 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
general_length += length_of_code; general_length += length_of_code;
continue; continue;
} else { } else {
let error_position: &usize = syndromes let error_position: &usize = SYNDROMES
.iter() .iter()
.find(move |&(&_error_position, &error)| error == checked_bits) .find(move |&(&_error_position, &error)| error == checked_bits)
.unwrap() .unwrap()
@ -113,11 +120,11 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
if !data.is_empty() { if !data.is_empty() {
if errors.is_empty() { if errors.is_empty() {
result_string.push_str("Все коды корректны."); result.push_str("Все коды корректны.");
} else { } else {
result_string.push_str(errors.as_str()); result.push_str(errors.as_str());
} }
} else {
result_string.push_str("Введите код для проверки.")
} }
result
} }