ass_core/parser/streaming/source.rs
1//! Source reconstruction helpers for incremental edits
2//!
3//! Provides [`build_modified_source`], a zero-fuss utility for producing a new
4//! source string by replacing a byte range with replacement text. Used by the
5//! incremental parsing path to materialize edited documents.
6
7use alloc::string::String;
8use core::ops::Range;
9
10/// Build modified source with range replacement
11///
12/// Creates a new source string by replacing the specified range with new text.
13///
14/// # Arguments
15///
16/// * `original` - The original source text
17/// * `range` - The byte range to replace
18/// * `replacement` - The text to insert in place of the range
19///
20/// # Returns
21///
22/// A new string with the replacement applied
23#[must_use]
24pub fn build_modified_source(original: &str, range: Range<usize>, replacement: &str) -> String {
25 let mut result =
26 String::with_capacity(original.len() - (range.end - range.start) + replacement.len());
27
28 // Add text before the range
29 result.push_str(&original[..range.start]);
30
31 // Add replacement text
32 result.push_str(replacement);
33
34 // Add text after the range
35 result.push_str(&original[range.end..]);
36
37 result
38}