| 334 | } |
| 335 | |
| 336 | void Stream::StreamInUtf16() const { |
| 337 | unsigned long ch = 0; |
| 338 | unsigned char bytes[2]; |
| 339 | int nBigEnd = (m_charSet == utf16be) ? 0 : 1; |
| 340 | |
| 341 | bytes[0] = GetNextByte(); |
| 342 | bytes[1] = GetNextByte(); |
| 343 | if (!m_input.good()) { |
| 344 | return; |
| 345 | } |
| 346 | ch = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) | |
| 347 | static_cast<unsigned long>(bytes[1 ^ nBigEnd]); |
| 348 | |
| 349 | if (ch >= 0xDC00 && ch < 0xE000) { |
| 350 | // Trailing (low) surrogate...ugh, wrong order |
| 351 | QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 356 | // ch is a leading (high) surrogate |
| 357 | |
| 358 | // Four byte UTF-8 code point |
| 359 | |
| 360 | // Read the trailing (low) surrogate |
| 361 | for (;;) { |
| 362 | bytes[0] = GetNextByte(); |
| 363 | bytes[1] = GetNextByte(); |
| 364 | if (!m_input.good()) { |
| 365 | QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); |
| 366 | return; |
| 367 | } |
| 368 | unsigned long chLow = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) | |
| 369 | static_cast<unsigned long>(bytes[1 ^ nBigEnd]); |
| 370 | if (chLow < 0xDC00 || chLow >= 0xE000) { |
| 371 | // Trouble...not a low surrogate. Dump a REPLACEMENT CHARACTER into the |
| 372 | // stream. |
| 373 | QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); |
| 374 | |
| 375 | // Deal with the next UTF-16 unit |
| 376 | if (chLow < 0xD800 || chLow >= 0xE000) { |
| 377 | // Easiest case: queue the codepoint and return |
| 378 | QueueUnicodeCodepoint(m_readahead, ch); |
| 379 | return; |
| 380 | } |
| 381 | // Start the loop over with the new high surrogate |
| 382 | ch = chLow; |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | // Select the payload bits from the high surrogate |
| 387 | ch &= 0x3FF; |
| 388 | ch <<= 10; |
| 389 | |
| 390 | // Include bits from low surrogate |
| 391 | ch |= (chLow & 0x3FF); |
| 392 | |
| 393 | // Add the surrogacy offset |
nothing calls this directly
no test coverage detected