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

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