Skip to content

Commit

Permalink
Removing start and end parameters from starts_with.
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-griffiths committed Oct 13, 2024
1 parent c6def3d commit 4797f4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
15 changes: 5 additions & 10 deletions bitformat/_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,22 +698,19 @@ def set(self, value: Any, pos: int | Iterable[int] | None = None) -> Bits:
s._bitstore = self._bitstore.set_from_iterable(v, pos)
return s

def starts_with(self, prefix: BitsType, start: int | None = None, end: int | None = None) -> bool:
def starts_with(self, prefix: BitsType) -> bool:
"""Return whether the current Bits starts with prefix.
:param prefix: The Bits to search for.
:type prefix: BitsType
:param start: The bit position to start from. Defaults to 0.
:type start: int, optional
:param end: The bit position to end at. Defaults to len(self).
:type end: int, optional
:return: True if the Bits starts with the prefix, otherwise False.
:rtype: bool
"""
prefix = self.from_auto(prefix)
start, end = self._validate_slice(start, end)
return self._slice_copy(start, start + len(prefix)) == prefix if end >= start + len(prefix) else False
if len(prefix) <= len(self):
return self._slice_copy(0, len(prefix)) == prefix
return False

def to_bytes(self) -> bytes:
"""Return the Bits as bytes, padding with zero bits if needed.
Expand Down Expand Up @@ -781,8 +778,7 @@ def unpack(self, fmt: Dtype | str | list[Dtype | str], /) -> Any | list[Any]:

# ----- Private Methods -----

def _findall(self, bs: Bits, count: int | None,
bytealigned: bool) -> Iterable[int]:
def _findall(self, bs: Bits, count: int | None, bytealigned: bool) -> Iterable[int]:
c = 0
for i in self._bitstore.findall(bs._bitstore, bytealigned):
if count is not None and c >= count:
Expand Down Expand Up @@ -852,7 +848,6 @@ def _setuint(self, i: int | str, length: int | None = None) -> None:
raise ValueError(f"Unsigned integers cannot be initialised with the negative number {i}.")
raise e


def _getuint(self) -> int:
"""Return data as an unsigned int."""
if len(self) == 0:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_bitstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,13 @@ def test_startswith(self):

def test_startswith_start_end(self):
s = Bits('0x123456')
assert s.starts_with('0x234', 4)
assert not s.starts_with('0x123', end=11)
assert s.starts_with('0x123', end=12)
assert s.starts_with('0x34', 8, 16)
assert not s.starts_with('0x34', 7, 16)
assert not s.starts_with('0x34', 9, 16)
assert not s.starts_with('0x34', 8, 15)
assert s[4:].starts_with('0x234')
assert not s[:11].starts_with('0x123')
assert s[:12].starts_with('0x123')
assert s[8:16].starts_with('0x34')
assert not s[7:16].starts_with('0x34')
assert not s[9:16].starts_with('0x34')
assert not s[8:15].starts_with('0x34')

def test_endswith(self):
a = Bits()
Expand Down Expand Up @@ -1299,9 +1299,9 @@ def test_function_negative_indices(self):

# startswith
s = Bits('0xfe0012fe1200fe')
assert s.starts_with('0x00f', start=-16)
assert s.starts_with('0xfe00', end=-40)
assert not s.starts_with('0xfe00', end=-41)
assert s[-16:].starts_with('0x00f')
assert s[:-40].starts_with('0xfe00')
assert not s[:-41].starts_with('0xfe00')

# endswith
assert s[-16:].ends_with('0x00fe')
Expand Down

0 comments on commit 4797f4b

Please sign in to comment.