Changed: Regex for Fx::uncolor() changed to string search and replace

This commit is contained in:
aristocratos 2021-10-26 22:56:19 +02:00
parent ce2a2797b8
commit 36c74fb08a
2 changed files with 20 additions and 1 deletions

View file

@ -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);

View file

@ -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);
}