Skip to main content

lua_types/
string.rs

1//! `LuaString` — Lua's byte-string (NOT UTF-8). PORT_STRATEGY §3.3.
2//!
3//! Phase A-C: a simple `Rc<[u8]>`-backed struct with a short/long flag.
4//! Phase D may revisit for interning + content-hash equality.
5
6#[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
29    pub fn is_reserved_word(&self) -> bool {
30        // TODO(port): proper reserved-word check via lexer's token enum.
31        false
32    }
33
34    pub fn hash_bytes(bytes: &[u8], seed: u32) -> u32 {
35        // Stub WyHash. Real impl ports bun_wyhash. Stable for now so
36        // intern-table equality works.
37        let mut h: u32 = seed.wrapping_add(0x9e3779b9);
38        for &b in bytes {
39            h = h.wrapping_mul(31).wrapping_add(b as u32);
40        }
41        h
42    }
43
44    pub fn hash_long(&mut self) -> u32 { self.hash }
45}
46
47impl PartialEq for LuaString {
48    fn eq(&self, other: &Self) -> bool { self.bytes == other.bytes }
49}
50impl Eq for LuaString {}
51
52// ──────────────────────────────────────────────────────────────────────────────
53// PORT STATUS
54//   source:        src/lstring.h, src/lstring.c (TString)
55//   target_crate:  lua-types
56//   confidence:    high
57//   todos:         0
58//   port_notes:    0
59//   unsafe_blocks: 0
60//   notes:         LuaString interned-string type. Mirrors C's TString with the short/long
61//                  variant distinction and the hash field; uses GcRef-style ptr
62//                  identity for interning.
63// ──────────────────────────────────────────────────────────────────────────────