changed display write method

This commit is contained in:
Johannes Wendel
2018-11-08 17:35:21 +01:00
parent 8e4b7391b0
commit c1d228b477
2 changed files with 20 additions and 7 deletions

View File

@@ -31,17 +31,28 @@ template<int DigitCount>
void Display<DigitCount>::write_score(int score)
{
auto score_string = std::to_string(score);
auto score_length = score_string.length();
if (score_length > DigitCount)
if (score_string.length() > DigitCount)
{
CLOG(DEBUG, OUTPUT_LOGGER) << "Score too long for display";
this->content = score_string.substr(score_length-DigitCount,score_length);
return;
}
std::string spaces;
std::generate_n(spaces.begin(), DigitCount-score_length, []{return " ";});
this->content = spaces + score_string;
auto score_c_string = this->fit_string(score_string).c_str();
std::copy(std::begin(score_c_string), std::end(score_c_string), std::begin(this->content));
}
template<int DigitCount>
std::string Display<DigitCount>::fit_string(std::string& score_string)
{
auto score_length = score_string.length();
for (int i = 0; i < DigitCount; i++)
{
score_string = " " + score_string;
}
score_length = score_string.length();
return score_string.substr(score_length-DigitCount, score_length);
}
template<int DigitCount>

View File

@@ -34,6 +34,8 @@ public:
private:
int address;
int id;
std::string fit_string(std::string& score_string);
};
} /* namespace output */