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

View File

@ -22,6 +22,7 @@ tab-size = 4
#include <vector> #include <vector>
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <regex>
#include <filesystem> #include <filesystem>
#include <ranges> #include <ranges>
#include <chrono> #include <chrono>
@ -64,8 +65,15 @@ namespace Fx {
//* Reset text effects and restore theme foregrund and background color //* Reset text effects and restore theme foregrund and background color
extern string reset; 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 //* 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);
} }