Skip to content

Commit

Permalink
Remove all *_without_bom functions from Attributes struct
Browse files Browse the repository at this point in the history
BOM cannot arise in the attribute values - this is by definition a mark that
can be located only at the begin of the stream and every attribute value is
inside the stream
  • Loading branch information
Mingun committed Jun 19, 2022
1 parent 2863674 commit 24fac69
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 94 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

- [#191]: Remove unused `reader.decoder().decode_owned()`. If you ever used it,
use `String::from_utf8` instead (which that function did)
- [#191]: Remove `*_without_bom` methods from the `Attributes` struct because they are useless.
Use the same-named methods without that suffix instead. Attribute values cannot contain BOM

### New Tests

Expand Down
94 changes: 0 additions & 94 deletions src/events/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,100 +131,6 @@ impl<'a> Attribute<'a> {
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}

/// helper method to unescape then decode self using the reader encoding
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
#[cfg(feature = "encoding")]
pub fn unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &mut Reader<B>,
) -> XmlResult<String> {
self.do_unescape_and_decode_without_bom(reader, None)
}

/// helper method to unescape then decode self using the reader encoding
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
#[cfg(not(feature = "encoding"))]
pub fn unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &Reader<B>,
) -> XmlResult<String> {
self.do_unescape_and_decode_without_bom(reader, None)
}

/// helper method to unescape then decode self using the reader encoding with custom entities
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[cfg(feature = "encoding")]
pub fn unescape_and_decode_without_bom_with_custom_entities<B: BufRead>(
&self,
reader: &mut Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> XmlResult<String> {
self.do_unescape_and_decode_without_bom(reader, Some(custom_entities))
}

/// helper method to unescape then decode self using the reader encoding with custom entities
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[cfg(not(feature = "encoding"))]
pub fn unescape_and_decode_without_bom_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> XmlResult<String> {
self.do_unescape_and_decode_without_bom(reader, Some(custom_entities))
}

#[cfg(feature = "encoding")]
fn do_unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &mut Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> XmlResult<String> {
let decoded = reader.decode_without_bom(&*self.value);
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}

#[cfg(not(feature = "encoding"))]
fn do_unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> XmlResult<String> {
let decoded = reader.decode_without_bom(&*self.value)?;
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
}

impl<'a> Debug for Attribute<'a> {
Expand Down

0 comments on commit 24fac69

Please sign in to comment.