summaryrefslogtreecommitdiff
path: root/crates/typst-utils
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-11-11 15:11:44 +0100
committerLaurenz <laurmaedje@gmail.com>2024-11-13 10:21:40 +0100
commit525154a730dfdb224fe2ced3dae0cfb33114fafa (patch)
treec81f490cb6b2091146d787fdc044f7be530d8181 /crates/typst-utils
parent5625914872b2824388ce65f3fb184d913f29cbff (diff)
Add support for raw range spans
Diffstat (limited to 'crates/typst-utils')
-rw-r--r--crates/typst-utils/src/pico.rs10
1 files changed, 7 insertions, 3 deletions
diff --git a/crates/typst-utils/src/pico.rs b/crates/typst-utils/src/pico.rs
index dcab39b6..7fcd3343 100644
--- a/crates/typst-utils/src/pico.rs
+++ b/crates/typst-utils/src/pico.rs
@@ -1,6 +1,7 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
+use std::num::NonZeroU32;
use std::sync::{LazyLock, RwLock};
/// The global string interner.
@@ -21,7 +22,7 @@ struct Interner {
/// unnecessarily. For this reason, the user should use the [`PicoStr::resolve`]
/// method to get the underlying string, such that the lookup is done only once.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
-pub struct PicoStr(u32);
+pub struct PicoStr(NonZeroU32);
impl PicoStr {
/// Creates a new interned string.
@@ -38,7 +39,10 @@ impl PicoStr {
// Create a new entry forever by leaking the string. PicoStr is only
// used for strings that aren't created en masse, so it is okay.
- let num = interner.from_id.len().try_into().expect("out of string ids");
+ let num = u32::try_from(interner.from_id.len() + 1)
+ .and_then(NonZeroU32::try_from)
+ .expect("out of string ids");
+
let id = Self(num);
let string = Box::leak(string.to_string().into_boxed_str());
interner.to_id.insert(string, id);
@@ -48,7 +52,7 @@ impl PicoStr {
/// Resolves the interned string.
pub fn resolve(&self) -> &'static str {
- INTERNER.read().unwrap().from_id[self.0 as usize]
+ INTERNER.read().unwrap().from_id[(self.0.get() - 1) as usize]
}
}