ucs2length.ts 629 B

1234567891011121314151617181920
  1. // https://mathiasbynens.be/notes/javascript-encoding
  2. // https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
  3. export default function ucs2length(str: string): number {
  4. const len = str.length
  5. let length = 0
  6. let pos = 0
  7. let value: number
  8. while (pos < len) {
  9. length++
  10. value = str.charCodeAt(pos++)
  11. if (value >= 0xd800 && value <= 0xdbff && pos < len) {
  12. // high surrogate, and there is a next character
  13. value = str.charCodeAt(pos)
  14. if ((value & 0xfc00) === 0xdc00) pos++ // low surrogate
  15. }
  16. }
  17. return length
  18. }
  19. ucs2length.code = 'require("ajv/dist/runtime/ucs2length").default'