Skip to content

Commit b41064f

Browse files
committed
rm write_to_string
1 parent 0fe731b commit b41064f

File tree

8 files changed

+3
-145
lines changed

8 files changed

+3
-145
lines changed

src/cached_source.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ impl Source for CachedSource {
136136
}
137137
}
138138

139-
fn write_to_string(&self, string: &mut String) {
140-
self.inner.write_to_string(string);
141-
}
142-
143139
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
144140
self.inner.to_writer(writer)
145141
}

src/concat_source.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ impl Source for ConcatSource {
167167
if children.len() == 1 {
168168
children[0].source()
169169
} else {
170-
// Use to_writer to avoid multiple heap allocations that would occur
171-
// when concatenating nested ConcatSource instances directly
172170
let mut string = String::with_capacity(self.size());
173-
self.write_to_string(&mut string);
171+
for chunk in self.rope() {
172+
string.push_str(chunk);
173+
}
174174
SourceValue::String(Cow::Owned(string))
175175
}
176176
}
@@ -215,12 +215,6 @@ impl Source for ConcatSource {
215215
result
216216
}
217217

218-
fn write_to_string(&self, string: &mut String) {
219-
for child in self.optimized_children() {
220-
child.write_to_string(string);
221-
}
222-
}
223-
224218
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
225219
for child in self.optimized_children() {
226220
child.to_writer(writer)?;

src/original_source.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ impl Source for OriginalSource {
7777
get_map(object_pool, chunks.as_ref(), options)
7878
}
7979

80-
fn write_to_string(&self, string: &mut String) {
81-
string.push_str(self.value.as_ref());
82-
}
83-
8480
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
8581
writer.write_all(self.value.as_bytes())
8682
}

src/raw_source.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ impl Source for RawStringSource {
8080
None
8181
}
8282

83-
fn write_to_string(&self, string: &mut String) {
84-
string.push_str(self.0.as_ref());
85-
}
86-
8783
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
8884
writer.write_all(self.0.as_bytes())
8985
}
@@ -230,10 +226,6 @@ impl Source for RawBufferSource {
230226
None
231227
}
232228

233-
fn write_to_string(&self, string: &mut String) {
234-
string.push_str(self.get_or_init_value_as_string());
235-
}
236-
237229
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
238230
writer.write_all(&self.value)
239231
}

src/replace_source.rs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -238,106 +238,6 @@ impl Source for ReplaceSource {
238238
get_map(&ObjectPool::default(), chunks.as_ref(), options)
239239
}
240240

241-
#[allow(unsafe_code)]
242-
fn write_to_string(&self, string: &mut String) {
243-
let inner_chunks = self.inner.rope();
244-
245-
let mut pos: usize = 0;
246-
let mut replacement_idx: usize = 0;
247-
let mut replacement_end: Option<usize> = None;
248-
let mut next_replacement: Option<usize> = (replacement_idx
249-
< self.replacements.len())
250-
.then(|| self.replacements[replacement_idx].start as usize);
251-
252-
'chunk_loop: for chunk in inner_chunks {
253-
let mut chunk_pos = 0;
254-
let end_pos = pos + chunk.len();
255-
256-
// Skip over when it has been replaced
257-
if let Some(replacement_end) =
258-
replacement_end.filter(|replacement_end| *replacement_end > pos)
259-
{
260-
// Skip over the whole chunk
261-
if replacement_end >= end_pos {
262-
pos = end_pos;
263-
continue;
264-
}
265-
// Partially skip over chunk
266-
chunk_pos = replacement_end - pos;
267-
pos += chunk_pos;
268-
}
269-
270-
// Is a replacement in the chunk?
271-
while let Some(next_replacement_pos) = next_replacement
272-
.filter(|next_replacement_pos| *next_replacement_pos < end_pos)
273-
{
274-
if next_replacement_pos > pos {
275-
// Emit chunk until replacement
276-
let offset = next_replacement_pos - pos;
277-
let chunk_slice =
278-
unsafe { &chunk.get_unchecked(chunk_pos..(chunk_pos + offset)) };
279-
string.push_str(chunk_slice);
280-
chunk_pos += offset;
281-
pos = next_replacement_pos;
282-
}
283-
// Insert replacement content split into chunks by lines
284-
let replacement =
285-
unsafe { &self.replacements.get_unchecked(replacement_idx) };
286-
string.push_str(&replacement.content);
287-
288-
// Remove replaced content by settings this variable
289-
replacement_end = if let Some(replacement_end) = replacement_end {
290-
Some(replacement_end.max(replacement.end as usize))
291-
} else {
292-
Some(replacement.end as usize)
293-
};
294-
295-
// Move to next replacement
296-
replacement_idx += 1;
297-
next_replacement = if replacement_idx < self.replacements.len() {
298-
Some(unsafe {
299-
self.replacements.get_unchecked(replacement_idx).start as usize
300-
})
301-
} else {
302-
None
303-
};
304-
305-
// Skip over when it has been replaced
306-
let offset = chunk.len() as i64 - end_pos as i64
307-
+ replacement_end.unwrap() as i64
308-
- chunk_pos as i64;
309-
if offset > 0 {
310-
// Skip over whole chunk
311-
if replacement_end
312-
.is_some_and(|replacement_end| replacement_end >= end_pos)
313-
{
314-
pos = end_pos;
315-
continue 'chunk_loop;
316-
}
317-
318-
// Partially skip over chunk
319-
chunk_pos += offset as usize;
320-
pos += offset as usize;
321-
}
322-
}
323-
324-
// Emit remaining chunk
325-
if chunk_pos < chunk.len() {
326-
let chunk = unsafe { &chunk.get_unchecked(chunk_pos..) };
327-
string.push_str(chunk);
328-
}
329-
pos = end_pos;
330-
}
331-
332-
// Handle remaining replacements one by one
333-
while replacement_idx < self.replacements.len() {
334-
let content =
335-
unsafe { &self.replacements.get_unchecked(replacement_idx).content };
336-
string.push_str(content);
337-
replacement_idx += 1;
338-
}
339-
}
340-
341241
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
342242
for text in self.rope() {
343243
writer.write_all(text.as_bytes())?;

src/source.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,6 @@ pub trait Source:
135135
self.dyn_hash(state);
136136
}
137137

138-
/// Appends the source content to the provided string buffer.
139-
///
140-
/// This method efficiently writes the source content directly into an existing
141-
/// string buffer, avoiding additional memory allocations when the buffer has
142-
/// sufficient capacity. This is particularly useful for concatenating multiple
143-
/// sources or building larger strings incrementally.
144-
fn write_to_string(&self, string: &mut String);
145-
146138
/// Writes the source into a writer, preferably a `std::io::BufWriter<std::io::Write>`.
147139
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()>;
148140
}
@@ -172,10 +164,6 @@ impl Source for BoxSource {
172164
self.as_ref().map(object_pool, options)
173165
}
174166

175-
fn write_to_string(&self, string: &mut String) {
176-
self.as_ref().write_to_string(string)
177-
}
178-
179167
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
180168
self.as_ref().to_writer(writer)
181169
}

src/source_map_source.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ impl Source for SourceMapSource {
118118
get_map(object_pool, chunks.as_ref(), options)
119119
}
120120

121-
fn write_to_string(&self, string: &mut String) {
122-
string.push_str(self.value.as_ref());
123-
}
124-
125121
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
126122
writer.write_all(self.value.as_bytes())
127123
}

tests/compat_source.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ impl Source for CompatSource {
3939
self.1.clone()
4040
}
4141

42-
fn write_to_string(&self, string: &mut String) {
43-
string.push_str(self.0.as_ref())
44-
}
45-
4642
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
4743
writer.write_all(self.0.as_bytes())
4844
}

0 commit comments

Comments
 (0)