From 20864d3c53acc513a5a803ed7a5a1a2d2f93b5b5 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 15 Jun 2019 14:26:13 +0800 Subject: [PATCH] Return a copy of stringw in translation to remove the mutex lock --- src/utils/translation.cpp | 37 +++++++++++++------------------------ src/utils/translation.hpp | 13 ++++--------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/utils/translation.cpp b/src/utils/translation.cpp index dc859c636..fa38f8cbe 100644 --- a/src/utils/translation.cpp +++ b/src/utils/translation.cpp @@ -552,7 +552,7 @@ bool Translations::isRTLText(const wchar_t *in_ptr) * \param context Optional, can be set to differentiate 2 strings that are identical * in English but could be different in other languages */ -const wchar_t* Translations::w_gettext(const wchar_t* original, const char* context) +irr::core::stringw Translations::w_gettext(const wchar_t* original, const char* context) { std::string in = StringUtils::wideToUtf8(original); return w_gettext(in.c_str(), context); @@ -563,13 +563,11 @@ const wchar_t* Translations::w_gettext(const wchar_t* original, const char* cont * \param context Optional, can be set to differentiate 2 strings that are identical * in English but could be different in other languages */ -const wchar_t* Translations::w_gettext(const char* original, const char* context) +irr::core::stringw Translations::w_gettext(const char* original, const char* context) { #ifdef SERVER_ONLY - static irr::core::stringw dummy_for_server; - dummy_for_server = StringUtils::utf8ToWide(original); - return dummy_for_server.c_str(); + return L""; #else if (original[0] == '\0') return L""; @@ -583,20 +581,17 @@ const wchar_t* Translations::w_gettext(const char* original, const char* context m_dictionary.translate_ctxt(context, original)); // print //for (int n=0;; n+=4) - std::lock_guard lock(m_gettext_mutex); - - static std::map original_tw; - original_tw[std::this_thread::get_id()] = StringUtils::utf8ToWide(original_t); - - const wchar_t* out_ptr = original_tw.at(std::this_thread::get_id()).c_str(); + const irr::core::stringw wide = StringUtils::utf8ToWide(original_t); + const wchar_t* out_ptr = wide.c_str(); if (REMOVE_BOM) out_ptr++; #if TRANSLATE_VERBOSE std::wcout << L" translation : " << out_ptr << std::endl; #endif - return out_ptr; + return wide; #endif + } /** @@ -606,7 +601,7 @@ const wchar_t* Translations::w_gettext(const char* original, const char* context * \param context Optional, can be set to differentiate 2 strings that are identical * in English but could be different in other languages */ -const wchar_t* Translations::w_ngettext(const wchar_t* singular, const wchar_t* plural, int num, const char* context) +irr::core::stringw Translations::w_ngettext(const wchar_t* singular, const wchar_t* plural, int num, const char* context) { std::string in = StringUtils::wideToUtf8(singular); std::string in2 = StringUtils::wideToUtf8(plural); @@ -620,12 +615,10 @@ const wchar_t* Translations::w_ngettext(const wchar_t* singular, const wchar_t* * \param context Optional, can be set to differentiate 2 strings that are identical * in English but could be different in other languages */ -const wchar_t* Translations::w_ngettext(const char* singular, const char* plural, int num, const char* context) +irr::core::stringw Translations::w_ngettext(const char* singular, const char* plural, int num, const char* context) { #ifdef SERVER_ONLY - static core::stringw str_buffer; - str_buffer = StringUtils::utf8ToWide(singular); - return str_buffer.c_str(); + return L""; #else @@ -633,19 +626,15 @@ const wchar_t* Translations::w_ngettext(const char* singular, const char* plural m_dictionary.translate_plural(singular, plural, num) : m_dictionary.translate_ctxt_plural(context, singular, plural, num)); - std::lock_guard lock(m_ngettext_mutex); - - static std::map str_buffer; - str_buffer[std::this_thread::get_id()] = StringUtils::utf8ToWide(res); - - const wchar_t* out_ptr = str_buffer.at(std::this_thread::get_id()).c_str(); + const irr::core::stringw wide = StringUtils::utf8ToWide(res); + const wchar_t* out_ptr = wide.c_str(); if (REMOVE_BOM) out_ptr++; #if TRANSLATE_VERBOSE std::wcout << L" translation : " << out_ptr << std::endl; #endif - return out_ptr; + return wide; #endif } diff --git a/src/utils/translation.hpp b/src/utils/translation.hpp index 7eb73920d..a15bcebe0 100644 --- a/src/utils/translation.hpp +++ b/src/utils/translation.hpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -58,18 +57,17 @@ private: std::string m_current_language_name; std::string m_current_language_name_code; std::string m_current_language_tag; - std::mutex m_gettext_mutex, m_ngettext_mutex; #endif public: Translations(); ~Translations(); - const wchar_t *w_gettext(const wchar_t* original, const char* context=NULL); - const wchar_t *w_gettext(const char* original, const char* context=NULL); + irr::core::stringw w_gettext(const wchar_t* original, const char* context=NULL); + irr::core::stringw w_gettext(const char* original, const char* context=NULL); - const wchar_t *w_ngettext(const wchar_t* singular, const wchar_t* plural, int num, const char* context=NULL); - const wchar_t *w_ngettext(const char* singular, const char* plural, int num, const char* context=NULL); + irr::core::stringw w_ngettext(const wchar_t* singular, const wchar_t* plural, int num, const char* context=NULL); + irr::core::stringw w_ngettext(const char* singular, const char* plural, int num, const char* context=NULL); bool isRTLLanguage() const { #ifdef SERVER_ONLY @@ -79,9 +77,6 @@ public: #endif } - const wchar_t* fribidize(const wchar_t* in_ptr); - const wchar_t* fribidize(const irr::core::stringw &str) { return fribidize(str.c_str()); } - bool isRTLText(const wchar_t* in_ptr); bool isRTLText(const irr::core::stringw &str) { return isRTLText(str.c_str()); }