lib/icu/Util: fold UCharToUTF8Throw() into UCharToUTF8()

This commit is contained in:
Max Kellermann 2016-04-21 10:58:21 +02:00
parent b9f535cd49
commit 423cd5900e
3 changed files with 4 additions and 30 deletions

View File

@ -100,7 +100,7 @@ DoConvert(iconv_t conv, const char *src)
AllocatedString<char>
IcuConverter::ToUTF8(const char *s) const
{
try {
#ifdef HAVE_ICU
const ScopeLock protect(mutex);
@ -123,6 +123,8 @@ IcuConverter::ToUTF8(const char *s) const
#elif defined(HAVE_ICONV)
return DoConvert(to_utf8, s);
#endif
} catch (const std::runtime_error &) {
return nullptr;
}
AllocatedString<char>

View File

@ -63,28 +63,6 @@ UCharToUTF8(ConstBuffer<UChar> src)
std::unique_ptr<char[]> dest(new char[dest_capacity + 1]);
UErrorCode error_code = U_ZERO_ERROR;
int32_t dest_length;
u_strToUTF8(dest.get(), dest_capacity, &dest_length,
src.data, src.size,
&error_code);
if (U_FAILURE(error_code))
return nullptr;
dest[dest_length] = 0;
return AllocatedString<>::Donate(dest.release());
}
AllocatedString<>
UCharToUTF8Throw(ConstBuffer<UChar> src)
{
assert(!src.IsNull());
/* worst-case estimate */
size_t dest_capacity = 4 * src.size;
std::unique_ptr<char[]> dest(new char[dest_capacity + 1]);
UErrorCode error_code = U_ZERO_ERROR;
int32_t dest_length;
u_strToUTF8(dest.get(), dest_capacity, &dest_length,

View File

@ -34,18 +34,12 @@ template<typename T> class AllocatedString;
AllocatedArray<UChar>
UCharFromUTF8(const char *src);
/**
* Wrapper for u_strToUTF8().
*/
AllocatedString<char>
UCharToUTF8(ConstBuffer<UChar> src);
/**
* Wrapper for u_strToUTF8().
*
* Throws std::runtime_error on error.
*/
AllocatedString<char>
UCharToUTF8Throw(ConstBuffer<UChar> src);
UCharToUTF8(ConstBuffer<UChar> src);
#endif