Skip to content

Commit

Permalink
Better support for string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Aug 26, 2023
1 parent b266d14 commit d9c96f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions include/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ struct set_button {
struct writer {
data_cache cache;

virtual writer& operator<<(const std::string& value) = 0;
virtual writer& operator<<(const double& value) = 0;
virtual writer& operator<<(std::string_view value) = 0;
virtual writer& operator<<(double value) = 0;

virtual writer& operator<<(const budget::money& m) = 0;
virtual writer& operator<<(const budget::month& m) = 0;
Expand Down Expand Up @@ -126,8 +126,8 @@ struct console_writer : writer {

explicit console_writer(std::ostream& os);

writer& operator<<(const std::string& value) override;
writer& operator<<(const double& value) override;
writer& operator<<(std::string_view value) override;
writer& operator<<(double value) override;

writer& operator<<(const budget::money& m) override;
writer& operator<<(const budget::month& m) override;
Expand Down
26 changes: 13 additions & 13 deletions src/console_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ std::string success_to_string(int success) {
return ss.str();
}

std::string format(const std::string& v) {
std::string format(std::string_view v) {
if (v.substr(0, 5) == "::red") {
auto value = v.substr(5);

return "\033[0;31m" + value + budget::format_reset();
return "\033[0;31m" + std::string(value) + budget::format_reset();
}
if (v.substr(0, 7) == "::green") {
auto value = v.substr(7);

return "\033[0;32m" + value + budget::format_reset();
return "\033[0;32m" + std::string(value) + budget::format_reset();
}

if (v.substr(0, 6) == "::blue") {
auto value = v.substr(6);

return "\033[0;33m" + value + budget::format_reset();
return "\033[0;33m" + std::string(value) + budget::format_reset();
}

if (v.substr(0, 9) == "::success") {
Expand All @@ -67,26 +67,26 @@ std::string format(const std::string& v) {
return success_to_string(success);
}

return v;
return std::string(v);
}

std::string underline_format(const std::string& v) {
std::string underline_format(std::string_view v) {
if (v.substr(0, 5) == "::red") {
auto value = v.substr(5);

return "\033[4;31m" + value + budget::format_reset();
return "\033[4;31m" + std::string(value) + budget::format_reset();
}

if (v.substr(0, 7) == "::green") {
auto value = v.substr(7);

return "\033[4;32m" + value + budget::format_reset();
return "\033[4;32m" + std::string(value) + budget::format_reset();
}

if (v.substr(0, 6) == "::blue") {
auto value = v.substr(6);

return "\033[4;33m" + value + budget::format_reset();
return "\033[4;33m" + std::string(value) + budget::format_reset();
}

if (v.substr(0, 9) == "::success") {
Expand All @@ -95,21 +95,21 @@ std::string underline_format(const std::string& v) {
return success_to_string(success);
}

return v;
return std::string(v);
}

} // end of anonymous namespace

budget::console_writer::console_writer(std::ostream& os)
: os(os) {}

budget::writer& budget::console_writer::operator<<(const std::string& value) {
os << format(value);
budget::writer& budget::console_writer::operator<<(std::string_view value) {
os << ::format(value);

return *this;
}

budget::writer& budget::console_writer::operator<<(const double& value) {
budget::writer& budget::console_writer::operator<<(double value) {
os << value;

return *this;
Expand Down

0 comments on commit d9c96f2

Please sign in to comment.