Skip to content

Commit

Permalink
Cleaned up utils::apply_modifier a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed May 28, 2018
1 parent 0ca8227 commit c5ba6f0
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/serialization/string_utils.cpp
Expand Up @@ -412,18 +412,25 @@ std::pair<string_view, string_view> vertical_split(const std::string& val)
}

// Modify a number by string representing integer difference, or optionally %
int apply_modifier( const int number, const std::string &amount, const int minimum ) {
// wassert( amount.empty() == false );
int apply_modifier(const int number, const std::string& amount, const int minimum)
{
int value = 0;

try {
value = std::stoi(amount);
} catch(const std::invalid_argument&) {}
if(amount[amount.size()-1] == '%') {
} catch(const std::invalid_argument&) {
}

if(amount.back() == '%') {
value = div100rounded(number * value);
}

value += number;
if (( minimum > 0 ) && ( value < minimum ))
value = minimum;

if(minimum > 0 && value < minimum) {
value = minimum;
}

return value;
}

Expand Down

0 comments on commit c5ba6f0

Please sign in to comment.