Skip to content

Commit c51d8ac

Browse files
Mingundralley
authored andcommitted
Move text reading to a dedicated function
This follow common pattern of other functions and also would allow to reduce duplication in async implementation, because this part is independent from a way of reading data
1 parent df16f79 commit c51d8ac

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

src/reader/mod.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -496,31 +496,7 @@ impl<R> Reader<R> {
496496
.reader
497497
.read_bytes_until(b'<', buf, &mut self.buf_position)
498498
{
499-
Ok(Some(bytes)) => {
500-
#[cfg(feature = "encoding")]
501-
if first && self.encoding.can_be_refined() {
502-
if let Some(encoding) = detect_encoding(bytes) {
503-
self.encoding = EncodingRef::BomDetected(encoding);
504-
}
505-
}
506-
507-
let content = if self.trim_text_end {
508-
// Skip the ending '<
509-
let len = bytes
510-
.iter()
511-
.rposition(|&b| !is_whitespace(b))
512-
.map_or_else(|| bytes.len(), |p| p + 1);
513-
&bytes[..len]
514-
} else {
515-
bytes
516-
};
517-
518-
Ok(if first {
519-
Event::StartText(BytesText::wrap(content, self.decoder()).into())
520-
} else {
521-
Event::Text(BytesText::wrap(content, self.decoder()))
522-
})
523-
}
499+
Ok(Some(bytes)) => self.read_text(bytes, first),
524500
Ok(None) => Ok(Event::Eof),
525501
Err(e) => Err(e),
526502
}
@@ -570,6 +546,44 @@ impl<R> Reader<R> {
570546
}
571547
}
572548

549+
/// Trims whitespaces from `bytes`, if required, and returns a [`StartText`]
550+
/// or a [`Text`] event. When [`StartText`] is returned, the method can change
551+
/// the encoding of the reader, detecting it from the beginning of the stream.
552+
///
553+
/// # Parameters
554+
/// - `bytes`: data from the start of stream to the first `<` or from `>` to `<`
555+
/// - `first`: if `true`, then this is the first call of that function,
556+
/// i. e. data from the start of stream and [`StartText`] will be returned,
557+
/// otherwise [`Text`] will be returned
558+
///
559+
/// [`StartText`]: Event::StartText
560+
/// [`Text`]: Event::Text
561+
fn read_text<'b>(&mut self, bytes: &'b [u8], first: bool) -> Result<Event<'b>> {
562+
#[cfg(feature = "encoding")]
563+
if first && self.encoding.can_be_refined() {
564+
if let Some(encoding) = detect_encoding(bytes) {
565+
self.encoding = EncodingRef::BomDetected(encoding);
566+
}
567+
}
568+
569+
let content = if self.trim_text_end {
570+
// Skip the ending '<
571+
let len = bytes
572+
.iter()
573+
.rposition(|&b| !is_whitespace(b))
574+
.map_or_else(|| bytes.len(), |p| p + 1);
575+
&bytes[..len]
576+
} else {
577+
bytes
578+
};
579+
580+
Ok(if first {
581+
Event::StartText(BytesText::wrap(content, self.decoder()).into())
582+
} else {
583+
Event::Text(BytesText::wrap(content, self.decoder()))
584+
})
585+
}
586+
573587
/// reads `BytesElement` starting with a `!`,
574588
/// return `Comment`, `CData` or `DocType` event
575589
fn read_bang<'b>(&mut self, bang_type: BangType, buf: &'b [u8]) -> Result<Event<'b>> {

0 commit comments

Comments
 (0)