MCPcopy Create free account
hub / github.com/SocketCluster/socketcluster / utf8Slice

Function utf8Slice

app/public/socketcluster-client.js:3552–3623  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

3550}
3551
3552function utf8Slice (buf, start, end) {
3553 end = Math.min(buf.length, end)
3554 var res = []
3555
3556 var i = start
3557 while (i < end) {
3558 var firstByte = buf[i]
3559 var codePoint = null
3560 var bytesPerSequence = (firstByte > 0xEF) ? 4
3561 : (firstByte > 0xDF) ? 3
3562 : (firstByte > 0xBF) ? 2
3563 : 1
3564
3565 if (i + bytesPerSequence <= end) {
3566 var secondByte, thirdByte, fourthByte, tempCodePoint
3567
3568 switch (bytesPerSequence) {
3569 case 1:
3570 if (firstByte < 0x80) {
3571 codePoint = firstByte
3572 }
3573 break
3574 case 2:
3575 secondByte = buf[i + 1]
3576 if ((secondByte & 0xC0) === 0x80) {
3577 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
3578 if (tempCodePoint > 0x7F) {
3579 codePoint = tempCodePoint
3580 }
3581 }
3582 break
3583 case 3:
3584 secondByte = buf[i + 1]
3585 thirdByte = buf[i + 2]
3586 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
3587 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
3588 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
3589 codePoint = tempCodePoint
3590 }
3591 }
3592 break
3593 case 4:
3594 secondByte = buf[i + 1]
3595 thirdByte = buf[i + 2]
3596 fourthByte = buf[i + 3]
3597 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
3598 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
3599 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
3600 codePoint = tempCodePoint
3601 }
3602 }
3603 }
3604 }
3605
3606 if (codePoint === null) {
3607 // we did not generate a valid codePoint so insert a
3608 // replacement char (U+FFFD) and advance only 1 byte
3609 codePoint = 0xFFFD

Callers 2

slowToStringFunction · 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…