wstring IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter) { wstring target; if(!v.empty()) { // // If there is a narrow string converter use it to convert to UTF8, otherwise the narrow // string is already UTF8 encoded. // string tmp; if(converter) { UTF8BufferI buffer; Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer); buffer.swap(tmp, last); } else { tmp = v; } const WstringConverterPtr& wConverterWithDefault = wConverter ? wConverter : getUnicodeWstringConverter(); // // Convert from UTF-8 to the wide string encoding // wConverterWithDefault->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()), reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target); } return target; }
string IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter) { string target; if(!v.empty()) { const WstringConverterPtr& wConverterWithDefault = wConverter ? wConverter : getUnicodeWstringConverter(); // // First convert to UTF-8 narrow string. // UTF8BufferI buffer; Byte* last = wConverterWithDefault->toUTF8(v.data(), v.data() + v.size(), buffer); buffer.swap(target, last); // // If narrow string converter is present convert to the native narrow string encoding, otherwise // native narrow string encoding is UTF8 and we are done. // if(converter) { string tmp; converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()), reinterpret_cast<const Byte*>(target.data() + target.size()), tmp); tmp.swap(target); } } return target; }
string IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) { string target; if(!v.empty()) { // // First convert to UTF8 narrow string. // if(wConverter) { UTF8BufferI buffer; Byte* last = wConverter->toUTF8(v.data(), v.data() + v.size(), buffer); target = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } else { size_t size = v.size() * 4 * sizeof(char); Byte* outBuf = new Byte[size]; Byte* targetStart = outBuf; Byte* targetEnd = outBuf + size; const wchar_t* sourceStart = v.data(); ConversionResult cr = convertUTFWstringToUTF8( sourceStart, sourceStart + v.size(), targetStart, targetEnd, flags); if(cr != conversionOK) { delete[] outBuf; assert(cr == sourceExhausted || cr == sourceIllegal); throw IllegalConversionException(__FILE__, __LINE__, cr == sourceExhausted ? "partial character" : "bad encoding"); } target = string(reinterpret_cast<char*>(outBuf), static_cast<size_t>(targetStart - outBuf)); delete[] outBuf; } // // If narrow string converter is present convert to the native narrow string encoding, otherwise // native narrow string encoding is UTF8 and we are done. // if(converter) { string tmp; converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()), reinterpret_cast<const Byte*>(target.data() + target.size()), tmp); tmp.swap(target); } } return target; }
wstring IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) { wstring target; if(!v.empty()) { // // If there is a narrow string converter use it to convert to UTF8, otherwise the narrow // string is already UTF8 encoded. // string tmp; if(converter) { UTF8BufferI buffer; Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer); tmp = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } else { tmp = v; } // // If there is a wide string converter use fromUTF8 to convert to the wide string native encoding. // if(wConverter) { wConverter->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()), reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target); } else { const Byte* sourceStart = reinterpret_cast<const Byte*>(tmp.data()); ConversionResult cr = convertUTF8ToUTFWstring(sourceStart, sourceStart + tmp.size(), target, flags); if(cr != conversionOK) { assert(cr == sourceExhausted || cr == sourceIllegal); throw IllegalConversionException(__FILE__, __LINE__, cr == sourceExhausted ? "partial character" : "bad encoding"); } } } return target; }