From 193988c9f0366f79303ff8c59ab949145f8d0337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 4 Apr 2024 17:17:26 +0200 Subject: [PATCH] Fix storing UTF-8 configuration values on Windows std::string values were accidentally converted to wxString using current charset instead of UTF-8, eventually resulting in double-encoded UTF-8. Fix by explicitly treating narrow strings as UTF-8 in both directions. The actual fix is using str::to_wx(value) instead of wxString(value) in Config::Write(key, std::string); the rest of the changes is just for consistency. --- src/configuration.cpp | 8 ++++---- src/str_helpers.h | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/configuration.cpp b/src/configuration.cpp index bc83b32a49..0560d5e8fa 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -220,14 +220,14 @@ bool Config::Read(const std::string& key, std::string *out) wxString s; if (!wxConfig::Get()->Read(key, &s)) return false; - *out = s.utf8_string(); + *out = str::to_utf8(s); return true; } void Config::Write(const std::string& key, const std::string& value) { CfgLock lock; - wxConfig::Get()->Write(key, wxString(value)); + wxConfig::Get()->Write(key, str::to_wx(value)); } bool Config::Read(const std::string& key, std::wstring *out) @@ -237,14 +237,14 @@ bool Config::Read(const std::string& key, std::wstring *out) wxString s; if (!wxConfig::Get()->Read(key, &s)) return false; - *out = s.ToStdWstring(); + *out = str::to_wstring(s); return true; } void Config::Write(const std::string& key, const std::wstring& value) { CfgLock lock; - wxConfig::Get()->Write(key, wxString(value)); + wxConfig::Get()->Write(key, str::to_wx(value)); } bool Config::Read(const std::string& key, bool *out) diff --git a/src/str_helpers.h b/src/str_helpers.h index 54a692ca15..a7d8b51ac9 100644 --- a/src/str_helpers.h +++ b/src/str_helpers.h @@ -115,6 +115,11 @@ inline wxString to_wx(const std::string& utf8) return wxString::FromUTF8(utf8.c_str()); } +inline wxString to_wx(const std::wstring& str) +{ + return wxString(str); +} + #if defined(__cplusplus) && defined(__OBJC__) inline NSString *to_NS(const wxString& str)