cutter.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class cutter:
  2. def __init__(self, target: bytes) -> None:
  3. self.__current = target
  4. def __check(self, count: int) -> None:
  5. if count == 0:
  6. raise RuntimeError("Cutter is already empty.")
  7. if count > self.size:
  8. raise RuntimeError("Count is higher than size.")
  9. def from_start(self, count: int) -> bytes:
  10. self.__check(count)
  11. result = self.__current[:count]
  12. self.__current = self.__current[count:]
  13. return result
  14. def from_end(self, count: int) -> bytes:
  15. self.__check(count)
  16. result = self.__current[self.size - count:]
  17. self.__current = self.__current[:self.size - count]
  18. return result
  19. def cut(self, count: int) -> bytes:
  20. return self.from_end(count)
  21. def trim(self, count: int) -> bytes:
  22. return self.from_start(count)
  23. @property
  24. def content(self) -> bytes:
  25. return self.__current
  26. @property
  27. def size(self) -> int:
  28. return len(self.__current)
  29. def __len__(self) -> int:
  30. return self.size