Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find(self, bs, start=None, end=None, bytealigned=None):
t = Bits.find(self, bs, start, end, bytealigned)
if t:
self._pos = t[0]
return t
Raises ValueError if the delimiter is empty.
"""
delimiter = Bits(delimiter)
if not delimiter.len:
raise ValueError("split delimiter cannot be empty.")
start, end = self._validate_slice(start, end)
if bytealigned is None:
bytealigned = bitstring.bytealigned
if count is not None and count < 0:
raise ValueError("Cannot split - count must be >= 0.")
if count == 0:
return
# Use the base class find as we don't want to ever alter _pos.
found = Bits.find(self, delimiter, start, end, bytealigned)
if not found:
# Initial bits are the whole bitstring being searched
yield self._slice(start, end)
return
# yield the bytes before the first occurrence of the delimiter, even if empty
yield self[start:found[0]]
startpos = pos = found[0]
c = 1
while count is None or c < count:
pos += delimiter.len
found = Bits.find(self, delimiter, pos, end, bytealigned)
if not found:
# No more occurrences, so return the rest of the bitstring
yield self[startpos:end]
return
c += 1
return s
def __add__(self, bs):
s = Bits.__add__(self, bs)
s._pos = 0
return s
__add__.__doc__ = Bits.__add__.__doc__
def find(self, bs, start=None, end=None, bytealigned=None):
t = Bits.find(self, bs, start, end, bytealigned)
if t:
self._pos = t[0]
return t
find.__doc__ = Bits.find.__doc__
def rfind(self, bs, start=None, end=None, bytealigned=None):
t = Bits.rfind(self, bs, start, end, bytealigned)
if t:
self._pos = t[0]
return t
rfind.__doc__ = Bits.rfind.__doc__
def read(self, fmt):
"""Interpret next bits according to the format string and return result.
fmt -- Token string describing how to interpret the next bits.
Token examples: 'int:12' : 12 bits as a signed integer
'uint:8' : 8 bits as an unsigned integer
raise ValueError("Cannot split - count must be >= 0.")
if count == 0:
return
# Use the base class find as we don't want to ever alter _pos.
found = Bits.find(self, delimiter, start, end, bytealigned)
if not found:
# Initial bits are the whole bitstring being searched
yield self._slice(start, end)
return
# yield the bytes before the first occurrence of the delimiter, even if empty
yield self[start:found[0]]
startpos = pos = found[0]
c = 1
while count is None or c < count:
pos += delimiter.len
found = Bits.find(self, delimiter, pos, end, bytealigned)
if not found:
# No more occurrences, so return the rest of the bitstring
yield self[startpos:end]
return
c += 1
yield self[startpos:found[0]]
startpos = pos = found[0]
# Have generated count bitstrings, so time to quit.
return