Skip to content

Commit

Permalink
Merge pull request #400 from Mingun/unescaped
Browse files Browse the repository at this point in the history
Remove `BytesStart::unescaped*` set of methods because they could return wrong results
  • Loading branch information
Mingun committed Jun 22, 2022
2 parents 038bfed + 703f078 commit 46b4d1d
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 235 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
is returned regardless of the status of the feature.
- [#180]: Error variant `Error::Utf8` replaced by `Error::NonDecodable`

- [#118]: Remove `BytesStart::unescaped*` set of methods because they could return wrong results
Use methods on `Attribute` instead

### New Tests

- [#9]: Added tests for incorrect nested tags in input
Expand Down
144 changes: 0 additions & 144 deletions examples/issue68.rs

This file was deleted.

88 changes: 0 additions & 88 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,94 +231,6 @@ impl<'a> BytesStart<'a> {
self.name().into()
}

/// Gets the unescaped tag name.
///
/// XML escape sequences like "`&lt;`" will be replaced by their unescaped characters like
/// "`<`".
///
/// See also [`unescaped_with_custom_entities()`](#method.unescaped_with_custom_entities)
#[inline]
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
self.make_unescaped(None)
}

/// Gets the unescaped tag name, using custom entities.
///
/// XML escape sequences like "`&lt;`" will be replaced by their unescaped characters like
/// "`<`".
/// Additional entities can be provided in `custom_entities`.
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
///
/// See also [`unescaped()`](#method.unescaped)
#[inline]
pub fn unescaped_with_custom_entities<'s>(
&'s self,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<Cow<'s, [u8]>> {
self.make_unescaped(Some(custom_entities))
}

#[inline]
fn make_unescaped<'s>(
&'s self,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<Cow<'s, [u8]>> {
do_unescape(&*self.buf, custom_entities).map_err(Error::EscapeError)
}

/// Returns the unescaped and decoded string value.
///
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
/// instead use one of:
///
/// * [`unescaped()`], as it doesn't allocate when no escape sequences are used.
/// * [`Reader::decode()`], as it only allocates when the decoding can't be performed otherwise.
///
/// [`unescaped()`]: #method.unescaped
/// [`Reader::decode()`]: ../reader/struct.Reader.html#method.decode
#[inline]
pub fn unescape_and_decode<B: BufRead>(&self, reader: &Reader<B>) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, None)
}

/// Returns the unescaped and decoded string value with custom entities.
///
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
/// instead use one of:
///
/// * [`unescaped_with_custom_entities()`], as it doesn't allocate when no escape sequences are used.
/// * [`Reader::decode()`], as it only allocates when the decoding can't be performed otherwise.
///
/// [`unescaped_with_custom_entities()`]: #method.unescaped_with_custom_entities
/// [`Reader::decode()`]: ../reader/struct.Reader.html#method.decode
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[inline]
pub fn unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, Some(custom_entities))
}

#[inline]
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decoder().decode(&*self)?;

let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
Ok(String::from_utf8(unescaped.into_owned())?)
}

/// Edit the name of the BytesStart in-place
///
/// # Warning
Expand Down
3 changes: 0 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ fn fuzz_101() {
loop {
match reader.read_event(&mut buf) {
Ok(Start(ref e)) | Ok(Empty(ref e)) => {
if e.unescaped().is_err() {
break;
}
for a in e.attributes() {
if a.ok().map_or(true, |a| a.unescaped_value().is_err()) {
break;
Expand Down

0 comments on commit 46b4d1d

Please sign in to comment.