utf8.js 867 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.utf8Size = void 0;
  4. function utf8Size(str) {
  5. const length = str.length;
  6. let size = 0;
  7. let pos = 0;
  8. while (pos < length) {
  9. let value = str.charCodeAt(pos++);
  10. if ((value & 0xffffff80) === 0) {
  11. size++;
  12. continue;
  13. }
  14. else if ((value & 0xfffff800) === 0)
  15. size += 2;
  16. else {
  17. if (value >= 0xd800 && value <= 0xdbff && pos < length) {
  18. const extra = str.charCodeAt(pos);
  19. if ((extra & 0xfc00) === 0xdc00)
  20. value = (pos++, ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
  21. }
  22. size += 3 + +((value & 0xffff0000) !== 0);
  23. }
  24. }
  25. return size;
  26. }
  27. exports.utf8Size = utf8Size;
  28. //# sourceMappingURL=utf8.js.map