mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
const new for SendPropIdentifier
This commit is contained in:
parent
c2fe8eca07
commit
9f96257b80
3 changed files with 51 additions and 7 deletions
46
src/consthash.rs
Normal file
46
src/consthash.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
pub struct ConstFnvHash(u64);
|
||||
|
||||
impl ConstFnvHash {
|
||||
pub const fn new() -> Self {
|
||||
Self(0xcbf29ce484222325)
|
||||
}
|
||||
|
||||
pub const fn push_string(self, str: &str) -> Self {
|
||||
self.update(str.as_bytes()).update(&[0xff])
|
||||
}
|
||||
|
||||
pub const fn update(self, bytes: &[u8]) -> Self {
|
||||
let Self(mut hash) = self;
|
||||
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
let byte = bytes[i];
|
||||
hash = hash ^ (byte as u64);
|
||||
hash = hash.wrapping_mul(0x100000001b3);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Self(hash)
|
||||
}
|
||||
|
||||
pub const fn finish(self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const_fnv() {
|
||||
use fnv::FnvHasher;
|
||||
use std::hash::Hash;
|
||||
use std::hash::Hasher;
|
||||
|
||||
let mut hasher = FnvHasher::default();
|
||||
"foobar".hash(&mut hasher);
|
||||
"another input".hash(&mut hasher);
|
||||
let hash = hasher.finish();
|
||||
|
||||
let hasher = ConstFnvHash::new();
|
||||
let hasher = hasher.push_string("foobar");
|
||||
let hasher = hasher.push_string("another input");
|
||||
assert_eq!(hasher.finish(), hash);
|
||||
}
|
||||
|
|
@ -6,16 +6,15 @@ use crate::{ParseError, ReadResult, Result, Stream};
|
|||
|
||||
use super::packet::datatable::ParseSendTable;
|
||||
use super::vector::{Vector, VectorXY};
|
||||
use crate::consthash::ConstFnvHash;
|
||||
use crate::demo::message::stringtable::log_base2;
|
||||
use crate::demo::packet::datatable::SendTableName;
|
||||
use crate::demo::parser::MalformedSendPropDefinitionError;
|
||||
use parse_display::Display;
|
||||
use std::cmp::min;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
use fnv::FnvHasher;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::hash::Hash;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(
|
||||
|
|
@ -766,10 +765,8 @@ impl<'a> TryFrom<&'a SendPropValue> for &'a [SendPropValue] {
|
|||
pub struct SendPropIdentifier(u64);
|
||||
|
||||
impl SendPropIdentifier {
|
||||
pub fn new(table: &str, prop: &str) -> Self {
|
||||
let mut hasher = FnvHasher::default();
|
||||
table.hash(&mut hasher);
|
||||
prop.hash(&mut hasher);
|
||||
pub const fn new(table: &str, prop: &str) -> Self {
|
||||
let hasher = ConstFnvHash::new().push_string(table).push_string(prop);
|
||||
SendPropIdentifier(hasher.finish())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,5 +9,6 @@ pub use crate::demo::{
|
|||
Demo, Stream,
|
||||
};
|
||||
|
||||
pub(crate) mod consthash;
|
||||
pub mod demo;
|
||||
mod nullhasher;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue