Avoid String allocations in BLT parser
This commit is contained in:
parent
35104055d9
commit
2bce8cfc3f
@ -191,10 +191,23 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
|||||||
|
|
||||||
/// Parse an integer into a [usize]
|
/// Parse an integer into a [usize]
|
||||||
fn usize(&mut self) -> Result<usize, ParseError> {
|
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> {
|
fn integer(&mut self) -> Result<String, ParseError> {
|
||||||
let mut result = String::from(self.digit_nonzero()?);
|
let mut result = String::from(self.digit_nonzero()?);
|
||||||
loop {
|
loop {
|
||||||
@ -204,7 +217,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/// Parse a number as an instance of N
|
/// Parse a number as an instance of N
|
||||||
fn ballot_value(&mut self) -> Result<(), ParseError> {
|
fn ballot_value(&mut self) -> Result<(), ParseError> {
|
||||||
|
Loading…
Reference in New Issue
Block a user