MCPcopy Create free account
hub / github.com/Wscats/vscode-explore / decodeUTF8

Function decodeUTF8

contributes/src/vs/base/common/strings.ts:648–700  ·  view source on GitHub ↗
(buffer: Uint8Array)

Source from the content-addressed store, hash-verified

646 * Use only in environments which do not offer native conversion methods!
647 */
648export function decodeUTF8(buffer: Uint8Array): string {
649 // https://en.wikipedia.org/wiki/UTF-8
650
651 const len = buffer.byteLength;
652 const result: string[] = [];
653 let offset = 0;
654 while (offset < len) {
655 const v0 = buffer[offset];
656 let codePoint: number;
657 if (v0 >= 0b11110000 && offset + 3 < len) {
658 // 4 bytes
659 codePoint = (
660 (((buffer[offset++] & 0b00000111) << 18) >>> 0)
661 | (((buffer[offset++] & 0b00111111) << 12) >>> 0)
662 | (((buffer[offset++] & 0b00111111) << 6) >>> 0)
663 | (((buffer[offset++] & 0b00111111) << 0) >>> 0)
664 );
665 } else if (v0 >= 0b11100000 && offset + 2 < len) {
666 // 3 bytes
667 codePoint = (
668 (((buffer[offset++] & 0b00001111) << 12) >>> 0)
669 | (((buffer[offset++] & 0b00111111) << 6) >>> 0)
670 | (((buffer[offset++] & 0b00111111) << 0) >>> 0)
671 );
672 } else if (v0 >= 0b11000000 && offset + 1 < len) {
673 // 2 bytes
674 codePoint = (
675 (((buffer[offset++] & 0b00011111) << 6) >>> 0)
676 | (((buffer[offset++] & 0b00111111) << 0) >>> 0)
677 );
678 } else {
679 // 1 byte
680 codePoint = buffer[offset++];
681 }
682
683 if ((codePoint >= 0 && codePoint <= 0xD7FF) || (codePoint >= 0xE000 && codePoint <= 0xFFFF)) {
684 // Basic Multilingual Plane
685 result.push(String.fromCharCode(codePoint));
686 } else if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
687 // Supplementary Planes
688 const uPrime = codePoint - 0x10000;
689 const w1 = 0xD800 + ((uPrime & 0b11111111110000000000) >>> 10);
690 const w2 = 0xDC00 + ((uPrime & 0b00000000001111111111) >>> 0);
691 result.push(String.fromCharCode(w1));
692 result.push(String.fromCharCode(w2));
693 } else {
694 // illegal code point
695 result.push(String.fromCharCode(0xFFFD));
696 }
697 }
698
699 return result.join('');
700}
701
702/**
703 * Generated using https://github.com/alexandrudima/unicode-utils/blob/master/generate-rtl-test.js

Callers

nothing calls this directly

Calls 2

joinMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected