WideString StringUtils::utf8String2WideString( const String& utf8String )
	{
		size_t widesize = utf8String.length();
		WideString returnWideString;

		if ( sizeof( wchar_t ) == 2 )
		{
			returnWideString.resize( widesize + 1, L'\0' );
			const UTF8* sourcestart = reinterpret_cast<const UTF8*>( utf8String.c_str() );
			const UTF8* sourceend = sourcestart + widesize;
			UTF16* targetstart = reinterpret_cast<UTF16*>( &((returnWideString)[ 0 ]) );
			UTF16* thisFirstWChar = targetstart;
			UTF16* targetend = targetstart + widesize;
			ConversionResult res = ConvertUTF8toUTF16( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
			returnWideString.resize(targetstart - thisFirstWChar);

			if ( res != conversionOK )
			{
				throw Exception(Exception::ERROR_UTF8_2_WIDE, String("Could not convert from UTF8 to wide string."));
			}

			*targetstart = 0;
		}

		else if ( sizeof( wchar_t ) == 4 )
		{
			returnWideString.resize( widesize + 1, L'\0' );
			const UTF8* sourcestart = reinterpret_cast<const UTF8*>( utf8String.c_str() );
			const UTF8* sourceend = sourcestart + widesize;
			UTF32* targetstart = reinterpret_cast<UTF32*>( &((returnWideString)[ 0 ]) );
			UTF32* thisFirstWChar = targetstart;
			UTF32* targetend = targetstart + widesize;
			ConversionResult res = ConvertUTF8toUTF32( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
			returnWideString.resize(targetstart - thisFirstWChar);

			if ( res != conversionOK )
			{
				throw Exception(Exception::ERROR_UTF8_2_WIDE, String("Could not convert from UTF8 to wide string."));
			}

			*targetstart = 0;
		}

		else
		{
			throw Exception(Exception::ERROR_UTF8_2_WIDE, String("Could not convert from UTF8 to wide string."));
		}
		return returnWideString;
	}
Exemplo n.º 2
0
static WideString
StringToWideString(std::string const &str)
{
    int size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
    WideString wide = WideString();
    wide.resize(size);
    MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wide[0], size);
    return wide;
}