lib/icu/Util: add overload which throws exception
This commit is contained in:
parent
a497cc46f9
commit
4eaa82fd22
@ -27,6 +27,7 @@
|
|||||||
#include <unicode/ustring.h>
|
#include <unicode/ustring.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -73,3 +74,25 @@ UCharToUTF8(ConstBuffer<UChar> src)
|
|||||||
dest[dest_length] = 0;
|
dest[dest_length] = 0;
|
||||||
return AllocatedString<>::Donate(dest.release());
|
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,
|
||||||
|
src.data, src.size,
|
||||||
|
&error_code);
|
||||||
|
if (U_FAILURE(error_code))
|
||||||
|
throw std::runtime_error(u_errorName(error_code));
|
||||||
|
|
||||||
|
dest[dest_length] = 0;
|
||||||
|
return AllocatedString<>::Donate(dest.release());
|
||||||
|
}
|
||||||
|
@ -40,4 +40,12 @@ UCharFromUTF8(const char *src);
|
|||||||
AllocatedString<char>
|
AllocatedString<char>
|
||||||
UCharToUTF8(ConstBuffer<UChar> src);
|
UCharToUTF8(ConstBuffer<UChar> src);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for u_strToUTF8().
|
||||||
|
*
|
||||||
|
* Throws std::runtime_error on error.
|
||||||
|
*/
|
||||||
|
AllocatedString<char>
|
||||||
|
UCharToUTF8Throw(ConstBuffer<UChar> src);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user