Skip to content

intern #

Description

intern provides a string interning pool. new_pool() creates a pool and pool.intern(text) returns a canonical string whose bytes are shared with every equal string in that pool. The pool clones the first input, so callers can release their original storage. Treat returned strings as immutable and do not free their bytes manually. pool.len() counts distinct strings.

intern.eq(a, b) first compares storage addresses, then compares values when needed. It also works for strings from different pools and ordinary strings. Pools require external synchronization when shared between threads.

fn eq #

fn eq(a string, b string) bool

eq reports whether a and b are equal, trying a pointer comparison first so interned values compare in O(1) without hashing. Plain (non-interned) strings fall back to value comparison and are always correct.

fn new_pool #

fn new_pool() Pool

new_pool creates an empty interning pool.

struct Pool #

struct Pool {
mut:
	canonical map[string]string
}

Pool deduplicates strings: interning the same text twice returns the same backing storage, so repeated identifiers, keys and tags share one copy and compare by pointer first. It is the V-idiomatic equivalent of Go's unique package.

Contract: treat interned strings as immutable. Mutating one's bytes would corrupt the canonical copy shared with every holder.

Persistent (immutable) collections are deliberately out of scope here; they need the GC-interaction design deferred in the tracking issue, plus a merged ordered map to build on.

fn (Pool) intern #

fn (mut p Pool) intern(s string) string

intern returns the canonical copy of s, cloning it on first sight. Repeated calls with equal strings return storage-identical values.

fn (Pool) len #

fn (p &Pool) len() int

len returns the number of distinct interned strings.