Skip to content

Commit 08bce3a

Browse files
authored
Also trim leading new lines for chunks in SplitRecursively. #109 (#131)
1 parent 6fef73b commit 08bce3a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/ops/functions/split_recursively.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,18 @@ impl<'t, 's: 't> RecursiveChunker<'s> {
272272

273273
fn add_output(&self, range: RangeValue, output: &mut Vec<(RangeValue, &'s str)>) {
274274
let text = range.extract_str(self.full_text);
275-
let trimmed_text = text.trim_end();
275+
276+
// Trim leading new lines.
277+
let trimmed_text = text.trim_start_matches(['\n', '\r']);
278+
let adjusted_start = range.start + (text.len() - trimmed_text.len());
279+
280+
// Trim trailing whitespaces
281+
let trimmed_text = trimmed_text.trim_end();
282+
283+
// Only record non-empty chunks.
276284
if !trimmed_text.is_empty() {
277285
output.push((
278-
RangeValue::new(range.start, range.start + trimmed_text.len()),
286+
RangeValue::new(adjusted_start, adjusted_start + trimmed_text.len()),
279287
trimmed_text,
280288
));
281289
}

0 commit comments

Comments
 (0)