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