bitstring icon indicating copy to clipboard operation
bitstring copied to clipboard

Iterating using over Bits.find() causes an infinite loop

Open ajahraus opened this issue 2 years ago • 0 comments

When you write

bs = BitStream(filename=name)
while current_pos := bs.find(target_substring):
    # Read additional bits, so that bs.pos is now past current_pos
    bs.read(8)

current_pos does not take the value of the position of subsequent target_substrings. Instead, bs.pos seems to be reset in each iteration, which creates an infinite loop, where current_pos always remains at the location of the first match.

The desired behaviour can be hacked together via

bs = BitStream(filename=name)
current_pos = 0
while bs.find(target_substring, start=current_pos):
    # Read additional bits, so that bs.pos is now past current_pos
    bs.read(8)
    current_pos = bs.pos

Incidentally, this is different behaviour from

bs = BitStream(filename=name)
for start in s.findall(target_substring):
    # Read additional bits, so that bs.pos is now past current_pos
    bs.read(8)

because while inside the loop, I would like to be able to advance the file past the next occurrence of the target_substring.

ajahraus avatar Jan 18 '24 22:01 ajahraus