mirror of
https://github.com/typst/typst
synced 2025-08-17 16:38:31 +08:00
Add PicoStr::get
This commit is contained in:
parent
9a6268050f
commit
830611ceef
@ -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<PicoStr> {
|
||||
// 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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user