src/vs/base/common/strings.ts/encodeUTF8
export function encodeUTF8(str: string): Uint8Array {
	const strLen = str.length;

	// See https://en.wikipedia.org/wiki/UTF-8

	// first loop to establish needed buffer size
	let neededSize = 0;
	let strOffset = 0;
	while (strOffset < strLen) {
		const codePoint = getNextCodePoint(str, strLen, strOffset);
		strOffset += (codePoint >= Constants.UNICODE_SUPPLEMENTARY_PLANE_BEGIN ? 2 : 1);

		if (codePoint < 0x0080) {
			neededSize += 1;
		} else if (codePoint < 0x0800) {
			neededSize += 2;
		} else if (codePoint < 0x10000) {
			neededSize += 3;
		} else {
			neededSize += 4;
		}
	}

	// second loop to actually encode
	const arr = new Uint8Array(neededSize);
	strOffset = 0;
	let arrOffset = 0;
	while (strOffset < strLen) {
		const codePoint = getNextCodePoint(str, strLen, strOffset);
		strOffset += (codePoint >= Constants.UNICODE_SUPPLEMENTARY_PLANE_BEGIN ? 2 : 1);

		if (codePoint < 0x0080) {
			arr[arrOffset++] = codePoint;
		} else if (codePoint < 0x0800) {
			arr[arrOffset++] = 0b11000000 | ((codePoint & 0b00000000000000000000011111000000) >>> 6);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
		} else if (codePoint < 0x10000) {
			arr[arrOffset++] = 0b11100000 | ((codePoint & 0b00000000000000001111000000000000) >>> 12);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
		} else {
			arr[arrOffset++] = 0b11110000 | ((codePoint & 0b00000000000111000000000000000000) >>> 18);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000111111000000000000) >>> 12);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
			arr[arrOffset++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
		}
	}

	return arr;
}