Skip to content

Commit a9b5a1c

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 d786551 commit a9b5a1c

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;
@@ -318,10 +319,10 @@ impl ReaderState {
318319
debug_assert!(content.ends_with(b"?>"), "{:?}", Bytes(content));
319320

320321
let content = &content[2..content.len() - 2];
321-
let len = content.len();
322+
let event = BytesStart::wrap(content);
322323

323-
if content.starts_with(b"xml") && (len == 3 || is_whitespace(content[3])) {
324-
let event = BytesDecl::from_start(BytesStart::wrap(content, 3));
324+
if event.name() == QName(b"xml") {
325+
let event = BytesDecl::from_start(event);
325326

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

343-
let content = &content[1..content.len() - 1];
344-
let len = content.len();
345-
let name_end = content
346-
.iter()
347-
.position(|&b| is_whitespace(b))
348-
.unwrap_or(len);
349-
// This is self-closed tag `<something/>`
350-
let name_len = if name_end < len { name_end } else { len - 1 };
351-
let event = BytesStart::wrap(&content[..len - 1], name_len);
344+
let event = BytesStart::wrap(&content[1..content.len() - 2]);
352345

353346
if self.config.expand_empty_elements {
354347
self.pending = true;
355348
self.opened_starts.push(self.opened_buffer.len());
356-
self.opened_buffer.extend(&content[..name_len]);
349+
self.opened_buffer.extend(event.name().as_ref());
357350
Ok(Event::Start(event))
358351
} else {
359352
Ok(Event::Empty(event))
@@ -363,18 +356,14 @@ impl ReaderState {
363356
debug_assert!(content.starts_with(b"<"), "{:?}", Bytes(content));
364357
debug_assert!(content.ends_with(b">"), "{:?}", Bytes(content));
365358

366-
let content = &content[1..content.len() - 1];
367-
let len = content.len();
368-
let name_end = content
369-
.iter()
370-
.position(|&b| is_whitespace(b))
371-
.unwrap_or(len);
359+
let event = BytesStart::wrap(&content[1..content.len() - 1]);
360+
372361
// #514: Always store names event when .check_end_names == false,
373362
// because checks can be temporary disabled and when they would be
374363
// enabled, we should have that information
375364
self.opened_starts.push(self.opened_buffer.len());
376-
self.opened_buffer.extend(&content[..name_end]);
377-
Ok(Event::Start(BytesStart::wrap(content, name_end)))
365+
self.opened_buffer.extend(event.name().as_ref());
366+
Ok(Event::Start(event))
378367
}
379368
FeedResult::EmitEndTag(_) => {
380369
debug_assert!(content.starts_with(b"</"), "{:?}", Bytes(content));

0 commit comments

Comments
 (0)