use std::sync::OnceLock;
use super::{LineIndex, Position, Span};
#[derive(Debug, Clone)]
pub struct SourceText {
text: String,
index: OnceLock<LineIndex>,
}
impl SourceText {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
index: OnceLock::new(),
}
}
pub fn as_str(&self) -> &str {
&self.text
}
pub fn len(&self) -> usize {
self.text.len()
}
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub fn slice(&self, span: Span) -> &str {
&self.text[span.start..span.end]
}
pub fn position(&self, offset: usize) -> Position {
self.line_index().position(&self.text, offset)
}
pub fn offset(&self, position: Position) -> Option<usize> {
self.line_index().offset(&self.text, position)
}
pub fn line(&self, line: usize) -> Option<&str> {
self.line_index().line_text(&self.text, line)
}
pub fn line_index(&self) -> &LineIndex {
self.index.get_or_init(|| LineIndex::new(&self.text))
}
}
impl From<String> for SourceText {
fn from(text: String) -> Self {
SourceText::new(text)
}
}
impl From<&str> for SourceText {
fn from(text: &str) -> Self {
SourceText::new(text)
}
}