伊地知ニジカ放送局だぬ゛ん゛. https://www.youtube.com/@deerjika
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
977 B

  1. import unicodedata
  2. class CommonModule:
  3. @staticmethod
  4. def is_wide (
  5. c: str) \
  6. -> bool:
  7. return unicodedata.east_asian_width (c) in ['F', 'W', 'A']
  8. @classmethod
  9. def len_by_full (
  10. cls,
  11. string: str) \
  12. -> float:
  13. return sum (1 if cls.is_wide (c) else .5 for c in string)
  14. @classmethod
  15. def index_by_f2c (
  16. cls,
  17. string: str,
  18. index: float) \
  19. -> int:
  20. i: int = 0
  21. work: str = ''
  22. for c in string:
  23. work += c
  24. if cls.len_by_full (work) > index:
  25. break
  26. else:
  27. i += 1
  28. return i
  29. @classmethod
  30. def mid_by_full (
  31. cls,
  32. string: str,
  33. start: float,
  34. length: float) \
  35. -> str:
  36. trimmed_left: str = string[cls.index_by_f2c (string, start):]
  37. return trimmed_left[:cls.index_by_f2c (trimmed_left, length)]