MCPcopy Create free account
hub / github.com/TruthHun/BookStack / utf8Slice

Function utf8Slice

static/word2md/mammoth.browser.js:10609–10680  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

10607}
10608
10609function utf8Slice (buf, start, end) {
10610 end = Math.min(buf.length, end)
10611 var res = []
10612
10613 var i = start
10614 while (i < end) {
10615 var firstByte = buf[i]
10616 var codePoint = null
10617 var bytesPerSequence = (firstByte > 0xEF) ? 4
10618 : (firstByte > 0xDF) ? 3
10619 : (firstByte > 0xBF) ? 2
10620 : 1
10621
10622 if (i + bytesPerSequence <= end) {
10623 var secondByte, thirdByte, fourthByte, tempCodePoint
10624
10625 switch (bytesPerSequence) {
10626 case 1:
10627 if (firstByte < 0x80) {
10628 codePoint = firstByte
10629 }
10630 break
10631 case 2:
10632 secondByte = buf[i + 1]
10633 if ((secondByte & 0xC0) === 0x80) {
10634 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
10635 if (tempCodePoint > 0x7F) {
10636 codePoint = tempCodePoint
10637 }
10638 }
10639 break
10640 case 3:
10641 secondByte = buf[i + 1]
10642 thirdByte = buf[i + 2]
10643 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
10644 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
10645 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
10646 codePoint = tempCodePoint
10647 }
10648 }
10649 break
10650 case 4:
10651 secondByte = buf[i + 1]
10652 thirdByte = buf[i + 2]
10653 fourthByte = buf[i + 3]
10654 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
10655 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
10656 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
10657 codePoint = tempCodePoint
10658 }
10659 }
10660 }
10661 }
10662
10663 if (codePoint === null) {
10664 // we did not generate a valid codePoint so insert a
10665 // replacement char (U+FFFD) and advance only 1 byte
10666 codePoint = 0xFFFD

Callers 2

slowToStringFunction · 0.70
mammoth.browser.jsFile · 0.70

Calls 1

decodeCodePointsArrayFunction · 0.70

Tested by

no test coverage detected