From b4355c6b4398b7b27a68964ba90b523d41e2c703 Mon Sep 17 00:00:00 2001 From: Arthur Pigeon Date: Wed, 8 Mar 2023 14:00:32 +0100 Subject: [PATCH] change DeEvent::Text to contain whole and trim version --- src/de/mod.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/de/mod.rs b/src/de/mod.rs index 6b5fea8a..be17a630 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -1852,7 +1852,7 @@ use std::io::BufRead; use std::mem::replace; #[cfg(feature = "overlapped-lists")] use std::num::NonZeroUsize; -use std::ops::Deref; +use std::ops::{Deref, Range}; /// Data represented by a text node or a CDATA node. XML markup is not expected pub(crate) const TEXT_KEY: &str = "$text"; @@ -1869,7 +1869,24 @@ pub(crate) const VALUE_KEY: &str = "$value"; /// [`PI`]: Event::PI #[derive(Debug, PartialEq, Eq)] pub struct Text<'a> { + /// Untrimmed text after concatenating content of all + /// [`Text`] and [`CData`] events text: Cow<'a, str>, + /// A range into `text` which contains data after trimming + content: Range, +} + +impl<'a> Text<'a> { + fn new(text: Cow<'a, str>) -> Self { + let start = text.find(|c| !char::is_whitespace(c)).unwrap_or(0); + let end = text + .rfind(|c| !char::is_whitespace(c)) + .unwrap_or(text.len().saturating_sub(1)); + Self { + text, + content: start..end, + } + } } impl<'a> Deref for Text<'a> { @@ -1884,9 +1901,7 @@ impl<'a> Deref for Text<'a> { impl<'a> From<&'a str> for Text<'a> { #[inline] fn from(text: &'a str) -> Self { - Self { - text: Cow::Borrowed(text), - } + Self::new(Cow::Borrowed(text)) } } @@ -2010,7 +2025,7 @@ impl<'i, R: XmlRead<'i>> XmlReader<'i, R> { _ => break, } } - Ok(DeEvent::Text(Text { text: result })) + Ok(DeEvent::Text(Text::new(result))) } /// Read one text event, panics if current event is not a text event