MCPcopy Index your code
hub / github.com/OneNoteDev/WebClipper / utf8Slice

Function utf8Slice

lib/sanitize-html.js:1513–1584  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

1511}
1512
1513function utf8Slice (buf, start, end) {
1514 end = Math.min(buf.length, end)
1515 var res = []
1516
1517 var i = start
1518 while (i < end) {
1519 var firstByte = buf[i]
1520 var codePoint = null
1521 var bytesPerSequence = (firstByte > 0xEF) ? 4
1522 : (firstByte > 0xDF) ? 3
1523 : (firstByte > 0xBF) ? 2
1524 : 1
1525
1526 if (i + bytesPerSequence <= end) {
1527 var secondByte, thirdByte, fourthByte, tempCodePoint
1528
1529 switch (bytesPerSequence) {
1530 case 1:
1531 if (firstByte < 0x80) {
1532 codePoint = firstByte
1533 }
1534 break
1535 case 2:
1536 secondByte = buf[i + 1]
1537 if ((secondByte & 0xC0) === 0x80) {
1538 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
1539 if (tempCodePoint > 0x7F) {
1540 codePoint = tempCodePoint
1541 }
1542 }
1543 break
1544 case 3:
1545 secondByte = buf[i + 1]
1546 thirdByte = buf[i + 2]
1547 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
1548 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
1549 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
1550 codePoint = tempCodePoint
1551 }
1552 }
1553 break
1554 case 4:
1555 secondByte = buf[i + 1]
1556 thirdByte = buf[i + 2]
1557 fourthByte = buf[i + 3]
1558 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
1559 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
1560 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
1561 codePoint = tempCodePoint
1562 }
1563 }
1564 }
1565 }
1566
1567 if (codePoint === null) {
1568 // we did not generate a valid codePoint so insert a
1569 // replacement char (U+FFFD) and advance only 1 byte
1570 codePoint = 0xFFFD

Callers 2

slowToStringFunction · 0.85
sanitize-html.jsFile · 0.85

Calls 1

decodeCodePointsArrayFunction · 0.85

Tested by

no test coverage detected