Skip to content

Commit abe68fe

Browse files
committed
Update event and attribute related structs to str
1 parent b816b2d commit abe68fe

14 files changed

+391
-577
lines changed

Changelog.md

-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@
238238
- [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method
239239
added to all events
240240

241-
- [#421]: `decode_and_unescape*` methods now does one less allocation if unescaping is not required
242241
- [#421]: Removed ability to deserialize byte arrays from serde deserializer.
243242
XML is not able to store binary data directly, you should always use some encoding
244243
scheme, for example, HEX or Base64

benches/macrobenches.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn parse_document_from_str(doc: &str) -> XmlResult<()> {
5050
match criterion::black_box(r.read_event()?) {
5151
Event::Start(e) | Event::Empty(e) => {
5252
for attr in e.attributes() {
53-
criterion::black_box(attr?.decode_and_unescape_value(&r)?);
53+
criterion::black_box(attr?.unescape_value()?);
5454
}
5555
}
5656
Event::Text(e) => {
@@ -75,7 +75,7 @@ fn parse_document_from_bytes(doc: &[u8]) -> XmlResult<()> {
7575
match criterion::black_box(r.read_event_into(&mut buf)?) {
7676
Event::Start(e) | Event::Empty(e) => {
7777
for attr in e.attributes() {
78-
criterion::black_box(attr?.decode_and_unescape_value(&r)?);
78+
criterion::black_box(attr?.unescape_value()?);
7979
}
8080
}
8181
Event::Text(e) => {
@@ -101,7 +101,7 @@ fn parse_document_from_str_with_namespaces(doc: &str) -> XmlResult<()> {
101101
(resolved_ns, Event::Start(e) | Event::Empty(e)) => {
102102
criterion::black_box(resolved_ns);
103103
for attr in e.attributes() {
104-
criterion::black_box(attr?.decode_and_unescape_value(&r)?);
104+
criterion::black_box(attr?.unescape_value()?);
105105
}
106106
}
107107
(resolved_ns, Event::Text(e)) => {
@@ -129,7 +129,7 @@ fn parse_document_from_bytes_with_namespaces(doc: &[u8]) -> XmlResult<()> {
129129
(resolved_ns, Event::Start(e) | Event::Empty(e)) => {
130130
criterion::black_box(resolved_ns);
131131
for attr in e.attributes() {
132-
criterion::black_box(attr?.decode_and_unescape_value(&r)?);
132+
criterion::black_box(attr?.unescape_value()?);
133133
}
134134
}
135135
(resolved_ns, Event::Text(e)) => {

examples/custom_entities.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3333
loop {
3434
match reader.read_event() {
3535
Ok(Event::DocType(ref e)) => {
36-
for cap in entity_re.captures_iter(e) {
36+
for cap in entity_re.captures_iter(e.as_bytes()) {
3737
custom_entities.insert(
3838
String::from_utf8(cap[1].to_owned())?,
3939
String::from_utf8(cap[2].to_owned())?,
@@ -46,7 +46,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4646
.attributes()
4747
.map(|a| {
4848
a.unwrap()
49-
.decode_and_unescape_value_with(&reader, |ent| {
49+
.unescape_value_with(|ent| {
5050
custom_entities.get(ent).map(|s| s.as_str())
5151
})
5252
.unwrap()

src/escapei.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ pub(crate) fn _escape<F: Fn(u8) -> bool>(raw: &str, escape_chars: F) -> Cow<str>
131131
if let Some(raw) = bytes.get(pos..) {
132132
escaped.extend_from_slice(raw);
133133
}
134-
// SAFETY: we operate on UTF-8 input and search for an one byte chars only,
135-
// so all slices that was put to the `escaped` is a valid UTF-8 encoded strings
134+
// SAFETY: we operate on UTF-8 input and search for only one-byte chars, so
135+
// the end point will always be at a character boundary, and we can yield a
136+
// valid UTF-8 slice always.
136137
// TODO: Can be replaced with `unsafe { String::from_utf8_unchecked() }`
137138
// if unsafe code will be allowed
138139
Cow::Owned(String::from_utf8(escaped).unwrap())

0 commit comments

Comments
 (0)