Skip to content

Commit

Permalink
Changed: Regex for Fx::uncolor() changed to string search and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
aristocratos committed Oct 26, 2021
1 parent ce2a279 commit 36c74fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ namespace Term {

//? --------------------------------------------------- FUNCTIONS -----------------------------------------------------

namespace Fx {
string uncolor(const string& s) {
string out = s;
for (size_t offset = 0, start_pos = 0, end_pos = 0, next_pos = 0;;) {
if ((start_pos = next_pos > 0 ? next_pos : out.find('\x1b', offset)) == string::npos)
break;
offset = start_pos;
if ((end_pos = out.find('m', offset)) == string::npos)
break;
else if (next_pos = out.find('\x1b', offset + 1); end_pos - start_pos > next_pos - start_pos)
continue;
out.replace(start_pos, (end_pos - start_pos) + 1, "");
next_pos = 0;
}
out.shrink_to_fit();
return out;
}
}

namespace Tools {

atomic<int> active_locks (0);
Expand Down
2 changes: 1 addition & 1 deletion src/btop_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Fx {
const regex color_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m){1}");

//* Return a string with all colors and text styling removed
inline string uncolor(const string& s) { return regex_replace(s, color_regex, ""); }
string uncolor(const string& s);

}

Expand Down

0 comments on commit 36c74fb

Please sign in to comment.