伊地知ニジカ放送局だぬ゛ん゛. 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.

common_module.py 1005 B

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