aiscript_vm/string/
mod.rs

1mod interned;
2mod utils;
3
4use aiscript_arena::Gc;
5pub use interned::{InternedString, InternedStringSet};
6
7use crate::{Value, vm::Context};
8
9/// Internal enum to handle string operations
10pub enum StringValue<'gc> {
11    Interned(InternedString<'gc>),
12    Dynamic(Gc<'gc, String>),
13}
14
15impl<'gc> StringValue<'gc> {
16    pub fn as_str(&self) -> &str {
17        match self {
18            StringValue::Interned(s) => s.to_str().unwrap(),
19            StringValue::Dynamic(s) => s,
20        }
21    }
22
23    // Convert back to Value based on heuristics
24    pub fn into_value(self, ctx: Context<'gc>, should_intern: bool) -> Value<'gc> {
25        match (self, should_intern) {
26            (StringValue::Interned(s), _) => Value::String(s),
27            (StringValue::Dynamic(s), true) => Value::String(ctx.intern(s.as_bytes())),
28            (StringValue::Dynamic(s), false) => Value::IoString(s),
29        }
30    }
31}