| 71 | } |
| 72 | |
| 73 | std::string StringUtilities::ToString(const std::wstring& input) { |
| 74 | // Assumption: The byte count of the resulting narrow string will be at most |
| 75 | // four times the character count of the input wstring. Allocate the buffer |
| 76 | // with that many char items (bytes) so that the first WideCharToMultiByte |
| 77 | // call will succeed most of the time as an optimization. |
| 78 | std::string output = ""; |
| 79 | int wide_string_length = static_cast<int>(input.size()) + 1; |
| 80 | int output_string_byte_count = wide_string_length * 4; |
| 81 | std::vector<char> string_buffer(output_string_byte_count); |
| 82 | bool convert_failed = (0 == ::WideCharToMultiByte(CP_UTF8, |
| 83 | 0, |
| 84 | input.c_str(), |
| 85 | wide_string_length, |
| 86 | &string_buffer[0], |
| 87 | output_string_byte_count, |
| 88 | NULL, |
| 89 | NULL)); |
| 90 | if (convert_failed) { |
| 91 | if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 92 | // Buffer wasn't big enough. Call WideCharToMultiByte again with |
| 93 | // NULL values to determine how big the buffer should be. |
| 94 | output_string_byte_count = ::WideCharToMultiByte(CP_UTF8, |
| 95 | 0, |
| 96 | input.c_str(), |
| 97 | wide_string_length, |
| 98 | NULL, |
| 99 | 0, |
| 100 | NULL, |
| 101 | NULL); |
| 102 | string_buffer.resize(output_string_byte_count); |
| 103 | convert_failed = (0 == ::WideCharToMultiByte(CP_UTF8, |
| 104 | 0, |
| 105 | input.c_str(), |
| 106 | wide_string_length, |
| 107 | &string_buffer[0], |
| 108 | output_string_byte_count, |
| 109 | NULL, |
| 110 | NULL)); |
| 111 | if (!convert_failed) { |
| 112 | output = &string_buffer[0]; |
| 113 | } |
| 114 | } |
| 115 | } else { |
| 116 | output = &string_buffer[0]; |
| 117 | } |
| 118 | return output; |
| 119 | } |
| 120 | |
| 121 | std::string StringUtilities::Format(const char* format, ...) { |
| 122 | va_list args; |