Skip to content

Commit a4d50a5

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

File tree

10 files changed

+33
-29
lines changed

10 files changed

+33
-29
lines changed

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
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()`,
119+
`Attribute::escaped_value()` renamed to `Attribute::escape_value()`, and `Attribute::escaped_value_with()`
120+
renamed to `Attribute::escape_value_with()` for consistency across the API.
117121
- [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method
118122
added to all events
119123

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());

benches/microbenches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn one_event(c: &mut Criterion) {
176176
.check_comments(false)
177177
.trim_text(true);
178178
match r.read_event_into(&mut buf) {
179-
Ok(Event::Comment(ref e)) => nbtxt += e.unescaped().unwrap().len(),
179+
Ok(Event::Comment(ref e)) => nbtxt += e.unescape().unwrap().len(),
180180
something_else => panic!("Did not expect {:?}", something_else),
181181
};
182182

src/de/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,14 @@ where
618618
allow_start: bool,
619619
) -> Result<BytesCData<'de>, DeError> {
620620
match self.next()? {
621-
DeEvent::Text(e) if unescape => e.unescape().map_err(Into::into),
621+
DeEvent::Text(e) if unescape => e.unescape_as_cdata().map_err(Into::into),
622622
DeEvent::Text(e) => Ok(BytesCData::new(e.into_inner())),
623623
DeEvent::CData(e) => Ok(e),
624624
DeEvent::Start(e) if allow_start => {
625625
// allow one nested level
626626
let inner = self.next()?;
627627
let t = match inner {
628-
DeEvent::Text(t) if unescape => t.unescape()?,
628+
DeEvent::Text(t) if unescape => t.unescape_as_cdata()?,
629629
DeEvent::Text(t) => BytesCData::new(t.into_inner()),
630630
DeEvent::CData(t) => t,
631631
DeEvent::Start(s) => {

src/events/attributes.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use std::{borrow::Cow, ops::Range};
1414
/// A struct representing a key/value XML attribute.
1515
///
1616
/// Field `value` stores raw bytes, possibly containing escape-sequences. Most users will likely
17-
/// want to access the value using one of the [`unescaped_value`] and [`decode_and_unescape_value`]
17+
/// want to access the value using one of the [`unescape_value`] and [`decode_and_unescape_value`]
1818
/// functions.
1919
///
20-
/// [`unescaped_value`]: Self::unescaped_value
20+
/// [`unescape_value`]: Self::unescape_value
2121
/// [`decode_and_unescape_value`]: Self::decode_and_unescape_value
2222
#[derive(Clone, PartialEq)]
2323
pub struct Attribute<'a> {
@@ -37,9 +37,9 @@ impl<'a> Attribute<'a> {
3737
///
3838
/// This will allocate if the value contains any escape sequences.
3939
///
40-
/// See also [`unescaped_value_with()`](Self::unescaped_value_with)
41-
pub fn unescaped_value(&self) -> XmlResult<Cow<[u8]>> {
42-
self.unescaped_value_with(|_| None)
40+
/// See also [`unescape_value_with()`](Self::unescape_value_with)
41+
pub fn unescape_value(&self) -> XmlResult<Cow<[u8]>> {
42+
self.unescape_value_with(|_| None)
4343
}
4444

4545
/// Returns the unescaped value, using custom entities.
@@ -51,12 +51,12 @@ impl<'a> Attribute<'a> {
5151
///
5252
/// This will allocate if the value contains any escape sequences.
5353
///
54-
/// See also [`unescaped_value()`](Self::unescaped_value)
54+
/// See also [`unescape_value()`](Self::unescape_value)
5555
///
5656
/// # Pre-condition
5757
///
5858
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
59-
pub fn unescaped_value_with<'s, 'entity>(
59+
pub fn unescape_value_with<'s, 'entity>(
6060
&'s self,
6161
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
6262
) -> XmlResult<Cow<'s, [u8]>> {
@@ -69,9 +69,9 @@ impl<'a> Attribute<'a> {
6969
/// instead use one of:
7070
///
7171
/// * [`Reader::decoder().decode()`], as it only allocates when the decoding can't be performed otherwise.
72-
/// * [`unescaped_value()`], as it doesn't allocate when no escape sequences are used.
72+
/// * [`unescape_value()`], as it doesn't allocate when no escape sequences are used.
7373
///
74-
/// [`unescaped_value()`]: Self::unescaped_value
74+
/// [`unescape_value()`]: Self::unescape_value
7575
/// [`Reader::decoder().decode()`]: crate::reader::Decoder::decode
7676
pub fn decode_and_unescape_value<B>(&self, reader: &Reader<B>) -> XmlResult<String> {
7777
self.decode_and_unescape_value_with(reader, |_| None)
@@ -83,9 +83,9 @@ impl<'a> Attribute<'a> {
8383
/// instead use one of:
8484
///
8585
/// * [`Reader::decoder().decode()`], as it only allocates when the decoding can't be performed otherwise.
86-
/// * [`unescaped_value_with()`], as it doesn't allocate when no escape sequences are used.
86+
/// * [`unescape_value_with()`], as it doesn't allocate when no escape sequences are used.
8787
///
88-
/// [`unescaped_value_with()`]: Self::unescaped_value_with
88+
/// [`unescape_value_with()`]: Self::unescape_value_with
8989
/// [`Reader::decoder().decode()`]: crate::reader::Decoder::decode
9090
///
9191
/// # Pre-condition

src/events/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ impl<'a> BytesText<'a> {
735735
/// Returns unescaped version of the text content, that can be written
736736
/// as CDATA in XML
737737
#[cfg(feature = "serialize")]
738-
pub(crate) fn unescape(self) -> std::result::Result<BytesCData<'a>, EscapeError> {
738+
pub(crate) fn unescape_as_cdata(self) -> std::result::Result<BytesCData<'a>, EscapeError> {
739739
//TODO: need to think about better API instead of dozens similar functions
740740
// Maybe use builder pattern. After that expose function as public API
741741
//FIXME: need to take into account entities defined in the document
@@ -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)