Skip to content

Commit 15797df

Browse files
committed
Make escape / unescape consistent throughout API
1 parent a4a0f8d commit 15797df

File tree

8 files changed

+18
-15
lines changed

8 files changed

+18
-15
lines changed

Changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
- [#415]: Renamed many functions following the pattern `unescape_and_decode*` to `decode_and_unescape*`
115115
to better communicate their function. Renamed functions following the pattern `*_with_custom_entities`
116116
to `decode_and_unescape_with` to be more consistent across the API.
117+
- [#415]: `BytesText::escaped()` renamed to `BytesText::escape()`, `BytesText::unescaped()` renamed to
118+
`BytesText::unescape()`, `BytesText::unescaped_with()` renamed to `BytesText::unescape_with()`, and
119+
`Attribute::escaped_value()` renamed to `Attribute::escape_value()` for consistency.
117120
- [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method
118121
added to all events
119122

benches/macrobenches.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ fn parse_document(doc: &[u8]) -> XmlResult<()> {
2424
match r.read_event()? {
2525
Event::Start(e) | Event::Empty(e) => {
2626
for attr in e.attributes() {
27-
criterion::black_box(attr?.unescaped_value()?);
27+
criterion::black_box(attr?.unescape_value()?);
2828
}
2929
}
3030
Event::Text(e) => {
31-
criterion::black_box(e.unescaped()?);
31+
criterion::black_box(e.unescape()?);
3232
}
3333
Event::CData(e) => {
3434
criterion::black_box(e.into_inner());

src/events/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a> Attribute<'a> {
3838
/// This will allocate if the value contains any escape sequences.
3939
///
4040
/// See also [`unescaped_value_with()`](Self::unescaped_value_with)
41-
pub fn unescaped_value(&self) -> XmlResult<Cow<[u8]>> {
41+
pub fn unescape_value(&self) -> XmlResult<Cow<[u8]>> {
4242
self.unescaped_value_with(|_| None)
4343
}
4444

src/events/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ impl<'a> BytesText<'a> {
752752
/// Searches for '&' into content and try to escape the coded character if possible
753753
/// returns Malformed error with index within element if '&' is not followed by ';'
754754
///
755-
/// See also [`unescaped_with()`](Self::unescaped_with)
756-
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
757-
self.unescaped_with(|_| None)
755+
/// See also [`unescape_with()`](Self::unescape_with)
756+
pub fn unescape(&self) -> Result<Cow<[u8]>> {
757+
self.unescape_with(|_| None)
758758
}
759759

760760
/// gets escaped content with custom entities
@@ -767,8 +767,8 @@ impl<'a> BytesText<'a> {
767767
///
768768
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
769769
///
770-
/// See also [`unescaped()`](Self::unescaped)
771-
pub fn unescaped_with<'s, 'entity>(
770+
/// See also [`unescape()`](Self::unescape)
771+
pub fn unescape_with<'s, 'entity>(
772772
&'s self,
773773
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
774774
) -> Result<Cow<'s, [u8]>> {
@@ -807,7 +807,7 @@ impl<'a> BytesText<'a> {
807807
}
808808

809809
/// Gets escaped content.
810-
pub fn escaped(&self) -> &[u8] {
810+
pub fn escape(&self) -> &[u8] {
811811
self.content.as_ref()
812812
}
813813
}

src/writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<W: Write> Writer<W> {
108108
Event::Empty(ref e) => self.write_wrapped(b"<", e, b"/>"),
109109
Event::Text(ref e) => {
110110
next_should_line_break = false;
111-
self.write(&e.escaped())
111+
self.write(&e.escape())
112112
}
113113
Event::Comment(ref e) => self.write_wrapped(b"<!--", e, b"-->"),
114114
Event::CData(ref e) => {

tests/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ fn fuzz_101() {
157157
match reader.read_event_into(&mut buf) {
158158
Ok(Start(ref e)) | Ok(Empty(ref e)) => {
159159
for a in e.attributes() {
160-
if a.ok().map_or(true, |a| a.unescaped_value().is_err()) {
160+
if a.ok().map_or(true, |a| a.unescape_value().is_err()) {
161161
break;
162162
}
163163
}
164164
}
165165
Ok(Text(ref e)) => {
166-
if e.unescaped().is_err() {
166+
if e.unescape().is_err() {
167167
break;
168168
}
169169
}

tests/unit_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ fn test_escaped_content() {
523523
"content unexpected: expecting '&lt;test&gt;', got '{:?}'",
524524
from_utf8(&*e)
525525
);
526-
match e.unescaped() {
526+
match e.unescape() {
527527
Ok(ref c) => assert_eq!(
528528
&**c,
529529
b"<test>",
@@ -620,7 +620,7 @@ fn test_read_write_roundtrip_escape() -> Result<()> {
620620
match reader.read_event_into(&mut buf)? {
621621
Eof => break,
622622
Text(e) => {
623-
let t = e.escaped();
623+
let t = e.escape();
624624
assert!(writer
625625
.write_event(Text(BytesText::from_escaped(t.to_vec())))
626626
.is_ok());

tests/xmlrs_reader_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn xmlrs_display(opt_event: Result<(ResolveResult, Event)>, decoder: Decoder) ->
459459
Ok((_, Event::CData(e))) => format!("CData({})", decoder.decode(&e).unwrap()),
460460
Ok((_, Event::Text(e))) => match unescape(decoder.decode(&e).unwrap().as_bytes()) {
461461
Ok(c) => format!("Characters({})", from_utf8(c.as_ref()).unwrap()),
462-
Err(err) => format!("FailedUnescape({:?}; {})", e.escaped(), err),
462+
Err(err) => format!("FailedUnescape({:?}; {})", e.escape(), err),
463463
},
464464
Ok((_, Event::Decl(e))) => {
465465
let version_cow = e.version().unwrap();

0 commit comments

Comments
 (0)