Skip to content

Commit 89f9613

Browse files
committed
Replace BytesText::unescape and unescape_with by decode
Text events produces by the Reader can not contain escaped data anymore, all such data is represented by the Event::GeneralRef
1 parent b4315c8 commit 89f9613

14 files changed

+26
-48
lines changed

benches/macrobenches.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn parse_document_from_str(doc: &str) -> XmlResult<()> {
5454
}
5555
}
5656
Event::Text(e) => {
57-
criterion::black_box(e.unescape()?);
57+
criterion::black_box(e.decode()?);
5858
}
5959
Event::CData(e) => {
6060
criterion::black_box(e.into_inner());
@@ -79,7 +79,7 @@ fn parse_document_from_bytes(doc: &[u8]) -> XmlResult<()> {
7979
}
8080
}
8181
Event::Text(e) => {
82-
criterion::black_box(e.unescape()?);
82+
criterion::black_box(e.decode()?);
8383
}
8484
Event::CData(e) => {
8585
criterion::black_box(e.into_inner());
@@ -105,7 +105,7 @@ fn parse_document_from_str_with_namespaces(doc: &str) -> XmlResult<()> {
105105
}
106106
}
107107
(resolved_ns, Event::Text(e)) => {
108-
criterion::black_box(e.unescape()?);
108+
criterion::black_box(e.decode()?);
109109
criterion::black_box(resolved_ns);
110110
}
111111
(resolved_ns, Event::CData(e)) => {
@@ -133,7 +133,7 @@ fn parse_document_from_bytes_with_namespaces(doc: &[u8]) -> XmlResult<()> {
133133
}
134134
}
135135
(resolved_ns, Event::Text(e)) => {
136-
criterion::black_box(e.unescape()?);
136+
criterion::black_box(e.decode()?);
137137
criterion::black_box(resolved_ns);
138138
}
139139
(resolved_ns, Event::CData(e)) => {

benches/microbenches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn one_event(c: &mut Criterion) {
145145
config.trim_text(true);
146146
config.check_end_names = false;
147147
match r.read_event() {
148-
Ok(Event::Comment(e)) => nbtxt += e.unescape().unwrap().len(),
148+
Ok(Event::Comment(e)) => nbtxt += e.decode().unwrap().len(),
149149
something_else => panic!("Did not expect {:?}", something_else),
150150
};
151151

fuzz/fuzz_targets/fuzz_target_1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ where
4343
| Ok(Event::Comment(ref e))
4444
| Ok(Event::DocType(ref e)) => {
4545
debug_format!(e);
46-
if let Err(err) = e.unescape() {
46+
if let Err(err) = e.decode() {
4747
debug_format!(err);
4848
break;
4949
}

src/de/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,9 +2193,7 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
21932193
// FIXME: Actually, we should trim after decoding text, but now we trim before
21942194
e.inplace_trim_end();
21952195
}
2196-
result
2197-
.to_mut()
2198-
.push_str(&e.unescape_with(|entity| self.entity_resolver.resolve(entity))?);
2196+
result.to_mut().push_str(&e.decode()?);
21992197
}
22002198
PayloadEvent::CData(e) => result.to_mut().push_str(&e.decode()?),
22012199

@@ -2217,7 +2215,7 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
22172215
// FIXME: Actually, we should trim after decoding text, but now we trim before
22182216
continue;
22192217
}
2220-
self.drain_text(e.unescape_with(|entity| self.entity_resolver.resolve(entity))?)
2218+
self.drain_text(e.decode()?)
22212219
}
22222220
PayloadEvent::CData(e) => self.drain_text(e.decode()?),
22232221
PayloadEvent::DocType(e) => {

src/events/mod.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ use std::str::from_utf8;
4747

4848
use crate::encoding::Decoder;
4949
use crate::errors::{Error, IllFormedError, Result};
50-
use crate::escape::{
51-
escape, minimal_escape, parse_number, partial_escape, resolve_predefined_entity, unescape_with,
52-
EscapeError,
53-
};
50+
use crate::escape::{escape, minimal_escape, parse_number, partial_escape, EscapeError};
5451
use crate::name::{LocalName, QName};
5552
#[cfg(feature = "serialize")]
5653
use crate::utils::CowRef;
@@ -580,29 +577,12 @@ impl<'a> BytesText<'a> {
580577
}
581578
}
582579

583-
/// Decodes then unescapes the content of the event.
584-
///
585-
/// This will allocate if the value contains any escape sequences or in
586-
/// non-UTF-8 encoding.
587-
pub fn unescape(&self) -> Result<Cow<'a, str>> {
588-
self.unescape_with(resolve_predefined_entity)
589-
}
590-
591-
/// Decodes then unescapes the content of the event with custom entities.
580+
/// Decodes the content of the event.
592581
///
593582
/// This will allocate if the value contains any escape sequences or in
594583
/// non-UTF-8 encoding.
595-
pub fn unescape_with<'entity>(
596-
&self,
597-
resolve_entity: impl FnMut(&str) -> Option<&'entity str>,
598-
) -> Result<Cow<'a, str>> {
599-
let decoded = self.decoder.decode_cow(&self.content)?;
600-
601-
match unescape_with(&decoded, resolve_entity)? {
602-
// Because result is borrowed, no replacements was done and we can use original string
603-
Cow::Borrowed(_) => Ok(decoded),
604-
Cow::Owned(s) => Ok(s.into()),
605-
}
584+
pub fn decode(&self) -> Result<Cow<'a, str>> {
585+
self.decoder.decode_cow(&self.content)
606586
}
607587

608588
/// Removes leading XML whitespace bytes from text content.

src/reader/async_tokio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<R: AsyncBufRead + Unpin> Reader<R> {
5858
/// loop {
5959
/// match reader.read_event_into_async(&mut buf).await {
6060
/// Ok(Event::Start(_)) => count += 1,
61-
/// Ok(Event::Text(e)) => txt.push(e.unescape().unwrap().into_owned()),
61+
/// Ok(Event::Text(e)) => txt.push(e.decode().unwrap().into_owned()),
6262
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
6363
/// Ok(Event::Eof) => break,
6464
/// _ => (),
@@ -192,7 +192,7 @@ impl<R: AsyncBufRead + Unpin> NsReader<R> {
192192
/// }
193193
/// }
194194
/// Event::Text(e) => {
195-
/// txt.push(e.unescape().unwrap().into_owned())
195+
/// txt.push(e.decode().unwrap().into_owned())
196196
/// }
197197
/// Event::Eof => break,
198198
/// _ => (),
@@ -328,7 +328,7 @@ impl<R: AsyncBufRead + Unpin> NsReader<R> {
328328
/// (_, Event::Start(_)) => unreachable!(),
329329
///
330330
/// (_, Event::Text(e)) => {
331-
/// txt.push(e.unescape().unwrap().into_owned())
331+
/// txt.push(e.decode().unwrap().into_owned())
332332
/// }
333333
/// (_, Event::Eof) => break,
334334
/// _ => (),

src/reader/buffered_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<R: BufRead> Reader<R> {
339339
/// loop {
340340
/// match reader.read_event_into(&mut buf) {
341341
/// Ok(Event::Start(_)) => count += 1,
342-
/// Ok(Event::Text(e)) => txt.push(e.unescape().unwrap().into_owned()),
342+
/// Ok(Event::Text(e)) => txt.push(e.decode().unwrap().into_owned()),
343343
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
344344
/// Ok(Event::Eof) => break,
345345
/// _ => (),

src/reader/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ impl EncodingRef {
637637
/// _ => (),
638638
/// }
639639
/// }
640-
/// Ok(Event::Text(e)) => txt.push(e.unescape().unwrap().into_owned()),
640+
/// Ok(Event::Text(e)) => txt.push(e.decode().unwrap().into_owned()),
641641
///
642642
/// // There are several other `Event`s we do not consider here
643643
/// _ => (),

src/reader/ns_reader.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<R: BufRead> NsReader<R> {
419419
/// }
420420
/// }
421421
/// Event::Text(e) => {
422-
/// txt.push(e.unescape().unwrap().into_owned())
422+
/// txt.push(e.decode().unwrap().into_owned())
423423
/// }
424424
/// Event::Eof => break,
425425
/// _ => (),
@@ -478,7 +478,7 @@ impl<R: BufRead> NsReader<R> {
478478
/// (_, Event::Start(_)) => unreachable!(),
479479
///
480480
/// (_, Event::Text(e)) => {
481-
/// txt.push(e.unescape().unwrap().into_owned())
481+
/// txt.push(e.decode().unwrap().into_owned())
482482
/// }
483483
/// (_, Event::Eof) => break,
484484
/// _ => (),
@@ -664,7 +664,7 @@ impl<'i> NsReader<&'i [u8]> {
664664
/// }
665665
/// }
666666
/// Event::Text(e) => {
667-
/// txt.push(e.unescape().unwrap().into_owned())
667+
/// txt.push(e.decode().unwrap().into_owned())
668668
/// }
669669
/// Event::Eof => break,
670670
/// _ => (),
@@ -726,7 +726,7 @@ impl<'i> NsReader<&'i [u8]> {
726726
/// (_, Event::Start(_)) => unreachable!(),
727727
///
728728
/// (_, Event::Text(e)) => {
729-
/// txt.push(e.unescape().unwrap().into_owned())
729+
/// txt.push(e.decode().unwrap().into_owned())
730730
/// }
731731
/// (_, Event::Eof) => break,
732732
/// _ => (),

src/reader/slice_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a> Reader<&'a [u8]> {
6161
/// loop {
6262
/// match reader.read_event().unwrap() {
6363
/// Event::Start(e) => count += 1,
64-
/// Event::Text(e) => txt.push(e.unescape().unwrap().into_owned()),
64+
/// Event::Text(e) => txt.push(e.decode().unwrap().into_owned()),
6565
/// Event::Eof => break,
6666
/// _ => (),
6767
/// }

tests/encodings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_koi8_r_encoding() {
3737
loop {
3838
match r.read_event_into(&mut buf) {
3939
Ok(Text(e)) => {
40-
e.unescape().unwrap();
40+
e.decode().unwrap();
4141
}
4242
Ok(Eof) => break,
4343
_ => (),

tests/fuzzing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn fuzz_101() {
3838
}
3939
}
4040
Ok(Event::Text(e)) => {
41-
if e.unescape().is_err() {
41+
if e.decode().is_err() {
4242
break;
4343
}
4444
}

tests/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn test_escaped_content() {
172172
"content unexpected: expecting 'test', got '{:?}'",
173173
from_utf8(&e)
174174
);
175-
match e.unescape() {
175+
match e.decode() {
176176
Ok(c) => assert_eq!(c, "test"),
177177
Err(e) => panic!(
178178
"cannot escape content at position {}: {:?}",

tests/roundtrip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn reescape_text() {
236236
match reader.read_event().unwrap() {
237237
Eof => break,
238238
Text(e) => {
239-
let t = e.unescape().unwrap();
239+
let t = e.decode().unwrap();
240240
assert!(writer.write_event(Text(BytesText::new(&t))).is_ok());
241241
}
242242
e => assert!(writer.write_event(e).is_ok()),

0 commit comments

Comments
 (0)