Skip to content

Commit

Permalink
refactor: use ends_with
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Oct 8, 2024
1 parent ae9be95 commit 2d97603
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ tile_type &tileset::create_tile_type( const std::string &id, tile_type &&new_til
};
bool has_season_suffix = false;
for( int i = 0; i < 4; i++ ) {
if( string_ends_with( id, season_suffix[i] ) ) {
if( id.ends_with( season_suffix[i] ) ) {
has_season_suffix = true;
// key is id without _season suffix
season_tile_value &value = tile_ids_by_season[i][id.substr( 0,
Expand Down
2 changes: 1 addition & 1 deletion src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ static std::optional<uintptr_t> debug_compute_load_offset(
while( !line.empty() && isspace( line.end()[-1] ) ) {
line.erase( line.end() - 1 );
}
if( string_ends_with( line, string_sought ) ) {
if( line.ends_with( string_sought ) ) {
std::istringstream line_is( line );
uintptr_t symbol_address;
line_is >> std::hex >> symbol_address;
Expand Down
4 changes: 2 additions & 2 deletions src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ std::vector<std::string> find_file_if_bfs( const std::string &root_path,
std::deque<std::string> directories;
if( root_path.empty() ) {
directories.emplace_back( "./" );
} else if( string_ends_with( root_path, "/" ) ) {
} else if( root_path.ends_with( "/" ) ) {
directories.emplace_back( root_path );
} else {
directories.emplace_back( root_path + "/" );
Expand Down Expand Up @@ -496,7 +496,7 @@ const char *CBN = "Q2F0YWNseXNtQnJpZ2h0TmlnaHRz";
bool can_write_to_dir( const std::string &dir_path )
{
std::string dummy_file;
if( string_ends_with( dir_path, "/" ) ) {
if( dir_path.ends_with( "/" ) ) {
dummy_file = dir_path + CBN;
} else {
dummy_file = dir_path + "/" + CBN;
Expand Down
2 changes: 1 addition & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4937,7 +4937,7 @@ std::string item::tname( unsigned int quantity, bool with_prefix, unsigned int t
} else if( has_flag( flag_IS_UPS ) && get_var( "cable" ) == "plugged_in" ) {
tagtext += _( " (plugged in)" );
} else if( is_active() && !is_food() && !is_corpse() &&
!string_ends_with( typeId().str(), "_on" ) ) {
! typeId().str().ends_with( "_on" ) ) {
// Usually the items whose ids end in "_on" have the "active" or "on" string already contained
// in their name, also food is active while it rots.
tagtext += _( " (active)" );
Expand Down
2 changes: 1 addition & 1 deletion src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2049,7 +2049,7 @@ int iuse::pack_item( player *p, item *it, bool t, const tripoint & )
return 0;
} else { // Turning it off
std::string oname = it->typeId().str();
if( string_ends_with( oname, "_on" ) ) {
if( oname.ends_with( "_on" ) ) {
oname.erase( oname.length() - 3, 3 );
} else {
debugmsg( "no item type to turn it into (%s)!", oname );
Expand Down
2 changes: 1 addition & 1 deletion src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6306,7 +6306,7 @@ om_direction::type oter_get_rotation_dir( const oter_id &oter )
{
for( const om_direction::type &rot : om_direction::all ) {
const std::string &rot_s = om_direction::get_suffix( rot );
if( string_ends_with( oter.id().str(), rot_s ) ) {
if( oter.id().str().ends_with( rot_s ) ) {
return rot;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/sdl_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::unique_ptr<Font> Font::load_font( SDL_Renderer_Ptr &renderer, SDL_PixelForm
const palette_array &palette,
const bool fontblending )
{
if( string_ends_with( typeface, ".bmp" ) || string_ends_with( typeface, ".png" ) ) {
if( typeface.ends_with( ".bmp" ) || typeface.ends_with( ".png" ) ) {
// Seems to be an image file, not a font.
// Try to load as bitmap font from user font dir, then from font dir.
try {
Expand Down Expand Up @@ -185,7 +185,7 @@ CachedTTFFont::CachedTTFFont(
std::vector<std::string> known_suffixes = { ".ttf", ".otf", ".ttc", ".fon" };
bool add_suffix = true;
for( const std::string &ks : known_suffixes ) {
if( string_ends_with( typeface, ks ) ) {
if( typeface.ends_with( ks ) ) {
add_suffix = false;
break;
}
Expand Down
6 changes: 0 additions & 6 deletions src/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ bool match_include_exclude( const std::string &text, std::string filter )
return found;
}

bool string_ends_with( const std::string &s1, const std::string &s2 )
{
return s1.size() >= s2.size() &&
s1.compare( s1.size() - s2.size(), s2.size(), s2 ) == 0;
}

std::string join( const std::vector<std::string> &strings, const std::string &joiner )
{
std::ostringstream buffer;
Expand Down
20 changes: 0 additions & 20 deletions src/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ bool lcequal( const std::string &str1, const std::string &str2 );
*/
bool match_include_exclude( const std::string &text, std::string filter );

/**
* \brief Returns true if s1 ends with s2
*
* TODO: Switch to ends_with method of std::string when we move to C++20
*/
bool string_ends_with( const std::string &s1, const std::string &s2 );

/**
* Returns true iff s1 ends with s2.
* This version accepts constant string literals and is ≈1.5 times faster than std::string version.
* Note: N is (size+1) for null-terminated strings.
*
* TODO: Maybe switch to std::string::ends_with + std::string_view when we move to C++20
*/
template <std::size_t N>
inline bool string_ends_with( const std::string &s1, const char( &s2 )[N] )
{
return s1.size() >= N - 1 && s1.compare( s1.size() - ( N - 1 ), std::string::npos, s2, N - 1 ) == 0;
}

/**
* Joins a vector of `std::string`s into a single string with a delimiter/joiner
*/
Expand Down
46 changes: 0 additions & 46 deletions tests/cata_utility_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,6 @@
#include "units_utility.h"
#include "units.h"

// tests both variants of string_ends_with
template <std::size_t N>
bool test_string_ends_with( const std::string &s1, const char( &s2 )[N] )
{
CAPTURE( s1, s2, N );
bool r1 = string_ends_with( s1, s2 );
bool r2 = string_ends_with( s1, std::string( s2 ) );
CHECK( r1 == r2 );
return r1;
}

TEST_CASE( "string_ends_with", "[utility]" )
{
CHECK( test_string_ends_with( "", "" ) );
CHECK( test_string_ends_with( "a", "" ) );
CHECK_FALSE( test_string_ends_with( "", "a" ) );
CHECK( test_string_ends_with( "ba", "a" ) );
CHECK_FALSE( test_string_ends_with( "ba", "b" ) );
CHECK_FALSE( test_string_ends_with( "a", "ba" ) );
}

TEST_CASE( "string_ends_with_benchmark", "[.][utility][benchmark]" )
{
const std::string s1 = "long_string_with_suffix";

BENCHMARK( "old version" ) {
return string_ends_with( s1, std::string( "_suffix" ) );
};
BENCHMARK( "new version" ) {
return string_ends_with( s1, "_suffix" );
};
}

TEST_CASE( "string_ends_with_season_suffix", "[utility]" )
{
constexpr size_t suffix_len = 15;
constexpr char season_suffix[4][suffix_len] = {
"_season_spring", "_season_summer", "_season_autumn", "_season_winter"
};

CHECK( test_string_ends_with( "t_tile_season_spring", season_suffix[0] ) );
CHECK_FALSE( test_string_ends_with( "t_tile", season_suffix[0] ) );
CHECK_FALSE( test_string_ends_with( "t_tile_season_summer", season_suffix[0] ) );
CHECK_FALSE( test_string_ends_with( "t_tile_season_spring1", season_suffix[0] ) );
}

TEST_CASE( "divide_round_up", "[utility]" )
{
CHECK( divide_round_up( 0, 5 ) == 0 );
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static std::string extract_user_dir( std::vector<const char *> &arg_vec )
if( option_user_dir.empty() ) {
return "./test_user_dir/";
}
if( !string_ends_with( option_user_dir, "/" ) ) {
if( !option_user_dir.ends_with( "/" ) ) {
option_user_dir += "/";
}
return option_user_dir;
Expand Down

0 comments on commit 2d97603

Please sign in to comment.