From 15797dfab16ef49f2eebbc5c069413a847c0f7a6 Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Fri, 15 Jul 2022 00:37:53 -0400 Subject: [PATCH] Make escape / unescape consistent throughout API --- Changelog.md | 3 +++ benches/macrobenches.rs | 4 ++-- src/events/attributes.rs | 2 +- src/events/mod.rs | 12 ++++++------ src/writer.rs | 2 +- tests/test.rs | 4 ++-- tests/unit_tests.rs | 4 ++-- tests/xmlrs_reader_tests.rs | 2 +- 8 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Changelog.md b/Changelog.md index b0f5fa70..97b76fdb 100644 --- a/Changelog.md +++ b/Changelog.md @@ -114,6 +114,9 @@ - [#415]: Renamed many functions following the pattern `unescape_and_decode*` to `decode_and_unescape*` to better communicate their function. Renamed functions following the pattern `*_with_custom_entities` to `decode_and_unescape_with` to be more consistent across the API. +- [#415]: `BytesText::escaped()` renamed to `BytesText::escape()`, `BytesText::unescaped()` renamed to + `BytesText::unescape()`, `BytesText::unescaped_with()` renamed to `BytesText::unescape_with()`, and + `Attribute::escaped_value()` renamed to `Attribute::escape_value()` for consistency. - [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method added to all events diff --git a/benches/macrobenches.rs b/benches/macrobenches.rs index 1dc74dac..cdb3c8e9 100644 --- a/benches/macrobenches.rs +++ b/benches/macrobenches.rs @@ -24,11 +24,11 @@ fn parse_document(doc: &[u8]) -> XmlResult<()> { match r.read_event()? { Event::Start(e) | Event::Empty(e) => { for attr in e.attributes() { - criterion::black_box(attr?.unescaped_value()?); + criterion::black_box(attr?.unescape_value()?); } } Event::Text(e) => { - criterion::black_box(e.unescaped()?); + criterion::black_box(e.unescape()?); } Event::CData(e) => { criterion::black_box(e.into_inner()); diff --git a/src/events/attributes.rs b/src/events/attributes.rs index dc218c32..ccedba0c 100644 --- a/src/events/attributes.rs +++ b/src/events/attributes.rs @@ -38,7 +38,7 @@ impl<'a> Attribute<'a> { /// This will allocate if the value contains any escape sequences. /// /// See also [`unescaped_value_with()`](Self::unescaped_value_with) - pub fn unescaped_value(&self) -> XmlResult> { + pub fn unescape_value(&self) -> XmlResult> { self.unescaped_value_with(|_| None) } diff --git a/src/events/mod.rs b/src/events/mod.rs index 58b64e35..956fb74e 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -752,9 +752,9 @@ impl<'a> BytesText<'a> { /// Searches for '&' into content and try to escape the coded character if possible /// returns Malformed error with index within element if '&' is not followed by ';' /// - /// See also [`unescaped_with()`](Self::unescaped_with) - pub fn unescaped(&self) -> Result> { - self.unescaped_with(|_| None) + /// See also [`unescape_with()`](Self::unescape_with) + pub fn unescape(&self) -> Result> { + self.unescape_with(|_| None) } /// gets escaped content with custom entities @@ -767,8 +767,8 @@ impl<'a> BytesText<'a> { /// /// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs. /// - /// See also [`unescaped()`](Self::unescaped) - pub fn unescaped_with<'s, 'entity>( + /// See also [`unescape()`](Self::unescape) + pub fn unescape_with<'s, 'entity>( &'s self, resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>, ) -> Result> { @@ -807,7 +807,7 @@ impl<'a> BytesText<'a> { } /// Gets escaped content. - pub fn escaped(&self) -> &[u8] { + pub fn escape(&self) -> &[u8] { self.content.as_ref() } } diff --git a/src/writer.rs b/src/writer.rs index 7c5cf307..ca29954d 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -108,7 +108,7 @@ impl Writer { Event::Empty(ref e) => self.write_wrapped(b"<", e, b"/>"), Event::Text(ref e) => { next_should_line_break = false; - self.write(&e.escaped()) + self.write(&e.escape()) } Event::Comment(ref e) => self.write_wrapped(b""), Event::CData(ref e) => { diff --git a/tests/test.rs b/tests/test.rs index 83261c51..162a8bd4 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -157,13 +157,13 @@ fn fuzz_101() { match reader.read_event_into(&mut buf) { Ok(Start(ref e)) | Ok(Empty(ref e)) => { for a in e.attributes() { - if a.ok().map_or(true, |a| a.unescaped_value().is_err()) { + if a.ok().map_or(true, |a| a.unescape_value().is_err()) { break; } } } Ok(Text(ref e)) => { - if e.unescaped().is_err() { + if e.unescape().is_err() { break; } } diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index a480adb0..6c6a0615 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -523,7 +523,7 @@ fn test_escaped_content() { "content unexpected: expecting '<test>', got '{:?}'", from_utf8(&*e) ); - match e.unescaped() { + match e.unescape() { Ok(ref c) => assert_eq!( &**c, b"", @@ -620,7 +620,7 @@ fn test_read_write_roundtrip_escape() -> Result<()> { match reader.read_event_into(&mut buf)? { Eof => break, Text(e) => { - let t = e.escaped(); + let t = e.escape(); assert!(writer .write_event(Text(BytesText::from_escaped(t.to_vec()))) .is_ok()); diff --git a/tests/xmlrs_reader_tests.rs b/tests/xmlrs_reader_tests.rs index 35ce90f3..8eaa66c1 100644 --- a/tests/xmlrs_reader_tests.rs +++ b/tests/xmlrs_reader_tests.rs @@ -459,7 +459,7 @@ fn xmlrs_display(opt_event: Result<(ResolveResult, Event)>, decoder: Decoder) -> Ok((_, Event::CData(e))) => format!("CData({})", decoder.decode(&e).unwrap()), Ok((_, Event::Text(e))) => match unescape(decoder.decode(&e).unwrap().as_bytes()) { Ok(c) => format!("Characters({})", from_utf8(c.as_ref()).unwrap()), - Err(err) => format!("FailedUnescape({:?}; {})", e.escaped(), err), + Err(err) => format!("FailedUnescape({:?}; {})", e.escape(), err), }, Ok((_, Event::Decl(e))) => { let version_cow = e.version().unwrap();