From 830611ceef2e5f85c1ea1756a000a94f1c80e4a4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 15 Jul 2025 10:43:53 +0200 Subject: [PATCH] Add `PicoStr::get` --- crates/typst-utils/src/pico.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/typst-utils/src/pico.rs b/crates/typst-utils/src/pico.rs index 9f4a2fde7..5f17a2672 100644 --- a/crates/typst-utils/src/pico.rs +++ b/crates/typst-utils/src/pico.rs @@ -65,6 +65,23 @@ impl PicoStr { id } + /// Try to create a `PicoStr`, but don't intern it if it does not exist yet. + /// + /// This is useful to try to compare against one or multiple `PicoStr` + /// without interning needlessly. + /// + /// Will always return `Some(_)` if the string can be represented inline. + pub fn get(string: &str) -> Option { + // Try to use bitcode or exception representations. + if let Ok(value) = PicoStr::try_constant(string) { + return Some(value); + } + + // Try to find an existing entry that we can reuse. + let interner = INTERNER.read().unwrap(); + interner.seen.get(string).copied() + } + /// Creates a compile-time constant `PicoStr`. /// /// Should only be used in const contexts because it can panic.