2 releases
0.1.1 | Apr 3, 2023 |
---|---|
0.1.0 | Apr 2, 2023 |
#57 in #string-interning
12KB
219 lines
ry_interner
crate.
Extracted 327 lines of Rust code that implement string internering for Ry programming language compiler.
Example
fn main() {
let interner = Interner::default();
let s1 = interner.get_or_intern("test");
let s2 = interner.get_or_intern("test");
assert_eq!(s1, s2);
assert_eq!(interner.resolve(0).unwrap(), "test");
}
lib.rs
:
327 lines of Rust code that implement string internering for Ry programming language compiler.
The crate caches strings and associates them with unique symbols. These allows constant time comparisons and look-ups to underlying interned strings.
Examples:
Internings:
use ry_interner::Interner;
let mut interner = Interner::default();
let symbol0 = interner.get_or_intern("A");
let symbol1 = interner.get_or_intern("B");
let symbol2 = interner.get_or_intern("C");
let symbol3 = interner.get_or_intern("A");
assert_ne!(symbol0, symbol1);
assert_ne!(symbol0, symbol2);
assert_ne!(symbol1, symbol2);
assert_eq!(symbol0, symbol3);
Resolving symbols:
use ry_interner::Interner;
let mut interner = Interner::default();
let symbol0 = interner.get_or_intern("A");
let symbol1 = interner.get_or_intern("B");
assert_eq!(interner.resolve(0), Some("A"));
assert_eq!(interner.resolve(1), Some("B"));
assert_eq!(interner.resolve(2), None);
Dependencies
~1.5MB
~24K SLoC