Skip to content

Commit

Permalink
Remove strcasestr
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Apr 25, 2024
1 parent 2d73b7f commit 867b2ca
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 58 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Expand Up @@ -867,7 +867,6 @@ endif()
check_type_size(errno_t ERRNOT)
check_function_exists("strcpy_s" HAVE_STRCPYS)
check_function_exists("strncpy_s" HAVE_STRNCPYS)
check_function_exists("strcasestr" HAVE_STRCASESTR)
check_function_exists("strnlen" HAVE_STRNLEN)
check_function_exists("getopt" HAVE_GETOPT)

Expand All @@ -891,10 +890,6 @@ if(HAVE_STRCATS)
add_definitions(-DHAVE_STRCATS)
endif()

if(HAVE_STRCASESTR)
add_definitions(-DHAVE_STRCASESTR)
endif()

if(HAVE_STRNLEN)
add_definitions(-DHAVE_STRNLEN)
endif()
Expand Down
18 changes: 13 additions & 5 deletions src/game/game.cpp
Expand Up @@ -771,6 +771,17 @@ static void GameTypeMachineVsMachineTraining()
-- Game creation
----------------------------------------------------------------------------*/

static bool has_any_extension(fs::path filename, std::initializer_list<fs::path> extensions)
{
while (filename.has_extension()) {
if (ranges::contains(extensions, filename.extension())) {
return true;
}
filename.replace_extension();
}
return false;
}

/**
** CreateGame.
**
Expand Down Expand Up @@ -800,12 +811,9 @@ void CreateGame(const fs::path &filename, CMap *map)
InitSyncRand();
}

if (Map.Info.Filename.empty() && !filename.empty()) {
if (Map.Info.Filename.empty() && has_any_extension(filename, {".smp", ".SMP"})) {
const std::string path = LibraryFileName(filename.string());

if (strcasestr(filename.string().c_str(), ".smp")) {
LuaLoadFile(path);
}
LuaLoadFile(path);
}

for (int i = 0; i < PlayerMax; ++i) {
Expand Down
5 changes: 0 additions & 5 deletions src/include/util.h
Expand Up @@ -104,11 +104,6 @@ extern errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t coun
extern errno_t strcat_s(char *dst, size_t dstsize, const char *src);
#endif

#ifndef HAVE_STRCASESTR
/// case insensitive strstr
extern const char *strcasestr(const char *str, const char *substr) noexcept;
#endif // !HAVE_STRCASESTR

#if defined(WIN32) && defined(UNICODE)
#define L(LITERAL) L"" LITERAL
#else
Expand Down
33 changes: 0 additions & 33 deletions src/stratagus/util.cpp
Expand Up @@ -296,39 +296,6 @@ errno_t strcat_s(char *dst, size_t dstsize, const char *src)
}
#endif

#if !defined(HAVE_STRCASESTR)
/**
** Case insensitive version of strstr
**
** @param a String to search in
** @param b Substring to search for
**
** @return Pointer to first occurrence of b or nullptr if not found.
*/
const char *strcasestr(const char *a, const char *b) noexcept
{
int x;

if (!a || !*a || !b || !*b || strlen(a) < strlen(b)) {
return nullptr;
}

x = 0;
while (*a) {
if (a[x] && (tolower(a[x]) == tolower(b[x]))) {
++x;
} else if (b[x]) {
++a;
x = 0;
} else {
return (char *)a;
}
}

return nullptr;
}
#endif // !HAVE_STRCASESTR

int to_number(std::string_view s, int base)
{
#if __has_include(<charconv>)
Expand Down
10 changes: 0 additions & 10 deletions tests/stratagus/test_util.cpp
Expand Up @@ -99,16 +99,6 @@ TEST_CASE("strcat_s")
CHECK(std::string_view{"hello world"} == buffer);
}

TEST_CASE("strcasestr")
{
const char *text = "HELLO world";

CHECK(nullptr == strcasestr(text, "not found"));
CHECK(text == strcasestr(text, "HelLo"));
CHECK(text + 6 == strcasestr(text, "WoRlD"));
CHECK(text + 4 == strcasestr(text, "o"));
}

TEST_CASE("strnlen")
{
CHECK(2u == strnlen("hello", 2));
Expand Down

0 comments on commit 867b2ca

Please sign in to comment.