MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / utf8Slice

Function utf8Slice

lib/models/dav.js:2775–2846  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

2773}
2774
2775function utf8Slice (buf, start, end) {
2776 end = Math.min(buf.length, end);
2777 var res = [];
2778
2779 var i = start;
2780 while (i < end) {
2781 var firstByte = buf[i];
2782 var codePoint = null;
2783 var bytesPerSequence = (firstByte > 0xEF) ? 4
2784 : (firstByte > 0xDF) ? 3
2785 : (firstByte > 0xBF) ? 2
2786 : 1;
2787
2788 if (i + bytesPerSequence <= end) {
2789 var secondByte, thirdByte, fourthByte, tempCodePoint;
2790
2791 switch (bytesPerSequence) {
2792 case 1:
2793 if (firstByte < 0x80) {
2794 codePoint = firstByte;
2795 }
2796 break
2797 case 2:
2798 secondByte = buf[i + 1];
2799 if ((secondByte & 0xC0) === 0x80) {
2800 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
2801 if (tempCodePoint > 0x7F) {
2802 codePoint = tempCodePoint;
2803 }
2804 }
2805 break
2806 case 3:
2807 secondByte = buf[i + 1];
2808 thirdByte = buf[i + 2];
2809 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
2810 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
2811 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
2812 codePoint = tempCodePoint;
2813 }
2814 }
2815 break
2816 case 4:
2817 secondByte = buf[i + 1];
2818 thirdByte = buf[i + 2];
2819 fourthByte = buf[i + 3];
2820 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
2821 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
2822 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
2823 codePoint = tempCodePoint;
2824 }
2825 }
2826 }
2827 }
2828
2829 if (codePoint === null) {
2830 // we did not generate a valid codePoint so insert a
2831 // replacement char (U+FFFD) and advance only 1 byte
2832 codePoint = 0xFFFD;

Callers 2

slowToStringFunction · 0.85
dav.jsFile · 0.85

Calls 1

decodeCodePointsArrayFunction · 0.85

Tested by

no test coverage detected