A lot more work on WASM script execution
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-09-07 18:01:26 +02:00
parent f9761f61da
commit b1890681a1
102 changed files with 748 additions and 202 deletions

43
src/utils/string_key.rs Normal file → Executable file
View File

@@ -1,4 +1,5 @@
use std::borrow::Borrow;
use std::ffi::CString;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
@@ -27,23 +28,13 @@ static STRING_CACHE: OnceCell<Mutex<HashMap<u32, Weak<str>>>> = OnceCell::uninit
static EMPTY: OnceCell<StringKey> = OnceCell::uninit();
impl StringKey {
/// Calculates the hash of a string key in a const manner.
pub const fn get_hash_const<const N: usize>(s: &[u8; N]) -> u32 {
let mut crc: u32 = 0xffffffff;
let mut i: usize = 0;
while i < N {
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(s[i]) as u32)) & 0xff) as usize];
i += 1;
}
crc ^ 0xffffffff
}
/// Gets the hash of a string.
pub fn get_hash(s: &str) -> u32 {
pub const fn get_hash(s: &str) -> u32 {
let mut crc: u32 = 0xffffffff;
for byte in s.bytes() {
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(byte) as u32)) & 0xff) as usize];
let mut i: usize = 0;
while i < s.len() {
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(s.as_bytes()[i]) as u32)) & 0xff) as usize];
i += 1;
}
crc ^ 0xffffffff
}
@@ -115,6 +106,18 @@ impl Display for StringKey {
}
}
impl Into<StringKey> for CString {
fn into(self) -> StringKey {
StringKey::new(self.to_str().unwrap())
}
}
impl Into<StringKey> for &CString {
fn into(self) -> StringKey {
StringKey::new(self.to_str().unwrap())
}
}
/// Converts a character to lowercased in a const safe way.
const fn to_lower(c: u8) -> u8 {
if c >= b'A' && c <= b'Z' {
@@ -167,7 +170,7 @@ mod tests {
let sk = StringKey::new("");
assert_eq!(sk.str(), "");
assert_eq!(sk.hash(), 0);
assert_eq!(sk.hash(), StringKey::get_hash_const(b""));
assert_eq!(sk.hash(), StringKey::get_hash(""));
}
#[test]
@@ -175,8 +178,8 @@ mod tests {
let sk = StringKey::new("foo");
assert_eq!(sk.str(), "foo");
assert_eq!(sk.hash(), 2356372769);
assert_eq!(sk.hash(), StringKey::get_hash_const(b"foo"));
assert_eq!(sk.hash(), StringKey::get_hash_const(b"FOo"));
assert_eq!(sk.hash(), StringKey::get_hash("foo"));
assert_eq!(sk.hash(), StringKey::get_hash("FOo"));
}
#[test]
@@ -184,7 +187,7 @@ mod tests {
let sk = StringKey::new("bar");
assert_eq!(sk.str(), "bar");
assert_eq!(sk.hash(), 1996459178);
assert_eq!(sk.hash(), StringKey::get_hash_const(b"bar"));
assert_eq!(sk.hash(), StringKey::get_hash_const(b"baR"));
assert_eq!(sk.hash(), StringKey::get_hash("bar"));
assert_eq!(sk.hash(), StringKey::get_hash("baR"));
}
}