Changed: Reverted uncolor() back to using regex

This commit is contained in:
aristocratos 2022-02-12 22:27:11 +01:00
parent fc1500007c
commit d804d3ab33
2 changed files with 32 additions and 23 deletions

View File

@ -141,29 +141,30 @@ namespace Term {
//? --------------------------------------------------- FUNCTIONS -----------------------------------------------------
namespace Fx {
string uncolor(const string& s) {
string out = s;
for (size_t offset = 0, start_pos = 0, end_pos = 0;;) {
start_pos = (offset == 0) ? out.find('\x1b') : offset;
if (start_pos == string::npos)
break;
offset = start_pos + 1;
end_pos = out.find('m', offset);
if (end_pos == string::npos)
break;
else if (auto next_pos = out.find('\x1b', offset); not isdigit(out[end_pos - 1]) or end_pos > next_pos) {
offset = next_pos;
continue;
}
// ! Dsiabled due to issue when compiling with musl, reverted back to using regex
// namespace Fx {
// string uncolor(const string& s) {
// string out = s;
// for (size_t offset = 0, start_pos = 0, end_pos = 0;;) {
// start_pos = (offset == 0) ? out.find('\x1b') : offset;
// if (start_pos == string::npos)
// break;
// offset = start_pos + 1;
// end_pos = out.find('m', offset);
// if (end_pos == string::npos)
// break;
// else if (auto next_pos = out.find('\x1b', offset); not isdigit(out[end_pos - 1]) or end_pos > next_pos) {
// offset = next_pos;
// continue;
// }
out.erase(start_pos, (end_pos - start_pos)+1);
offset = 0;
}
out.shrink_to_fit();
return out;
}
}
// out.erase(start_pos, (end_pos - start_pos)+1);
// offset = 0;
// }
// out.shrink_to_fit();
// return out;
// }
// }
namespace Tools {

View File

@ -22,6 +22,7 @@ tab-size = 4
#include <vector>
#include <array>
#include <atomic>
#include <regex>
#include <filesystem>
#include <ranges>
#include <chrono>
@ -64,8 +65,15 @@ namespace Fx {
//* Reset text effects and restore theme foregrund and background color
extern string reset;
//* Regex for matching color, style and cursor move escape sequences
const std::regex escape_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m|f|s|u|C|D|A|B){1}");
//* Regex for matching only color and style escape sequences
const std::regex color_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m){1}");
//* Return a string with all colors and text styling removed
string uncolor(const string& s);
inline string uncolor(const string& s) { return std::regex_replace(s, color_regex, ""); }
// string uncolor(const string& s);
}