Switch to handling the unescaping during the initial parsing.

This preserves the line ending characters unlike the rust .lines() iterator.
This commit is contained in:
Tom Alexander
2023-10-04 13:08:24 -04:00
parent afb43ff34f
commit 7ee48ff65c
2 changed files with 23 additions and 17 deletions

View File

@@ -45,7 +45,7 @@ pub struct ExampleBlock<'s> {
pub retain_labels: bool,
pub use_labels: bool,
pub label_format: Option<&'s str>,
pub contents: &'s str,
pub contents: Vec<&'s str>,
}
#[derive(Debug)]
@@ -241,21 +241,12 @@ impl<'s> Comment<'s> {
impl<'s> ExampleBlock<'s> {
/// Get the inner contents of the ExampleBlock with the escaping commas removed.
pub fn get_contents(&self) -> String {
let mut ret = String::with_capacity(self.contents.len());
for line in self.contents.lines() {
let first_comma = line.find(",#+").or_else(|| line.find(",*"));
if let Some(first_comma) = first_comma {
let before_first_comma = &line[..first_comma];
let is_escaping_comma = before_first_comma.chars().all(char::is_whitespace);
if is_escaping_comma {
ret.push_str(&line[(first_comma + 1)..]);
} else {
ret.push_str(line);
}
} else {
ret.push_str(line);
}
let final_size = self.contents.iter().map(|line| line.len()).sum();
let mut ret = String::with_capacity(final_size);
for line in &self.contents {
ret.push_str(line);
}
ret
}
}