mirror of
https://github.com/typst/typst
synced 2025-08-18 00:48:34 +08:00
Add PicoStr::get
This commit is contained in:
parent
9a6268050f
commit
830611ceef
@ -65,6 +65,23 @@ impl PicoStr {
|
|||||||
id
|
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`.
|
/// Creates a compile-time constant `PicoStr`.
|
||||||
///
|
///
|
||||||
/// Should only be used in const contexts because it can panic.
|
/// Should only be used in const contexts because it can panic.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user