Only allocate memory if removing text for lesser blocks.
All checks were successful
clippy Build clippy has succeeded
rust-build Build rust-build has succeeded
rust-foreign-document-test Build rust-foreign-document-test has succeeded
rust-test Build rust-test has succeeded

This commit is contained in:
Tom Alexander 2023-10-27 21:28:12 -04:00
parent ee201e1336
commit 816c164996
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 35 additions and 14 deletions

View File

@ -1,3 +0,0 @@
foo <<bar>> baz
lorem << ipsum >> dolar

View File

@ -1576,7 +1576,7 @@ fn compare_example_block<'b, 's>(
[], [],
( (
EmacsField::Required(":value"), EmacsField::Required(":value"),
|r| Some(r.contents.as_str()), |r| Some(&r.contents),
compare_property_quoted_string compare_property_quoted_string
), ),
( (
@ -1654,7 +1654,7 @@ fn compare_export_block<'b, 's>(
), ),
( (
EmacsField::Required(":value"), EmacsField::Required(":value"),
|r| Some(r.contents.as_str()), |r| Some(&r.contents),
compare_property_quoted_string compare_property_quoted_string
) )
) { ) {
@ -1702,7 +1702,7 @@ fn compare_src_block<'b, 's>(
), ),
( (
EmacsField::Required(":value"), EmacsField::Required(":value"),
|r| Some(r.contents.as_str()), |r| Some(&r.contents),
compare_property_quoted_string compare_property_quoted_string
), ),
( (

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::is_not; use nom::bytes::complete::is_not;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
@ -651,6 +653,11 @@ fn switch_word<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
))(input) ))(input)
} }
enum ContentState {
Normal,
Modified(String),
}
#[cfg_attr( #[cfg_attr(
feature = "tracing", feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context)) 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>( pub(crate) fn content<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, String> { ) -> Res<OrgSource<'s>, Cow<'s, str>> {
let mut ret = String::new(); let mut state = ContentState::Normal;
let mut remaining = input; let mut remaining = input;
let exit_matcher_parser = parser_with_context!(exit_matcher_parser)(context); let exit_matcher_parser = parser_with_context!(exit_matcher_parser)(context);
loop { loop {
@ -669,13 +676,28 @@ pub(crate) fn content<'b, 'g, 'r, 's>(
let (remain, (pre_escape_whitespace, line)) = content_line(remaining)?; let (remain, (pre_escape_whitespace, line)) = content_line(remaining)?;
if let Some(val) = pre_escape_whitespace { 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; 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"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use super::object::Object; use super::object::Object;
use super::AffiliatedKeywords; use super::AffiliatedKeywords;
use super::GetAffiliatedKeywords; use super::GetAffiliatedKeywords;
@ -59,7 +61,7 @@ pub struct ExampleBlock<'s> {
pub retain_labels: RetainLabels, pub retain_labels: RetainLabels,
pub use_labels: bool, pub use_labels: bool,
pub label_format: Option<&'s str>, pub label_format: Option<&'s str>,
pub contents: String, pub contents: Cow<'s, str>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -68,7 +70,7 @@ pub struct ExportBlock<'s> {
pub affiliated_keywords: AffiliatedKeywords<'s>, pub affiliated_keywords: AffiliatedKeywords<'s>,
pub export_type: Option<&'s str>, pub export_type: Option<&'s str>,
pub data: Option<&'s str>, pub data: Option<&'s str>,
pub contents: String, pub contents: Cow<'s, str>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -83,7 +85,7 @@ pub struct SrcBlock<'s> {
pub retain_labels: RetainLabels, pub retain_labels: RetainLabels,
pub use_labels: bool, pub use_labels: bool,
pub label_format: Option<&'s str>, pub label_format: Option<&'s str>,
pub contents: String, pub contents: Cow<'s, str>,
} }
#[derive(Debug)] #[derive(Debug)]