From 0a9d351fe24a195ee5f8e4dff956eb80d3bf12be Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Wed, 31 Jul 2024 23:13:17 -0400 Subject: [PATCH] Replace `filesystem::ends_with` with `boost::algorithm::ends_with` The former had nothing to do with fs functionality at all. --- src/filesystem.cpp | 5 +++-- src/filesystem.hpp | 2 -- src/filesystem_common.cpp | 9 --------- src/picture.cpp | 6 +++--- src/server/campaignd/addon_utils.cpp | 2 +- src/tests/test_filesystem.cpp | 2 -- 6 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 929b65ff426..b7678be9035 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -29,6 +29,7 @@ #include "serialization/unicode.hpp" #include "utils/general.hpp" +#include #include #include #include @@ -520,7 +521,7 @@ void get_files_in_dir(const std::string& dir, if(files != nullptr && reorder == reorder_mode::DO_REORDER) { // move finalcfg_filename, if present, to the end of the vector for(unsigned int i = 0; i < files->size(); i++) { - if(ends_with((*files)[i], "/" + finalcfg_filename)) { + if(boost::algorithm::ends_with((*files)[i], "/" + finalcfg_filename)) { files->push_back((*files)[i]); files->erase(files->begin() + i); break; @@ -530,7 +531,7 @@ void get_files_in_dir(const std::string& dir, // move initialcfg_filename, if present, to the beginning of the vector int foundit = -1; for(unsigned int i = 0; i < files->size(); i++) - if(ends_with((*files)[i], "/" + initialcfg_filename)) { + if(boost::algorithm::ends_with((*files)[i], "/" + initialcfg_filename)) { foundit = i; break; } diff --git a/src/filesystem.hpp b/src/filesystem.hpp index 4d2ed41aeda..6d6cda017c2 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -327,8 +327,6 @@ int file_size(const std::string& fname); /** Returns the sum of the sizes of the files contained in a directory. */ int dir_size(const std::string& path); -bool ends_with(const std::string& str, const std::string& suffix); - /** * Returns the base filename of a file, with directory name stripped. * Equivalent to a portable basename() function. diff --git a/src/filesystem_common.cpp b/src/filesystem_common.cpp index 45620e5a150..8eff92cc4d6 100644 --- a/src/filesystem_common.cpp +++ b/src/filesystem_common.cpp @@ -291,15 +291,6 @@ bool file_tree_checksum::operator==(const file_tree_checksum &rhs) const modified == rhs.modified; } -bool ends_with(const std::string& str, const std::string& suffix) -{ -#ifdef __cpp_lib_starts_ends_with - return str.ends_with(suffix); -#else - return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size()); -#endif -} - std::string read_map(const std::string& name) { std::string res; diff --git a/src/picture.cpp b/src/picture.cpp index fb8f89bdedc..48aed1fff48 100644 --- a/src/picture.cpp +++ b/src/picture.cpp @@ -354,7 +354,7 @@ static surface load_image_file(const image::locator& loc) // but the old filename may still be saved in savegame files etc. // If the file does not exist in ".png" format, also try ".webp". // Similarly for ".jpg", which conveniently has the same number of letters as ".png". - if(!location && (filesystem::ends_with(name, ".png") || filesystem::ends_with(name, ".jpg"))) { + if(!location && (boost::algorithm::ends_with(name, ".png") || boost::algorithm::ends_with(name, ".jpg"))) { std::string webp_name = name.substr(0, name.size() - 4) + ".webp"; location = filesystem::get_binary_file_location("images", webp_name); if(location) { @@ -932,14 +932,14 @@ save_result save_image(const surface& surf, const std::string& filename) return save_result::no_image; } - if(filesystem::ends_with(filename, ".jpeg") || filesystem::ends_with(filename, ".jpg") || filesystem::ends_with(filename, ".jpe")) { + if(boost::algorithm::ends_with(filename, ".jpeg") || boost::algorithm::ends_with(filename, ".jpg") || boost::algorithm::ends_with(filename, ".jpe")) { LOG_IMG << "Writing a JPG image to " << filename; const int err = IMG_SaveJPG_RW(surf, filesystem::make_write_RWops(filename).release(), true, 75); // SDL takes ownership of the RWops return err == 0 ? save_result::success : save_result::save_failed; } - if(filesystem::ends_with(filename, ".png")) { + if(boost::algorithm::ends_with(filename, ".png")) { LOG_IMG << "Writing a PNG image to " << filename; const int err = IMG_SavePNG_RW(surf, filesystem::make_write_RWops(filename).release(), true); // SDL takes ownership of the RWops diff --git a/src/server/campaignd/addon_utils.cpp b/src/server/campaignd/addon_utils.cpp index b65b0b34c6d..831f87d7d13 100644 --- a/src/server/campaignd/addon_utils.cpp +++ b/src/server/campaignd/addon_utils.cpp @@ -107,7 +107,7 @@ void find_translations(const config& base_dir, config& addon) { for(const config& file : base_dir.child_range("file")) { const std::string& fn = file["name"].str(); - if(filesystem::ends_with(fn, ".po")) { + if(boost::algorithm::ends_with(fn, ".po")) { support_translation(addon, filesystem::base_name(fn, true)); } } diff --git a/src/tests/test_filesystem.cpp b/src/tests/test_filesystem.cpp index 4b347a54cae..dc627c2035a 100644 --- a/src/tests/test_filesystem.cpp +++ b/src/tests/test_filesystem.cpp @@ -215,8 +215,6 @@ BOOST_AUTO_TEST_CASE( test_fs_search ) BOOST_AUTO_TEST_CASE( test_fs_fluff ) { - BOOST_CHECK( ends_with("foobarbazbat", "bazbat") ); - BOOST_CHECK( looks_like_pbl("foo.pbl") ); BOOST_CHECK( looks_like_pbl("FOO.PBL") ); BOOST_CHECK( looks_like_pbl("Foo.Pbl") );