From afb224b8603b0dd737a250b077c30c483affc851 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 11 Jan 2018 00:07:44 +0100 Subject: [PATCH] Protect against empty spans --- src/utils/lines/spans/chunk_iterator.rs | 14 ++++++++++ src/utils/markup/mod.rs | 34 +++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/utils/lines/spans/chunk_iterator.rs b/src/utils/lines/spans/chunk_iterator.rs index b096334..bfd9b4d 100644 --- a/src/utils/lines/spans/chunk_iterator.rs +++ b/src/utils/lines/spans/chunk_iterator.rs @@ -44,6 +44,13 @@ where type Item = Chunk<'b>; fn next(&mut self) -> Option { + // Protect agains empty spans + while self.current_span < self.spans.len() + && self.spans[self.current_span].text.is_empty() + { + self.current_span += 1; + } + if self.current_span >= self.spans.len() { return None; } @@ -111,6 +118,13 @@ where // we need to look at the next span first. self.current_span += 1; + // Skip empty spans + while self.current_span < self.spans.len() + && self.spans[self.current_span].text.is_empty() + { + self.current_span += 1; + } + if self.current_span >= self.spans.len() { // If this was the last chunk, return as is! return Some(Chunk { diff --git a/src/utils/markup/mod.rs b/src/utils/markup/mod.rs index f7ec329..d2aaf2c 100644 --- a/src/utils/markup/mod.rs +++ b/src/utils/markup/mod.rs @@ -7,7 +7,6 @@ pub mod markdown; #[cfg(feature = "markdown")] pub use self::markdown::MarkdownText; - use owning_ref::OwningHandle; use owning_ref::StringRef; use std::borrow::Cow; @@ -43,7 +42,6 @@ pub trait Markup { /// This only wraps the text and indicates how it should be parsed; /// it does not parse the text itself. pub trait MarkupText { - /// Markup format to use to parse the string. type M: Markup; @@ -67,12 +65,16 @@ impl Markup for Plain { type Error = (); fn parse<'a>(input: &'a str) -> Result>, Self::Error> { - Ok(vec![ - Span { - text: Cow::Borrowed(input), - style: Style::none(), - }, - ]) + Ok(if input.is_empty() { + Vec::new() + } else { + vec![ + Span { + text: Cow::Borrowed(input), + style: Style::none(), + }, + ] + }) } } @@ -109,8 +111,9 @@ impl StyledString { /// Returns a plain StyledString without any style. /// /// > You got no style, Dutch. You know that. - pub fn plain(content: S) -> Self - where S: Into + pub fn plain(content: S) -> Self + where + S: Into, { Self::new(content).unwrap() } @@ -138,16 +141,21 @@ impl StyledString { } /// Sets the content of this string to plain text. - pub fn set_plain(&mut self, content: S) where S: Into { + pub fn set_plain(&mut self, content: S) + where + S: Into, + { self.set_content(content).unwrap(); } /// Append `content` to the end. /// /// Re-parse everything after. - pub fn append_content(&mut self, content: T) -> Result<(), ::Error> + pub fn append_content( + &mut self, content: T + ) -> Result<(), ::Error> where - T: MarkupText + T: MarkupText, { self.with_content::(|c| c.push_str(&content.to_string())) }