qstringhelpers.py 723 B

12345678910111213141516171819202122232425262728
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © Spyder Project Contributors
  4. # Licensed under the terms of the MIT License
  5. # (see spyder/__init__.py for details)
  6. """QString compatibility."""
  7. import sys
  8. PY2 = sys.version[0] == '2'
  9. def qstring_length(text):
  10. """
  11. Tries to compute what the length of an utf16-encoded QString would be.
  12. """
  13. if PY2:
  14. # I don't know what this is encoded in, so there is nothing I can do.
  15. return len(text)
  16. utf16_text = text.encode('utf16')
  17. length = len(utf16_text) // 2
  18. # Remove Byte order mark.
  19. # TODO: All unicode Non-characters should be removed
  20. if utf16_text[:2] in [b'\xff\xfe', b'\xff\xff', b'\xfe\xff']:
  21. length -= 1
  22. return length