Skip to content

Commit

Permalink
Localize a bunch of string comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytheway committed Apr 30, 2020
1 parent 39ab7c1 commit 60cf7b5
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/faction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ int npc::faction_display( const catacurses::window &fac_w, const int width ) con
const auto skillslist = Skill::get_skills_sorted_by( [&]( const Skill & a, const Skill & b ) {
const int level_a = get_skill_level( a.ident() );
const int level_b = get_skill_level( b.ident() );
return level_a > level_b || ( level_a == level_b && a.name() < b.name() );
return localized_compare( std::make_pair( -level_a, a.name() ),
std::make_pair( -level_b, b.name() ) );
} );
size_t count = 0;
std::vector<std::string> skill_strs;
Expand Down
7 changes: 4 additions & 3 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2804,11 +2804,12 @@ void basecamp::recruit_return( const std::string &task, int score )
description += string_format( _( "Perception: %10d\n\n" ), recruit->per_max );
description += _( "Top 3 Skills:\n" );

const auto skillslist = Skill::get_skills_sorted_by( [&]( const Skill & a,
const Skill & b ) {
const auto skillslist = Skill::get_skills_sorted_by(
[&]( const Skill & a, const Skill & b ) {
const int level_a = recruit->get_skill_level( a.ident() );
const int level_b = recruit->get_skill_level( b.ident() );
return level_a > level_b || ( level_a == level_b && a.name() < b.name() );
return localized_compare( std::make_pair( -level_a, a.name() ),
std::make_pair( -level_b, b.name() ) );
} );

description += string_format( "%s: %4d\n", right_justify( skillslist[0]->name(), 12 ),
Expand Down
4 changes: 2 additions & 2 deletions src/mod_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ const mod_manager::t_mod_list &mod_manager::get_default_mods() const
inline bool compare_mod_by_name_and_category( const MOD_INFORMATION *const a,
const MOD_INFORMATION *const b )
{
return ( a->category < b->category ) || ( ( a->category == b->category ) &&
( a->name() < b->name() ) );
return localized_compare( std::make_pair( a->category, a->name() ),
std::make_pair( b->category, b->name() ) );
}

void mod_manager::set_usable_mods()
Expand Down
7 changes: 2 additions & 5 deletions src/morale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,8 @@ void player_morale::display( int focus_eq, int pain_penalty, int fatigue_penalty
const auto sort_morale = []( const morale_point & lhs, const morale_point & rhs ) -> bool {
const int lhs_percent = lhs.get_percent_contribution();
const int rhs_percent = rhs.get_percent_contribution();
if( lhs_percent != rhs_percent )
{
return lhs_percent > rhs_percent;
}
return lhs.get_name() < rhs.get_name();
return localized_compare( std::make_pair( -lhs_percent, lhs.get_name() ),
std::make_pair( -rhs_percent, rhs.get_name() ) );
};
std::sort( positive_morale.begin(), positive_morale.end(), sort_morale );
std::sort( negative_morale.begin(), negative_morale.end(), sort_morale );
Expand Down
9 changes: 1 addition & 8 deletions src/mutation_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,7 @@ bool trait_display_sort( const trait_id &a, const trait_id &b ) noexcept
return false;
}

if( a->name() < b->name() ) {
return true;
}
if( a->name() > b->name() ) {
return false;
}

return false;
return localized_compare( a->name(), b->name() );
}

void mutation_branch::load_trait_blacklist( const JsonObject &jsobj )
Expand Down
13 changes: 7 additions & 6 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ struct {
if( sort_by_points ) {
return a->point_cost() < b->point_cost();
} else {
return a->gender_appropriate_name( male ) <
b->gender_appropriate_name( male );
return localized_compare( a->gender_appropriate_name( male ),
b->gender_appropriate_name( male ) );
}
}
} profession_sorter;
Expand Down Expand Up @@ -1701,7 +1701,7 @@ tab_direction set_skills( avatar &u, points_left &points )
ui.on_screen_resize( init_windows );

auto sorted_skills = Skill::get_skills_sorted_by( []( const Skill & a, const Skill & b ) {
return a.name() < b.name();
return localized_compare( a.name(), b.name() );
} );

const int num_skills = sorted_skills.size();
Expand Down Expand Up @@ -1921,8 +1921,8 @@ struct {
} else if( sort_by_points ) {
return a->point_cost() < b->point_cost();
} else {
return a->gender_appropriate_name( male ) <
b->gender_appropriate_name( male );
return localized_compare( a->gender_appropriate_name( male ),
b->gender_appropriate_name( male ) );
}
}
} scenario_sorter;
Expand Down Expand Up @@ -2443,7 +2443,8 @@ tab_direction set_description( avatar &you, const bool allow_reroll,
auto skillslist = Skill::get_skills_sorted_by( [&]( const Skill & a, const Skill & b ) {
const int level_a = you.get_skill_level_object( a.ident() ).exercised_level();
const int level_b = you.get_skill_level_object( b.ident() ).exercised_level();
return level_a > level_b || ( level_a == level_b && a.name() < b.name() );
return localized_compare( std::make_pair( -level_a, a.name() ),
std::make_pair( -level_b, b.name() ) );
} );

int line = 1;
Expand Down
11 changes: 3 additions & 8 deletions src/npctrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,9 @@ std::vector<item_pricing> npc_trading::init_buying( player &buyer, player &selle
}

const auto cmp = []( const item_pricing & a, const item_pricing & b ) {

// Sort items by category first, if we can.
if( a.loc->get_category() != b.loc->get_category() ) {
return a.loc->get_category() < b.loc->get_category();
}

// If categories are equal, sort by name.
return a.loc->display_name() < b.loc->display_name();
// Sort items by category first, then name.
return localized_compare( std::make_pair( a.loc->get_category(), a.loc->display_name() ),
std::make_pair( b.loc->get_category(), b.loc->display_name() ) );
};

std::sort( result.begin(), result.end(), cmp );
Expand Down
11 changes: 4 additions & 7 deletions src/player_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,16 +1230,13 @@ void player::disp_info()
std::vector<bionic> bionicslist = *my_bionics;
unsigned int bionics_win_size_y = 2 + bionicslist.size();

const std::vector<const Skill *> player_skill = Skill::get_skills_sorted_by( [&]( const Skill & a,
const Skill & b ) {
const std::vector<const Skill *> player_skill = Skill::get_skills_sorted_by(
[&]( const Skill & a, const Skill & b ) {
skill_displayType_id type_a = a.display_category();
skill_displayType_id type_b = b.display_category();

if( type_a != type_b ) {
return type_a < type_b;
} else {
return a.name() < b.name();
}
return localized_compare( std::make_pair( type_a, a.name() ),
std::make_pair( type_b, b.name() ) );
} );

std::vector<HeaderSkill> skillslist;
Expand Down
2 changes: 1 addition & 1 deletion src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ bool translation::empty() const

bool translation::translated_lt( const translation &that ) const
{
return translated() < that.translated();
return localized_compare( translated(), that.translated() );
}

bool translation::translated_eq( const translation &that ) const
Expand Down
2 changes: 1 addition & 1 deletion src/wish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ void debug_menu::wishskill( player *p )
skmenu.addentry( 0, true, '1', _( "Modify all skills…" ) );

auto sorted_skills = Skill::get_skills_sorted_by( []( const Skill & a, const Skill & b ) {
return a.name() < b.name();
return localized_compare( a.name(), b.name() );
} );

std::vector<int> origskills;
Expand Down

0 comments on commit 60cf7b5

Please sign in to comment.