Skip to content

Commit 58ce8da

Browse files
committed
parse numbers in one pass
1 parent 0b3dd7a commit 58ce8da

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/parser/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,16 @@ pub enum TabElement {
227227
fn numeric(s: &str) -> Result<(&str, u8), &str> {
228228
let bytes = s.as_bytes();
229229
let mut i = 0;
230-
while i < bytes.len() && matches!(bytes[i], 48..=58) {
230+
let mut sum = 0;
231+
while i < bytes.len() && bytes[i].is_ascii_digit() {
232+
sum *= 10;
233+
sum += bytes[i] - b'0';
231234
i += 1;
232235
}
233236
if i == 0 {
234237
return Err(s);
235238
};
236-
let parsed: u8 = bytes[0..i]
237-
.iter()
238-
.rev()
239-
.map(|x| x - 48)
240-
.enumerate()
241-
.map(|(idx, x)| 10u8.pow(idx as u32) * x)
242-
.sum();
243-
Ok((&s[i..], parsed))
239+
Ok((&s[i..], sum))
244240
}
245241

246242
fn tab_element(s: &str, set_bend_target: impl FnOnce(u8)) -> Result<(&str, TabElement), &str> {

0 commit comments

Comments
 (0)