From 9229956f04731a38ee382807faeb08a7a0b2dff4 Mon Sep 17 00:00:00 2001 From: frosch Date: Fri, 4 Apr 2025 21:42:45 +0200 Subject: [PATCH] Codechange: Replace strcasestr with StrContainsIgnoreCase. --- src/console_cmds.cpp | 2 +- src/os/windows/font_win32.cpp | 13 ++++++------- src/string.cpp | 16 ---------------- src/string_func.h | 8 -------- 4 files changed, 7 insertions(+), 32 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index ef863cc8c5..c78015253c 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2205,7 +2205,7 @@ DEF_CONSOLE_CMD(ConContent) if (StrEqualsIgnoreCase(argv[1], "state")) { IConsolePrint(CC_WHITE, "id, type, state, name"); for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) { - if (argc > 2 && strcasestr((*iter)->name.c_str(), argv[2]) == nullptr) continue; + if (argc > 2 && !StrContainsIgnoreCase((*iter)->name, argv[2])) continue; OutputContentState(*iter); } return true; diff --git a/src/os/windows/font_win32.cpp b/src/os/windows/font_win32.cpp index db1573d20a..5e2bbce4a4 100644 --- a/src/os/windows/font_win32.cpp +++ b/src/os/windows/font_win32.cpp @@ -337,7 +337,7 @@ static bool TryLoadFontFromFile(const std::string &font_name, LOGFONT &logfont) _wsplitpath(fontPath, nullptr, nullptr, fname, nullptr); wcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE); - logfont.lfWeight = strcasestr(font_name.c_str(), " bold") != nullptr || strcasestr(font_name.c_str(), "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. + logfont.lfWeight = StrContainsIgnoreCase(font_name, " bold") || StrContainsIgnoreCase(font_name, "-bold") ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. } } } @@ -345,7 +345,7 @@ static bool TryLoadFontFromFile(const std::string &font_name, LOGFONT &logfont) return logfont.lfFaceName[0] != 0; } -static void LoadWin32Font(FontSize fs, const LOGFONT &logfont, uint size, const char *font_name) +static void LoadWin32Font(FontSize fs, const LOGFONT &logfont, uint size, std::string_view font_name) { HFONT font = CreateFontIndirect(&logfont); if (font == nullptr) { @@ -369,7 +369,6 @@ void LoadWin32Font(FontSize fs) std::string font = GetFontCacheFontName(fs); if (font.empty()) return; - const char *font_name = font.c_str(); LOGFONT logfont; MemSetT(&logfont, 0); logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH; @@ -379,7 +378,7 @@ void LoadWin32Font(FontSize fs) if (settings->os_handle != nullptr) { logfont = *(const LOGFONT *)settings->os_handle; - } else if (strchr(font_name, '.') != nullptr) { + } else if (font.find('.') != std::string::npos) { /* Might be a font file name, try load it. */ if (!TryLoadFontFromFile(font, logfont)) { ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font, FontSizeToName(fs)); @@ -387,9 +386,9 @@ void LoadWin32Font(FontSize fs) } if (logfont.lfFaceName[0] == 0) { - logfont.lfWeight = strcasestr(font_name, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. - convert_to_fs(font_name, logfont.lfFaceName); + logfont.lfWeight = StrContainsIgnoreCase(font, " bold") ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. + convert_to_fs(font, logfont.lfFaceName); } - LoadWin32Font(fs, logfont, GetFontCacheFontSize(fs), font_name); + LoadWin32Font(fs, logfont, GetFontCacheFontSize(fs), font); } diff --git a/src/string.cpp b/src/string.cpp index 4f7b920f42..2fadbb2e30 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -470,22 +470,6 @@ size_t Utf8Decode(char32_t *c, const char *s) return 1; } -#ifdef DEFINE_STRCASESTR -char *strcasestr(const char *haystack, const char *needle) -{ - size_t hay_len = strlen(haystack); - size_t needle_len = strlen(needle); - while (hay_len >= needle_len) { - if (strncasecmp(haystack, needle, needle_len) == 0) return const_cast(haystack); - - haystack++; - hay_len--; - } - - return nullptr; -} -#endif /* DEFINE_STRCASESTR */ - /** * Test if a unicode character is considered garbage to be skipped. * @param c Character to test. diff --git a/src/string_func.h b/src/string_func.h index b44e6de3eb..f692ff7b1a 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -243,12 +243,4 @@ inline bool IsWhitespace(char32_t c) #include #endif -/* strcasestr is available for _GNU_SOURCE, BSD and some Apple */ -#if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE) -# undef DEFINE_STRCASESTR -#else -# define DEFINE_STRCASESTR -char *strcasestr(const char *haystack, const char *needle); -#endif /* strcasestr is available */ - #endif /* STRING_FUNC_H */