Only allocate memory if removing text for lesser blocks.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::is_not;
|
||||
use nom::bytes::complete::tag;
|
||||
@@ -651,6 +653,11 @@ fn switch_word<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
))(input)
|
||||
}
|
||||
|
||||
enum ContentState {
|
||||
Normal,
|
||||
Modified(String),
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "tracing",
|
||||
tracing::instrument(ret, level = "debug", skip(context))
|
||||
@@ -658,8 +665,8 @@ fn switch_word<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
pub(crate) fn content<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, String> {
|
||||
let mut ret = String::new();
|
||||
) -> Res<OrgSource<'s>, Cow<'s, str>> {
|
||||
let mut state = ContentState::Normal;
|
||||
let mut remaining = input;
|
||||
let exit_matcher_parser = parser_with_context!(exit_matcher_parser)(context);
|
||||
loop {
|
||||
@@ -669,13 +676,28 @@ pub(crate) fn content<'b, 'g, 'r, 's>(
|
||||
|
||||
let (remain, (pre_escape_whitespace, line)) = content_line(remaining)?;
|
||||
if let Some(val) = pre_escape_whitespace {
|
||||
ret.push_str(Into::<&str>::into(val));
|
||||
if let ContentState::Modified(ref mut ret) = state {
|
||||
ret.push_str(Into::<&str>::into(val));
|
||||
} else {
|
||||
let mut ret = String::new();
|
||||
ret.push_str(Into::<&str>::into(input.get_until(remaining)));
|
||||
ret.push_str(Into::<&str>::into(val));
|
||||
state = ContentState::Modified(ret);
|
||||
}
|
||||
}
|
||||
if let ContentState::Modified(ref mut ret) = state {
|
||||
ret.push_str(line.into());
|
||||
}
|
||||
ret.push_str(line.into());
|
||||
remaining = remain;
|
||||
}
|
||||
|
||||
Ok((remaining, ret))
|
||||
match state {
|
||||
ContentState::Normal => Ok((
|
||||
remaining,
|
||||
Cow::Borrowed(Into::<&str>::into(input.get_until(remaining))),
|
||||
)),
|
||||
ContentState::Modified(ret) => Ok((remaining, Cow::Owned(ret))),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
|
||||
Reference in New Issue
Block a user