Skip to content

Commit

Permalink
Avoid copies in ltrim() and ltrim()
Browse files Browse the repository at this point in the history
  • Loading branch information
bad code committed Sep 22, 2024
1 parent 9c35b38 commit 1454472
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,10 +1866,10 @@ namespace Proc {
width_left -= (ulen(p.name) + 1);
}
if (width_left > 7) {
const string& cmd = width_left > 40 ? rtrim(p.cmd) : p.short_cmd;
const string_view cmd = width_left > 40 ? rtrim(p.cmd) : p.short_cmd;
if (not cmd.empty() and cmd != p.name) {
out += g_color + '(' + uresize(cmd, width_left - 3, p_wide_cmd[p.pid]) + ") ";
width_left -= (ulen(cmd, true) + 3);
out += g_color + '(' + uresize(string{cmd}, width_left - 3, p_wide_cmd[p.pid]) + ") ";
width_left -= (ulen(string{cmd}, true) + 3);
}
}
out += string(max(0, width_left), ' ') + Mv::to(y+2+lc, x+2+tree_size);
Expand Down
4 changes: 2 additions & 2 deletions src/btop_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ namespace Theme {

for (const auto& [name, source_arr] : rgbs) {
if (not name.ends_with("_start")) continue;
const string color_name = rtrim(name, "_start");
const string color_name { rtrim(name, "_start") };

//? input_colors[start,mid,end][red,green,blue]
const array<array<int, 3>, 3> input_colors = {
Expand Down Expand Up @@ -357,7 +357,7 @@ namespace Theme {

for (const auto& c : colors) {
if (not c.first.ends_with("_start")) continue;
const string base_name = rtrim(c.first, "_start");
const string base_name { rtrim(c.first, "_start") };
string section = "_start";
int split = colors.at(base_name + "_mid").empty() ? 50 : 33;
for (int i : iota(0, 101)) {
Expand Down
18 changes: 8 additions & 10 deletions src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,18 @@ namespace Tools {
return out;
}

string ltrim(const string& str, const string& t_str) {
std::string_view str_v{str};
while (str_v.starts_with(t_str))
str_v.remove_prefix(t_str.size());
string_view ltrim(string_view str, const string_view t_str) {
while (str.starts_with(t_str))
str.remove_prefix(t_str.size());

return string{str_v};
return str;
}

string rtrim(const string& str, const string& t_str) {
std::string_view str_v{str};
while (str_v.ends_with(t_str))
str_v.remove_suffix(t_str.size());
string_view rtrim(string_view str, const string_view t_str) {
while (str.ends_with(t_str))
str.remove_suffix(t_str.size());

return string{str_v};
return str;
}

auto ssplit(const string& str, const char& delim) -> vector<string> {
Expand Down
8 changes: 5 additions & 3 deletions src/btop_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tab-size = 4
#include <ranges>
#include <regex>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
#include <vector>
Expand All @@ -54,6 +55,7 @@ using std::array;
using std::atomic;
using std::string;
using std::to_string;
using std::string_view;
using std::tuple;
using std::vector;
using namespace fmt::literals;
Expand Down Expand Up @@ -292,13 +294,13 @@ namespace Tools {
}

//* Left-trim <t_str> from <str> and return new string
string ltrim(const string& str, const string& t_str = " ");
string_view ltrim(string_view str, string_view t_str = " ");

//* Right-trim <t_str> from <str> and return new string
string rtrim(const string& str, const string& t_str = " ");
string_view rtrim(string_view str, string_view t_str = " ");

//* Left/right-trim <t_str> from <str> and return new string
inline string trim(const string& str, const string& t_str = " ") {
inline string_view trim(string_view str, string_view t_str = " ") {
return ltrim(rtrim(str, t_str), t_str);
}

Expand Down

0 comments on commit 1454472

Please sign in to comment.