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 #else
/* fall back to ucol_strcoll() */ /* fall back to ucol_strcoll() */
const auto au = UCharFromUTF8(a); try {
const auto bu = UCharFromUTF8(b); const auto au = UCharFromUTF8(a);
const auto bu = UCharFromUTF8(b);
return !au.IsNull() && !bu.IsNull() return ucol_strcoll(collator, au.begin(), au.size(),
? (int)ucol_strcoll(collator, au.begin(), au.size(), bu.begin(), bu.size());
bu.begin(), bu.size()) } catch (const std::runtime_error &) {
: strcasecmp(a, b); /* fall back to plain strcasecmp() */
return strcasecmp(a, b);
}
#endif #endif
#elif defined(WIN32) #elif defined(WIN32)

View File

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

View File

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

View File

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