----------------------------------------------------------------------------- Convert a UTF-16 string into UTF-8 encoding. -----------------------------------------------------------------------------
| 472 | // Convert a UTF-16 string into UTF-8 encoding. |
| 473 | //----------------------------------------------------------------------------- |
| 474 | uint32 OpenZWave::ConvertUFT16ToUTF8 |
| 475 | ( |
| 476 | uint16 _utf16, |
| 477 | char* _buffer, |
| 478 | uint32 pos |
| 479 | ) |
| 480 | { |
| 481 | static uint16 surrogate = 0; |
| 482 | |
| 483 | if( ( surrogate != 0 ) && ( ( _utf16 & 0xdc00 ) == 0xdc00 ) ) |
| 484 | { |
| 485 | // UTF-16 surrogate character pair converts to four UTF-8 characters. |
| 486 | // We have the second member of the pair, so we can do the conversion now. |
| 487 | _buffer[pos++] = (char)( ((surrogate>>7) & 0x0007) | 0x00f0 ); |
| 488 | _buffer[pos++] = (char)( ((surrogate>>1) & 0x0020) | ((surrogate>>2) & 0x000f) | 0x0090 ); |
| 489 | _buffer[pos++] = (char)( ((_utf16>>6) & 0x000f) | ((surrogate<<4) & 0x0030) | 0x0080 ); |
| 490 | _buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 ); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | surrogate = 0; |
| 495 | if( _utf16 & 0xff80 ) |
| 496 | { |
| 497 | if( _utf16 & 0xf800 ) |
| 498 | { |
| 499 | if( ( _utf16 & 0xd800 ) == 0xd800 ) |
| 500 | { |
| 501 | // UTF-16 surrogate character pair converts to four UTF-8 characters. |
| 502 | // We have the first member of the pair, so we store it for next time. |
| 503 | surrogate = _utf16; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | // UTF-16 character can be represented by three UTF-8 characters. |
| 508 | _buffer[pos++] = (char)( (_utf16>>12) | 0x00e0 ); |
| 509 | _buffer[pos++] = (char)( ((_utf16>>6) & 0x003f) | 0x0080 ); |
| 510 | _buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 ); |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | // UTF-16 character can be represented by two UTF-8 characters. |
| 516 | _buffer[pos++] = (char)( (_utf16>>6) | 0x00c0 ); |
| 517 | _buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 ); |
| 518 | } |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | // UTF-16 character matches single ascii character. |
| 523 | _buffer[pos++] = (char)_utf16; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return pos; |
| 528 | } |
| 529 | |
| 530 | |
| 531 |
nothing calls this directly
no outgoing calls
no test coverage detected