MCPcopy Index your code
hub / github.com/plotly/plotly.js / utf8Slice

Function utf8Slice

stackgl_modules/index.js:834–896  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

832 }
833}
834function utf8Slice(buf, start, end) {
835 end = Math.min(buf.length, end);
836 var res = [];
837 var i = start;
838 while (i < end) {
839 var firstByte = buf[i];
840 var codePoint = null;
841 var bytesPerSequence = firstByte > 0xEF ? 4 : firstByte > 0xDF ? 3 : firstByte > 0xBF ? 2 : 1;
842 if (i + bytesPerSequence <= end) {
843 var secondByte, thirdByte, fourthByte, tempCodePoint;
844 switch (bytesPerSequence) {
845 case 1:
846 if (firstByte < 0x80) {
847 codePoint = firstByte;
848 }
849 break;
850 case 2:
851 secondByte = buf[i + 1];
852 if ((secondByte & 0xC0) === 0x80) {
853 tempCodePoint = (firstByte & 0x1F) << 0x6 | secondByte & 0x3F;
854 if (tempCodePoint > 0x7F) {
855 codePoint = tempCodePoint;
856 }
857 }
858 break;
859 case 3:
860 secondByte = buf[i + 1];
861 thirdByte = buf[i + 2];
862 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
863 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | thirdByte & 0x3F;
864 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
865 codePoint = tempCodePoint;
866 }
867 }
868 break;
869 case 4:
870 secondByte = buf[i + 1];
871 thirdByte = buf[i + 2];
872 fourthByte = buf[i + 3];
873 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
874 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | fourthByte & 0x3F;
875 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
876 codePoint = tempCodePoint;
877 }
878 }
879 }
880 }
881 if (codePoint === null) {
882 // we did not generate a valid codePoint so insert a
883 // replacement char (U+FFFD) and advance only 1 byte
884 codePoint = 0xFFFD;
885 bytesPerSequence = 1;
886 } else if (codePoint > 0xFFFF) {
887 // encode to utf16 (surrogate pair dance)
888 codePoint -= 0x10000;
889 res.push(codePoint >>> 10 & 0x3FF | 0xD800);
890 codePoint = 0xDC00 | codePoint & 0x3FF;
891 }

Callers 2

slowToStringFunction · 0.85
index.jsFile · 0.85

Calls 1

decodeCodePointsArrayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…