pub struct Interner { /* private fields */ }Expand description
The string interner for Boa.
Implementations§
Source§impl Interner
impl Interner
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of strings interned by the interner.
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
let initial_len = interner.len();
interner.get_or_intern("hello");
assert_eq!(interner.len(), initial_len + 1);Sourcepub fn get<'a, T>(&self, string: T) -> Option<Sym>
pub fn get<'a, T>(&self, string: T) -> Option<Sym>
Returns the symbol for the given string if any.
Can be used to query if a string has already been interned without interning.
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
assert!(interner.get("hello").is_none());
interner.get_or_intern("hello");
assert!(interner.get("hello").is_some());Sourcepub fn get_or_intern<'a, T>(&mut self, string: T) -> Sym
pub fn get_or_intern<'a, T>(&mut self, string: T) -> Sym
Interns the given string.
Returns a symbol for resolution into the original string.
§Panics
If the interner already interns the maximum number of strings possible by the chosen symbol type.
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
let sym1 = interner.get_or_intern("hello");
let sym2 = interner.get_or_intern("hello");
assert_eq!(sym1, sym2);
let sym3 = interner.get_or_intern("world");
assert_ne!(sym1, sym3);Sourcepub fn get_or_intern_static(
&mut self,
utf8: &'static str,
utf16: &'static [u16],
) -> Sym
pub fn get_or_intern_static( &mut self, utf8: &'static str, utf16: &'static [u16], ) -> Sym
Interns the given 'static string.
Returns a symbol for resolution into the original string.
§Note
This is more efficient than Interner::get_or_intern, since it avoids allocating space
for one string inside the Interner, with the disadvantage that you need to provide
both the UTF-8 and the UTF-16 representation of the string.
§Panics
If the interner already interns the maximum number of strings possible by the chosen symbol type.
§Examples
use boa_interner::Interner;
static HELLO_UTF16: &[u16] = &[0x68, 0x65, 0x6C, 0x6C, 0x6F];
let mut interner = Interner::new();
let sym1 = interner.get_or_intern_static("hello", HELLO_UTF16);
let sym2 = interner.get_or_intern("hello");
assert_eq!(sym1, sym2);Sourcepub fn resolve(&self, symbol: Sym) -> Option<JSInternedStrRef<'_, '_>>
pub fn resolve(&self, symbol: Sym) -> Option<JSInternedStrRef<'_, '_>>
Returns the string for the given symbol if any.
§Panics
Panics if the size of both statics is not equal or the interners do not have the same size
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let resolved = interner.resolve(sym);
assert!(resolved.is_some());
assert_eq!(resolved.unwrap().utf8(), Some("hello"));Sourcepub fn resolve_expect(&self, symbol: Sym) -> JSInternedStrRef<'_, '_>
pub fn resolve_expect(&self, symbol: Sym) -> JSInternedStrRef<'_, '_>
Returns the string for the given symbol.
§Panics
If the interner cannot resolve the given symbol.
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
let sym = interner.get_or_intern("hello");
let resolved = interner.resolve_expect(sym);
assert_eq!(resolved.utf8(), Some("hello"));Sourcepub fn is_latin1(&self, symbol: Sym) -> bool
pub fn is_latin1(&self, symbol: Sym) -> bool
Returns true if the string identified by symbol can be encoded as Latin1
(i.e. all code units are in the range 0x00..=0xFF).
This information is computed once when the string is first interned, so callers pay no O(n) scanning cost beyond the initial intern call.
§Examples
use boa_interner::Interner;
let mut interner = Interner::new();
let ascii = interner.get_or_intern("hello");
assert!(interner.is_latin1(ascii));
let non_latin1: Vec<u16> = vec![0x4e2d, 0x6587]; // "中文"
let sym = interner.get_or_intern(non_latin1.as_slice());
assert!(!interner.is_latin1(sym));