mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
utf8_to_wstrbuf now handles invalid utf8 sequences (#3244)
This commit is contained in:
parent
63f0875000
commit
5673e9e545
@ -483,12 +483,12 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
|||||||
|
|
||||||
// find the size to allocate for the result buffer
|
// find the size to allocate for the result buffer
|
||||||
int result_size =
|
int result_size =
|
||||||
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
|
||||||
|
|
||||||
if (result_size > 0) {
|
if (result_size > 0) {
|
||||||
target.resize(result_size);
|
target.resize(result_size);
|
||||||
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
|
result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(),
|
||||||
target.data(), result_size);
|
result_size);
|
||||||
if (result_size > 0) {
|
if (result_size > 0) {
|
||||||
assert(result_size == target.size());
|
assert(result_size == target.size());
|
||||||
return;
|
return;
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
#include "spdlog/details/fmt_helper.h"
|
#include "spdlog/details/fmt_helper.h"
|
||||||
|
#include "spdlog/details/os.h"
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_TLS
|
#ifndef SPDLOG_NO_TLS
|
||||||
#include "spdlog/mdc.h"
|
#include "spdlog/mdc.h"
|
||||||
|
@ -167,3 +167,21 @@ TEST_CASE("default logger API", "[default logger]") {
|
|||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
spdlog::set_pattern("%v");
|
spdlog::set_pattern("%v");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
|
TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") {
|
||||||
|
spdlog::wmemory_buf_t buffer;
|
||||||
|
|
||||||
|
spdlog::details::os::utf8_to_wstrbuf("", buffer);
|
||||||
|
REQUIRE(buffer.data() == std::wstring(L""));
|
||||||
|
|
||||||
|
spdlog::details::os::utf8_to_wstrbuf("abc", buffer);
|
||||||
|
REQUIRE(buffer.data() == std::wstring(L"abc"));
|
||||||
|
|
||||||
|
spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence.
|
||||||
|
REQUIRE(buffer.data() == std::wstring(L"\xfffd("));
|
||||||
|
|
||||||
|
spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana.
|
||||||
|
REQUIRE(buffer.data() == std::wstring(L"\x306d\x3053"));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user