aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunasSudo <runassudo@yingtongli.me>2022-08-19 00:16:29 +1000
committerRunasSudo <runassudo@yingtongli.me>2022-08-19 00:16:29 +1000
commit2bce8cfc3f748c2adf46b2c62add9073a68a9585 (patch)
treeed6c3fb3f26dede29069419e07ad05cea8036d5e
parent35104055d9ed8be0bff12d53f0cb9f24f888d58a (diff)
Avoid String allocations in BLT parserHEADmaster
-rw-r--r--src/parser/blt.rs19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/parser/blt.rs b/src/parser/blt.rs
index 85a1cf0..e8a080a 100644
--- a/src/parser/blt.rs
+++ b/src/parser/blt.rs
@@ -191,10 +191,23 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
/// Parse an integer into a [usize]
fn usize(&mut self) -> Result<usize, ParseError> {
- return Ok(self.integer()?.parse().expect("Invalid usize"));
+ // return Ok(self.integer()?.parse().expect("Invalid usize"));
+ // Use a separate implementation to avoid allocating String
+
+ let mut result = self.digit_nonzero()?.to_digit(10).unwrap() as usize;
+ loop {
+ match self.digit() {
+ Err(_) => { break; }
+ Ok(d) => {
+ result = result.checked_mul(10).expect("Integer overflows usize");
+ result = result.checked_add(d.to_digit(10).unwrap() as usize).expect("Integer overflows usize");
+ }
+ }
+ }
+ return Ok(result);
}
- /// Parse an integer as a [String]
+ /*/// Parse an integer as a [String]
fn integer(&mut self) -> Result<String, ParseError> {
let mut result = String::from(self.digit_nonzero()?);
loop {
@@ -204,7 +217,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
}
}
return Ok(result);
- }
+ }*/
/// Parse a number as an instance of N
fn ballot_value(&mut self) -> Result<(), ParseError> {
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.