From a247e186da7e88e18b7991d4ce77221fa63f1b25 Mon Sep 17 00:00:00 2001 From: Binbin Zhang Date: Fri, 21 Jul 2023 10:52:53 +0800 Subject: [PATCH] [runtime/fix] fix locale wstring conversion error (#1921) --- runtime/core/utils/string.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/runtime/core/utils/string.cc b/runtime/core/utils/string.cc index 1ab93adf3..4765af3f7 100644 --- a/runtime/core/utils/string.cc +++ b/runtime/core/utils/string.cc @@ -148,13 +148,17 @@ std::string ProcessBlank(const std::string& str, bool lowercase) { } // NOTE: convert string to wstring // see issue 745: https://github.com/wenet-e2e/wenet/issues/745 - std::locale loc(""); - std::wstring_convert, wchar_t> converter; - std::wstring wsresult = converter.from_bytes(result); - for (auto& c : wsresult) { - c = lowercase ? tolower(c, loc) : toupper(c, loc); + try { + std::locale loc(""); + std::wstring_convert, wchar_t> converter; + std::wstring wsresult = converter.from_bytes(result); + for (auto& c : wsresult) { + c = lowercase ? tolower(c, loc) : toupper(c, loc); + } + result = converter.to_bytes(wsresult); + } catch (std::exception& e) { + LOG(ERROR) << "convert wstring error " << e.what(); } - result = converter.to_bytes(wsresult); } return result; }