From b2ee44ec0963a9103bb909860046968b972967b1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 24 Apr 2023 17:58:10 -0400 Subject: [PATCH] Simplify the implementation of plain text. --- src/parser/plain_text.rs | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/parser/plain_text.rs b/src/parser/plain_text.rs index e65faaa..b1d3322 100644 --- a/src/parser/plain_text.rs +++ b/src/parser/plain_text.rs @@ -1,4 +1,3 @@ -use nom::combinator::not; use nom::combinator::recognize; use super::object::PlainText; @@ -11,6 +10,7 @@ use crate::parser::object_parser::any_object_except_plain_text; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; +use crate::parser::util::text_until_exit; #[tracing::instrument(ret, level = "debug")] pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> { @@ -24,32 +24,8 @@ pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s class: ExitClass::Beta, exit_matcher: &plain_text_end, })); - let mut current_input = input.char_indices(); - loop { - match current_input.next() { - Some((offset, _char)) => { - let remaining = &input[offset..]; - let exit_matcher_status = not(|i| parser_context.check_exit_matcher(i))(remaining); - if exit_matcher_status.is_err() { - if offset == 0 { - // If we're at the start of the input, then nothing is plain text, so fire an error for zero-length match. - exit_matcher_status?; - } else { - return Ok(( - &input[offset..], - PlainText { - source: &input[..offset], - }, - )); - } - } - } - None => { - // We hit the end of the file, so all input must be plain text - return Ok((&input[input.len()..], PlainText { source: input })); - } - }; - } + let (remaining, source) = text_until_exit(&parser_context, input)?; + Ok((remaining, PlainText { source })) } #[tracing::instrument(ret, level = "debug")]