Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/CRC-32/Rust/crc-32.rust
Normal file
26
Task/CRC-32/Rust/crc-32.rust
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
fn crc32_compute_table() -> [u32; 256] {
|
||||
let mut crc32_table = [0; 256];
|
||||
|
||||
for n in 0..256 {
|
||||
crc32_table[n as usize] = (0..8).fold(n as u32, |acc, _| {
|
||||
match acc & 1 {
|
||||
1 => 0xedb88320 ^ (acc >> 1),
|
||||
_ => acc >> 1,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
crc32_table
|
||||
}
|
||||
|
||||
fn crc32(buf: &str) -> u32 {
|
||||
let crc_table = crc32_compute_table();
|
||||
|
||||
!buf.bytes().fold(!0, |acc, octet| {
|
||||
(acc >> 8) ^ crc_table[((acc & 0xff) ^ octet as u32) as usize]
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog"));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue