1
0
Fork 0

Codechange: The compiler ensures static variables are initialised only once, no need to track that manually.

pull/14226/head
frosch 2025-05-04 19:05:43 +02:00 committed by frosch
parent 75a775e59d
commit b20b6da937
3 changed files with 32 additions and 41 deletions

View File

@ -2225,11 +2225,11 @@ static void OutputContentState(const ContentInfo &ci)
static bool ConContent(std::span<std::string_view> argv)
{
static ContentCallback *cb = nullptr;
if (cb == nullptr) {
cb = new ConsoleContentCallback();
_network_content_client.AddCallback(cb);
}
[[maybe_unused]] static ContentCallback *const cb = []() {
auto res = new ConsoleContentCallback();
_network_content_client.AddCallback(res);
return res;
}();
if (argv.size() <= 1) {
IConsolePrint(CC_HELP, "Query, select and download content. Usage: 'content update|upgrade|select [id]|unselect [all|id]|state [filter]|download'.");

View File

@ -144,6 +144,19 @@ static std::string convert_tofrom_fs(iconv_t convd, std::string_view name)
return buf;
}
/**
* Open iconv converter.
*/
static std::optional<iconv_t> OpenIconv(std::string from, std::string to)
{
iconv_t convd = iconv_open(from.c_str(), to.c_str());
if (convd == reinterpret_cast<iconv_t>(-1)) {
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", from, to);
return std::nullopt;
}
return convd;
}
/**
* Convert from OpenTTD's encoding to that of the local environment
* @param name pointer to a valid string that will be converted
@ -151,17 +164,10 @@ static std::string convert_tofrom_fs(iconv_t convd, std::string_view name)
*/
std::string OTTD2FS(std::string_view name)
{
static iconv_t convd = (iconv_t)(-1);
if (convd == (iconv_t)(-1)) {
std::string env{GetLocalCode()};
convd = iconv_open(env.c_str(), INTERNALCODE);
if (convd == (iconv_t)(-1)) {
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
return std::string{name};
}
}
static const auto convd = OpenIconv(GetLocalCode(), INTERNALCODE);
if (!convd.has_value()) return std::string{name};
return convert_tofrom_fs(convd, name);
return convert_tofrom_fs(*convd, name);
}
/**
@ -171,17 +177,10 @@ std::string OTTD2FS(std::string_view name)
*/
std::string FS2OTTD(std::string_view name)
{
static iconv_t convd = (iconv_t)(-1);
if (convd == (iconv_t)(-1)) {
std::string env{GetLocalCode()};
convd = iconv_open(INTERNALCODE, env.c_str());
if (convd == (iconv_t)(-1)) {
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
return std::string{name};
}
}
static const auto convd = OpenIconv(INTERNALCODE, GetLocalCode());
if (!convd.has_value()) return std::string{name};
return convert_tofrom_fs(convd, name);
return convert_tofrom_fs(*convd, name);
}
#endif /* WITH_ICONV */

View File

@ -430,11 +430,16 @@ void Win32SetCurrentLocaleName(std::string iso_code)
MultiByteToWideChar(CP_UTF8, 0, iso_code.data(), static_cast<int>(iso_code.size()), _cur_iso_locale, static_cast<int>(std::size(_cur_iso_locale)));
}
static LibraryLoader::Function GetKernel32Function(const std::string &symbol_name)
{
static LibraryLoader _kernel32("Kernel32.dll");
return _kernel32.GetFunction(symbol_name);
}
int OTTDStringCompare(std::string_view s1, std::string_view s2)
{
typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
static PFNCOMPARESTRINGEX _CompareStringEx = nullptr;
static bool first_time = true;
static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function("CompareStringEx");
#ifndef SORT_DIGITSASNUMBERS
# define SORT_DIGITSASNUMBERS 0x00000008 // use digits as numbers sort method
@ -443,12 +448,6 @@ int OTTDStringCompare(std::string_view s1, std::string_view s2)
# define LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case'
#endif
if (first_time) {
static LibraryLoader _kernel32("Kernel32.dll");
_CompareStringEx = _kernel32.GetFunction("CompareStringEx");
first_time = false;
}
int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), nullptr, 0);
int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), nullptr, 0);
@ -478,14 +477,7 @@ int OTTDStringCompare(std::string_view s1, std::string_view s2)
int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive)
{
typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM);
static PFNFINDNLSSTRINGEX _FindNLSStringEx = nullptr;
static bool first_time = true;
if (first_time) {
static LibraryLoader _kernel32("Kernel32.dll");
_FindNLSStringEx = _kernel32.GetFunction("FindNLSStringEx");
first_time = false;
}
static const PFNFINDNLSSTRINGEX _FindNLSStringEx = GetKernel32Function("FindNLSStringEx");
if (_FindNLSStringEx != nullptr) {
int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);