Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. /**
  3. * A minimal UTF8 implementation for number arrays.
  4. * @memberof util
  5. * @namespace
  6. */
  7. var utf8 = exports;
  8. /**
  9. * Calculates the UTF8 byte length of a string.
  10. * @param {string} string String
  11. * @returns {number} Byte length
  12. */
  13. utf8.length = function utf8_length(string) {
  14. var len = 0,
  15. c = 0;
  16. for (var i = 0; i < string.length; ++i) {
  17. c = string.charCodeAt(i);
  18. if (c < 128)
  19. len += 1;
  20. else if (c < 2048)
  21. len += 2;
  22. else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
  23. ++i;
  24. len += 4;
  25. } else
  26. len += 3;
  27. }
  28. return len;
  29. };
  30. /**
  31. * Reads UTF8 bytes as a string.
  32. * @param {Uint8Array} buffer Source buffer
  33. * @param {number} start Source start
  34. * @param {number} end Source end
  35. * @returns {string} String read
  36. */
  37. utf8.read = function utf8_read(buffer, start, end) {
  38. var len = end - start;
  39. if (len < 1)
  40. return "";
  41. var parts = null,
  42. chunk = [],
  43. i = 0, // char offset
  44. t; // temporary
  45. while (start < end) {
  46. t = buffer[start++];
  47. if (t < 128)
  48. chunk[i++] = t;
  49. else if (t > 191 && t < 224)
  50. chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
  51. else if (t > 239 && t < 365) {
  52. t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
  53. chunk[i++] = 0xD800 + (t >> 10);
  54. chunk[i++] = 0xDC00 + (t & 1023);
  55. } else
  56. chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
  57. if (i > 8191) {
  58. (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
  59. i = 0;
  60. }
  61. }
  62. if (parts) {
  63. if (i)
  64. parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
  65. return parts.join("");
  66. }
  67. return String.fromCharCode.apply(String, chunk.slice(0, i));
  68. };
  69. /**
  70. * Writes a string as UTF8 bytes.
  71. * @param {string} string Source string
  72. * @param {Uint8Array} buffer Destination buffer
  73. * @param {number} offset Destination offset
  74. * @returns {number} Bytes written
  75. */
  76. utf8.write = function utf8_write(string, buffer, offset) {
  77. var start = offset,
  78. c1, // character 1
  79. c2; // character 2
  80. for (var i = 0; i < string.length; ++i) {
  81. c1 = string.charCodeAt(i);
  82. if (c1 < 128) {
  83. buffer[offset++] = c1;
  84. } else if (c1 < 2048) {
  85. buffer[offset++] = c1 >> 6 | 192;
  86. buffer[offset++] = c1 & 63 | 128;
  87. } else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
  88. c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
  89. ++i;
  90. buffer[offset++] = c1 >> 18 | 240;
  91. buffer[offset++] = c1 >> 12 & 63 | 128;
  92. buffer[offset++] = c1 >> 6 & 63 | 128;
  93. buffer[offset++] = c1 & 63 | 128;
  94. } else {
  95. buffer[offset++] = c1 >> 12 | 224;
  96. buffer[offset++] = c1 >> 6 & 63 | 128;
  97. buffer[offset++] = c1 & 63 | 128;
  98. }
  99. }
  100. return offset - start;
  101. };