1#[derive(Debug, Clone)]
7pub struct LuaString {
8 bytes: std::rc::Rc<[u8]>,
9 is_short: bool,
10 hash: u32,
11}
12
13impl LuaString {
14 pub fn from_bytes(b: Vec<u8>) -> Self {
15 let is_short = b.len() <= 40;
16 let hash = Self::hash_bytes(&b, 0);
17 LuaString { bytes: b.into(), is_short, hash }
18 }
19
20 pub fn placeholder() -> Self { Self::from_bytes(Vec::new()) }
21
22 pub fn as_bytes(&self) -> &[u8] { &self.bytes }
23 pub fn len(&self) -> usize { self.bytes.len() }
24 pub fn is_empty(&self) -> bool { self.bytes.is_empty() }
25 pub fn is_short(&self) -> bool { self.is_short }
26 pub fn is_long(&self) -> bool { !self.is_short }
27 pub fn hash(&self) -> u32 { self.hash }
28 pub fn buffer_bytes(&self) -> usize {
29 self.bytes.len() + 2 * std::mem::size_of::<usize>()
30 }
31
32 pub fn is_reserved_word(&self) -> bool {
33 false
35 }
36
37 pub fn hash_bytes(bytes: &[u8], seed: u32) -> u32 {
38 let mut h: u32 = seed.wrapping_add(0x9e3779b9);
41 for &b in bytes {
42 h = h.wrapping_mul(31).wrapping_add(b as u32);
43 }
44 h
45 }
46
47 pub fn hash_long(&mut self) -> u32 { self.hash }
48}
49
50impl PartialEq for LuaString {
51 fn eq(&self, other: &Self) -> bool { self.bytes == other.bytes }
52}
53impl Eq for LuaString {}
54
55