Skip to content

Commit b4355c6

Browse files
author
Arthur Pigeon
committed
change DeEvent::Text to contain whole and trim version
1 parent 7792983 commit b4355c6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/de/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ use std::io::BufRead;
18521852
use std::mem::replace;
18531853
#[cfg(feature = "overlapped-lists")]
18541854
use std::num::NonZeroUsize;
1855-
use std::ops::Deref;
1855+
use std::ops::{Deref, Range};
18561856

18571857
/// Data represented by a text node or a CDATA node. XML markup is not expected
18581858
pub(crate) const TEXT_KEY: &str = "$text";
@@ -1869,7 +1869,24 @@ pub(crate) const VALUE_KEY: &str = "$value";
18691869
/// [`PI`]: Event::PI
18701870
#[derive(Debug, PartialEq, Eq)]
18711871
pub struct Text<'a> {
1872+
/// Untrimmed text after concatenating content of all
1873+
/// [`Text`] and [`CData`] events
18721874
text: Cow<'a, str>,
1875+
/// A range into `text` which contains data after trimming
1876+
content: Range<usize>,
1877+
}
1878+
1879+
impl<'a> Text<'a> {
1880+
fn new(text: Cow<'a, str>) -> Self {
1881+
let start = text.find(|c| !char::is_whitespace(c)).unwrap_or(0);
1882+
let end = text
1883+
.rfind(|c| !char::is_whitespace(c))
1884+
.unwrap_or(text.len().saturating_sub(1));
1885+
Self {
1886+
text,
1887+
content: start..end,
1888+
}
1889+
}
18731890
}
18741891

18751892
impl<'a> Deref for Text<'a> {
@@ -1884,9 +1901,7 @@ impl<'a> Deref for Text<'a> {
18841901
impl<'a> From<&'a str> for Text<'a> {
18851902
#[inline]
18861903
fn from(text: &'a str) -> Self {
1887-
Self {
1888-
text: Cow::Borrowed(text),
1889-
}
1904+
Self::new(Cow::Borrowed(text))
18901905
}
18911906
}
18921907

@@ -2010,7 +2025,7 @@ impl<'i, R: XmlRead<'i>> XmlReader<'i, R> {
20102025
_ => break,
20112026
}
20122027
}
2013-
Ok(DeEvent::Text(Text { text: result }))
2028+
Ok(DeEvent::Text(Text::new(result)))
20142029
}
20152030

20162031
/// Read one text event, panics if current event is not a text event

0 commit comments

Comments
 (0)