Util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.polyUint32Array = exports.polyUint16Array = exports.decode = exports.fromCodePoint = exports.toCodePoints = void 0;
  4. var toCodePoints = function (str) {
  5. var codePoints = [];
  6. var i = 0;
  7. var length = str.length;
  8. while (i < length) {
  9. var value = str.charCodeAt(i++);
  10. if (value >= 0xd800 && value <= 0xdbff && i < length) {
  11. var extra = str.charCodeAt(i++);
  12. if ((extra & 0xfc00) === 0xdc00) {
  13. codePoints.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
  14. }
  15. else {
  16. codePoints.push(value);
  17. i--;
  18. }
  19. }
  20. else {
  21. codePoints.push(value);
  22. }
  23. }
  24. return codePoints;
  25. };
  26. exports.toCodePoints = toCodePoints;
  27. var fromCodePoint = function () {
  28. var codePoints = [];
  29. for (var _i = 0; _i < arguments.length; _i++) {
  30. codePoints[_i] = arguments[_i];
  31. }
  32. if (String.fromCodePoint) {
  33. return String.fromCodePoint.apply(String, codePoints);
  34. }
  35. var length = codePoints.length;
  36. if (!length) {
  37. return '';
  38. }
  39. var codeUnits = [];
  40. var index = -1;
  41. var result = '';
  42. while (++index < length) {
  43. var codePoint = codePoints[index];
  44. if (codePoint <= 0xffff) {
  45. codeUnits.push(codePoint);
  46. }
  47. else {
  48. codePoint -= 0x10000;
  49. codeUnits.push((codePoint >> 10) + 0xd800, (codePoint % 0x400) + 0xdc00);
  50. }
  51. if (index + 1 === length || codeUnits.length > 0x4000) {
  52. result += String.fromCharCode.apply(String, codeUnits);
  53. codeUnits.length = 0;
  54. }
  55. }
  56. return result;
  57. };
  58. exports.fromCodePoint = fromCodePoint;
  59. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  60. // Use a lookup table to find the index.
  61. var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
  62. for (var i = 0; i < chars.length; i++) {
  63. lookup[chars.charCodeAt(i)] = i;
  64. }
  65. var decode = function (base64) {
  66. var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
  67. if (base64[base64.length - 1] === '=') {
  68. bufferLength--;
  69. if (base64[base64.length - 2] === '=') {
  70. bufferLength--;
  71. }
  72. }
  73. var buffer = typeof ArrayBuffer !== 'undefined' &&
  74. typeof Uint8Array !== 'undefined' &&
  75. typeof Uint8Array.prototype.slice !== 'undefined'
  76. ? new ArrayBuffer(bufferLength)
  77. : new Array(bufferLength);
  78. var bytes = Array.isArray(buffer) ? buffer : new Uint8Array(buffer);
  79. for (i = 0; i < len; i += 4) {
  80. encoded1 = lookup[base64.charCodeAt(i)];
  81. encoded2 = lookup[base64.charCodeAt(i + 1)];
  82. encoded3 = lookup[base64.charCodeAt(i + 2)];
  83. encoded4 = lookup[base64.charCodeAt(i + 3)];
  84. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  85. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  86. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  87. }
  88. return buffer;
  89. };
  90. exports.decode = decode;
  91. var polyUint16Array = function (buffer) {
  92. var length = buffer.length;
  93. var bytes = [];
  94. for (var i = 0; i < length; i += 2) {
  95. bytes.push((buffer[i + 1] << 8) | buffer[i]);
  96. }
  97. return bytes;
  98. };
  99. exports.polyUint16Array = polyUint16Array;
  100. var polyUint32Array = function (buffer) {
  101. var length = buffer.length;
  102. var bytes = [];
  103. for (var i = 0; i < length; i += 4) {
  104. bytes.push((buffer[i + 3] << 24) | (buffer[i + 2] << 16) | (buffer[i + 1] << 8) | buffer[i]);
  105. }
  106. return bytes;
  107. };
  108. exports.polyUint32Array = polyUint32Array;
  109. //# sourceMappingURL=Util.js.map