| 586 | namespace UtfConverter |
| 587 | { |
| 588 | std::wstring FromUtf8(const std::string& utf8string) |
| 589 | { |
| 590 | size_t widesize = utf8string.length(); |
| 591 | if (sizeof(wchar_t) == 2) |
| 592 | { |
| 593 | wchar_t* widestringnative = new wchar_t[widesize+1]; |
| 594 | const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
| 595 | const UTF8* sourceend = sourcestart + widesize; |
| 596 | UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative); |
| 597 | UTF16* targetend = targetstart + widesize+1; |
| 598 | ConversionResult res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
| 599 | if (res != conversionOK) |
| 600 | { |
| 601 | delete [] widestringnative; |
| 602 | throw std::exception(); |
| 603 | } |
| 604 | *targetstart = 0; |
| 605 | std::wstring resultstring(widestringnative); |
| 606 | delete [] widestringnative; |
| 607 | return resultstring; |
| 608 | } |
| 609 | else if (sizeof(wchar_t) == 4) // somewhat pointless as it's always 2 on WIN32, but whatever. |
| 610 | { |
| 611 | wchar_t* widestringnative = new wchar_t[widesize]; |
| 612 | const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
| 613 | const UTF8* sourceend = sourcestart + widesize; |
| 614 | UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative); |
| 615 | UTF32* targetend = targetstart + widesize; |
| 616 | ConversionResult res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
| 617 | if (res != conversionOK) |
| 618 | { |
| 619 | delete [] widestringnative; |
| 620 | throw std::exception(); |
| 621 | } |
| 622 | *targetstart = 0; |
| 623 | std::wstring resultstring(widestringnative); |
| 624 | delete [] widestringnative; |
| 625 | return resultstring; |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | throw std::exception(); |
| 630 | } |
| 631 | return L""; |
| 632 | } |
| 633 | |
| 634 | std::string ToUtf8(const std::wstring& widestring) |
| 635 | { |
no test coverage detected