Code refactoring

Is there a better way to write this? x: u8 -- rustfmt wants to put each equality check on it's own line, which makes the function take up half the screen:

        x == b'@'
            || x == b'$'
            || x == b'%'
            || x == b'^'
            || x == b'$'
            || x == b'_'
            || x == b':'
            || x == b'.'
            // compaarison
            || x == b'='
            || x == b'<'
            || x == b'>'
            || x == b'&'
            || x == b'!'
            // arith
            || x == b'+'
            || x == b'-'
            || x == b'*'
            || x == b'/'

Since it is a byte and all of them compare for equality, I think it is a good candidate to be implemented as lookup table. Performance could be much better as well.

Does 'lookup table' mean "giant match statement" :slight_smile: ?

Like creating an array of [u8; 256].
Initialising the array like: arr[b'@'] = 1 for all tokens while remaining offsets initialised with zero.
Then arr[x ] == 1 is suffice.

1 Like

I'd suggest the match statement, and let LLVM decide the best way to do it. It shouldn't be too bad,

match x {
    b'@' | b'$' | ... => ...,
    _ => ...,
}

You could also just make it shorter to code with something like b"@$%".contains(&x).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.