UNPKG

protobufjs

Version:

Protocol Buffers for JavaScript & TypeScript.

165 lines (149 loc) 4.82 kB
"use strict"; /** * A minimal UTF8 implementation. * @memberof util * @namespace */ var utf8 = exports, looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }), strictDecoder; var TEXT_DECODER_MIN_LENGTH = 64; try { strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); } catch (err) { // "fatal" option is not supported on Node.js compiled without ICU strictDecoder = looseDecoder; } /** * Calculates the UTF8 byte length of a string. * @param {string} string String * @returns {number} Byte length */ utf8.length = function utf8_length(string) { var len = 0, c = 0; for (var i = 0; i < string.length; ++i) { c = string.charCodeAt(i); if (c < 128) len += 1; else if (c < 2048) len += 2; else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) { ++i; len += 4; } else len += 3; } return len; }; function utf8_read_decoder(decoder, buffer, start, end) { var source = start === 0 && end === buffer.length ? buffer : buffer.subarray(start, end); return decoder.decode(source); } /** * Reads UTF8 bytes as a string. * @param {Uint8Array} buffer Source buffer * @param {number} start Source start * @param {number} end Source end * @returns {string} String read */ utf8.read = function utf8_read_loose(buffer, start, end) { if (end - start < 1) return ""; if (end - start >= TEXT_DECODER_MIN_LENGTH) return utf8_read_decoder(looseDecoder, buffer, start, end); var str = "", i = start, c1, c2, c3, c4, c5, c6, c7, c8; for (; i + 7 < end; i += 8) { c1 = buffer[i]; c2 = buffer[i + 1]; c3 = buffer[i + 2]; c4 = buffer[i + 3]; c5 = buffer[i + 4]; c6 = buffer[i + 5]; c7 = buffer[i + 6]; c8 = buffer[i + 7]; if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80) return str + utf8_read_decoder(looseDecoder, buffer, i, end); str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8); } for (; i < end; ++i) { c1 = buffer[i]; if (c1 & 0x80) return str + utf8_read_decoder(looseDecoder, buffer, i, end); str += String.fromCharCode(c1); } return str; }; /** * Reads UTF8 bytes as a string, rejecting invalid UTF8. * @param {Uint8Array} buffer Source buffer * @param {number} start Source start * @param {number} end Source end * @returns {string} String read */ utf8.readStrict = function utf8_read_strict(buffer, start, end) { if (end - start < 1) return ""; if (end - start >= TEXT_DECODER_MIN_LENGTH) return utf8_read_decoder(strictDecoder, buffer, start, end); var str = "", i = start, c1, c2, c3, c4, c5, c6, c7, c8; for (; i + 7 < end; i += 8) { c1 = buffer[i]; c2 = buffer[i + 1]; c3 = buffer[i + 2]; c4 = buffer[i + 3]; c5 = buffer[i + 4]; c6 = buffer[i + 5]; c7 = buffer[i + 6]; c8 = buffer[i + 7]; if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80) return str + utf8_read_decoder(strictDecoder, buffer, i, end); str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8); } for (; i < end; ++i) { c1 = buffer[i]; if (c1 & 0x80) return str + utf8_read_decoder(strictDecoder, buffer, i, end); str += String.fromCharCode(c1); } return str; }; /** * Writes a string as UTF8 bytes. * @param {string} string Source string * @param {Uint8Array} buffer Destination buffer * @param {number} offset Destination offset * @returns {number} Bytes written */ utf8.write = function utf8_write(string, buffer, offset) { var start = offset, c1, // character 1 c2; // character 2 for (var i = 0; i < string.length; ++i) { c1 = string.charCodeAt(i); if (c1 < 128) { buffer[offset++] = c1; } else if (c1 < 2048) { buffer[offset++] = c1 >> 6 | 192; buffer[offset++] = c1 & 63 | 128; } else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) { c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF); ++i; buffer[offset++] = c1 >> 18 | 240; buffer[offset++] = c1 >> 12 & 63 | 128; buffer[offset++] = c1 >> 6 & 63 | 128; buffer[offset++] = c1 & 63 | 128; } else { buffer[offset++] = c1 >> 12 | 224; buffer[offset++] = c1 >> 6 & 63 | 128; buffer[offset++] = c1 & 63 | 128; } } return offset - start; };