Skip to content

Commit a199202

Browse files
committed
Move name length calculation to BytesStart::wrap
It also will be used in a new BytesPI event that will be introduced in a separate PR
1 parent 8771d65 commit a199202

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/events/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ pub struct BytesStart<'a> {
7676
impl<'a> BytesStart<'a> {
7777
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
7878
#[inline]
79-
pub(crate) fn wrap(content: &'a [u8], name_len: usize) -> Self {
80-
BytesStart {
79+
pub(crate) fn wrap(content: &'a [u8]) -> Self {
80+
let name_len = content
81+
.iter()
82+
.position(|&b| is_whitespace(b))
83+
.unwrap_or(content.len());
84+
Self {
8185
buf: Cow::Borrowed(content),
8286
name_len,
8387
}

src/reader/state.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use encoding_rs::{UTF_16BE, UTF_16LE, UTF_8};
44
use crate::encoding::Decoder;
55
use crate::errors::{Error, IllFormedError, Result};
66
use crate::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event};
7+
use crate::name::QName;
78
use crate::parser::{FeedResult, Parser};
89
#[cfg(feature = "encoding")]
910
use crate::reader::EncodingRef;
@@ -333,10 +334,10 @@ impl ReaderState {
333334

334335
// Cut of `<?` and `?>` from start and end
335336
let content = &content[2..content.len() - 2];
336-
let len = content.len();
337+
let event = BytesStart::wrap(content);
337338

338-
if content.starts_with(b"xml") && (len == 3 || is_whitespace(content[3])) {
339-
let event = BytesDecl::from_start(BytesStart::wrap(content, 3));
339+
if event.name() == QName(b"xml") {
340+
let event = BytesDecl::from_start(event);
340341

341342
// Try getting encoding from the declaration event
342343
#[cfg(feature = "encoding")]
@@ -355,20 +356,12 @@ impl ReaderState {
355356
debug_assert!(content.starts_with(b"<"), "{:?}", Bytes(content));
356357
debug_assert!(content.ends_with(b"/>"), "{:?}", Bytes(content));
357358

358-
let content = &content[1..content.len() - 1];
359-
let len = content.len();
360-
let name_end = content
361-
.iter()
362-
.position(|&b| is_whitespace(b))
363-
.unwrap_or(len);
364-
// This is self-closed tag `<something/>`
365-
let name_len = if name_end < len { name_end } else { len - 1 };
366-
let event = BytesStart::wrap(&content[..len - 1], name_len);
359+
let event = BytesStart::wrap(&content[1..content.len() - 2]);
367360

368361
if self.config.expand_empty_elements {
369362
self.pending = true;
370363
self.opened_starts.push(self.opened_buffer.len());
371-
self.opened_buffer.extend(&content[..name_len]);
364+
self.opened_buffer.extend(event.name().as_ref());
372365
Ok(Event::Start(event))
373366
} else {
374367
Ok(Event::Empty(event))
@@ -378,18 +371,14 @@ impl ReaderState {
378371
debug_assert!(content.starts_with(b"<"), "{:?}", Bytes(content));
379372
debug_assert!(content.ends_with(b">"), "{:?}", Bytes(content));
380373

381-
let content = &content[1..content.len() - 1];
382-
let len = content.len();
383-
let name_end = content
384-
.iter()
385-
.position(|&b| is_whitespace(b))
386-
.unwrap_or(len);
374+
let event = BytesStart::wrap(&content[1..content.len() - 1]);
375+
387376
// #514: Always store names event when .check_end_names == false,
388377
// because checks can be temporary disabled and when they would be
389378
// enabled, we should have that information
390379
self.opened_starts.push(self.opened_buffer.len());
391-
self.opened_buffer.extend(&content[..name_end]);
392-
Ok(Event::Start(BytesStart::wrap(content, name_end)))
380+
self.opened_buffer.extend(event.name().as_ref());
381+
Ok(Event::Start(event))
393382
}
394383
FeedResult::EmitEndTag(_) => {
395384
debug_assert!(content.starts_with(b"</"), "{:?}", Bytes(content));

0 commit comments

Comments
 (0)