// This method should do the same thing as:
// ```
// fn is_end_of_stack(&self, line: &[u8]) -> bool {
//     match std::str::from_utf8(line) {
//         Ok(line) => {
//             let line = line.trim();
//             match line.parse::<usize>() {
//                 Ok(_) => true,
//                 Err(_) => false,
//             }
//         }
//         Err(_) => false,
//     }
// }
// ```
// But it is much faster since it works directly on bytes and because all we're interested in is
// whether the provided bytes **can** be parsed into a `usize`, not which `usize` they parse into.
// Also, we don't need to validate that the input is utf8.
//
// Benchmarking results for the two methods:
// * Using the method above: 281 MiB/s
// * Using the method below: 437 MiB/s
//


Benchmarking results for the two methods:
Using the method above: 281 MiB/s
Using the method below: 437 MiB/s
Benchmarking results for the two methods : Using the method above : 281 MiB / s Using the method below : 437 MiB / s
fn would_end_stack(&mut self, line: &[u8]) -> bool {
        // In order to return `true`, as we iterate over the provided bytes, we need to progress
        // through each of the follow states, in order; if we can't, immediately return `false`.
        #[allow(clippy::enum_variant_names)]
        enum State {
            StartOfLine,  // Accept any number of whitespace characters
            MiddleOfLine, // Accept any number of ascii digits
            EndOfLine,    // Accept any number of whitespace characters
        }
        let mut state = State::StartOfLine;
        for b in line {
            let c = *b as char;
            match state {
                State::StartOfLine => {
                    if c.is_whitespace() {
                        continue;
                    } else if c.is_ascii_digit() {
                        state = State::MiddleOfLine;
                    } else {
                        return false;
                    }
                }
                State::MiddleOfLine => {
                    if c.is_ascii_digit() {
                        continue;
                    } else if c.is_whitespace() {
                        state = State::EndOfLine;
                    } else {
                        return false;
                    }
                }
                State::EndOfLine => {
                    if c.is_whitespace() {
                        continue;
                    } else {
                        return false;
                    }
                }
            }
        }
        true
    }
fn would_end_stack ( & mut self , line : & [ u8 ] ) -> bool { // In order to return `true`, as we iterate over the provided bytes, we need to progress // through each of the follow states, in order; if we can't, immediately return `false`. # [ allow ( clippy enum_variant_names ) ] enum State { StartOfLine , // Accept any number of whitespace characters MiddleOfLine , // Accept any number of ascii digits EndOfLine , // Accept any number of whitespace characters } let mut state = State :: StartOfLine ; for b in line { let c = * b as char ; match state { State :: StartOfLine => { if c . is_whitespace ( ) { continue ; } else if c . is_ascii_digit ( ) { state = State :: MiddleOfLine ; } else { return false ; } } State :: MiddleOfLine => { if c . is_ascii_digit ( ) { continue ; } else if c . is_whitespace ( ) { state = State :: EndOfLine ; } else { return false ; } } State :: EndOfLine => { if c . is_whitespace ( ) { continue ; } else { return false ; } } } } true }
This method should do the same thing as:
```
fn is_end_of_stack(&self, line: &[u8]) -> bool {
match std::str::from_utf8(line) {
Ok(line) => {
let line = line.trim();
match line.parse::<usize>() {
Ok(_) => true,
Err(_) => false,
}
}
Err(_) => false,
}
}
```
But it is much faster since it works directly on bytes and because all we're interested in is
whether the provided bytes **can** be parsed into a `usize`, not which `usize` they parse into.
This method should do the same thing as : ` ` ` fn is_end_of_stack ( &self line : & [ u8 ] ) - > bool { match std :: str :: from_utf8 ( line ) { Ok ( line ) = > { let line = line . trim () ; match line . parse :: <usize > () { Ok ( _ ) = > true Err ( _ ) = > false } } Err ( _ ) = > false } } ` ` ` But it is much faster since it works directly on bytes and because all we ' re interested in is whether the provided bytes ** can ** be parsed into a ` usize ` not which ` usize ` they parse into .
&mut self
line
&[u8]