From 09c4b931406b1849824acc7c170d105de78ffd37 Mon Sep 17 00:00:00 2001 From: Blake-Madden <66873089+Blake-Madden@users.noreply.github.com> Date: Sun, 18 Aug 2024 09:13:24 -0400 Subject: [PATCH] Fix GCC error --- src/analyze.cpp | 6 +++--- src/i18n_string_util.h | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index b8d5ee4..ba24d66 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -16,7 +16,7 @@ namespace i18n_check bool valid_utf8_file(const std::wstring& file_name, bool& startsWithBom) { startsWithBom = false; - std::ifstream ifs(file_name); + std::ifstream ifs(i18n_string_util::lazy_wstring_to_string(file_name)); if (!ifs) { return false; @@ -37,7 +37,7 @@ namespace i18n_check //------------------------------------------------------ std::pair read_utf16_file(const std::wstring& file_name) { - std::ifstream fs8(file_name); + std::ifstream fs8(i18n_string_util::lazy_wstring_to_string(file_name)); if (!fs8.is_open()) { std::wcout << L"Could not open " << file_name << L"\n"; @@ -72,7 +72,7 @@ namespace i18n_check return std::make_pair(false, std::wstring{}); } - std::ifstream fs8(file_name); + std::ifstream fs8(i18n_string_util::lazy_wstring_to_string(file_name)); if (!fs8.is_open()) { std::wcout << L"Could not open " << file_name << L"\n"; diff --git a/src/i18n_string_util.h b/src/i18n_string_util.h index cce145e..5e1993e 100644 --- a/src/i18n_string_util.h +++ b/src/i18n_string_util.h @@ -139,6 +139,24 @@ namespace i18n_string_util return retVal; } + /** @brief Converts a wstring to string (assuming that the string is simple 8-bit ASCII). + @param str The string to convert. + @returns The string, converted to a wstring. + @warning This assumes 8-bit ASCII strings and does not perform any sort + of charset conversion. This should only be used for very simple strings, + such as `what()` from an untranslated `std::exception`.*/ + [[nodiscard]] + inline std::string lazy_wstring_to_string(const std::wstring& str) + { + std::string retVal; + retVal.reserve(str.length()); + for (const auto& ch : str) + { + retVal += static_cast(ch); + } + return retVal; + } + /// @brief Converts escaped control characters (e.g., "\n") /// inside of a string into spaces. /// @param[out] str The string being escaped.