Much more readable code!

This commit is contained in:
CosminPerRam 2022-10-16 17:55:48 +03:00
parent c2742fbcf0
commit 3a83588802
2 changed files with 78 additions and 142 deletions

View file

@ -1,18 +1,12 @@
use std::ops::Add;
use crate::GDError;
pub fn concat_u8(first: &[u8], second: &[u8]) -> Vec<u8> {
[first, second].concat()
}
pub fn find_first_null(arr: &[u8]) -> usize {
match arr.iter().position(|&x| x == 0) {
None => arr.len(),
Some(position) => position
}
}
pub fn find_first_string(arr: &[u8]) -> String {
arr.iter().take_while(|&&b| b != 0).map(|&e| e as char).collect::<String>()
std::str::from_utf8(&arr[..arr.iter().position(|&x| x == 0).unwrap()]).unwrap().to_string()
}
pub fn complete_address(address: &str, port: u16) -> String {
@ -30,29 +24,31 @@ pub fn combine_eight_u8(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8)
pub mod buffer {
use super::*;
pub fn get_u8(buf: &[u8], pos: usize) -> (u8, usize) {
(buf[pos], pos + 1)
pub fn get_u8(buf: &[u8], pos: &mut usize) -> Result<u8, GDError> {
let value = buf[*pos];
*pos += 1;
Ok(value)
}
pub fn get_u16(buf: &[u8], pos: usize) -> (u16, usize) {
(combine_two_u8(buf[pos + 1], buf[pos]), pos + 2)
pub fn get_u16(buf: &[u8], pos: &mut usize) -> u16 {
let value = combine_two_u8(buf[*pos + 1], buf[*pos]);
*pos += 2;
value
}
pub fn get_u64(buf: &[u8], pos: usize) -> (u64, usize) {
(combine_eight_u8(buf[pos + 7], buf[pos + 6], buf[pos + 5], buf[pos + 4], buf[pos + 3], buf[pos + 2], buf[pos + 1], buf[pos]), pos + 8)
pub fn get_u64(buf: &[u8], pos: &mut usize) -> u64 {
let value = combine_eight_u8(buf[*pos + 7], buf[*pos + 6], buf[*pos + 5], buf[*pos + 4], buf[*pos + 3], buf[*pos + 2], buf[*pos + 1], buf[*pos]);
*pos += 8;
value
}
pub fn get_string(buf: &[u8], pos: usize) -> (String, usize) {
let string = find_first_string(&buf[pos..]);
let string_size = string.len();
(string, pos + string_size + 1)
pub fn get_string(buf: &[u8], pos: &mut usize) -> String {
let value = find_first_string(&buf[*pos..]);
*pos += value.len() + 1;
value
}
}
pub fn get_u64_from_buf(buf: &[u8]) -> u64 {
combine_eight_u8(buf[7], buf[6], buf[5], buf[4], buf[3], buf[2], buf[1], buf[0])
}
#[cfg(test)]
mod utils {
use super::*;
@ -68,12 +64,6 @@ mod utils {
assert_eq!(b[1], combined[3]);
}
#[test]
fn find_null_in_array_test() {
let arr: [u8; 4] = [0x64, 0x32, 0x00, 0x20];
assert_eq!(2, find_first_null(&arr));
}
#[test]
fn complete_address_test() {
let address = "192.168.0.1";