Improved match statement to make it more readable and slightly faster

This commit is contained in:
Dreaded_X 2022-12-08 00:29:44 +01:00
parent dc44463638
commit d0bef9e654
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9

View File

@ -67,42 +67,27 @@ mod implementation {
input input
.lines() .lines()
.flat_map(|line| line.split_once(" ")) .map(|line| line.rsplit_once(" ").unwrap())
.for_each(|(typ, rest)| { .for_each(|split| {
match typ { match split {
"$" => { ("$ cd", "/") => current = &mut root as *mut Node,
// There are two cmds: ls, cd ${dir}, we only care about dir ("$ cd", "..") => unsafe {
if let Some((_, dir)) = rest.split_once(" ") { let parent = (*current).parent;
match dir { if parent == std::ptr::null_mut() {
"/" => current = &mut root as *mut Node, panic!("Node has no parent")
".." => {
unsafe {
let parent = (*current).parent;
if parent == std::ptr::null_mut() {
panic!("Node has no parent")
}
current = parent;
}
}
name => {
unsafe {
current = (*current).get_directory(name).unwrap();
}
},
};
} }
current = parent;
}, },
"dir" => { ("$ cd", name) => unsafe {
unsafe { current = (*current).get_directory(name).unwrap();
(*current).add_directory(rest.to_owned())
}
}, },
size => { ("$", "ls") => {},
unsafe { ("dir", name) => unsafe {
(*current).add_file(size.parse().unwrap()) (*current).add_directory(name.to_owned())
} },
(size, _name) => unsafe {
(*current).add_file(size.parse().unwrap())
}, },
} }
}); });