MCPcopy Create free account
hub / github.com/bitpay/bitcore-message / utf8Slice

Function utf8Slice

bitcore-message.js:1209–1280  ·  view source on GitHub ↗
(buf, start, end)

Source from the content-addressed store, hash-verified

1207}
1208
1209function utf8Slice (buf, start, end) {
1210 end = Math.min(buf.length, end)
1211 var res = []
1212
1213 var i = start
1214 while (i < end) {
1215 var firstByte = buf[i]
1216 var codePoint = null
1217 var bytesPerSequence = (firstByte > 0xEF) ? 4
1218 : (firstByte > 0xDF) ? 3
1219 : (firstByte > 0xBF) ? 2
1220 : 1
1221
1222 if (i + bytesPerSequence <= end) {
1223 var secondByte, thirdByte, fourthByte, tempCodePoint
1224
1225 switch (bytesPerSequence) {
1226 case 1:
1227 if (firstByte < 0x80) {
1228 codePoint = firstByte
1229 }
1230 break
1231 case 2:
1232 secondByte = buf[i + 1]
1233 if ((secondByte & 0xC0) === 0x80) {
1234 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
1235 if (tempCodePoint > 0x7F) {
1236 codePoint = tempCodePoint
1237 }
1238 }
1239 break
1240 case 3:
1241 secondByte = buf[i + 1]
1242 thirdByte = buf[i + 2]
1243 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
1244 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
1245 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
1246 codePoint = tempCodePoint
1247 }
1248 }
1249 break
1250 case 4:
1251 secondByte = buf[i + 1]
1252 thirdByte = buf[i + 2]
1253 fourthByte = buf[i + 3]
1254 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
1255 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
1256 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
1257 codePoint = tempCodePoint
1258 }
1259 }
1260 }
1261 }
1262
1263 if (codePoint === null) {
1264 // we did not generate a valid codePoint so insert a
1265 // replacement char (U+FFFD) and advance only 1 byte
1266 codePoint = 0xFFFD

Callers 2

slowToStringFunction · 0.85
bitcore-message.jsFile · 0.85

Calls 1

decodeCodePointsArrayFunction · 0.85

Tested by

no test coverage detected