ucs2length.js 558 B

123456789101112131415161718192021
  1. 'use strict';
  2. // https://mathiasbynens.be/notes/javascript-encoding
  3. // https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
  4. module.exports = function ucs2length(str) {
  5. var length = 0
  6. , len = str.length
  7. , pos = 0
  8. , value;
  9. while (pos < len) {
  10. length++;
  11. value = str.charCodeAt(pos++);
  12. if (value >= 0xD800 && value <= 0xDBFF && pos < len) {
  13. // high surrogate, and there is a next character
  14. value = str.charCodeAt(pos);
  15. if ((value & 0xFC00) == 0xDC00) pos++; // low surrogate
  16. }
  17. }
  18. return length;
  19. };