Skip to content

Commit 62a6f6d

Browse files
committed
Reduce code size
1 parent 7eec14c commit 62a6f6d

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

src/reader/parser.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -288,35 +288,34 @@ impl PullParser {
288288
// While lexer gives us Ok(maybe_token) -- we loop.
289289
// Upon having a complete XML-event -- we return from the whole function.
290290
match self.lexer.next_token(r) {
291-
Ok(maybe_token) =>
292-
match maybe_token {
293-
None => break,
294-
Some(token) =>
295-
match self.dispatch_token(token) {
296-
None => {} // continue
297-
Some(Ok(XmlEvent::EndDocument)) =>
298-
return {
299-
self.next_pos();
300-
self.set_final_result(Ok(XmlEvent::EndDocument))
301-
},
302-
Some(Ok(xml_event)) =>
303-
return {
304-
self.next_pos();
305-
Ok(xml_event)
306-
},
307-
Some(Err(xml_error)) =>
308-
return {
309-
self.next_pos();
310-
self.set_final_result(Err(xml_error))
311-
},
312-
}
313-
},
291+
Ok(Some(token)) => {
292+
match self.dispatch_token(token) {
293+
None => {} // continue
294+
Some(Ok(XmlEvent::EndDocument)) => {
295+
self.next_pos();
296+
return self.set_final_result(Ok(XmlEvent::EndDocument))
297+
},
298+
Some(Ok(xml_event)) => {
299+
self.next_pos();
300+
return Ok(xml_event)
301+
},
302+
Some(Err(xml_error)) => {
303+
self.next_pos();
304+
return self.set_final_result(Err(xml_error))
305+
},
306+
}
307+
},
308+
Ok(None) => break,
314309
Err(lexer_error) =>
315310
return self.set_final_result(Err(lexer_error)),
316311
}
317312
}
318313

319-
// Handle end of stream
314+
self.handle_eof()
315+
}
316+
317+
/// Handle end of stream
318+
fn handle_eof(&mut self) -> std::result::Result<XmlEvent, super::Error> {
320319
// Forward pos to the lexer head
321320
self.next_pos();
322321
let ev = if self.depth() == 0 {
@@ -339,6 +338,7 @@ impl PullParser {
339338

340339
// This function is to be called when a terminal event is reached.
341340
// The function sets up the `self.final_result` into `Some(result)` and return `result`.
341+
#[inline]
342342
fn set_final_result(&mut self, result: Result) -> Result {
343343
self.final_result = Some(result.clone());
344344
result
@@ -363,6 +363,7 @@ impl PullParser {
363363
self.pos.push(self.lexer.position());
364364
}
365365

366+
#[inline(never)]
366367
fn dispatch_token(&mut self, t: Token) -> Option<Result> {
367368
match self.st {
368369
State::OutsideTag => self.outside_tag(t),

src/reader/parser/inside_reference.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ impl PullParser {
6464

6565
pub(crate) fn numeric_reference_from_str(&self, num_str: &str) -> std::result::Result<char, String> {
6666
let val = if let Some(hex) = num_str.strip_prefix('x') {
67-
u32::from_str_radix(hex, 16).map_err(|_| format!("Invalid hexadecimal character number in an entity: {num_str}"))?
67+
u32::from_str_radix(hex, 16).map_err(move |_| format!("Invalid hexadecimal character number in an entity: {num_str}"))?
6868
} else {
69-
u32::from_str_radix(num_str, 10).map_err(|_| format!("Invalid character number in an entity: {num_str}"))?
69+
u32::from_str_radix(num_str, 10).map_err(move |_| format!("Invalid character number in an entity: {num_str}"))?
7070
};
7171
match char::from_u32(val) {
7272
Some('\0') => Err("NUL character entity is not allowed".into()),

src/util.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,23 @@ pub enum Encoding {
5757
Unknown,
5858
}
5959

60+
// Rustc inlines eq_ignore_ascii_case and creates kilobytes of code!
61+
#[inline(never)]
62+
fn icmp(lower: &str, varcase: &str) -> bool {
63+
lower.bytes().zip(varcase.bytes()).all(|(l, v)| l == v.to_ascii_lowercase())
64+
}
65+
6066
impl FromStr for Encoding {
6167
type Err = &'static str;
6268

6369
fn from_str(val: &str) -> Result<Self, Self::Err> {
64-
if ["utf-8", "utf8"].iter().any(|label| label.eq_ignore_ascii_case(val)) {
70+
if ["utf-8", "utf8"].into_iter().any(move |label| icmp(label, val)) {
6571
Ok(Encoding::Utf8)
66-
} else if ["iso-8859-1", "latin1"].iter().any(|label| label.eq_ignore_ascii_case(val)) {
72+
} else if ["iso-8859-1", "latin1"].into_iter().any(move |label| icmp(label, val)) {
6773
Ok(Encoding::Latin1)
68-
} else if ["utf-16", "utf16"].iter().any(|label| label.eq_ignore_ascii_case(val)) {
74+
} else if ["utf-16", "utf16"].into_iter().any(move |label| icmp(label, val)) {
6975
Ok(Encoding::Utf16)
70-
} else if ["ascii", "us-ascii"].iter().any(|label| label.eq_ignore_ascii_case(val)) {
76+
} else if ["ascii", "us-ascii"].into_iter().any(move |label| icmp(label, val)) {
7177
Ok(Encoding::Ascii)
7278
} else {
7379
Err("unknown encoding name")

0 commit comments

Comments
 (0)