()
| 6439 | } |
| 6440 | |
| 6441 | function decodeSymbol() { |
| 6442 | var byte1; |
| 6443 | var byte2; |
| 6444 | var byte3; |
| 6445 | var byte4; |
| 6446 | var codePoint; |
| 6447 | |
| 6448 | if (byteIndex > byteCount) { |
| 6449 | throw Error('Invalid byte index'); |
| 6450 | } |
| 6451 | |
| 6452 | if (byteIndex == byteCount) { |
| 6453 | return false; |
| 6454 | } |
| 6455 | |
| 6456 | // Read the first byte. |
| 6457 | byte1 = byteArray[byteIndex] & 0xFF; |
| 6458 | byteIndex++; |
| 6459 | |
| 6460 | // 1-byte sequence (no continuation bytes) |
| 6461 | if ((byte1 & 0x80) == 0) { |
| 6462 | return byte1; |
| 6463 | } |
| 6464 | |
| 6465 | // 2-byte sequence |
| 6466 | if ((byte1 & 0xE0) == 0xC0) { |
| 6467 | var byte2 = readContinuationByte(); |
| 6468 | codePoint = ((byte1 & 0x1F) << 6) | byte2; |
| 6469 | if (codePoint >= 0x80) { |
| 6470 | return codePoint; |
| 6471 | } else { |
| 6472 | throw Error('Invalid continuation byte'); |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | // 3-byte sequence (may include unpaired surrogates) |
| 6477 | if ((byte1 & 0xF0) == 0xE0) { |
| 6478 | byte2 = readContinuationByte(); |
| 6479 | byte3 = readContinuationByte(); |
| 6480 | codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; |
| 6481 | if (codePoint >= 0x0800) { |
| 6482 | return codePoint; |
| 6483 | } else { |
| 6484 | throw Error('Invalid continuation byte'); |
| 6485 | } |
| 6486 | } |
| 6487 | |
| 6488 | // 4-byte sequence |
| 6489 | if ((byte1 & 0xF8) == 0xF0) { |
| 6490 | byte2 = readContinuationByte(); |
| 6491 | byte3 = readContinuationByte(); |
| 6492 | byte4 = readContinuationByte(); |
| 6493 | codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) | |
| 6494 | (byte3 << 0x06) | byte4; |
| 6495 | if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { |
| 6496 | return codePoint; |
| 6497 | } |
| 6498 | } |
no test coverage detected