lib/icu/Util: UCharFromUTF8() throws on error

This commit is contained in:
Max Kellermann 2016-04-21 11:20:41 +02:00
parent 423cd5900e
commit 21f17270a1
4 changed files with 15 additions and 8 deletions

View File

@ -99,13 +99,16 @@ IcuCollate(const char *a, const char *b)
#else
/* fall back to ucol_strcoll() */
const auto au = UCharFromUTF8(a);
const auto bu = UCharFromUTF8(b);
try {
const auto au = UCharFromUTF8(a);
const auto bu = UCharFromUTF8(b);
return !au.IsNull() && !bu.IsNull()
? (int)ucol_strcoll(collator, au.begin(), au.size(),
bu.begin(), bu.size())
: strcasecmp(a, b);
return ucol_strcoll(collator, au.begin(), au.size(),
bu.begin(), bu.size());
} catch (const std::runtime_error &) {
/* fall back to plain strcasecmp() */
return strcasecmp(a, b);
}
#endif
#elif defined(WIN32)

View File

@ -129,7 +129,7 @@ try {
AllocatedString<char>
IcuConverter::FromUTF8(const char *s) const
{
try {
#ifdef HAVE_ICU
const ScopeLock protect(mutex);
@ -156,6 +156,8 @@ IcuConverter::FromUTF8(const char *s) const
#elif defined(HAVE_ICONV)
return DoConvert(from_utf8, s);
#endif
} catch (const std::runtime_error &) {
return nullptr;
}
#endif

View File

@ -47,7 +47,7 @@ UCharFromUTF8(const char *src)
src, src_length,
&error_code);
if (U_FAILURE(error_code))
return {};
throw std::runtime_error(u_errorName(error_code));
dest.SetSize(dest_length);
return dest;

View File

@ -30,6 +30,8 @@ template<typename T> class AllocatedString;
/**
* Wrapper for u_strFromUTF8().
*
* Throws std::runtime_error on error.
*/
AllocatedArray<UChar>
UCharFromUTF8(const char *src);