MCPcopy Index your code
hub / github.com/plotly/plotly.js / utf8ToBytes

Function utf8ToBytes

stackgl_modules/index.js:1419–1481  ·  view source on GitHub ↗
(string, units)

Source from the content-addressed store, hash-verified

1417 return str;
1418}
1419function utf8ToBytes(string, units) {
1420 units = units || Infinity;
1421 var codePoint;
1422 var length = string.length;
1423 var leadSurrogate = null;
1424 var bytes = [];
1425 for (var i = 0; i < length; ++i) {
1426 codePoint = string.charCodeAt(i);
1427
1428 // is surrogate component
1429 if (codePoint > 0xD7FF && codePoint < 0xE000) {
1430 // last char was a lead
1431 if (!leadSurrogate) {
1432 // no lead yet
1433 if (codePoint > 0xDBFF) {
1434 // unexpected trail
1435 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1436 continue;
1437 } else if (i + 1 === length) {
1438 // unpaired lead
1439 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1440 continue;
1441 }
1442
1443 // valid lead
1444 leadSurrogate = codePoint;
1445 continue;
1446 }
1447
1448 // 2 leads in a row
1449 if (codePoint < 0xDC00) {
1450 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1451 leadSurrogate = codePoint;
1452 continue;
1453 }
1454
1455 // valid surrogate pair
1456 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
1457 } else if (leadSurrogate) {
1458 // valid bmp char, but last char was a lead
1459 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1460 }
1461 leadSurrogate = null;
1462
1463 // encode utf8
1464 if (codePoint < 0x80) {
1465 if ((units -= 1) < 0) break;
1466 bytes.push(codePoint);
1467 } else if (codePoint < 0x800) {
1468 if ((units -= 2) < 0) break;
1469 bytes.push(codePoint >> 0x6 | 0xC0, codePoint & 0x3F | 0x80);
1470 } else if (codePoint < 0x10000) {
1471 if ((units -= 3) < 0) break;
1472 bytes.push(codePoint >> 0xC | 0xE0, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80);
1473 } else if (codePoint < 0x110000) {
1474 if ((units -= 4) < 0) break;
1475 bytes.push(codePoint >> 0x12 | 0xF0, codePoint >> 0xC & 0x3F | 0x80, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80);
1476 } else {

Callers 2

byteLengthFunction · 0.85
utf8WriteFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…