MCPcopy Create free account
hub / github.com/microsoft/SandDance / utf8Slice

Function utf8Slice

docs/app/js/sanddance-app.js:113475–113529  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

113473 else return base64.fromByteArray(buf.slice(start, end));
113474}
113475function utf8Slice(buf, start, end) {
113476 end = Math.min(buf.length, end);
113477 var res = [];
113478 var i = start;
113479 while(i < end){
113480 var firstByte = buf[i];
113481 var codePoint = null;
113482 var bytesPerSequence = firstByte > 0xEF ? 4 : firstByte > 0xDF ? 3 : firstByte > 0xBF ? 2 : 1;
113483 if (i + bytesPerSequence <= end) {
113484 var secondByte, thirdByte, fourthByte, tempCodePoint;
113485 switch(bytesPerSequence){
113486 case 1:
113487 if (firstByte < 0x80) codePoint = firstByte;
113488 break;
113489 case 2:
113490 secondByte = buf[i + 1];
113491 if ((secondByte & 0xC0) === 0x80) {
113492 tempCodePoint = (firstByte & 0x1F) << 0x6 | secondByte & 0x3F;
113493 if (tempCodePoint > 0x7F) codePoint = tempCodePoint;
113494 }
113495 break;
113496 case 3:
113497 secondByte = buf[i + 1];
113498 thirdByte = buf[i + 2];
113499 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
113500 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | thirdByte & 0x3F;
113501 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) codePoint = tempCodePoint;
113502 }
113503 break;
113504 case 4:
113505 secondByte = buf[i + 1];
113506 thirdByte = buf[i + 2];
113507 fourthByte = buf[i + 3];
113508 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
113509 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | fourthByte & 0x3F;
113510 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) codePoint = tempCodePoint;
113511 }
113512 }
113513 }
113514 if (codePoint === null) {
113515 // we did not generate a valid codePoint so insert a
113516 // replacement char (U+FFFD) and advance only 1 byte
113517 codePoint = 0xFFFD;
113518 bytesPerSequence = 1;
113519 } else if (codePoint > 0xFFFF) {
113520 // encode to utf16 (surrogate pair dance)
113521 codePoint -= 0x10000;
113522 res.push(codePoint >>> 10 & 0x3FF | 0xD800);
113523 codePoint = 0xDC00 | codePoint & 0x3FF;
113524 }
113525 res.push(codePoint);
113526 i += bytesPerSequence;
113527 }
113528 return decodeCodePointsArray(res);
113529}
113530// Based on http://stackoverflow.com/a/22747272/680742, the browser with
113531// the lowest limit is Chrome, with 0x10000 args.
113532// We go 1 magnitude less, for safety

Callers 2

slowToStringFunction · 0.70
sanddance-app.jsFile · 0.70

Calls 2

decodeCodePointsArrayFunction · 0.70
minMethod · 0.45

Tested by

no test coverage detected