aboutsummaryrefslogtreecommitdiff
path: root/src/util/string_util.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-03-19 16:21:54 +0800
committercrupest <crupest@outlook.com>2019-03-19 16:21:54 +0800
commit5dc738a57930271194bd86673eb86f149096a7b2 (patch)
tree71174aba0d1c0918cc7d7a1be0b86ec0d5c20401 /src/util/string_util.cpp
parent06edefebe8dfb138404397fb2c46732da6cd733a (diff)
downloadcru-5dc738a57930271194bd86673eb86f149096a7b2.tar.gz
cru-5dc738a57930271194bd86673eb86f149096a7b2.tar.bz2
cru-5dc738a57930271194bd86673eb86f149096a7b2.zip
...
Diffstat (limited to 'src/util/string_util.cpp')
-rw-r--r--src/util/string_util.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/util/string_util.cpp b/src/util/string_util.cpp
new file mode 100644
index 00000000..3c765259
--- /dev/null
+++ b/src/util/string_util.cpp
@@ -0,0 +1,21 @@
+#include "string_util.hpp"
+
+#include "system_headers.hpp"
+#include "exception.hpp"
+
+namespace cru::util {
+MultiByteString ToUtf8String(const StringView& string) {
+ if (string.empty()) return MultiByteString();
+
+ const auto length = ::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1,
+ nullptr, 0, nullptr, nullptr);
+ MultiByteString result;
+ result.reserve(length);
+ if (::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, result.data(),
+ static_cast<int>(result.capacity()), nullptr,
+ nullptr) == 0)
+ throw Win32Error(::GetLastError(),
+ "Failed to convert wide string to UTF-8.");
+ return result;
+}
+}