Reduce memory usage of Docstring struct (#16183)

This commit is contained in:
Alex Waygood 2025-02-16 15:23:52 +00:00 committed by GitHub
parent 93aff36147
commit 61fef0a64a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 151 additions and 90 deletions

View file

@ -1645,6 +1645,16 @@ impl StringLiteral {
flags: StringLiteralFlags::empty().with_invalid(),
}
}
/// The range of the string literal's contents.
///
/// This excludes any prefixes, opening quotes or closing quotes.
pub fn content_range(&self) -> TextRange {
TextRange::new(
self.start() + self.flags.opener_len(),
self.end() - self.flags.closer_len(),
)
}
}
impl From<StringLiteral> for Expr {

View file

@ -1,3 +1,5 @@
use ruff_text_size::TextSize;
use std::fmt;
/// Enumerations of the valid prefixes a string literal can have.
@ -33,6 +35,13 @@ impl StringLiteralPrefix {
Self::Raw { uppercase: false } => "r",
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Empty => TextSize::new(0),
Self::Unicode | Self::Raw { .. } => TextSize::new(1),
}
}
}
impl fmt::Display for StringLiteralPrefix {