Add UTF8ToUTF16 and UTF16ToUTF8.

This commit is contained in:
Victor Zverovich 2014-04-30 07:23:43 -07:00
parent 859a4975f6
commit da9aeab810
2 changed files with 68 additions and 1 deletions

View File

@ -167,6 +167,42 @@ void fmt::internal::ReportUnknownType(char code, const char *type) {
<< static_cast<unsigned>(code) << type));
}
#ifdef _WIN32
fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, 0, 0);
static const char ERROR[] = "cannot convert string from UTF-8 to UTF-16";
if (length == 0)
ThrowSystemError(GetLastError(), ERROR);
buffer_.resize(length);
length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, &buffer_[0], length);
if (length == 0)
ThrowSystemError(GetLastError(), ERROR);
}
fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) {
if (int error_code = Convert(s)) {
ThrowSystemError(GetLastError(),
"cannot convert string from UTF-16 to UTF-8");
}
}
int fmt::internal::UTF16ToUTF8::Convert(fmt::WStringRef s) {
int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, 0, 0, 0, 0);
if (length == 0)
return GetLastError();
buffer_.resize(length);
length = WideCharToMultiByte(
CP_UTF8, 0, s.c_str(), -1, &buffer_[0], length, 0, 0);
if (length == 0)
return GetLastError();
return 0;
}
#endif
void fmt::internal::FormatSystemErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
#ifndef _WIN32

View File

@ -458,6 +458,37 @@ template <typename Char, typename T>
void FormatCustomArg(
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec);
#ifdef _WIN32
// A converter from UTF-8 to UTF-16.
// It is only provided for Windows since other systems use UTF-8.
class UTF8ToUTF16 {
private:
Array<wchar_t, INLINE_BUFFER_SIZE> buffer_;
public:
explicit UTF8ToUTF16(StringRef s);
operator const wchar_t*() const { return &buffer_[0]; }
size_t size() const { return buffer_.size() - 1; }
};
// A converter from UTF-16 to UTF-8.
// It is only provided for Windows since other systems use UTF-8.
class UTF16ToUTF8 {
private:
Array<char, INLINE_BUFFER_SIZE> buffer_;
public:
UTF16ToUTF8() {}
explicit UTF16ToUTF8(WStringRef s);
operator const char*() const { return &buffer_[0]; }
size_t size() const { return buffer_.size() - 1; }
// Performs conversion returning a system error code instead of
// throwing exception on error.
int Convert(WStringRef s);
};
#endif
// Formats a system error message writing the output to out.
void FormatSystemErrorMessage(Writer &out, int error_code, StringRef message);
@ -1507,7 +1538,7 @@ class SystemErrorSink {
/** Throws SystemError with a code and a formatted message. */
inline Formatter<SystemErrorSink> ThrowSystemError(
int error_code, StringRef format) {
int error_code, StringRef format = 0) {
Formatter<SystemErrorSink> f(format, SystemErrorSink(error_code));
return f;
}