scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
412 stars 68 forks source link

Use standard range checking - throw less exceptions #302

Open scott-griffiths opened 1 year ago

scott-griffiths commented 1 year ago

Currently a lot of methods will check bit ranges and throw exceptions for out of range positions.

Basically I'm thinking of anything that uses the _validate_slice method.

Standard slice checking doesn't do this - if an index is out of range it gets clipped to the extreme instead. By using this behaviour we can simplify some code and perhaps also be more Pythonic.

It's a breaking change, but not hugely so - some exceptions wouldn't be raised.

Validating a slice could then be done with just this:

i = slice(start, end, None).indices(len(self))
        return i[0], i[1]